Skip to content

Commit

Permalink
chore(crypto): update test code
Browse files Browse the repository at this point in the history
  • Loading branch information
nabetti1720 committed Dec 16, 2024
1 parent 5b785da commit 78c5fad
Showing 1 changed file with 87 additions and 168 deletions.
255 changes: 87 additions & 168 deletions tests/unit/crypto.subtle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,7 @@ describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
}
}
});
});

describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
it("should be processing AES-CTR algorithm", async () => {
const parameters = [
{
Expand Down Expand Up @@ -539,9 +537,7 @@ describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
}
}
});
});

describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
it("should be processing AES-GCM algorithm", async () => {
const parameters = [
{
Expand Down Expand Up @@ -602,10 +598,8 @@ describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
}
}
});
});

// Caveat: The current RSA implementation is too slow to complete the test within the time limit.
describe("SubtleCrypto generateKey/encrypt/decrypt", () => {
// Caveat: The current RSA implementation is too slow to complete the test within the time limit.
it.skip("should be processing RSA-OAEP algorithm", async () => {
const parameters = [
{
Expand Down Expand Up @@ -728,34 +722,6 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
name: "AES-GCM",
length: 256,
},
// {
// name: "AES-KW",
// length: 128,
// },
// {
// name: "AES-KW",
// length: 192,
// },
// {
// name: "AES-KW",
// length: 256,
// },
// {
// name: "HMAC",
// hash: "SHA-1",
// },
// {
// name: "HMAC",
// hash: "SHA-256",
// },
// {
// name: "HMAC",
// hash: "SHA-384",
// },
// {
// name: "HMAC",
// hash: "SHA-512",
// },
];

// 1. Generate Alice's key pair
Expand Down Expand Up @@ -922,34 +888,6 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
name: "AES-GCM",
length: 256,
},
{
name: "AES-KW",
length: 128,
},
{
name: "AES-KW",
length: 192,
},
{
name: "AES-KW",
length: 256,
},
{
name: "HMAC",
hash: "SHA-1",
},
{
name: "HMAC",
hash: "SHA-256",
},
{
name: "HMAC",
hash: "SHA-384",
},
{
name: "HMAC",
hash: "SHA-512",
},
];

// 1. Generate Alice's key pair
Expand All @@ -973,30 +911,31 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
);

// 3. Export Bob's public key to share with Alice
const bobPublicKey = await crypto.subtle.exportKey(
"raw",
bobKeyPair.publicKey
);
// const bobPublicKey = await crypto.subtle.exportKey(
// "raw",
// bobKeyPair.publicKey
// );

// 3.5. Alice imports Bob's public key
const bobImportKey = await crypto.subtle.importKey(
"raw",
bobPublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
// const bobImportKey = await crypto.subtle.importKey(
// "raw",
// bobPublicKey,
// {
// name: "ECDH",
// namedCurve: "P-256",
// },
// true,
// []
// );

for (const generated of generatedParams) {
for (const derived of derivedParams) {
// 4. Alice derives a shared secret using Bob's public key
const aliceSharedSecret = await crypto.subtle.deriveBits(
{
name: "ECDH",
public: bobImportKey,
// public: bobImportKey,
public: bobKeyPair.publicKey,
},
aliceKeyPair.privateKey,
256 // number of bits to derive
Expand All @@ -1020,27 +959,28 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
);

// 6. Export Alice's public key to share with Bob
const alicePublicKey = await crypto.subtle.exportKey(
"raw",
aliceKeyPair.publicKey
);
// const alicePublicKey = await crypto.subtle.exportKey(
// "raw",
// aliceKeyPair.publicKey
// );

// 7. Bob derives a shared secret using Alice's public key
const aliceImportKey = await crypto.subtle.importKey(
"raw",
alicePublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
// const aliceImportKey = await crypto.subtle.importKey(
// "raw",
// alicePublicKey,
// {
// name: "ECDH",
// namedCurve: "P-256",
// },
// true,
// []
// );

const bobSharedSecret = await crypto.subtle.deriveBits(
{
name: "ECDH",
public: aliceImportKey,
// public: aliceImportKey,
public: aliceKeyPair.publicKey,
},
bobKeyPair.privateKey,
256
Expand All @@ -1062,7 +1002,6 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
true,
["encrypt", "decrypt"]
);
console.log("8. Bob derives the final key using HKDF : complete");

// 9. Verify if both derived keys are the same
const aliceKeyBuffer = new Uint8Array(
Expand All @@ -1089,21 +1028,24 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
// salt: pbkdf2Salt,
// iterations: pbkdf2Iterations,
// hash: "SHA-1"
// }, {
// },
// {
name: "PBKDF2",
salt: pbkdf2Salt,
iterations: pbkdf2Iterations,
hash: "SHA-256",
// }, {
// },
// {
// name: "PBKDF2",
// salt: pbkdf2Salt,
// iterations: pbkdf2Iterations,
// hash: "SHA-384"
// }, {
// name: "PBKDF2",
// salt: pbkdf2Salt,
// iterations: pbkdf2Iterations,
// hash: "SHA-512"
// },
// {
// name: "PBKDF2",
// salt: pbkdf2Salt,
// iterations: pbkdf2Iterations,
// hash: "SHA-512"
},
];
const derivedParams = [
Expand All @@ -1114,52 +1056,27 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
{
// name: "AES-CBC",
// length: 192,
// }, {
// name: "AES-CBC",
// length: 256,
// }, {
// },
// {
// name: "AES-CBC",
// length: 256,
// },
// {
name: "AES-CTR",
length: 128,
},
{
// name: "AES-CTR",
// length: 192,
// }, {
// name: "AES-CTR",
// length: 256,
// }, {
name: "AES-GCM",
length: 128,
},
{
// name: "AES-GCM",
// length: 192,
// }, {
// name: "AES-GCM",
// },
// {
// name: "AES-CTR",
// length: 256,
// }, {
name: "AES-KW",
// },
// {
name: "AES-GCM",
length: 128,
},
{
// name: "AES-KW",
// length: 192,
// }, {
// name: "AES-KW",
// length: 256,
// }, {
name: "HMAC",
hash: "SHA-1",
// }, {
// name: "HMAC",
// hash: "SHA-256",
// }, {
// name: "HMAC",
// hash: "SHA-384",
// }, {
// name: "HMAC",
// hash: "SHA-512",
},
];

// 1. Generate Alice's key pair
Expand All @@ -1183,30 +1100,31 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
);

// 3. Export Bob's public key to share with Alice
const bobPublicKey = await crypto.subtle.exportKey(
"raw",
bobKeyPair.publicKey
);
// const bobPublicKey = await crypto.subtle.exportKey(
// "raw",
// bobKeyPair.publicKey
// );

// 3.5. Alice imports Bob's public key
const bobImportKey = await crypto.subtle.importKey(
"raw",
bobPublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
// const bobImportKey = await crypto.subtle.importKey(
// "raw",
// bobPublicKey,
// {
// name: "ECDH",
// namedCurve: "P-256",
// },
// true,
// []
// );

for (const generated of generatedParams) {
for (const derived of derivedParams) {
// 4. Alice derives a shared secret using Bob's public key
const aliceSharedSecret = await crypto.subtle.deriveBits(
{
name: "ECDH",
public: bobImportKey,
// public: bobImportKey,
public: bobKeyPair.publicKey,
},
aliceKeyPair.privateKey,
256 // number of bits to derive
Expand All @@ -1230,27 +1148,28 @@ describe("SubtleCrypto deriveBits/deriveKey", () => {
);

// 6. Export Alice's public key to share with Bob
const alicePublicKey = await crypto.subtle.exportKey(
"raw",
aliceKeyPair.publicKey
);
// const alicePublicKey = await crypto.subtle.exportKey(
// "raw",
// aliceKeyPair.publicKey
// );

// 7. Bob derives a shared secret using Alice's public key
const aliceImportKey = await crypto.subtle.importKey(
"raw",
alicePublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
// const aliceImportKey = await crypto.subtle.importKey(
// "raw",
// alicePublicKey,
// {
// name: "ECDH",
// namedCurve: "P-256",
// },
// true,
// []
// );

const bobSharedSecret = await crypto.subtle.deriveBits(
{
name: "ECDH",
public: aliceImportKey,
// public: aliceImportKey,
public: aliceKeyPair.publicKey,
},
bobKeyPair.privateKey,
256
Expand Down

0 comments on commit 78c5fad

Please sign in to comment.