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
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
12 * All curves taken from NIST recommendation paper of July 1999
13 * Available at http://csrc.nist.gov/cryptval/dss.htm
18 @file ecc_ansi_x963_import.c
19 ECC Crypto, Tom St Denis
24 /** Import an ANSI X9.63 format public key
25 @param in The input data to read
26 @param inlen The length of the input data
27 @param key [out] destination to store imported key \
29 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
31 return ecc_ansi_x963_import_ex(in, inlen, key, NULL);
34 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp)
38 LTC_ARGCHK(in != NULL);
39 LTC_ARGCHK(key != NULL);
42 if ((inlen & 1) == 0) {
43 return CRYPT_INVALID_ARG;
47 if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
51 /* check for 4, 6 or 7 */
52 if (in[0] != 4 && in[0] != 6 && in[0] != 7) {
53 err = CRYPT_INVALID_PACKET;
58 if ((err = mp_read_unsigned_bin(key->pubkey.x, (unsigned char *)in+1, (inlen-1)>>1)) != CRYPT_OK) {
62 if ((err = mp_read_unsigned_bin(key->pubkey.y, (unsigned char *)in+1+((inlen-1)>>1), (inlen-1)>>1)) != CRYPT_OK) {
65 if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; }
68 /* determine the idx */
69 for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
70 if ((unsigned)ltc_ecc_sets[x].size >= ((inlen-1)>>1)) {
74 if (ltc_ecc_sets[x].size == 0) {
75 err = CRYPT_INVALID_PACKET;
80 key->dp = <c_ecc_sets[x];
82 if (((inlen-1)>>1) != (unsigned long) dp->size) {
83 err = CRYPT_INVALID_PACKET;
89 key->type = PK_PUBLIC;
94 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
100 /* ref: $Format:%D$ */
101 /* git commit: $Format:%H$ */
102 /* commit time: $Format:%ai$ */