]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/short_integer/der_length_short_integer.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / short_integer / der_length_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_length_short_integer.c
13   ASN.1 DER, get length of encoding, Tom St Denis
14 */
15
16
17 #ifdef LTC_DER
18 /**
19   Gets length of DER encoding of num
20   @param num    The integer to get the size of
21   @param outlen [out] The length of the DER encoding for the given integer
22   @return CRYPT_OK if successful
23 */
24 int der_length_short_integer(unsigned long num, unsigned long *outlen)
25 {
26    unsigned long z, y, len;
27
28    LTC_ARGCHK(outlen  != NULL);
29
30    /* force to 32 bits */
31    num &= 0xFFFFFFFFUL;
32
33    /* get the number of bytes */
34    z = 0;
35    y = num;
36    while (y) {
37      ++z;
38      y >>= 8;
39    }
40
41    /* handle zero */
42    if (z == 0) {
43       z = 1;
44    }
45
46    /* we need a 0x02 to indicate it's INTEGER */
47    len = 1;
48
49    /* length byte */
50    ++len;
51
52    /* bytes in value */
53    len += z;
54
55    /* see if msb is set */
56    len += (num&(1UL<<((z<<3) - 1))) ? 1 : 0;
57
58    /* return length */
59    *outlen = len;
60
61    return CRYPT_OK;
62 }
63
64 #endif
65
66 /* ref:         $Format:%D$ */
67 /* git commit:  $Format:%H$ */
68 /* commit time: $Format:%ai$ */