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" ;
55import { 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 /**
0 commit comments