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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_decode_bit_string.c
15 ASN.1 DER, encode a BIT STRING, Tom St Denis
23 @param in The DER encoded BIT STRING
24 @param inlen The size of the DER BIT STRING
25 @param out [out] The array of bits stored (one per char)
26 @param outlen [in/out] The number of bits stored
27 @return CRYPT_OK if successful
29 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
32 unsigned long dlen, blen, x, y;
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
38 /* packet must be at least 4 bytes */
40 return CRYPT_INVALID_ARG;
44 if ((in[0]&0x1F) != 0x03) {
45 return CRYPT_INVALID_PACKET;
48 /* offset in the data */
51 /* get the length of the data */
53 /* long format get number of length bytes */
56 /* invalid if 0 or > 2 */
57 if (y == 0 || y > 2) {
58 return CRYPT_INVALID_PACKET;
61 /* read the data len */
64 dlen = (dlen << 8) | (unsigned long)in[x++];
68 dlen = in[x++] & 0x7F;
71 /* is the data len too long or too short? */
72 if ((dlen == 0) || (dlen + x > inlen)) {
73 return CRYPT_INVALID_PACKET;
76 /* get padding count */
77 blen = ((dlen - 1) << 3) - (in[x++] & 7);
82 return CRYPT_BUFFER_OVERFLOW;
85 /* decode/store the bits */
86 for (y = 0; y < blen; y++) {
87 out[y] = (in[x] & (1 << (7 - (y & 7)))) ? 1 : 0;
100 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/bit/der_decode_bit_string.c,v $ */
101 /* $Revision: 1.4 $ */
102 /* $Date: 2006/06/16 21:53:41 $ */