Mercurial > dropbear
comparison libtomcrypt/src/pk/ecc/ecc_ansi_x963_export.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 | 9 |
12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b | 10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b |
13 * | 11 * |
14 * All curves taken from NIST recommendation paper of July 1999 | 12 * All curves taken from NIST recommendation paper of July 1999 |
17 #include "tomcrypt.h" | 15 #include "tomcrypt.h" |
18 | 16 |
19 /** | 17 /** |
20 @file ecc_ansi_x963_export.c | 18 @file ecc_ansi_x963_export.c |
21 ECC Crypto, Tom St Denis | 19 ECC Crypto, Tom St Denis |
22 */ | 20 */ |
23 | 21 |
24 #ifdef LTC_MECC | 22 #ifdef LTC_MECC |
25 | 23 |
26 /** ECC X9.63 (Sec. 4.3.6) uncompressed export | 24 /** ECC X9.63 (Sec. 4.3.6) uncompressed export |
27 @param key Key to export | 25 @param key Key to export |
30 Return CRYPT_OK on success | 28 Return CRYPT_OK on success |
31 */ | 29 */ |
32 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen) | 30 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen) |
33 { | 31 { |
34 unsigned char buf[ECC_BUF_SIZE]; | 32 unsigned char buf[ECC_BUF_SIZE]; |
35 unsigned long numlen; | 33 unsigned long numlen, xlen, ylen; |
36 | 34 |
37 LTC_ARGCHK(key != NULL); | 35 LTC_ARGCHK(key != NULL); |
38 LTC_ARGCHK(out != NULL); | |
39 LTC_ARGCHK(outlen != NULL); | 36 LTC_ARGCHK(outlen != NULL); |
40 | 37 |
41 if (ltc_ecc_is_valid_idx(key->idx) == 0) { | 38 if (ltc_ecc_is_valid_idx(key->idx) == 0) { |
42 return CRYPT_INVALID_ARG; | 39 return CRYPT_INVALID_ARG; |
43 } | 40 } |
44 numlen = key->dp->size; | 41 numlen = key->dp->size; |
42 xlen = mp_unsigned_bin_size(key->pubkey.x); | |
43 ylen = mp_unsigned_bin_size(key->pubkey.y); | |
44 | |
45 if (xlen > numlen || ylen > numlen || sizeof(buf) < numlen) { | |
46 return CRYPT_BUFFER_OVERFLOW; | |
47 } | |
45 | 48 |
46 if (*outlen < (1 + 2*numlen)) { | 49 if (*outlen < (1 + 2*numlen)) { |
47 *outlen = 1 + 2*numlen; | 50 *outlen = 1 + 2*numlen; |
48 return CRYPT_BUFFER_OVERFLOW; | 51 return CRYPT_BUFFER_OVERFLOW; |
49 } | 52 } |
50 | 53 |
54 LTC_ARGCHK(out != NULL); | |
55 | |
51 /* store byte 0x04 */ | 56 /* store byte 0x04 */ |
52 out[0] = 0x04; | 57 out[0] = 0x04; |
53 | 58 |
54 /* pad and store x */ | 59 /* pad and store x */ |
55 zeromem(buf, sizeof(buf)); | 60 zeromem(buf, sizeof(buf)); |
56 mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - mp_unsigned_bin_size(key->pubkey.x))); | 61 mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - xlen)); |
57 XMEMCPY(out+1, buf, numlen); | 62 XMEMCPY(out+1, buf, numlen); |
58 | 63 |
59 /* pad and store y */ | 64 /* pad and store y */ |
60 zeromem(buf, sizeof(buf)); | 65 zeromem(buf, sizeof(buf)); |
61 mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - mp_unsigned_bin_size(key->pubkey.y))); | 66 mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - ylen)); |
62 XMEMCPY(out+1+numlen, buf, numlen); | 67 XMEMCPY(out+1+numlen, buf, numlen); |
63 | 68 |
64 *outlen = 1 + 2*numlen; | 69 *outlen = 1 + 2*numlen; |
65 return CRYPT_OK; | 70 return CRYPT_OK; |
66 } | 71 } |
67 | 72 |
68 #endif | 73 #endif |
69 | 74 |
70 /* $Source$ */ | 75 /* ref: $Format:%D$ */ |
71 /* $Revision$ */ | 76 /* git commit: $Format:%H$ */ |
72 /* $Date$ */ | 77 /* commit time: $Format:%ai$ */ |