1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file der_encode_bit_string.c
7   ASN.1 DER, encode a BIT STRING, Tom St Denis
8 */
9 
10 
11 #ifdef LTC_DER
12 
13 #define getbit(n, k) (((n) & ( 1 << (k) )) >> (k))
14 
15 /**
16   Store a BIT STRING
17   @param in       The array of bits to store (8 per char)
18   @param inlen    The number of bits to store
19   @param out      [out] The destination for the DER encoded BIT STRING
20   @param outlen   [in/out] The max size and resulting size of the DER BIT STRING
21   @return CRYPT_OK if successful
22 */
der_encode_raw_bit_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)23 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
24                                 unsigned char *out, unsigned long *outlen)
25 {
26    unsigned long len, x, y;
27    unsigned char buf;
28    int           err;
29 
30    LTC_ARGCHK(in     != NULL);
31    LTC_ARGCHK(out    != NULL);
32    LTC_ARGCHK(outlen != NULL);
33 
34    /* avoid overflows */
35    if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
36       return err;
37    }
38 
39    if (len > *outlen) {
40       *outlen = len;
41       return CRYPT_BUFFER_OVERFLOW;
42    }
43 
44    /* store header (include bit padding count in length) */
45    x = 0;
46    y = ((inlen + 7) >> 3) + 1;
47 
48    out[x++] = 0x03;
49    len = *outlen - x;
50    if ((err = der_encode_asn1_length(y, out + x, &len)) != CRYPT_OK) {
51       return err;
52    }
53    x += len;
54 
55    /* store number of zero padding bits */
56    out[x++] = (unsigned char)((8 - inlen) & 7);
57 
58    /* store the bits in big endian format */
59    for (y = buf = 0; y < inlen; y++) {
60       buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
61       if ((y & 7) == 7) {
62          out[x++] = buf;
63          buf      = 0;
64       }
65    }
66    /* store last byte */
67    if (inlen & 7) {
68       out[x++] = buf;
69    }
70 
71    *outlen = x;
72    return CRYPT_OK;
73 }
74 
75 #endif
76