Skip to content

Commit 49031cb

Browse files
committed
adopting PK::ECC to the new libtomcrypt API
1 parent 7b46dfc commit 49031cb

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

inc/CryptX_PK_ECC.xs.inc

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ sign_hash(Crypt::PK::ECC self, SV * data, const char * hash_name = NULL, const c
421421
unsigned char buffer[1024], tmp[MAXBLOCKSIZE], *data_ptr = NULL;
422422
unsigned long tmp_len = MAXBLOCKSIZE, buffer_len = 1024;
423423
STRLEN data_len = 0;
424+
ltc_ecc_sig_opts sig_opts = { .prng = &self->pstate, .wprng = self->pindex, .type = LTC_ECCSIG_ANSIX962 };
424425

425426
// Handle dual signature modes for backward compatibility
426427
// For sign_hash_*: if only 2 params passed, treat second as RFC6979 hash
@@ -445,30 +446,28 @@ sign_hash(Crypt::PK::ECC self, SV * data, const char * hash_name = NULL, const c
445446
if (hash_rfc6979_name != NULL) {
446447
hash_rfc6979_id = cryptx_internal_find_hash(hash_rfc6979_name);
447448
if (hash_rfc6979_id == -1) croak("FATAL: find_hash failed for rfc6979 hash '%s'", hash_rfc6979_name);
448-
ECC_SET_RFC6979_HASH_ALG(&self->key, hash_descriptor[hash_rfc6979_id].name);
449+
sig_opts.rfc6979_hash_alg = hash_descriptor[hash_rfc6979_id].name;
449450
} else {
450451
// Clear any previously set RFC6979 hash to ensure non-deterministic signing
451452
// when RFC6979 parameter is not provided. This prevents hidden state from
452453
// previous method calls affecting current signature behavior.
453-
ECC_SET_RFC6979_HASH_ALG(&self->key, NULL);
454+
sig_opts.rfc6979_hash_alg = NULL;
454455
}
455456

456457
if (ix == 2 || ix == 3) {
457-
rv = ecc_sign_hash_rfc7518(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
458-
&self->pstate, self->pindex,
459-
&self->key);
458+
/* rfc7518 */
459+
sig_opts.type = LTC_ECCSIG_RFC7518;
460460
}
461461
else if (ix == 4) {
462-
rv = ecc_sign_hash_eth27(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
463-
&self->pstate, self->pindex,
464-
&self->key);
462+
/* eth27 */
463+
sig_opts.type = LTC_ECCSIG_ETH27;
465464
}
466465
else {
467-
rv = ecc_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
468-
&self->pstate, self->pindex,
469-
&self->key);
466+
/* default */
467+
sig_opts.type = LTC_ECCSIG_ANSIX962;
470468
}
471-
if (rv != CRYPT_OK) croak("FATAL: ecc_sign_hash_ex failed: %s", error_to_string(rv));
469+
rv = ecc_sign_hash_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &sig_opts, &self->key);
470+
if (rv != CRYPT_OK) croak("FATAL: ecc_sign_hash_v2 failed: %s", error_to_string(rv));
472471
RETVAL = newSVpvn((char*)buffer, buffer_len);
473472
}
474473
OUTPUT:
@@ -487,6 +486,7 @@ verify_hash(Crypt::PK::ECC self, SV * sig, SV * data, const char * hash_name = "
487486
unsigned char tmp[MAXBLOCKSIZE], *data_ptr = NULL, *sig_ptr = NULL;
488487
unsigned long tmp_len = MAXBLOCKSIZE;
489488
STRLEN data_len = 0, sig_len = 0;
489+
ltc_ecc_sig_opts sig_opts = { .prng = &self->pstate, .wprng = self->pindex, .type = LTC_ECCSIG_ANSIX962 };
490490

491491
data_ptr = (unsigned char *)SvPVbyte(data, data_len);
492492
sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
@@ -501,14 +501,18 @@ verify_hash(Crypt::PK::ECC self, SV * sig, SV * data, const char * hash_name = "
501501
RETVAL = 1;
502502
stat = 0;
503503
if (ix == 2 || ix == 3) {
504-
rv = ecc_verify_hash_rfc7518(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
504+
/* rfc7518 */
505+
sig_opts.type = LTC_ECCSIG_RFC7518;
505506
}
506507
else if (ix == 4) {
507-
rv = ecc_verify_hash_eth27(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
508+
/* eth27 */
509+
sig_opts.type = LTC_ECCSIG_ETH27;
508510
}
509511
else {
510-
rv = ecc_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
512+
/* default */
513+
sig_opts.type = LTC_ECCSIG_ANSIX962;
511514
}
515+
rv = ecc_verify_hash_v2(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &sig_opts, &stat, &self->key);
512516
if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
513517
}
514518
OUTPUT:
@@ -536,38 +540,35 @@ recovery_pub(Crypt::PK::ECC self, SV * sig, SV* hash, SV* recid = NULL)
536540
recovery_pub_eth = 2
537541
CODE:
538542
{
539-
int rv;
543+
int rv, r = -1;
540544
unsigned char *sig_ptr = NULL, *hash_ptr = NULL;
541545
STRLEN hash_len = 0, sig_len = 0;
542-
int recovery_id = -1;
546+
ltc_ecc_sig_opts sig_opts = { .prng = &self->pstate, .wprng = self->pindex, .type = LTC_ECCSIG_ANSIX962 };
543547

544548
if (recid != NULL) {
545-
recovery_id = (int) SvIV(recid);
546-
} else if (ix != 2) {
549+
r = (int) SvIV(recid);
550+
}
551+
else if (ix != 2) {
547552
if (ix == 1) {
548553
croak("FATAL: recovery_pub_rfc7518 requires recid argument");
549554
} else {
550555
croak("FATAL: recovery_pub requires recid argument");
551556
}
552557
}
558+
sig_opts.recid = &r;
553559

554560
sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
555561
hash_ptr = (unsigned char *)SvPVbyte(hash, hash_len);
556562
if (ix == 1) {
557-
rv = ecc_recover_key(sig_ptr, (unsigned long)sig_len, hash_ptr, (unsigned long) hash_len,
558-
recovery_id, LTC_ECCSIG_RFC7518, &self->key);
563+
sig_opts.type = LTC_ECCSIG_RFC7518;
559564
}
560565
else if (ix == 2) {
561-
/* Ethereum's signature contains the recovery id in the last byte of the signature
562-
* don't need to pass it as a separate argument
563-
*/
564-
rv = ecc_recover_key(sig_ptr, (unsigned long)sig_len, hash_ptr, (unsigned long) hash_len,
565-
recovery_id, LTC_ECCSIG_ETH27, &self->key);
566+
sig_opts.type = LTC_ECCSIG_ETH27;
566567
}
567568
else {
568-
rv = ecc_recover_key(sig_ptr, (unsigned long)sig_len, hash_ptr, (unsigned long) hash_len,
569-
recovery_id, LTC_ECCSIG_ANSIX962, &self->key);
569+
sig_opts.type = LTC_ECCSIG_ANSIX962;
570570
}
571+
rv = ecc_recover_key(sig_ptr, (unsigned long)sig_len, hash_ptr, (unsigned long) hash_len, &sig_opts, &self->key);
571572
if (rv != CRYPT_OK) croak("FATAL: ecc_recover_key failed: %s", error_to_string(rv));
572573
RETVAL = 1;
573574
}

0 commit comments

Comments
 (0)