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