annotate src/pk/asn1/der/integer/der_encode_integer.c @ 212:a3e31d3983cb libtomcrypt

merge of 24ccd6df75086f1028c185f4ad5946f449bf2e05 and cc138b6aed8d554f52f041a11afecb95e95ed3b8
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 13:24:05 +0000
parents 39d5d58461d6
children d5faf4814ddb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.org
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include "tomcrypt.h"
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /**
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 @file der_encode_integer.c
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 ASN.1 DER, encode an integer, Tom St Denis
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 #ifdef LTC_DER
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 /* Exports a positive bignum as DER format (upto 2^32 bytes in size) */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 /**
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 Store a mp_int integer
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param num The first mp_int to encode
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @param out [out] The destination for the DER encoded integers
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @param outlen [in/out] The max size and resulting size of the DER encoded integers
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 @return CRYPT_OK if successful
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 unsigned long tmplen, y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 int err, leading_zero;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 LTC_ARGCHK(num != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 LTC_ARGCHK(out != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 LTC_ARGCHK(outlen != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 /* find out how big this will be */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 if ((err = der_length_integer(num, &tmplen)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 return err;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if (*outlen < tmplen) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return CRYPT_BUFFER_OVERFLOW;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if (mp_cmp_d(num, 0) != MP_LT) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 /* we only need a leading zero if the msb of the first byte is one */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == MP_YES) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 leading_zero = 1;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 leading_zero = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 /* get length of num in bytes (plus 1 since we force the msbyte to zero) */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 y = mp_unsigned_bin_size(num) + leading_zero;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 leading_zero = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 y = mp_count_bits(num);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 y = y + (8 - (y & 7));
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 y = y >> 3;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 /* now store initial data */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 *out++ = 0x02;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 if (y < 128) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 /* short form */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 *out++ = (unsigned char)y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 } else if (y < 256) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 *out++ = 0x81;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 *out++ = y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 } else if (y < 65536UL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 *out++ = 0x82;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 *out++ = (y>>8)&255;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 *out++ = y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 } else if (y < 16777216UL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 *out++ = 0x83;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 *out++ = (y>>16)&255;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 *out++ = (y>>8)&255;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 *out++ = y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return CRYPT_INVALID_ARG;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 /* now store msbyte of zero if num is non-zero */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 if (leading_zero) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 *out++ = 0x00;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /* if it's not zero store it as big endian */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 if (mp_cmp_d(num, 0) == MP_GT) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 /* now store the mpint */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 if ((err = mp_to_unsigned_bin(num, out)) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 return mpi_to_ltc_error(err);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 } else if (mp_iszero(num) != MP_YES) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 mp_int tmp;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 /* negative */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 if (mp_init(&tmp) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 return CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 /* 2^roundup and subtract */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 y = mp_count_bits(num);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 y = y + (8 - (y & 7));
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 if (mp_2expt(&tmp, y) != MP_OKAY || mp_add(&tmp, num, &tmp) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 mp_clear(&tmp);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 return CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 if ((err = mp_to_unsigned_bin(&tmp, out)) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 mp_clear(&tmp);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 return mpi_to_ltc_error(err);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 mp_clear(&tmp);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 /* we good */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 *outlen = tmplen;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 return CRYPT_OK;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #endif
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c,v $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 /* $Revision: 1.1 $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 /* $Date: 2005/05/16 15:08:11 $ */