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
21 @param in The DER encoded BIT STRING
22 @param inlen The size of the DER BIT STRING
23 @param out [out] The array of bits stored (one per char)
24 @param outlen [in/out] The number of bits stored
25 @return CRYPT_OK if successful
27 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
28 unsigned char *out, unsigned long *outlen)
30 unsigned long dlen, blen, x, y;
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(out != NULL);
34 LTC_ARGCHK(outlen != NULL);
36 /* packet must be at least 4 bytes */
38 return CRYPT_INVALID_ARG;
42 if ((in[0]&0x1F) != 0x03) {
43 return CRYPT_INVALID_PACKET;
46 /* offset in the data */
49 /* get the length of the data */
51 /* long format get number of length bytes */
54 /* invalid if 0 or > 2 */
55 if (y == 0 || y > 2) {
56 return CRYPT_INVALID_PACKET;
59 /* read the data len */
62 dlen = (dlen << 8) | (unsigned long)in[x++];
66 dlen = in[x++] & 0x7F;
69 /* is the data len too long or too short? */
70 if ((dlen == 0) || (dlen + x > inlen)) {
71 return CRYPT_INVALID_PACKET;
74 /* get padding count */
75 blen = ((dlen - 1) << 3) - (in[x++] & 7);
80 return CRYPT_BUFFER_OVERFLOW;
83 /* decode/store the bits */
84 for (y = 0; y < blen; y++) {
85 out[y] = (in[x] & (1 << (7 - (y & 7)))) ? 1 : 0;
98 /* ref: $Format:%D$ */
99 /* git commit: $Format:%H$ */
100 /* commit time: $Format:%ai$ */