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 /**
14 Store a BIT STRING
15 @param in The array of bits to store (one per char)
16 @param inlen The number of bits tostore
17 @param out [out] The destination for the DER encoded BIT STRING
18 @param outlen [in/out] The max size and resulting size of the DER BIT STRING
19 @return CRYPT_OK if successful
20 */
der_encode_bit_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)21 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
22 unsigned char *out, unsigned long *outlen)
23 {
24 unsigned long len, x, y;
25 unsigned char buf;
26 int err;
27
28 LTC_ARGCHK(in != NULL);
29 LTC_ARGCHK(out != NULL);
30 LTC_ARGCHK(outlen != NULL);
31
32 /* avoid overflows */
33 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
34 return err;
35 }
36
37 if (len > *outlen) {
38 *outlen = len;
39 return CRYPT_BUFFER_OVERFLOW;
40 }
41
42 /* store header (include bit padding count in length) */
43 x = 0;
44 y = ((inlen + 7) >> 3) + 1;
45
46 out[x++] = 0x03;
47 len = *outlen - x;
48 if ((err = der_encode_asn1_length(y, out + x, &len)) != CRYPT_OK) {
49 return err;
50 }
51 x += len;
52
53 /* store number of zero padding bits */
54 out[x++] = (unsigned char)((8 - inlen) & 7);
55
56 /* store the bits in big endian format */
57 for (y = buf = 0; y < inlen; y++) {
58 buf |= (in[y] ? 1 : 0) << (7 - (y & 7));
59 if ((y & 7) == 7) {
60 out[x++] = buf;
61 buf = 0;
62 }
63 }
64 /* store last byte */
65 if (inlen & 7) {
66 out[x++] = buf;
67 }
68 *outlen = x;
69 return CRYPT_OK;
70 }
71
72 #endif
73