1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file der_encode_octet_string.c
13 ASN.1 DER, encode a OCTET STRING, Tom St Denis
21 @param in The array of OCTETS to store (one per char)
22 @param inlen The number of OCTETS to store
23 @param out [out] The destination for the DER encoded OCTET STRING
24 @param outlen [in/out] The max size and resulting size of the DER OCTET STRING
25 @return CRYPT_OK if successful
27 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
28 unsigned char *out, unsigned long *outlen)
30 unsigned long x, y, len;
33 LTC_ARGCHK(in != NULL);
34 LTC_ARGCHK(out != NULL);
35 LTC_ARGCHK(outlen != NULL);
38 if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
45 return CRYPT_BUFFER_OVERFLOW;
48 /* encode the header+len */
52 out[x++] = (unsigned char)inlen;
53 } else if (inlen < 256) {
55 out[x++] = (unsigned char)inlen;
56 } else if (inlen < 65536UL) {
58 out[x++] = (unsigned char)((inlen>>8)&255);
59 out[x++] = (unsigned char)(inlen&255);
60 } else if (inlen < 16777216UL) {
62 out[x++] = (unsigned char)((inlen>>16)&255);
63 out[x++] = (unsigned char)((inlen>>8)&255);
64 out[x++] = (unsigned char)(inlen&255);
66 return CRYPT_INVALID_ARG;
70 for (y = 0; y < inlen; y++) {
82 /* ref: $Format:%D$ */
83 /* git commit: $Format:%H$ */
84 /* commit time: $Format:%ai$ */