Skip to content

Commit 8614c0e

Browse files
committed
add key generation functionality into namedgroup.js
1 parent c3e61ac commit 8614c0e

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.1.9",
3+
"version": "0.2.0",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/dep.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export { Struct, Constrained, Byte } from "@tls/struct"
22
export { Uint8, Uint16, Uint24, Uint32 } from "@tls/struct"
33
export { Extension, KeyExchange, KeyShareEntry } from "@tls/extension"
4-
/* export { p256 } from 'npm:@noble/[email protected]/p256'
4+
export { p256 } from 'npm:@noble/[email protected]/p256'
55
export { p384 } from 'npm:@noble/[email protected]/p384'
66
export { p521 } from 'npm:@noble/[email protected]/p521'
77
export { x25519 } from 'npm:@noble/[email protected]/ed25519'
8-
export { x448 } from 'npm:@noble/[email protected]/ed448' */
8+
export { x448 } from 'npm:@noble/[email protected]/ed448'

src/namedgroup.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// deno-lint-ignore-file no-slow-types
22
// @ts-self-types="../type/namedgroup.d.ts"
33

4-
import { /* p256, p384, p521, x25519, x448, */ Uint16, KeyExchange, KeyShareEntry } from "./dep.ts";
4+
import { p256, p384, p521, x25519, x448, Uint16, KeyExchange, KeyShareEntry } from "./dep.ts";
55
import { Enum } from "./enum.js";
66

77
/**
@@ -53,25 +53,7 @@ export class NamedGroup extends Enum {
5353
*/
5454
constructor(name, value) {
5555
super(name, value);
56-
/* switch (this.name) {
57-
case "SECP256R1": {
58-
this.#keyGen = p256; break;
59-
}
60-
case "SECP384R1": {
61-
this.#keyGen = p384; break;
62-
}
63-
case "SECP521R1": {
64-
this.#keyGen = p521; break;
65-
}
66-
case "X25519": {
67-
this.#keyGen = x25519; break;
68-
}
69-
case "X448": {
70-
this.#keyGen = x448; break;
71-
}
72-
} */
73-
this.#privateKey = this.#keyGen?.utils.randomPrivateKey();
74-
this.#publicKey = this.#keyGen?.getPublicKey(this.#privateKey);
56+
7557
}
7658

7759
/**
@@ -86,21 +68,38 @@ export class NamedGroup extends Enum {
8668
*
8769
* @returns {Function} The key generation function.
8870
*/
89-
get keyGen() { return this.#keyGen; }
71+
get keyGen() {
72+
switch (this.name) {
73+
case "SECP256R1": return p256;
74+
case "SECP384R1": return p384;
75+
case "SECP521R1": return p521;
76+
case "X25519": return x25519;
77+
case "X448": return x448;
78+
default: return x25519;
79+
}
80+
}
9081

9182
/**
9283
* Gets the private key associated with the NamedGroup.
9384
*
9485
* @returns {Uint8Array} The private key.
9586
*/
96-
get privateKey() { return this.#privateKey; }
87+
get privateKey() {
88+
if (this.#privateKey) return this.#privateKey;
89+
this.#privateKey = this.keyGen?.utils.randomPrivateKey();
90+
return this.#privateKey
91+
}
9792

9893
/**
9994
* Gets the public key associated with the NamedGroup.
10095
*
10196
* @returns {Uint8Array} The public key.
10297
*/
103-
get publicKey() { return this.#publicKey; }
98+
get publicKey() {
99+
if(this.#publicKey)return this.#publicKey;
100+
this.#publicKey = this.keyGen?.getPublicKey(this.privateKey);
101+
return this.#publicKey;
102+
}
104103

105104
/**
106105
* Computes the shared key with a peer's public key.
@@ -109,7 +108,7 @@ export class NamedGroup extends Enum {
109108
* @returns {Uint8Array} The shared secret.
110109
*/
111110
getSharedKey(peerPublicKey) {
112-
return this.#keyGen?.getSharedSecret(this.#privateKey, peerPublicKey);
111+
return this.keyGen?.getSharedSecret(this.#privateKey, peerPublicKey);
113112
}
114113

115114
/**

test/namedgroup_test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ Deno.test(
99

1010
const x25519 = NamedGroup.X25519;
1111
assertEquals(x25519.name, 'X25519')
12-
/* const pub = x25519.keyGen.getPublicKey(x25519.privateKey);
12+
const pub = x25519.keyGen.getPublicKey(x25519.privateKey);
1313
assertEquals(pub.length, 32)
1414
const peerPublicKey = crypto.getRandomValues(new Uint8Array(32));
1515
assertEquals(peerPublicKey.length, 32)
1616
const sharedKey = x25519.getSharedKey(peerPublicKey)
17-
assertEquals(sharedKey.length, 32) */
17+
assertEquals(sharedKey.length, 32)
1818

1919
}
2020
)
2121

2222

23-
24-

0 commit comments

Comments
 (0)