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_bit_string.c
15 ASN.1 DER, encode a BIT STRING, Tom St Denis
23 @param in The array of bits to store (one per char)
24 @param inlen The number of bits tostore
25 @param out [out] The destination for the DER encoded BIT STRING
26 @param outlen [in/out] The max size and resulting size of the DER BIT STRING
27 @return CRYPT_OK if successful
29 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
32 unsigned long len, x, y;
36 LTC_ARGCHK(in != NULL);
37 LTC_ARGCHK(out != NULL);
38 LTC_ARGCHK(outlen != NULL);
41 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
47 return CRYPT_BUFFER_OVERFLOW;
50 /* store header (include bit padding count in length) */
52 y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1;
56 out[x++] = (unsigned char)y;
59 out[x++] = (unsigned char)y;
60 } else if (y < 65536) {
62 out[x++] = (unsigned char)((y>>8)&255);
63 out[x++] = (unsigned char)(y&255);
66 /* store number of zero padding bits */
67 out[x++] = (unsigned char)((8 - inlen) & 7);
69 /* store the bits in big endian format */
70 for (y = buf = 0; y < inlen; y++) {
71 buf |= (in[y] ? 1 : 0) << (7 - (y & 7));
87 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/bit/der_encode_bit_string.c,v $ */
88 /* $Revision: 1.4 $ */
89 /* $Date: 2006/12/04 21:34:03 $ */