1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file der_encode_octet_string.c
14 ASN.1 DER, encode a OCTET STRING, Tom St Denis
15 */
16
17
18 #ifdef LTC_DER
19
20 /**
21 Store an OCTET STRING
22 @param in The array of OCTETS to store (one per char)
23 @param inlen The number of OCTETS to store
24 @param out [out] The destination for the DER encoded OCTET STRING
25 @param outlen [in/out] The max size and resulting size of the DER OCTET STRING
26 @return CRYPT_OK if successful
27 */
der_encode_octet_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)28 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
29 unsigned char *out, unsigned long *outlen)
30 {
31 unsigned long x, y, len;
32 int err;
33
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
37
38 /* get the size */
39 if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
40 return err;
41 }
42
43 /* too big? */
44 if (len > *outlen) {
45 *outlen = len;
46 return CRYPT_BUFFER_OVERFLOW;
47 }
48
49 /* encode the header+len */
50 x = 0;
51 out[x++] = 0x04;
52 len = *outlen - x;
53 if ((err = der_encode_asn1_length(inlen, out + x, &len)) != CRYPT_OK) {
54 return err;
55 }
56 x += len;
57
58 /* store octets */
59 for (y = 0; y < inlen; y++) {
60 out[x++] = in[y];
61 }
62
63 /* retun length */
64 *outlen = x;
65
66 return CRYPT_OK;
67 }
68
69 #endif
70
71 /* ref: $Format:%D$ */
72 /* git commit: $Format:%H$ */
73 /* commit time: $Format:%ai$ */
74