1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_length_integer.c
15 ASN.1 DER, get length of encoding, Tom St Denis
21 Gets length of DER encoding of num
22 @param num The 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
26 int der_length_integer(void *num, unsigned long *outlen)
31 LTC_ARGCHK(num != NULL);
32 LTC_ARGCHK(outlen != NULL);
34 if (mp_cmp_d(num, 0) != LTC_MP_LT) {
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) == LTC_MP_YES) {
45 z = len = leading_zero + mp_unsigned_bin_size(num);
48 /* find power of 2 that is a multiple of eight and greater than count bits */
50 z = mp_count_bits(num);
51 z = z + (8 - (z & 7));
52 if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --z;
56 /* now we need a length */
61 /* long form (relies on z != 0), assumes length bytes < 128 */
70 /* we need a 0x02 to indicate it's INTEGER */
80 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c,v $ */
81 /* $Revision: 1.4 $ */
82 /* $Date: 2006/04/22 01:22:55 $ */