380
|
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.com |
|
10 */ |
|
11 |
|
12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b |
|
13 * |
|
14 * All curves taken from NIST recommendation paper of July 1999 |
|
15 * Available at http://csrc.nist.gov/cryptval/dss.htm |
|
16 */ |
|
17 #include "tomcrypt.h" |
|
18 |
|
19 /** |
|
20 @file ecc_ansi_x963_import.c |
|
21 ECC Crypto, Tom St Denis |
|
22 */ |
|
23 |
|
24 #ifdef MECC |
|
25 |
|
26 /** Import an ANSI X9.63 format public key |
|
27 @param in The input data to read |
|
28 @param inlen The length of the input data |
|
29 @param key [out] destination to store imported key \ |
|
30 */ |
|
31 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key) |
|
32 { |
|
33 return ecc_ansi_x963_import_ex(in, inlen, key, NULL); |
|
34 } |
|
35 |
|
36 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp) |
|
37 { |
|
38 int x, err; |
|
39 |
|
40 LTC_ARGCHK(in != NULL); |
|
41 LTC_ARGCHK(key != NULL); |
|
42 |
|
43 /* must be odd */ |
|
44 if ((inlen & 1) == 0) { |
|
45 return CRYPT_INVALID_ARG; |
|
46 } |
|
47 |
|
48 /* init key */ |
|
49 if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) { |
|
50 return CRYPT_MEM; |
|
51 } |
|
52 |
|
53 /* check for 4, 6 or 7 */ |
|
54 if (in[0] != 4 && in[0] != 6 && in[0] != 7) { |
|
55 err = CRYPT_INVALID_PACKET; |
|
56 goto error; |
|
57 } |
|
58 |
|
59 /* read data */ |
|
60 if ((err = mp_read_unsigned_bin(key->pubkey.x, (unsigned char *)in+1, (inlen-1)>>1)) != CRYPT_OK) { |
|
61 goto error; |
|
62 } |
|
63 |
|
64 if ((err = mp_read_unsigned_bin(key->pubkey.y, (unsigned char *)in+1+((inlen-1)>>1), (inlen-1)>>1)) != CRYPT_OK) { |
|
65 goto error; |
|
66 } |
|
67 if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; } |
|
68 |
|
69 if (dp == NULL) { |
|
70 /* determine the idx */ |
|
71 for (x = 0; ltc_ecc_sets[x].size != 0; x++) { |
|
72 if ((unsigned)ltc_ecc_sets[x].size >= ((inlen-1)>>1)) { |
|
73 break; |
|
74 } |
|
75 } |
|
76 if (ltc_ecc_sets[x].size == 0) { |
|
77 err = CRYPT_INVALID_PACKET; |
|
78 goto error; |
|
79 } |
|
80 /* set the idx */ |
|
81 key->idx = x; |
|
82 key->dp = <c_ecc_sets[x]; |
|
83 } else { |
|
84 if (((inlen-1)>>1) != (unsigned long) dp->size) { |
|
85 err = CRYPT_INVALID_PACKET; |
|
86 goto error; |
|
87 } |
|
88 key->idx = -1; |
|
89 key->dp = dp; |
|
90 } |
|
91 key->type = PK_PUBLIC; |
|
92 |
|
93 /* we're done */ |
|
94 return CRYPT_OK; |
|
95 error: |
|
96 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL); |
|
97 return err; |
|
98 } |
|
99 |
|
100 #endif |
|
101 |
|
102 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_ansi_x963_import.c,v $ */ |
|
103 /* $Revision: 1.9 $ */ |
|
104 /* $Date: 2006/12/04 22:17:46 $ */ |