version 0.3.33
[fms.git] / libs / libtomcrypt / pk / asn1 / der / short_integer / der_decode_short_integer.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  *
9  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12
13 /**
14   @file der_decode_short_integer.c
15   ASN.1 DER, decode an integer, Tom St Denis
16 */
17
18
19 #ifdef LTC_DER
20
21 /**
22   Read a short integer
23   @param in       The DER encoded data
24   @param inlen    Size of data
25   @param num      [out] The integer to decode
26   @return CRYPT_OK if successful
27 */
28 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num)
29 {
30    unsigned long len, x, y;
31
32    LTC_ARGCHK(num    != NULL);
33    LTC_ARGCHK(in     != NULL);
34
35    /* check length */
36    if (inlen < 2) {
37       return CRYPT_INVALID_PACKET;
38    }
39
40    /* check header */
41    x = 0;
42    if ((in[x++] & 0x1F) != 0x02) {
43       return CRYPT_INVALID_PACKET;
44    }
45
46    /* get the packet len */
47    len = in[x++];
48
49    if (x + len > inlen) {
50       return CRYPT_INVALID_PACKET;
51    }
52
53    /* read number */
54    y = 0;
55    while (len--) {
56       y = (y<<8) | (unsigned long)in[x++];
57    }
58    *num = y;
59
60    return CRYPT_OK;
61
62 }
63
64 #endif
65
66 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/short_integer/der_decode_short_integer.c,v $ */
67 /* $Revision: 1.6 $ */
68 /* $Date: 2006/03/31 14:15:35 $ */