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_decode_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 DER encoded BIT STRING
23   @param inlen   The size of the DER BIT STRING
24   @param out     [out] The array of bits stored (one per char)
25   @param outlen  [in/out] The number of bits stored
26   @return CRYPT_OK if successful
27 */
der_decode_bit_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)28 int der_decode_bit_string(const unsigned char *in,  unsigned long inlen,
29                                 unsigned char *out, unsigned long *outlen)
30 {
31    unsigned long dlen, blen, x, y;
32    int err;
33 
34    LTC_ARGCHK(in     != NULL);
35    LTC_ARGCHK(out    != NULL);
36    LTC_ARGCHK(outlen != NULL);
37 
38    /* packet must be at least 4 bytes */
39    if (inlen < 4) {
40        return CRYPT_INVALID_ARG;
41    }
42 
43    /* check for 0x03 */
44    if ((in[0]&0x1F) != 0x03) {
45       return CRYPT_INVALID_PACKET;
46    }
47 
48    /* offset in the data */
49    x = 1;
50 
51    /* get the length of the data */
52    y = inlen - 1;
53    if ((err = der_decode_asn1_length(in + x, &y, &dlen)) != CRYPT_OK) {
54       return err;
55    }
56    x += y;
57    /* is the data len too long or too short? */
58    if ((dlen == 0) || (dlen > (inlen - x))) {
59        return CRYPT_INVALID_PACKET;
60    }
61 
62    /* get padding count */
63    blen = ((dlen - 1) << 3) - (in[x++] & 7);
64 
65    /* too many bits? */
66    if (blen > *outlen) {
67       *outlen = blen;
68       return CRYPT_BUFFER_OVERFLOW;
69    }
70 
71    /* decode/store the bits */
72    for (y = 0; y < blen; y++) {
73        out[y] = (in[x] & (1 << (7 - (y & 7)))) ? 1 : 0;
74        if ((y & 7) == 7) {
75           ++x;
76        }
77    }
78 
79    /* we done */
80    *outlen = blen;
81    return CRYPT_OK;
82 }
83 
84 #endif
85 
86 /* ref:         $Format:%D$ */
87 /* git commit:  $Format:%H$ */
88 /* commit time: $Format:%ai$ */
89