comparison der_length_integer.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
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
12 #include "mycrypt.h"
13
14 /* Gets length of DER encoding of num */
15
16 int der_length_integer(mp_int *num, unsigned long *outlen)
17 {
18 unsigned long z, len;
19 int leading_zero;
20
21 _ARGCHK(num != NULL);
22 _ARGCHK(outlen != NULL);
23
24 /* we only need a leading zero if the msb of the first byte is one */
25 if ((mp_count_bits(num) & 7) == 7 || mp_iszero(num) == MP_YES) {
26 leading_zero = 1;
27 } else {
28 leading_zero = 0;
29 }
30
31 /* size for bignum */
32 z = len = leading_zero + mp_unsigned_bin_size(num);
33
34 /* we need a 0x02 */
35 ++len;
36
37 /* now we need a length */
38 if (z < 128) {
39 /* short form */
40 ++len;
41 } else {
42 /* long form (relies on z != 0) */
43 ++len;
44
45 while (z) {
46 ++len;
47 z >>= 8;
48 }
49 }
50
51 *outlen = len;
52 return CRYPT_OK;
53 }
54