Skip to content

Commit

Permalink
refactor(INJI-449): simplify usage of methods in node-forge
Browse files Browse the repository at this point in the history
Signed-off-by: Kiruthika Jeyashankar <[email protected]>
  • Loading branch information
KiruthikaJeyashankar committed Nov 29, 2023
1 parent 4b91338 commit a19773d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fileignoreconfig:
- filename: package-lock.json
checksum: 3d98844cbc77fe3721077ea606713cd5adc2f238db1bbc10081141a7e4cd06a9
- filename: lib/jsonld-signatures/suites/ed255192018/ed25519.ts
checksum: f21b4263b6349e4ec7a8d54a5dd05737cd90aa434bab80e804d789334159decf
checksum: 493b6e31144116cb612c24d98b97d8adcad5609c0a52c865a6847ced0a0ddc3a
- filename: components/PasscodeVerify.tsx
checksum: 14654c0f038979fcd0d260170a45894a072f81e0767ca9a0e66935d33b5cc703
- filename: i18n.ts
Expand Down
81 changes: 46 additions & 35 deletions lib/jsonld-signatures/suites/ed255192018/ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@
*/
import {Buffer} from 'buffer';

const forge = require('node-forge');
import forge, {pki, asn1, util, random, md} from 'node-forge';

type PrivateKey = forge.pki.PrivateKey;
type PublicKey = forge.pki.PublicKey;

const {
publicKeyToAsn1,
publicKeyFromAsn1,
privateKeyToAsn1,
privateKeyFromAsn1,
ed25519,
} = pki;
const {toDer, fromDer: forgeFromDer} = asn1;
const {createBuffer} = util;
const {getBytesSync: getRandomBytes} = random;
const {sha256} = md;

// used to export node's public keys to buffers
const publicKeyEncoding = {format: 'der', type: 'spki'};
Expand All @@ -24,15 +39,15 @@ const api = {
* @returns {object} The object with the public and private key material.
*/
async generateKeyPairFromSeed(seedBytes) {
const privateKey: forge.pki.PrivateKey = forgePrivateKey(
const privateKey: PrivateKey = forgePrivateKey(
_privateKeyDerEncode({seedBytes}),
);

// this expects either a PEM encoded key or a node privateKeyObject
const publicKey: forge.pki.PublicKey =
createForgePublicKeyFromPrivateKeyBuffer(privateKeyToBuffer(privateKey));
const publicKey: PublicKey =
createForgePublicKeyFromPrivateKeyBuffer(privateKey);
const publicKeyBuffer: Buffer = Buffer.from(
forge.asn1.toDer(forge.pki.publicKeyToAsn1(publicKey)).getBytes(),
toDer(publicKeyToAsn1(publicKey)).getBytes(),
'binary',
);

Expand All @@ -48,7 +63,7 @@ const api = {
return api.generateKeyPairFromSeed(seed);
},
async sign(privateKeyBytes, data) {
const privateKey: forge.pki.PrivateKey = forgePrivateKey(
const privateKey: PrivateKey = forgePrivateKey(
_privateKeyDerEncode({privateKeyBytes}),
);
const signature: string = forgeSign(data, privateKey);
Expand All @@ -65,45 +80,41 @@ const api = {

export default api;

function forgePrivateKey(privateKeyBuffer: Buffer): forge.pki.PrivateKey {
return forge.pki.privateKeyFromAsn1(fromDer(privateKeyBuffer));
function forgePrivateKey(privateKeyBuffer: Buffer): PrivateKey {
return privateKeyFromAsn1(fromDer(privateKeyBuffer));
}

function fromDer(keyBuffer: Buffer) {
return forge.asn1.fromDer(keyBuffer.toString('binary'));
return forgeFromDer(keyBuffer.toString('binary'));
}

function createForgePublicKeyFromPrivateKeyBuffer(
privateKey: Buffer,
): forge.pki.PublicKey {
const publicKey = forge.pki.ed25519.publicKeyFromPrivateKey({privateKey});
privateKeyObject: PrivateKey,
): PublicKey {
const privateKeyBuffer = privateKeyToBuffer(privateKeyObject);
const publicKey = ed25519.publicKeyFromPrivateKey({
privateKey: privateKeyBuffer,
});
return publicKey;
}

async function createForgePublicKeyFromPublicKeyBuffer(
function createForgePublicKeyFromPublicKeyBuffer(
publicKeyBuffer: Buffer,
): Promise<string> {
const publicKeyObject = forge.pki.publicKeyFromAsn1(fromDer(publicKeyBuffer));
const publicKeyDer = forge.asn1
.toDer(forge.pki.publicKeyToAsn1(publicKeyObject))
.getBytes();
): string {
const publicKeyObject = publicKeyFromAsn1(fromDer(publicKeyBuffer));
const publicKeyDer = toDer(publicKeyToAsn1(publicKeyObject)).getBytes();

return publicKeyDer;
}

function forgeSign(
data: string,
privateKeyObject: forge.pki.PrivateKey,
): string {
const privateKeyBytes = forge.asn1
.toDer(forge.pki.privateKeyToAsn1(privateKeyObject))
.getBytes();
function forgeSign(data: string, privateKeyObject: PrivateKey): string {
const privateKeyBytes = toDer(privateKeyToAsn1(privateKeyObject)).getBytes();

const privateKey = forge.util.createBuffer(privateKeyBytes);
const privateKey = createBuffer(privateKeyBytes);

const signature = forge.pki.ed25519.sign({
const signature = ed25519.sign({
privateKey,
md: forge.md.sha256.create(),
md: sha256.create(),
message: data,
});

Expand All @@ -115,20 +126,20 @@ function forgeVerifyEd25519(
publicKey: string,
signature: string,
): boolean {
return forge.pki.ed25519.verify({
return ed25519.verify({
publicKey: publicKey,
signature: forge.util.createBuffer(signature),
message: forge.util.createBuffer(data),
signature: createBuffer(signature),
message: createBuffer(data),
});
}

function randomBytes(length: number) {
return Buffer.from(forge.random.getBytesSync(length), 'binary');
return Buffer.from(getRandomBytes(length), 'binary');
}

function privateKeyToBuffer(privateKey: forge.pki.PrivateKey): Buffer {
const privateKeyAsn1 = forge.pki.privateKeyToAsn1(privateKey);
const privateKeyDer = forge.asn1.toDer(privateKeyAsn1).getBytes();
function privateKeyToBuffer(privateKey: PrivateKey): Buffer {
const privateKeyAsn1 = privateKeyToAsn1(privateKey);
const privateKeyDer = toDer(privateKeyAsn1).getBytes();

const privateKeyBuffer = Buffer.from(privateKeyDer, 'binary');

Expand Down
1 change: 1 addition & 0 deletions shared/cryptoutil/cryptoUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export async function decryptJson(
}

function encryptWithForge(text: string, key: string): EncryptedOutput {
//iv - initialization vector
const iv = forge.random.getBytesSync(16);
const salt = forge.random.getBytesSync(128);
const encryptionKey = forge.pkcs5.pbkdf2(key, salt, 4, 16);
Expand Down

0 comments on commit a19773d

Please sign in to comment.