annotate src/pk/asn1/der/integer/der_decode_integer.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents
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_decode_integer.c
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 ASN.1 DER, decode 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 /**
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 Read a mp_int integer
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param in The DER encoded data
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param inlen Size of DER encoded data
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @param num The first mp_int to decode
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @return CRYPT_OK if successful
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num)
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 unsigned long x, y, z;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 int err;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 LTC_ARGCHK(num != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 LTC_ARGCHK(in != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 /* min DER INTEGER is 0x02 01 00 == 0 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if (inlen < (1 + 1 + 1)) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 /* ok expect 0x02 when we AND with 0001 1111 [1F] */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 x = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if ((in[x++] & 0x1F) != 0x02) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return CRYPT_INVALID_PACKET;
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 /* now decode the len stuff */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 z = in[x++];
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 if ((z & 0x80) == 0x00) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /* short form */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 /* will it overflow? */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 if (x + z > inlen) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 /* no so read it */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 if ((err = mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in + x, z))) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return err;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 /* long form */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 z &= 0x7F;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 /* will number of length bytes overflow? (or > 4) */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 if (((x + z) > inlen) || (z > 4) || (z == 0)) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 /* now read it in */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 y = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 while (z--) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 y = ((unsigned long)(in[x++])) | (y << 8);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 /* now will reading y bytes overrun? */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 if ((x + y) > inlen) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 /* no so read it */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if ((err = mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in + x, y))) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 return err;
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 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 /* see if it's negative */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 if (in[x] & 0x80) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 mp_int tmp;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 if (mp_init(&tmp) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 return CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 if (mp_2expt(&tmp, mp_count_bits(num)) != MP_OKAY || mp_sub(num, &tmp, num) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 mp_clear(&tmp);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 return CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 mp_clear(&tmp);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 return CRYPT_OK;
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 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 #endif
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c,v $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 /* $Revision: 1.2 $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 /* $Date: 2005/06/01 00:06:05 $ */