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 #define getbit(n, k) (((n) & ( 1 << (k) )) >> (k))
21
22 /**
23 Store a BIT STRING
24 @param in The array of bits to store (8 per char)
25 @param inlen The number of bits to store
26 @param out [out] The destination for the DER encoded BIT STRING
27 @param outlen [in/out] The max size and resulting size of the DER BIT STRING
28 @return CRYPT_OK if successful
29 */
der_encode_raw_bit_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)30 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
31 unsigned char *out, unsigned long *outlen)
32 {
33 unsigned long len, x, y;
34 unsigned char buf;
35 int err;
36
37 LTC_ARGCHK(in != NULL);
38 LTC_ARGCHK(out != NULL);
39 LTC_ARGCHK(outlen != NULL);
40
41 /* avoid overflows */
42 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) {
43 return err;
44 }
45
46 if (len > *outlen) {
47 *outlen = len;
48 return CRYPT_BUFFER_OVERFLOW;
49 }
50
51 /* store header (include bit padding count in length) */
52 x = 0;
53 y = ((inlen + 7) >> 3) + 1;
54
55 out[x++] = 0x03;
56 len = *outlen - x;
57 if ((err = der_encode_asn1_length(y, out + x, &len)) != CRYPT_OK) {
58 return err;
59 }
60 x += len;
61
62 /* store number of zero padding bits */
63 out[x++] = (unsigned char)((8 - inlen) & 7);
64
65 /* store the bits in big endian format */
66 for (y = buf = 0; y < inlen; y++) {
67 buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
68 if ((y & 7) == 7) {
69 out[x++] = buf;
70 buf = 0;
71 }
72 }
73 /* store last byte */
74 if (inlen & 7) {
75 out[x++] = buf;
76 }
77
78 *outlen = x;
79 return CRYPT_OK;
80 }
81
82 #endif
83
84 /* ref: $Format:%D$ */
85 /* git commit: $Format:%H$ */
86 /* commit time: $Format:%ai$ */
87