Skip to content

Commit

Permalink
Test ECDSA public key generation against testvectors
Browse files Browse the repository at this point in the history
- make ECDSA public key generation an own method so we can test it
- test against the testvectors
  • Loading branch information
cplussharp committed Nov 14, 2021
1 parent f203728 commit fae1b81
Show file tree
Hide file tree
Showing 3 changed files with 696 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/ecdsa-modified-1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,37 @@ KJUR.crypto.ECDSA = function(params) {
this.generateKeyPairHex = function() {
var biN = this.ecparams['n'];
var biPrv = this.getBigRandom(biN);
var epPub = this.ecparams['G'].multiply(biPrv);
var biX = epPub.getX().toBigInteger();
var biY = epPub.getY().toBigInteger();

var charlen = this.ecparams.keycharlen;
var hPrv = ("0000000000" + biPrv.toString(16)).slice(- charlen);
var hX = ("0000000000" + biX.toString(16)).slice(- charlen);
var hY = ("0000000000" + biY.toString(16)).slice(- charlen);
var hPub = "04" + hX + hY;

this.setPrivateKeyHex(hPrv);
this.setPublicKeyHex(hPub);
hPub = this.generatePublicKeyHex();
return {'ecprvhex': hPrv, 'ecpubhex': hPub};
};

/**
* generate public key for EC private key
* @name generatePublicKeyHex
* @memberOf KJUR.crypto.ECDSA#
* @function
* @return {String} associative array of hexadecimal string of private and public key
* @example
* var ec = new KJUR.crypto.ECDSA({'curve': 'secp256r1', 'prv': prvHex});
* var pubhex = ec.generatePublicKeyHex(); // hexadecimal string of EC public key
* var pub ec.getPublicKeyXYHex() → { x: '01bacf...', y: 'c3bc22...' }
*/
this.generatePublicKeyHex = function() {
var biPrv = new _BigInteger(this.prvKeyHex, 16);
var epPub = this.ecparams['G'].multiply(biPrv);
var biX = epPub.getX().toBigInteger();
var biY = epPub.getY().toBigInteger();
var charlen = this.ecparams.keycharlen;;
var hX = ("0000000000" + biX.toString(16)).slice(- charlen);
var hY = ("0000000000" + biY.toString(16)).slice(- charlen);
var hPub = "04" + hX + hY;
this.setPublicKeyHex(hPub);
return hPub;
}

this.signWithMessageHash = function(hashHex) {
return this.signHex(hashHex, this.prvKeyHex);
};
Expand Down
Loading

0 comments on commit fae1b81

Please sign in to comment.