Mercurial > dropbear
comparison libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c @ 293:9d110777f345 contrib-blacklist
propagate from branch 'au.asn.ucc.matt.dropbear' (head 7ad1775ed65e75dbece27fe6b65bf1a234db386a)
to branch 'au.asn.ucc.matt.dropbear.contrib.blacklist' (head 1d86a4f0a401cc68c2670d821a2f6366c37af143)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 10 Mar 2006 06:31:29 +0000 |
parents | 1b9e69c058d2 |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
247:c07de41b53d7 | 293:9d110777f345 |
---|---|
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 if (mp_cmp_d(num, 0) != MP_LT) { | |
35 /* positive */ | |
36 | |
37 /* we only need a leading zero if the msb of the first byte is one */ | |
38 if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == MP_YES) { | |
39 leading_zero = 1; | |
40 } else { | |
41 leading_zero = 0; | |
42 } | |
43 | |
44 /* size for bignum */ | |
45 z = len = leading_zero + mp_unsigned_bin_size(num); | |
46 } else { | |
47 /* it's negative */ | |
48 /* find power of 2 that is a multiple of eight and greater than count bits */ | |
49 leading_zero = 0; | |
50 z = mp_count_bits(num); | |
51 z = z + (8 - (z & 7)); | |
52 len = z = z >> 3; | |
53 } | |
54 | |
55 /* now we need a length */ | |
56 if (z < 128) { | |
57 /* short form */ | |
58 ++len; | |
59 } else { | |
60 /* long form (relies on z != 0), assumes length bytes < 128 */ | |
61 ++len; | |
62 | |
63 while (z) { | |
64 ++len; | |
65 z >>= 8; | |
66 } | |
67 } | |
68 | |
69 /* we need a 0x02 to indicate it's INTEGER */ | |
70 ++len; | |
71 | |
72 /* return length */ | |
73 *outlen = len; | |
74 return CRYPT_OK; | |
75 } | |
76 | |
77 #endif | |
78 | |
79 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c,v $ */ | |
80 /* $Revision: 1.1 $ */ | |
81 /* $Date: 2005/05/16 15:08:11 $ */ |