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
12
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
14
* All curves taken from NIST recommendation paper of July 1999
15
* Available at http://csrc.nist.gov/cryptval/dss.htm
20
@file ecc_ansi_x963_import.c
21
ECC Crypto, Tom St Denis
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 \
31
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
33
return ecc_ansi_x963_import_ex(in, inlen, key, NULL);
36
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp)
40
LTC_ARGCHK(in != NULL);
41
LTC_ARGCHK(key != NULL);
44
if ((inlen & 1) == 0) {
45
return CRYPT_INVALID_ARG;
49
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
53
/* check for 4, 6 or 7 */
54
if (in[0] != 4 && in[0] != 6 && in[0] != 7) {
55
err = CRYPT_INVALID_PACKET;
60
if ((err = mp_read_unsigned_bin(key->pubkey.x, (unsigned char *)in+1, (inlen-1)>>1)) != CRYPT_OK) {
64
if ((err = mp_read_unsigned_bin(key->pubkey.y, (unsigned char *)in+1+((inlen-1)>>1), (inlen-1)>>1)) != CRYPT_OK) {
67
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; }
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)) {
76
if (ltc_ecc_sets[x].size == 0) {
77
err = CRYPT_INVALID_PACKET;
82
key->dp = <c_ecc_sets[x];
84
if (((inlen-1)>>1) != (unsigned long) dp->size) {
85
err = CRYPT_INVALID_PACKET;
91
key->type = PK_PUBLIC;
96
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
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 $ */