]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/short_integer/der_decode_short_integer.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / 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 #include "tomcrypt.h"
10
11 /**
12   @file der_decode_short_integer.c
13   ASN.1 DER, decode an integer, Tom St Denis
14 */
15
16
17 #ifdef LTC_DER
18
19 /**
20   Read a short integer
21   @param in       The DER encoded data
22   @param inlen    Size of data
23   @param num      [out] The integer to decode
24   @return CRYPT_OK if successful
25 */
26 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num)
27 {
28    unsigned long len, x, y;
29
30    LTC_ARGCHK(num    != NULL);
31    LTC_ARGCHK(in     != NULL);
32
33    /* check length */
34    if (inlen < 2) {
35       return CRYPT_INVALID_PACKET;
36    }
37
38    /* check header */
39    x = 0;
40    if ((in[x++] & 0x1F) != 0x02) {
41       return CRYPT_INVALID_PACKET;
42    }
43
44    /* get the packet len */
45    len = in[x++];
46
47    if (x + len > inlen) {
48       return CRYPT_INVALID_PACKET;
49    }
50
51    /* read number */
52    y = 0;
53    while (len--) {
54       y = (y<<8) | (unsigned long)in[x++];
55    }
56    *num = y;
57
58    return CRYPT_OK;
59
60 }
61
62 #endif
63
64 /* ref:         $Format:%D$ */
65 /* git commit:  $Format:%H$ */
66 /* commit time: $Format:%ai$ */