Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// generate EC key
let keys = session.generateKeyPair(graphene.KeyGenMechanism.ECDSA, {
keyType: graphene.KeyType.ECDSA,
token: false,
derive: true,
paramsECDSA: graphene.NamedCurve.getByName("secp192r1").value
}, {
keyType: graphene.KeyType.ECDSA,
token: false,
derive: true
});
// derive algorithm
let alg = {
name: "ECDH1_DERIVE",
params: new graphene.EcdhParams(
graphene.EcKdf.SHA1,
null,
keys.publicKey.getAttribute({ pointEC: null }).pointEC)
};
// Template for derived key
let template = {
"class": graphene.ObjectClass.SECRET_KEY,
"token": false,
"keyType": graphene.KeyType.AES,
"valueLen": 256 / 8,
"encrypt": true,
"decrypt": true
};
// Key derivation
const template: graphene.ITemplate = {
token: false,
sensitive: false,
class: graphene.ObjectClass.SECRET_KEY,
keyType: graphene.KeyType.GENERIC_SECRET,
extractable: true,
encrypt: true,
decrypt: true,
valueLen: valueLen >> 3,
};
// derive key
const ecPoint = (algorithm.public as EcCryptoKey).key.getAttribute({ pointEC: null }).pointEC!;
this.crypto.session.deriveKey(
{
name: "ECDH1_DERIVE",
params: new graphene.EcdhParams(
graphene.EcKdf.NULL,
null as any,
ecPoint, // CKA_EC_POINT
),
},
baseKey.key,
template,
(err, key) => {
if (err) {
reject(err);
} else {
const secretKey = key.toType();
const value = secretKey.getAttribute({ value: null }).value as Buffer;
resolve(new Uint8Array(value.slice(0, length >> 3)).buffer);
}
});
return new Promise((resolve, reject) => {
const template = aes.create_template(session!, derivedKeyType, extractable, keyUsages);
template.valueLen = derivedKeyType.length >> 3;
// derive key
session!.deriveKey(
{
name: "ECDH1_DERIVE",
params: new graphene.EcdhParams(
EcKdf.NULL,
null as any,
(algorithm.public as CryptoKey).key.getAttribute({ pointEC: null }).pointEC!, // CKA_EC_POINT
),
},
baseKey.key,
template,
(err, key) => {
if (err) {
reject(err);
} else {
resolve(new CryptoKey(key, derivedKeyType));
}
});
});
});