Generate private and calculate public keys
For elliptic curves algorithms used in GOST 34.10-2012 it's possible to split generation key procedure on two stages. In the first step we obtain the private key using the random number generator or by manual entry. In the second step procedure calculates an open key for the selected private key.
In different sources we can find different hex view of keys - in Big-endian or in Little-endial. It's a common error for first stages of study. Feel free to use swap procedure
Public key
Generate random key procedure
// Generate random
var random = new Uint8Array(namedCurve.value.indexOf('512') >=0 ? 64 : 32);
gostCrypto.getRandomValues(random);
privateKey.textContent = gostCoding.Hex.encode(random);
Calculate keys procedure
// Generate public key
var algorithm = {name: 'GOST R 34.10'};
if (namedCurve.value.indexOf('512') >= 0)
algorithm.name = algorithm.name + '-512';
// Preset if private key already defined
if (privateKey.textContent)
algorithm.ukm = gostCoding.Hex.decode(privateKey.textContent);
// Set curve parameters
if (namedCurve.value === 'Other') {
algorithm.a = gostCoding.Hex.decode(paramA.value, 'BigEndian');
algorithm.b = gostCoding.Hex.decode(paramB.value, 'BigEndian');
algorithm.p = gostCoding.Hex.decode(paramP.value, 'BigEndian');
algorithm.q = gostCoding.Hex.decode(paramQ.value, 'BigEndian');
algorithm.x = gostCoding.Hex.decode(paramX.value, 'BigEndian');
algorithm.y = gostCoding.Hex.decode(paramY.value, 'BigEndian');
} else
algorithm.namedCurve = namedCurve.value;
// Generate keys
gostCrypto.subtle.generateKey(algorithm, true, ['sign', 'verify']).then(function(keyPair) {
// Store key in secluded place
return gostCrypto.subtle.exportKey('raw', keyPair.privateKey).then(function(result) {
privateKey.textContent = gostCoding.Hex.encode(result);
// Provide the public key to recepient
return gostCrypto.subtle.exportKey('raw', keyPair.publicKey).then(function(result) {
publicKey.textContent = gostCoding.Hex.encode(result);
});
});
});
Swap bytes procedure
// Swap bytes in keys
if (privateKey.textContent)
privateKey.textContent = gostCoding.Hex.encode(gostCoding.Hex.decode(privateKey.textContent, true));
if (publicKey.textContent)
publicKey.textContent = gostCoding.Hex.encode(gostCoding.Hex.decode(publicKey.textContent, true));