Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ expect(resultAlice).toStrictEqual(resultBob);
// Symmetric encryption
const data = utils.UTF8ToUint8('Sensitive information to encrypt'); // convert to Uint8Array
const additionalData = 'Additional non-secret data';
const key = await symmetric.genSymmetricCryptoKey(); // CryptoKey
const key = await symmetric.genSymmetricKey();
const ciphertext: Uint8Array = await symmetric.encryptSymmetrically(key, data, additionalData);
const plainText = await symmetric.decryptSymmetrically(encryptionKey, ciphertext, additionalData);
expect(data).toStrictEqual(plainText);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@noble/ciphers": "^2.1.1",
"@noble/curves": "^2.0.1",
"@noble/hashes": "^2.0.1",
"@noble/post-quantum": "^0.5.2",
"@scure/bip39": "^2.0.1",
Expand Down
27 changes: 14 additions & 13 deletions src/asymmetric-crypto/ellipticCurve.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { ECC_ALGORITHM, AES_KEY_BIT_LENGTH } from '../constants';
import { x25519 } from '@noble/curves/webcrypto.js';

/**
* Derives secret key from the other user's public key and own private key
*
* @param otherUserPublicKey - The public key of the other user
* @param ownPrivateKey - The private key
* @param aliceSecX - The secret key of the user deriving the shared secret key
* @param bobPubX - The public key of the other user
* @returns The derived secret key bits
*/
export async function deriveSecretKey(otherUserPublicKey: CryptoKey, ownPrivateKey: CryptoKey): Promise<Uint8Array> {
export async function deriveSecretKey(aliceSecX: Uint8Array, bobPubX: Uint8Array): Promise<Uint8Array> {
try {
const result = await crypto.subtle.deriveBits(
{
name: ECC_ALGORITHM,
public: otherUserPublicKey,
},
ownPrivateKey,
AES_KEY_BIT_LENGTH,
);
return new Uint8Array(result);
return await x25519.getSharedSecret(aliceSecX, bobPubX);
} catch (error) {
throw new Error('Failed to derive elliptic curve secret key', { cause: error });
}
}

/**
* Generates elliptic curve key pair
*
* @returns The generated key pair
*/
export async function generateEccKeys(): Promise<{ secretKey: Uint8Array; publicKey: Uint8Array }> {
return x25519.keygen();
}
1 change: 0 additions & 1 deletion src/asymmetric-crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './ellipticCurve';
export * from './keys';
90 changes: 0 additions & 90 deletions src/asymmetric-crypto/keys.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export const KEY_WRAPPING_ALGORITHM = 'AES-KW';
export const KEY_FORMAT = 'raw';
export const CONTEXT_WRAPPING = 'CRYPTO library 2025-08-22 18:10:00 key derived from ecc and kyber secrets';

export const ECC_ALGORITHM = 'X25519';

export const CONTEXT_ENC_KEYSTORE = 'CRYPTO library 2025-07-30 16:18:03 key for opening encryption keys keystore';
export const CONTEXT_RECOVERY = 'CRYPTO library 2025-07-30 16:20:00 key for account recovery';
export const CONTEXT_INDEX = 'CRYPTO library 2025-07-30 17:20:00 key for protecting current search indices';
Expand Down
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export {
deriveSecretKey,
generateEccKeys,
exportPublicKey,
importPublicKey,
exportPrivateKey,
importPrivateKey,
} from './asymmetric-crypto';
export { deriveSecretKey, generateEccKeys } from './asymmetric-crypto';
export {
deriveSymmetricKeyFromTwoKeys,
deriveSymmetricCryptoKeyFromTwoKeys,
Expand Down
10 changes: 0 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export type UserWithPublicKey = User & {
publicHybridKey: Uint8Array;
};

export type PublicKeys = {
eccPublicKey: CryptoKey;
kyberPublicKey: Uint8Array;
};

export type PublicKeysBase64 = {
eccPublicKeyBase64: string;
kyberPublicKeyBase64: string;
};

export type HybridKeyPair = {
publicKey: Uint8Array;
secretKey: Uint8Array;
Expand Down
16 changes: 11 additions & 5 deletions tests/asymmetric-crypto/ecc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { describe, expect, it } from 'vitest';
import { generateEccKeys, deriveSecretKey } from '../../src/asymmetric-crypto';

describe('Test ecc functions', () => {
it('should generate elliptic curves key pair', async () => {
const keyPair = await generateEccKeys();
expect(keyPair.publicKey).toBeInstanceOf(Uint8Array);
expect(keyPair.secretKey).toBeInstanceOf(Uint8Array);
});

it('should derive the same keys for Bob and Alice', async () => {
const keysAlice = await generateEccKeys();
const keysBob = await generateEccKeys();

const resultAlice = await deriveSecretKey(keysBob.publicKey, keysAlice.privateKey);
const resultBob = await deriveSecretKey(keysAlice.publicKey, keysBob.privateKey);
const resultAlice = await deriveSecretKey(keysBob.secretKey, keysAlice.publicKey);
const resultBob = await deriveSecretKey(keysAlice.secretKey, keysBob.publicKey);

expect(resultAlice).toStrictEqual(resultBob);
});
Expand All @@ -17,16 +23,16 @@ describe('Test ecc functions', () => {
const keysBob = await generateEccKeys();
const keysEve = await generateEccKeys();

const resultAliceEve = await deriveSecretKey(keysEve.publicKey, keysAlice.privateKey);
const resultAliceBob = await deriveSecretKey(keysBob.publicKey, keysAlice.privateKey);
const resultAliceEve = await deriveSecretKey(keysEve.secretKey, keysAlice.publicKey);
const resultAliceBob = await deriveSecretKey(keysBob.secretKey, keysAlice.publicKey);

expect(resultAliceBob).not.toStrictEqual(resultAliceEve);
});

it('should throw an error if cannot derive', async () => {
const keysAlice = await generateEccKeys();

await expect(deriveSecretKey(keysAlice.privateKey, keysAlice.privateKey)).rejects.toThrowError(
await expect(deriveSecretKey(keysAlice.secretKey, new Uint8Array())).rejects.toThrowError(
/Failed to derive elliptic curve secret key/,
);
});
Expand Down
107 changes: 0 additions & 107 deletions tests/asymmetric-crypto/keys.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-2.1.1.tgz#c8c74fcda8c3d1f88797d0ecda24f9fc8b92b052"
integrity sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw==

"@noble/curves@~2.0.0":
"@noble/curves@^2.0.1", "@noble/curves@~2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-2.0.1.tgz#64ba8bd5e8564a02942655602515646df1cdb3ad"
integrity sha512-vs1Az2OOTBiP4q0pwjW5aF0xp9n4MxVrmkFBxc6EKZc6ddYx5gaZiAsZoq0uRRXWbi3AT/sBqn05eRPtn1JCPw==
Expand Down
Loading