Debian Codegraph @

src/ltc/pk/asn1/der/bit/der_decode_raw_bit_string.c

raw
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file der_decode_bit_string.c
7 ASN.1 DER, encode a BIT STRING, Tom St Denis
8 */
9
10
11 #ifdef LTC_DER
12
13 #define SETBIT(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
14 #define CLRBIT(v, n) (v=((unsigned char)(v) & ~(1U << (unsigned char)(n))))
15
16 /**
17 Store a BIT STRING
18 @param in The DER encoded BIT STRING
19 @param inlen The size of the DER BIT STRING
20 @param out [out] The array of bits stored (8 per char)
21 @param outlen [in/out] The number of bits stored
22 @return CRYPT_OK if successful
23 */
24 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
25 unsigned char *out, unsigned long *outlen)
26 {
27 unsigned long dlen, blen, x, y, unused;
28 int err;
29
30 LTC_ARGCHK(in != NULL);
31 LTC_ARGCHK(out != NULL);
32 LTC_ARGCHK(outlen != NULL);
33
34 /* packet must be at least 3 bytes */
35 if (inlen < 3) {
36 return CRYPT_INVALID_ARG;
37 }
38
39 /* check for 0x03 */
40 if ((in[0]&0x1F) != 0x03) {
41 return CRYPT_INVALID_PACKET;
42 }
43
44 /* offset in the data */
45 x = 1;
46
47 /* get the length of the data */
48 y = inlen - 1;
49 if ((err = der_decode_asn1_length(in + x, &y, &dlen)) != CRYPT_OK) {
50 return err;
51 }
52 x += y;
53 /* is the data len too long or too short? */
54 if ((dlen == 0) || (dlen > (inlen - x))) {
55 return CRYPT_INVALID_PACKET;
56 }
57
58 /* DER permits 0..7 unused bits, and any declared padding bits must be zero. */
59 unused = in[x];
60 if (unused > 7) {
61 return CRYPT_INVALID_PACKET;
62 }
63 if ((dlen == 1) && (unused != 0)) {
64 return CRYPT_INVALID_PACKET;
65 }
66 if ((unused != 0) && ((in[x + dlen - 1] & ((1uL << unused) - 1uL)) != 0)) {
67 return CRYPT_INVALID_PACKET;
68 }
69 x++;
70 blen = ((dlen - 1) << 3) - unused;
71
72 /* too many bits? */
73 if (blen > *outlen) {
74 *outlen = blen;
75 return CRYPT_BUFFER_OVERFLOW;
76 }
77
78 /* decode/store the bits */
79 for (y = 0; y < blen; y++) {
80 if (in[x] & (1 << (7 - (y & 7)))) {
81 SETBIT(out[y/8], 7-(y%8));
82 } else {
83 CLRBIT(out[y/8], 7-(y%8));
84 }
85 if ((y & 7) == 7) {
86 ++x;
87 }
88 }
89
90 /* we done */
91 *outlen = blen;
92 return CRYPT_OK;
93 }
94
95 #endif