Mercurial > dropbear
comparison src/pk/asn1/der/der_length_integer.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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, [email protected], http://libtomcrypt.org | |
10 */ | |
11 #include "tomcrypt.h" | |
12 | |
13 /** | |
14 @file der_length_integer.c | |
15 ASN.1 DER, get length of encoding, Tom St Denis | |
16 */ | |
17 | |
18 | |
19 #ifdef LTC_DER | |
20 /** | |
21 Gets length of DER encoding of num | |
22 @param num The mp_int to get the size of | |
23 @param outlen [out] The length of the DER encoding for the given integer | |
24 @return CRYPT_OK if successful | |
25 */ | |
26 int der_length_integer(mp_int *num, unsigned long *outlen) | |
27 { | |
28 unsigned long z, len; | |
29 int leading_zero; | |
30 | |
31 LTC_ARGCHK(num != NULL); | |
32 LTC_ARGCHK(outlen != NULL); | |
33 | |
34 /* we only need a leading zero if the msb of the first byte is one */ | |
35 if ((mp_count_bits(num) & 7) == 7 || mp_iszero(num) == MP_YES) { | |
36 leading_zero = 1; | |
37 } else { | |
38 leading_zero = 0; | |
39 } | |
40 | |
41 /* size for bignum */ | |
42 z = len = leading_zero + mp_unsigned_bin_size(num); | |
43 | |
44 /* we need a 0x02 */ | |
45 ++len; | |
46 | |
47 /* now we need a length */ | |
48 if (z < 128) { | |
49 /* short form */ | |
50 ++len; | |
51 } else { | |
52 /* long form (relies on z != 0) */ | |
53 ++len; | |
54 | |
55 while (z) { | |
56 ++len; | |
57 z >>= 8; | |
58 } | |
59 } | |
60 | |
61 *outlen = len; | |
62 return CRYPT_OK; | |
63 } | |
64 | |
65 #endif |