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_bit_string.c
13 ASN.1 DER, encode a BIT STRING, Tom St Denis
21 @param in The array of bits to store (one per char)
22 @param inlen The number of bits tostore
23 @param out [out] The destination for the DER encoded BIT STRING
24 @param outlen [in/out] The max size and resulting size of the DER BIT STRING
25 @return CRYPT_OK if successful
27 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
28 unsigned char *out, unsigned long *outlen)
30 unsigned long len, x, y;
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
39 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
45 return CRYPT_BUFFER_OVERFLOW;
48 /* store header (include bit padding count in length) */
50 y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1;
54 out[x++] = (unsigned char)y;
57 out[x++] = (unsigned char)y;
58 } else if (y < 65536) {
60 out[x++] = (unsigned char)((y>>8)&255);
61 out[x++] = (unsigned char)(y&255);
64 /* store number of zero padding bits */
65 out[x++] = (unsigned char)((8 - inlen) & 7);
67 /* store the bits in big endian format */
68 for (y = buf = 0; y < inlen; y++) {
69 buf |= (in[y] ? 1 : 0) << (7 - (y & 7));
85 /* ref: $Format:%D$ */
86 /* git commit: $Format:%H$ */
87 /* commit time: $Format:%ai$ */