Package list libcryptx-perl / 23152ca
ltc update Karel Miko 3 years ago
31 changed file(s) with 256 addition(s) and 216 deletion(s). Raw diff Collapse all Expand all
7070 }
7171
7272 #ifdef LTC_CLEAN_STACK
73 static int _md4_compress(hash_state *md, unsigned char *buf)
73 static int _md4_compress(hash_state *md, const unsigned char *buf)
7474 #else
7575 static int md4_compress(hash_state *md, const unsigned char *buf)
7676 #endif
154154 }
155155
156156 #ifdef LTC_CLEAN_STACK
157 static int md4_compress(hash_state *md, unsigned char *buf)
157 static int md4_compress(hash_state *md, const unsigned char *buf)
158158 {
159159 int err;
160160 err = _md4_compress(md, buf);
9595 #endif
9696
9797 #ifdef LTC_CLEAN_STACK
98 static int _md5_compress(hash_state *md, unsigned char *buf)
98 static int _md5_compress(hash_state *md, const unsigned char *buf)
9999 #else
100100 static int md5_compress(hash_state *md, const unsigned char *buf)
101101 #endif
213213 }
214214
215215 #ifdef LTC_CLEAN_STACK
216 static int md5_compress(hash_state *md, unsigned char *buf)
216 static int md5_compress(hash_state *md, const unsigned char *buf)
217217 {
218218 int err;
219219 err = _md5_compress(md, buf);
7878 (a) = ROLc((a), (s));
7979
8080 #ifdef LTC_CLEAN_STACK
81 static int _rmd128_compress(hash_state *md, unsigned char *buf)
81 static int _rmd128_compress(hash_state *md, const unsigned char *buf)
8282 #else
8383 static int rmd128_compress(hash_state *md, const unsigned char *buf)
8484 #endif
252252 }
253253
254254 #ifdef LTC_CLEAN_STACK
255 static int rmd128_compress(hash_state *md, unsigned char *buf)
255 static int rmd128_compress(hash_state *md, const unsigned char *buf)
256256 {
257257 int err;
258258 err = _rmd128_compress(md, buf);
9898
9999
100100 #ifdef LTC_CLEAN_STACK
101 static int _rmd160_compress(hash_state *md, unsigned char *buf)
101 static int _rmd160_compress(hash_state *md, const unsigned char *buf)
102102 #else
103103 static int rmd160_compress(hash_state *md, const unsigned char *buf)
104104 #endif
310310 }
311311
312312 #ifdef LTC_CLEAN_STACK
313 static int rmd160_compress(hash_state *md, unsigned char *buf)
313 static int rmd160_compress(hash_state *md, const unsigned char *buf)
314314 {
315315 int err;
316316 err = _rmd160_compress(md, buf);
7272 (a) = ROLc((a), (s));
7373
7474 #ifdef LTC_CLEAN_STACK
75 static int _rmd256_compress(hash_state *md, unsigned char *buf)
75 static int _rmd256_compress(hash_state *md, const unsigned char *buf)
7676 #else
7777 static int rmd256_compress(hash_state *md, const unsigned char *buf)
7878 #endif
261261 }
262262
263263 #ifdef LTC_CLEAN_STACK
264 static int rmd256_compress(hash_state *md, unsigned char *buf)
264 static int rmd256_compress(hash_state *md, const unsigned char *buf)
265265 {
266266 int err;
267267 err = _rmd256_compress(md, buf);
9393
9494
9595 #ifdef LTC_CLEAN_STACK
96 static int _rmd320_compress(hash_state *md, unsigned char *buf)
96 static int _rmd320_compress(hash_state *md, const unsigned char *buf)
9797 #else
9898 static int rmd320_compress(hash_state *md, const unsigned char *buf)
9999 #endif
324324 }
325325
326326 #ifdef LTC_CLEAN_STACK
327 static int rmd320_compress(hash_state *md, unsigned char *buf)
327 static int rmd320_compress(hash_state *md, const unsigned char *buf)
328328 {
329329 int err;
330330 err = _rmd320_compress(md, buf);
3939 #define F3(x,y,z) (x ^ y ^ z)
4040
4141 #ifdef LTC_CLEAN_STACK
42 static int _sha1_compress(hash_state *md, unsigned char *buf)
42 static int _sha1_compress(hash_state *md, const unsigned char *buf)
4343 #else
4444 static int sha1_compress(hash_state *md, const unsigned char *buf)
4545 #endif
145145 }
146146
147147 #ifdef LTC_CLEAN_STACK
148 static int sha1_compress(hash_state *md, unsigned char *buf)
148 static int sha1_compress(hash_state *md, const unsigned char *buf)
149149 {
150150 int err;
151151 err = _sha1_compress(md, buf);
6363
6464 /* compress 512-bits */
6565 #ifdef LTC_CLEAN_STACK
66 static int _sha256_compress(hash_state * md, unsigned char *buf)
66 static int _sha256_compress(hash_state * md, const unsigned char *buf)
6767 #else
6868 static int sha256_compress(hash_state * md, const unsigned char *buf)
6969 #endif
186186 }
187187
188188 #ifdef LTC_CLEAN_STACK
189 static int sha256_compress(hash_state * md, unsigned char *buf)
189 static int sha256_compress(hash_state * md, const unsigned char *buf)
190190 {
191191 int err;
192192 err = _sha256_compress(md, buf);
8888
8989 /* compress 1024-bits */
9090 #ifdef LTC_CLEAN_STACK
91 static int _sha512_compress(hash_state * md, unsigned char *buf)
91 static int _sha512_compress(hash_state * md, const unsigned char *buf)
9292 #else
9393 static int sha512_compress(hash_state * md, const unsigned char *buf)
9494 #endif
155155
156156 /* compress 1024-bits */
157157 #ifdef LTC_CLEAN_STACK
158 static int sha512_compress(hash_state * md, unsigned char *buf)
158 static int sha512_compress(hash_state * md, const unsigned char *buf)
159159 {
160160 int err;
161161 err = _sha512_compress(md, buf);
638638 }
639639
640640 #ifdef LTC_CLEAN_STACK
641 static int tiger_compress(hash_state *md, unsigned char *buf)
641 static int tiger_compress(hash_state *md, const unsigned char *buf)
642642 {
643643 int err;
644644 err = _tiger_compress(md, buf);
108108
109109
110110 #ifdef LTC_CLEAN_STACK
111 static int whirlpool_compress(hash_state *md, unsigned char *buf)
111 static int whirlpool_compress(hash_state *md, const unsigned char *buf)
112112 {
113113 int err;
114114 err = _whirlpool_compress(md, buf);
527527 #define LTC_ECC_SECP256R1
528528 #define LTC_ECC_SECP384R1
529529 #define LTC_ECC_SECP521R1
530 /* OLD deprecated (but still working) defines */
531 #define LTC_ECC112
532 #define LTC_ECC128
533 #define LTC_ECC160
534 #define LTC_ECC192
535 #define LTC_ECC224
536 #define LTC_ECC256
537 #define LTC_ECC384
538 #define LTC_ECC521
539 #endif
530 #endif
531 #endif
532
533 #if defined(LTC_DER)
534 #ifndef LTC_DER_MAX_RECURSION
535 /* Maximum recursion limit when processing nested ASN.1 types. */
536 #define LTC_DER_MAX_RECURSION 30
537 #endif
540538 #endif
541539
542540 #if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT)
645643 #endif
646644 #endif
647645
646 /* ECC backwards compatibility */
647 #if !defined(LTC_ECC_SECP112R1) && defined(LTC_ECC112)
648 #define LTC_ECC_SECP112R1
649 #undef LTC_ECC112
650 #endif
651 #if !defined(LTC_ECC_SECP128R1) && defined(LTC_ECC128)
652 #define LTC_ECC_SECP128R1
653 #undef LTC_ECC128
654 #endif
655 #if !defined(LTC_ECC_SECP160R1) && defined(LTC_ECC160)
656 #define LTC_ECC_SECP160R1
657 #undef LTC_ECC160
658 #endif
659 #if !defined(LTC_ECC_SECP192R1) && defined(LTC_ECC192)
660 #define LTC_ECC_SECP192R1
661 #undef LTC_ECC192
662 #endif
663 #if !defined(LTC_ECC_SECP224R1) && defined(LTC_ECC224)
664 #define LTC_ECC_SECP224R1
665 #undef LTC_ECC224
666 #endif
667 #if !defined(LTC_ECC_SECP256R1) && defined(LTC_ECC256)
668 #define LTC_ECC_SECP256R1
669 #undef LTC_ECC256
670 #endif
671 #if !defined(LTC_ECC_SECP384R1) && defined(LTC_ECC384)
672 #define LTC_ECC_SECP384R1
673 #undef LTC_ECC384
674 #endif
675 #if !defined(LTC_ECC_SECP512R1) && defined(LTC_ECC521)
676 #define LTC_ECC_SECP521R1
677 #undef LTC_ECC521
678 #endif
679
648680 /* ref: $Format:%D$ */
649681 /* git commit: $Format:%H$ */
650682 /* commit time: $Format:%ai$ */
522522 } \
523523 while (inlen > 0) { \
524524 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
525 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
525 if ((err = compress_name (md, in)) != CRYPT_OK) { \
526526 return err; \
527527 } \
528528 md-> state_var .length += block_size * 8; \
339339
340340 int ecc_test(void);
341341 void ecc_sizes(int *low, int *high);
342 int ecc_get_size(ecc_key *key);
342 int ecc_get_size(const ecc_key *key);
343343
344344 int ecc_get_curve(const char* name_or_oid, const ltc_ecc_curve** cu);
345345 int ecc_set_dp(const ltc_ecc_curve *cu, ecc_key *key);
346346 int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
347347 int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
348 int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
348 int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
349349
350350 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
351351 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
352352 void ecc_free(ecc_key *key);
353353
354 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
354 int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
355355 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
356356 int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);
357357
358 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
358 int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen);
359359 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
360360 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);
361361
362 int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
362 int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
363363 int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key);
364364 int ecc_import_pkcs8(const unsigned char *in, unsigned long inlen, const void *pwd, unsigned long pwdlen, ecc_key *key);
365365 int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key);
366366
367 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
367 int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
368368 unsigned char *out, unsigned long *outlen);
369369
370370 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
371371 unsigned char *out, unsigned long *outlen,
372372 prng_state *prng, int wprng, int hash,
373 ecc_key *key);
373 const ecc_key *key);
374374
375375 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
376376 unsigned char *out, unsigned long *outlen,
377 ecc_key *key);
377 const ecc_key *key);
378378
379379 int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
380380 unsigned char *out, unsigned long *outlen,
381 prng_state *prng, int wprng, ecc_key *key);
381 prng_state *prng, int wprng, const ecc_key *key);
382382
383383 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
384384 unsigned char *out, unsigned long *outlen,
385 prng_state *prng, int wprng, ecc_key *key);
385 prng_state *prng, int wprng, const ecc_key *key);
386386
387387 int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
388388 const unsigned char *hash, unsigned long hashlen,
389 int *stat, ecc_key *key);
389 int *stat, const ecc_key *key);
390390
391391 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
392392 const unsigned char *hash, unsigned long hashlen,
393 int *stat, ecc_key *key);
393 int *stat, const ecc_key *key);
394394
395395
396396 #ifdef LTC_SOURCE
409409 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
410410 int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
411411 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
412 int ltc_ecc_verify_key(ecc_key *key);
412 int ltc_ecc_verify_key(const ecc_key *key);
413413
414414 /* point ops (mp == montgomery digit) */
415415 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
427427 #endif
428428 #if defined(LTC_DER)
429429 " DER "
430 " " NAME_VALUE(LTC_DER_MAX_RECURSION) " "
430431 #endif
431432 #if defined(LTC_PKCS_1)
432433 " PKCS#1 "
125125
126126 #ifdef LTC_DER
127127 /* DER handling */
128 {"LTC_DER", 1},
128129 _C_STRINGIFY(LTC_ASN1_EOL),
129130 _C_STRINGIFY(LTC_ASN1_BOOLEAN),
130131 _C_STRINGIFY(LTC_ASN1_INTEGER),
145146 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
146147 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
147148 _C_STRINGIFY(LTC_ASN1_CUSTOM_TYPE),
149 _C_STRINGIFY(LTC_DER_MAX_RECURSION),
150 #else
151 {"LTC_DER", 0},
148152 #endif
149153
150154 #ifdef LTC_CTR_MODE
4242 */
4343 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out)
4444 {
45 ltc_asn1_list *l;
45 ltc_asn1_list *l, *t;
4646 unsigned long err, identifier, len, totlen, data_offset, id_len, len_len;
4747 void *realloc_tmp;
4848
462462 l->child->parent = l;
463463 }
464464
465 t = l;
466 len_len = 0;
467 while((t != NULL) && (t->child != NULL)) {
468 len_len++;
469 t = t->child;
470 }
471 if (len_len > LTC_DER_MAX_RECURSION) {
472 err = CRYPT_PK_ASN1_ERROR;
473 goto error;
474 }
475
465476 break;
466477
467478 case 0x80: /* Context-specific */
2323 * - http://www.ecc-brainpool.org/download/Domain-parameters.pdf (named: BRAINPOOLP*)
2424 */
2525 const ltc_ecc_curve ltc_ecc_curves[] = {
26 #if defined(LTC_ECC_SECP112R1) || defined(LTC_ECC112)
26 #ifdef LTC_ECC_SECP112R1
2727 {
2828 /* prime */ "DB7C2ABF62E35E668076BEAD208B",
2929 /* A */ "DB7C2ABF62E35E668076BEAD2088",
4747 /* OID */ "1.3.132.0.7"
4848 },
4949 #endif
50 #if defined(LTC_ECC_SECP128R1) || defined(LTC_ECC128)
50 #ifdef LTC_ECC_SECP128R1
5151 {
5252 /* prime */ "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
5353 /* A */ "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC",
7171 /* OID */ "1.3.132.0.29"
7272 },
7373 #endif
74 #if defined(LTC_ECC_SECP160R1) || defined(LTC_ECC160)
74 #ifdef LTC_ECC_SECP160R1
7575 {
7676 /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF",
7777 /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC",
107107 /* OID */ "1.3.132.0.9"
108108 },
109109 #endif
110 #if defined(LTC_ECC_SECP192R1) || defined(LTC_ECC192)
110 #ifdef LTC_ECC_SECP192R1
111111 {
112112 /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF",
113113 /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC",
155155 /* OID */ "1.3.132.0.31"
156156 },
157157 #endif
158 #if defined(LTC_ECC_SECP224R1) || defined(LTC_ECC224)
158 #ifdef LTC_ECC_SECP224R1
159159 {
160160 /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
161161 /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
179179 /* OID */ "1.3.132.0.32"
180180 },
181181 #endif
182 #if defined(LTC_ECC_SECP256R1) || defined(LTC_ECC256)
182 #ifdef LTC_ECC_SECP256R1
183183 {
184184 /* prime */ "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
185185 /* A */ "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
203203 /* OID */ "1.3.132.0.10"
204204 },
205205 #endif
206 #if defined(LTC_ECC_SECP384R1) || defined(LTC_ECC384)
206 #ifdef LTC_ECC_SECP384R1
207207 {
208208 /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
209209 /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC",
215215 /* OID */ "1.3.132.0.34"
216216 },
217217 #endif
218 #if defined(LTC_ECC_SECP521R1) || defined(LTC_ECC521)
218 #ifdef LTC_ECC_SECP521R1
219219 {
220220 /* prime */ "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
221221 /* A */ "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
2121 @param outlen [in/out] Length of destination and final output size
2222 Return CRYPT_OK on success
2323 */
24 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen)
24 int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen)
2525 {
2626 unsigned char buf[ECC_BUF_SIZE];
2727 unsigned long numlen, xlen, ylen;
2626 */
2727 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
2828 unsigned char *out, unsigned long *outlen,
29 ecc_key *key)
29 const ecc_key *key)
3030 {
3131 unsigned char *ecc_shared, *skey, *pub_expt;
3232 unsigned long x, y;
3030 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
3131 unsigned char *out, unsigned long *outlen,
3232 prng_state *prng, int wprng, int hash,
33 ecc_key *key)
33 const ecc_key *key)
3434 {
3535 unsigned char *pub_expt, *ecc_shared, *skey;
3636 ecc_key pubkey;
2323 @param key The key to export
2424 @return CRYPT_OK if successful
2525 */
26 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
26 int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key)
2727 {
2828 int err;
2929 unsigned char flags[1];
1919 @return CRYPT_OK if successful
2020 */
2121
22 int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
22 int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key)
2323 {
24 int err;
25 void *prime, *order, *a, *b, *gx, *gy;
26 unsigned char bin_a[256], bin_b[256], bin_k[256], bin_g[512], bin_xy[512];
27 unsigned long len_a, len_b, len_k, len_g, len_xy;
28 unsigned long cofactor, one = 1;
29 oid_st oid;
30 ltc_asn1_list seq_fieldid[2], seq_curve[2], seq_ecparams[6], seq_priv[4], pub_xy, ecparams;
31 int flag_oid = type & PK_CURVEOID ? 1 : 0;
32 int flag_com = type & PK_COMPRESSED ? 1 : 0;
33 int flag_pri = type & PK_PRIVATE ? 1 : 0;
24 int err;
25 void *prime, *order, *a, *b, *gx, *gy;
26 unsigned char bin_a[256], bin_b[256], bin_k[256], bin_g[512], bin_xy[512];
27 unsigned long len_a, len_b, len_k, len_g, len_xy;
28 unsigned long cofactor, one = 1;
29 oid_st oid;
30 ltc_asn1_list seq_fieldid[2], seq_curve[2], seq_ecparams[6], seq_priv[4], pub_xy, ecparams;
31 int flag_oid = type & PK_CURVEOID ? 1 : 0;
32 int flag_com = type & PK_COMPRESSED ? 1 : 0;
33 int flag_pri = type & PK_PRIVATE ? 1 : 0;
3434
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
37 LTC_ARGCHK(key != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
37 LTC_ARGCHK(key != NULL);
3838
39 if (key->type != PK_PRIVATE && flag_pri) return CRYPT_PK_TYPE_MISMATCH;
39 if (key->type != PK_PRIVATE && flag_pri) return CRYPT_PK_TYPE_MISMATCH;
4040
41 prime = key->dp.prime;
42 order = key->dp.order;
43 b = key->dp.B;
44 a = key->dp.A;
45 gx = key->dp.base.x;
46 gy = key->dp.base.y;
41 prime = key->dp.prime;
42 order = key->dp.order;
43 b = key->dp.B;
44 a = key->dp.A;
45 gx = key->dp.base.x;
46 gy = key->dp.base.y;
4747
48 /* curve param a */
49 len_a = mp_unsigned_bin_size(a);
50 if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
51 if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) goto error;
52 if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* XXX-TODO hack to handle case a == 0 */
48 /* curve param a */
49 len_a = mp_unsigned_bin_size(a);
50 if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
51 if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) { goto error; }
52 if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* handle case a == 0 */
5353
54 /* curve param b */
55 len_b = mp_unsigned_bin_size(b);
56 if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
57 if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) goto error;
58 if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* XXX-TODO hack to handle case b == 0 */
54 /* curve param b */
55 len_b = mp_unsigned_bin_size(b);
56 if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
57 if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) { goto error; }
58 if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* handle case b == 0 */
5959
60 /* base point - (un)compressed based on flag_com */
61 len_g = sizeof(bin_g);
62 if ((err = ltc_ecc_export_point(bin_g, &len_g, gx, gy, key->dp.size, flag_com)) != CRYPT_OK) goto error;
60 /* base point - (un)compressed based on flag_com */
61 len_g = sizeof(bin_g);
62 err = ltc_ecc_export_point(bin_g, &len_g, gx, gy, key->dp.size, flag_com);
63 if (err != CRYPT_OK) { goto error; }
6364
64 /* public key - (un)compressed based on flag_com */
65 len_xy = sizeof(bin_xy);
66 if ((err = ltc_ecc_export_point(bin_xy, &len_xy, key->pubkey.x, key->pubkey.y, key->dp.size, flag_com)) != CRYPT_OK) goto error;
65 /* public key - (un)compressed based on flag_com */
66 len_xy = sizeof(bin_xy);
67 err = ltc_ecc_export_point(bin_xy, &len_xy, key->pubkey.x, key->pubkey.y, key->dp.size, flag_com);
68 if (err != CRYPT_OK) { goto error; }
6769
68 /* co-factor */
69 cofactor = key->dp.cofactor;
70 /* co-factor */
71 cofactor = key->dp.cofactor;
7072
71 /* we support only prime-field EC */
72 if ((err = pk_get_oid(PKA_EC_PRIMEF, &oid)) != CRYPT_OK) goto error;
73 /* we support only prime-field EC */
74 if ((err = pk_get_oid(PKA_EC_PRIMEF, &oid)) != CRYPT_OK) { goto error; }
7375
74 if (flag_oid) {
75 /* from http://tools.ietf.org/html/rfc5912
76
77 ECParameters ::= CHOICE {
78 namedCurve CURVE.&id({NamedCurve}) # OBJECT
79 }
76 if (flag_oid) {
77 /* http://tools.ietf.org/html/rfc5912
78 ECParameters ::= CHOICE {
79 namedCurve CURVE.&id({NamedCurve}) # OBJECT
80 }
8081 */
81 if (key->dp.oidlen == 0) {
82 err = CRYPT_INVALID_ARG;
83 goto error;
84 }
82 if (key->dp.oidlen == 0) { err = CRYPT_INVALID_ARG; goto error; }
8583 LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_OBJECT_IDENTIFIER, key->dp.oid, key->dp.oidlen);
86 }
87 else {
88 /* from http://tools.ietf.org/html/rfc3279
89
90 ECParameters ::= SEQUENCE { # SEQUENCE
91 version INTEGER { ecpVer1(1) } (ecpVer1), # INTEGER :01
92 FieldID ::= SEQUENCE { # SEQUENCE
93 fieldType FIELD-ID.&id({IOSet}), # OBJECT :prime-field
94 parameters FIELD-ID.&Type({IOSet}{@fieldType}) # INTEGER
95 }
96 Curve ::= SEQUENCE { # SEQUENCE
97 a FieldElement ::= OCTET STRING # OCTET STRING
98 b FieldElement ::= OCTET STRING # OCTET STRING
99 seed BIT STRING OPTIONAL
100 }
101 base ECPoint ::= OCTET STRING # OCTET STRING
102 order INTEGER, # INTEGER
103 cofactor INTEGER OPTIONAL # INTEGER
104 }
84 }
85 else {
86 /* http://tools.ietf.org/html/rfc3279
87 ECParameters ::= SEQUENCE { # SEQUENCE
88 version INTEGER { ecpVer1(1) } (ecpVer1) # INTEGER :01
89 FieldID ::= SEQUENCE { # SEQUENCE
90 fieldType FIELD-ID.&id({IOSet}), # OBJECT :prime-field
91 parameters FIELD-ID.&Type({IOSet}{@fieldType}) # INTEGER
92 }
93 Curve ::= SEQUENCE { # SEQUENCE
94 a FieldElement ::= OCTET STRING # OCTET STRING
95 b FieldElement ::= OCTET STRING # OCTET STRING
96 seed BIT STRING OPTIONAL
97 }
98 base ECPoint ::= OCTET STRING # OCTET STRING
99 order INTEGER, # INTEGER
100 cofactor INTEGER OPTIONAL # INTEGER
101 }
105102 */
106103
107104 /* FieldID SEQUENCE */
122119
123120 /* ECParameters used by ECPrivateKey or SubjectPublicKeyInfo below */
124121 LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
125 }
122 }
126123
127 if (flag_pri) {
128 /* private key format: http://tools.ietf.org/html/rfc5915
129
130 ECPrivateKey ::= SEQUENCE { # SEQUENCE
131 version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), # INTEGER :01
132 privateKey OCTET STRING, # OCTET STRING
133 [0] ECParameters # see above
134 [1] publicKey # BIT STRING
135 }
124 if (flag_pri) {
125 /* http://tools.ietf.org/html/rfc5915
126 ECPrivateKey ::= SEQUENCE { # SEQUENCE
127 version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1) # INTEGER :01
128 privateKey OCTET STRING, # OCTET STRING
129 [0] ECParameters # see above
130 [1] publicKey # BIT STRING
131 }
136132 */
137133
138134 /* private key */
139135 len_k = mp_unsigned_bin_size(key->k);
140 if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
141 if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) goto error;
136 if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
137 if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) { goto error; }
142138
143 LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy);
144 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1);
145 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, len_k);
139 LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy);
140 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1);
141 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, len_k);
146142 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, &ecparams); /* context specific 0 */
147143 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, &pub_xy); /* context specific 1 */
148144
149145 err = der_encode_sequence(seq_priv, 4, out, outlen);
150 }
151 else {
152 /* public key format: http://tools.ietf.org/html/rfc5480
153
154 SubjectPublicKeyInfo ::= SEQUENCE { # SEQUENCE
155 AlgorithmIdentifier ::= SEQUENCE { # SEQUENCE
156 algorithm OBJECT IDENTIFIER # OBJECT :id-ecPublicKey
157 ECParameters # see above
158 }
159 subjectPublicKey BIT STRING # BIT STRING
160 }
146 }
147 else {
148 /* http://tools.ietf.org/html/rfc5480
149 SubjectPublicKeyInfo ::= SEQUENCE { # SEQUENCE
150 AlgorithmIdentifier ::= SEQUENCE { # SEQUENCE
151 algorithm OBJECT IDENTIFIER # OBJECT :id-ecPublicKey
152 ECParameters # see above
153 }
154 subjectPublicKey BIT STRING # BIT STRING
155 }
161156 */
162 err = x509_encode_subject_public_key_info( out, outlen,
163 PKA_EC, bin_xy, len_xy,
164 ecparams.type, ecparams.data, ecparams.size );
165 }
157 err = x509_encode_subject_public_key_info( out, outlen, PKA_EC, bin_xy, len_xy,
158 ecparams.type, ecparams.data, ecparams.size );
159 }
166160
167161 error:
168 return err;
162 return err;
169163 }
170164
171165 #endif
1010
1111 #ifdef LTC_MECC
1212
13 const struct {
13 static const struct {
1414 const char *OID;
1515 const char *names[6];
1616 } _curve_names[] = {
17 #if defined(LTC_ECC_SECP112R1) || defined(LTC_ECC112)
17 #ifdef LTC_ECC_SECP112R1
1818 {
1919 "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL }
2020 },
2424 "1.3.132.0.7", { "SECP112R2", NULL }
2525 },
2626 #endif
27 #if defined(LTC_ECC_SECP128R1) || defined(LTC_ECC128)
27 #ifdef LTC_ECC_SECP128R1
2828 {
2929 "1.3.132.0.28", { "SECP128R1", "ECC-128", NULL }
3030 },
3434 "1.3.132.0.29", { "SECP128R2", NULL }
3535 },
3636 #endif
37 #if defined(LTC_ECC_SECP160R1) || defined(LTC_ECC160)
37 #ifdef LTC_ECC_SECP160R1
3838 {
3939 "1.3.132.0.8", { "SECP160R1", "ECC-160", NULL }
4040 },
4949 "1.3.132.0.9", { "SECP160K1", NULL }
5050 },
5151 #endif
52 #if defined(LTC_ECC_SECP192R1) || defined(LTC_ECC192)
52 #ifdef LTC_ECC_SECP192R1
5353 {
5454 "1.2.840.10045.3.1.1", { "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192", NULL }
5555 },
6969 "1.3.132.0.31", { "SECP192K1", NULL }
7070 },
7171 #endif
72 #if defined(LTC_ECC_SECP224R1) || defined(LTC_ECC224)
72 #ifdef LTC_ECC_SECP224R1
7373 {
7474 "1.3.132.0.33", { "SECP224R1", "NISTP224", "ECC-224", "P-224", NULL }
7575 },
7979 "1.3.132.0.32", { "SECP224K1", NULL }
8080 },
8181 #endif
82 #if defined(LTC_ECC_SECP256R1) || defined(LTC_ECC256)
82 #ifdef LTC_ECC_SECP256R1
8383 {
8484 "1.2.840.10045.3.1.7", { "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256", NULL }
8585 },
8989 "1.3.132.0.10", { "SECP256K1", NULL }
9090 },
9191 #endif
92 #if defined(LTC_ECC_SECP384R1) || defined(LTC_ECC384)
92 #ifdef LTC_ECC_SECP384R1
9393 {
9494 "1.3.132.0.34", { "SECP384R1", "NISTP384", "ECC-384", "P-384", NULL }
9595 },
9696 #endif
97 #if defined(LTC_ECC_SECP521R1) || defined(LTC_ECC521)
97 #ifdef LTC_ECC_SECP521R1
9898 {
9999 "1.3.132.0.35", { "SECP521R1", "NISTP521", "ECC-521", "P-521", NULL }
100100 },
1818 Return CRYPT_OK on success
1919 */
2020
21 int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
21 int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key)
2222 {
2323 unsigned long size, ksize;
2424 int err, compressed;
2020 @param key The key to get the size of
2121 @return The size (octets) of the key or INT_MAX on error
2222 */
23 int ecc_get_size(ecc_key *key)
23 int ecc_get_size(const ecc_key *key)
2424 {
2525 if (key == NULL) {
2626 return INT_MAX;
1414 {
1515 void *prime, *order, *a, *b, *gx, *gy;
1616 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
17 unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE], bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
17 unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE];
18 unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
1819 unsigned long len_a, len_b, len_k, len_g, len_xy, len_oid, len;
1920 unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16], curveoid[16];
2021 char OID[256];
2930
3031 len_xy = sizeof(bin_xy);
3132 len_oid = 16;
32 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
33 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy,
34 LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
3335 if (err == CRYPT_OK) {
3436 /* load curve parameters for given curve OID */
3537 len = sizeof(OID);
4446 /* ### 2. try to load public key - curve parameters included */
4547
4648 /* ECParameters SEQUENCE */
47 LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
48 LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
49 LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
50 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
51 LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
52 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
49 LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
50 LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
51 LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
52 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
53 LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
54 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
5355 seq_ecparams[5].optional = 1;
5456 /* FieldID SEQUENCE */
55 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
56 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
57 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
58 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
5759 /* Curve SEQUENCE */
58 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
59 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
60 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
60 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
61 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
62 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
6163 seq_curve[2].optional = 1;
6264 /* try to load public key */
6365 len_xy = sizeof(bin_xy);
6971 len_b = seq_curve[1].size;
7072 len_g = seq_ecparams[3].size;
7173 /* create bignums */
72 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
73 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
74 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
74 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
75 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
76 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
7577 /* load curve parameters */
7678 if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
7779 /* load public key */
78 if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
80 if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
7981 goto success;
8082 }
8183
8284 /* ### 3. try to load private key - no curve parameters just curve OID */
8385
8486 /* ECPrivateKey SEQUENCE */
85 LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL);
86 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
87 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
88 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
87 LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL);
88 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
89 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
90 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
8991 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
9092 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
9193
120122 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
121123 seq_ecparams[5].optional = 1;
122124 /* FieldID SEQUENCE */
123 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
124 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
125 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
126 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
125127 /* Curve SEQUENCE */
126 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
127 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
128 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
128 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
129 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
130 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
129131 seq_curve[2].optional = 1;
130132 /* try to load private key */
131133 err = der_decode_sequence(in, inlen, seq_priv, 4);
136138 len_b = seq_curve[1].size;
137139 len_g = seq_ecparams[3].size;
138140 /* create bignums */
139 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
140 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
141 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
141 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
142 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
143 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
142144 /* load curve parameters */
143145 if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
144146 /* load private+public key */
145 if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
147 if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
146148 goto success;
147149 }
148150
149151 /* ### 5. backward compatibility - try to load old-DER format */
150152
151 if ((err = ecc_import(in, inlen, key)) != CRYPT_OK) { goto error; }
153 if ((err = ecc_import(in, inlen, key)) != CRYPT_OK) { goto error; }
152154
153155 success:
154156 err = CRYPT_OK;
2323 @param outlen [in/out] The max size and resulting size of the shared secret
2424 @return CRYPT_OK if successful
2525 */
26 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
26 int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
2727 unsigned char *out, unsigned long *outlen)
2828 {
2929 unsigned long x;
1717
1818 static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen,
1919 unsigned char *out, unsigned long *outlen,
20 prng_state *prng, int wprng, ecc_key *key, int sigformat)
20 prng_state *prng, int wprng, const ecc_key *key, int sigformat)
2121 {
2222 ecc_key pubkey;
2323 void *r, *s, *e, *p;
131131 */
132132 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
133133 unsigned char *out, unsigned long *outlen,
134 prng_state *prng, int wprng, ecc_key *key)
134 prng_state *prng, int wprng, const ecc_key *key)
135135 {
136136 return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 0);
137137 }
149149 */
150150 int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
151151 unsigned char *out, unsigned long *outlen,
152 prng_state *prng, int wprng, ecc_key *key)
152 prng_state *prng, int wprng, const ecc_key *key)
153153 {
154154 return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 1);
155155 }
1717
1818 static int _ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
1919 const unsigned char *hash, unsigned long hashlen,
20 int *stat, ecc_key *key, int sigformat)
20 int *stat, const ecc_key *key, int sigformat)
2121 {
2222 ecc_point *mG = NULL, *mQ = NULL;
2323 void *r, *s, *v, *w, *u1, *u2, *e, *p, *m, *a, *a_plus3 = NULL, *mu = NULL, *ma = NULL;
3636
3737 /* allocate ints */
3838 if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &e, &a_plus3, NULL)) != CRYPT_OK) {
39 return CRYPT_MEM;
39 return err;
4040 }
4141
4242 p = key->dp.order;
170170 */
171171 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
172172 const unsigned char *hash, unsigned long hashlen,
173 int *stat, ecc_key *key)
173 int *stat, const ecc_key *key)
174174 {
175175 return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 0);
176176 }
187187 */
188188 int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
189189 const unsigned char *hash, unsigned long hashlen,
190 int *stat, ecc_key *key)
190 int *stat, const ecc_key *key)
191191 {
192192 return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 1);
193193 }
1818 @return CRYPT_OK if successful
1919 */
2020
21 int ltc_ecc_verify_key(ecc_key *key)
21 int ltc_ecc_verify_key(const ecc_key *key)
2222 {
2323 int err, inf;
24 void *prime = NULL;
25 void *order = NULL;
26 void *a = NULL;
2724 ecc_point *point;
28
29 prime = key->dp.prime;
30 order = key->dp.order;
31 a = key->dp.A;
25 void *prime = key->dp.prime;
26 void *order = key->dp.order;
27 void *a = key->dp.A;
3228
3329 /* Test 1: Are the x and y points of the public key in the field? */
3430 if (ltc_mp.compare_d(key->pubkey.z, 1) == LTC_MP_EQ) {