]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ecc_ansi_x963_import.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ecc_ansi_x963_import.c
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
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11  *
12  * All curves taken from NIST recommendation paper of July 1999
13  * Available at http://csrc.nist.gov/cryptval/dss.htm
14  */
15 #include "tomcrypt.h"
16
17 /**
18   @file ecc_ansi_x963_import.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
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 \
28 */
29 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
30 {
31    return ecc_ansi_x963_import_ex(in, inlen, key, NULL);
32 }
33
34 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp)
35 {
36    int x, err;
37
38    LTC_ARGCHK(in  != NULL);
39    LTC_ARGCHK(key != NULL);
40
41    /* must be odd */
42    if ((inlen & 1) == 0) {
43       return CRYPT_INVALID_ARG;
44    }
45
46    /* init key */
47    if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
48       return CRYPT_MEM;
49    }
50
51    /* check for 4, 6 or 7 */
52    if (in[0] != 4 && in[0] != 6 && in[0] != 7) {
53       err = CRYPT_INVALID_PACKET;
54       goto error;
55    }
56
57    /* read data */
58    if ((err = mp_read_unsigned_bin(key->pubkey.x, (unsigned char *)in+1, (inlen-1)>>1)) != CRYPT_OK) {
59       goto error;
60    }
61
62    if ((err = mp_read_unsigned_bin(key->pubkey.y, (unsigned char *)in+1+((inlen-1)>>1), (inlen-1)>>1)) != CRYPT_OK) {
63       goto error;
64    }
65    if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; }
66
67    if (dp == NULL) {
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)) {
71             break;
72          }
73       }
74       if (ltc_ecc_sets[x].size == 0) {
75          err = CRYPT_INVALID_PACKET;
76          goto error;
77       }
78       /* set the idx */
79       key->idx  = x;
80       key->dp = &ltc_ecc_sets[x];
81    } else {
82       if (((inlen-1)>>1) != (unsigned long) dp->size) {
83          err = CRYPT_INVALID_PACKET;
84          goto error;
85       }
86       key->idx = -1;
87       key->dp  = dp;
88    }
89    key->type = PK_PUBLIC;
90
91    /* we're done */
92    return CRYPT_OK;
93 error:
94    mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
95    return err;
96 }
97
98 #endif
99
100 /* ref:         $Format:%D$ */
101 /* git commit:  $Format:%H$ */
102 /* commit time: $Format:%ai$ */