1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 /**
7   @file ecc_ansi_x963_import.c
8   ECC Crypto, Tom St Denis
9 */
10 
11 #ifdef LTC_MECC
12 
13 /** Import an ANSI X9.63 format public key
14   @param in      The input data to read
15   @param inlen   The length of the input data
16   @param key     [out] destination to store imported key \
17 */
ecc_ansi_x963_import(const unsigned char * in,unsigned long inlen,ecc_key * key)18 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
19 {
20    return ecc_ansi_x963_import_ex(in, inlen, key, NULL);
21 }
22 
ecc_ansi_x963_import_ex(const unsigned char * in,unsigned long inlen,ecc_key * key,const ltc_ecc_curve * cu)23 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu)
24 {
25    int err;
26 
27    LTC_ARGCHK(in  != NULL);
28    LTC_ARGCHK(key != NULL);
29 
30    /* must be odd */
31    if ((inlen & 1) == 0) {
32       return CRYPT_INVALID_ARG;
33    }
34 
35    /* initialize key->dp */
36    if (cu == NULL) {
37       /* this case works only for uncompressed public keys  */
38       if ((err = ecc_set_curve_by_size((inlen-1)>>1, key)) != CRYPT_OK)             { return err; }
39    }
40    else {
41       /* this one works for both compressed / uncompressed pubkeys */
42       if ((err = ecc_set_curve(cu, key)) != CRYPT_OK)                               { return err; }
43    }
44 
45    /* load public key */
46    if ((err = ecc_set_key((unsigned char *)in, inlen, PK_PUBLIC, key)) != CRYPT_OK) { return err; }
47 
48    /* we're done */
49    return CRYPT_OK;
50 }
51 
52 #endif
53