1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file der_decode_bit_string.c
13 ASN.1 DER, encode a BIT STRING, Tom St Denis
19 #define SETBIT(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
20 #define CLRBIT(v, n) (v=((unsigned char)(v) & ~(1U << (unsigned char)(n))))
24 @param in The DER encoded BIT STRING
25 @param inlen The size of the DER BIT STRING
26 @param out [out] The array of bits stored (8 per char)
27 @param outlen [in/out] The number of bits stored
28 @return CRYPT_OK if successful
30 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
31 unsigned char *out, unsigned long *outlen)
33 unsigned long dlen, blen, x, y;
35 LTC_ARGCHK(in != NULL);
36 LTC_ARGCHK(out != NULL);
37 LTC_ARGCHK(outlen != NULL);
39 /* packet must be at least 4 bytes */
41 return CRYPT_INVALID_ARG;
45 if ((in[0]&0x1F) != 0x03) {
46 return CRYPT_INVALID_PACKET;
49 /* offset in the data */
52 /* get the length of the data */
54 /* long format get number of length bytes */
57 /* invalid if 0 or > 2 */
58 if (y == 0 || y > 2) {
59 return CRYPT_INVALID_PACKET;
62 /* read the data len */
65 dlen = (dlen << 8) | (unsigned long)in[x++];
69 dlen = in[x++] & 0x7F;
72 /* is the data len too long or too short? */
73 if ((dlen == 0) || (dlen + x > inlen)) {
74 return CRYPT_INVALID_PACKET;
77 /* get padding count */
78 blen = ((dlen - 1) << 3) - (in[x++] & 7);
83 return CRYPT_BUFFER_OVERFLOW;
86 /* decode/store the bits */
87 for (y = 0; y < blen; y++) {
88 if (in[x] & (1 << (7 - (y & 7)))) {
89 SETBIT(out[y/8], 7-(y%8));
91 CLRBIT(out[y/8], 7-(y%8));
105 /* ref: $Format:%D$ */
106 /* git commit: $Format:%H$ */
107 /* commit time: $Format:%ai$ */