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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_encode_octet_string.c
15 ASN.1 DER, encode a OCTET STRING, Tom St Denis
23 @param in The array of OCTETS to store (one per char)
24 @param inlen The number of OCTETS to store
25 @param out [out] The destination for the DER encoded OCTET STRING
26 @param outlen [in/out] The max size and resulting size of the DER OCTET STRING
27 @return CRYPT_OK if successful
29 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
32 unsigned long x, y, len;
35 LTC_ARGCHK(in != NULL);
36 LTC_ARGCHK(out != NULL);
37 LTC_ARGCHK(outlen != NULL);
40 if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
47 return CRYPT_BUFFER_OVERFLOW;
50 /* encode the header+len */
54 out[x++] = (unsigned char)inlen;
55 } else if (inlen < 256) {
57 out[x++] = (unsigned char)inlen;
58 } else if (inlen < 65536UL) {
60 out[x++] = (unsigned char)((inlen>>8)&255);
61 out[x++] = (unsigned char)(inlen&255);
62 } else if (inlen < 16777216UL) {
64 out[x++] = (unsigned char)((inlen>>16)&255);
65 out[x++] = (unsigned char)((inlen>>8)&255);
66 out[x++] = (unsigned char)(inlen&255);
68 return CRYPT_INVALID_ARG;
72 for (y = 0; y < inlen; y++) {
84 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/octet/der_encode_octet_string.c,v $ */
85 /* $Revision: 1.4 $ */
86 /* $Date: 2006/12/04 21:34:03 $ */