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 , Constrained , Struct , safeuint8array } from "./dep.ts" ;
4+ import { p256 , p384 , p521 , x25519 , x448 , Uint16 } from "./dep.ts" ;
55import { Enum } from "./enum.js" ;
66
77/**
@@ -58,13 +58,7 @@ export class NamedGroup extends Enum {
5858
5959 }
6060
61- /**
62- * Converts the NamedGroup to a Uint16 representation.
63- *
64- * @returns {Uint16 } The Uint16 representation of the NamedGroup.
65- */
66- get Uint16 ( ) { return Uint16 . fromValue ( + this ) ; }
67- get byte ( ) { return this . Uint16 }
61+ get byte ( ) { return Uint16 . fromValue ( + this ) }
6862
6963 /**
7064 * Gets the key generation algorithm associated with the NamedGroup.
@@ -122,20 +116,6 @@ export class NamedGroup extends Enum {
122116 return sharedKey ;
123117 }
124118
125- /**
126- * Creates a key share entry from the NamedGroup.
127- *
128- * @returns {KeyShareEntry } A new KeyShareEntry instance.
129- */
130- async keyShareEntryAsync ( ) {
131- const key_exchange = KeyExchange . fromKey ( await this . exportPublicKey ( ) ) . key_exchange ;
132- return KeyShareEntry . fromGroupKey ( this , key_exchange ) ;
133- }
134-
135- keyShareEntry ( ) {
136- return KeyShareEntry . fromGroupKey ( this , this . publicKey ) ;
137- }
138-
139119 async keyPair ( ) {
140120 if ( this . #keyPair) return this . #keyPair
141121 let algo
@@ -187,117 +167,4 @@ export class NamedGroup extends Enum {
187167 }
188168}
189169
190- /**
191- * Represents a key exchange mechanism.
192- */
193- export class KeyExchange_0 extends Constrained {
194-
195- static fromKey ( octet ) { return new KeyExchange_0 ( octet ) ; }
196-
197- static from ( array ) {
198- const copy = Uint8Array . from ( array ) ;
199- const lengthOf = Uint16 . from ( copy . subarray ( 0 , 2 ) ) . value ;
200- const octet = copy . subarray ( 2 , 2 + lengthOf ) ;
201- return new KeyExchange_0 ( octet ) ;
202- }
203-
204- constructor ( octet ) {
205- super ( 1 , 65535 , octet ) ;
206- this . key_exchange = octet ;
207- }
208- }
209-
210- export class KeyExchange extends Uint8Array {
211- #lengthOf
212- #key_exchange
213- static sanitize ( array ) {
214- if ( isUint8Array ( array ) == false ) throw Error ( `Expected Uint8Array` )
215- const lengthOf = Uint16 . from ( array ) . value ;
216- if ( lengthOf < 1 ) throw Error ( `Minimum length is 1` )
217- if ( lengthOf > 2 ** 16 - 1 ) throw Error ( `Minimum length is 2**16-1` )
218- return [ array . slice ( 0 , 2 + lengthOf ) ]
219- }
220- static fromKey ( key ) {
221- if ( isUint8Array ( key ) == false ) throw Error ( `Expected Uint8Array` )
222- const lengthOf = Uint16 . fromValue ( key . length )
223- return new KeyExchange (
224- safeuint8array ( lengthOf , key )
225- ) ;
226- }
227-
228- static from ( array ) {
229- return new KeyExchange ( array ) ;
230- }
231-
232- constructor ( ...args ) {
233- args = isUint8Array ( args [ 0 ] ) ? KeyExchange . sanitize ( args [ 0 ] ) : args ;
234- super ( ...args ) ;
235- }
236-
237- get lengthOf ( ) {
238- if ( this . #lengthOf) return this . #lengthOf
239- this . #lengthOf ||= Uint16 . from ( this . subarray ( 0 , 2 ) ) . value ;
240- return this . #lengthOf;
241- }
242-
243- get key_exchange ( ) {
244- if ( this . #key_exchange) return this . #key_exchange;
245- this . #key_exchange ||= this . subarray ( 2 , 2 + this . lengthOf ) ;
246- return this . #key_exchange
247- }
248- }
249-
250- /**
251- * Represents a key share entry.
252- */
253- export class KeyShareEntry_0 extends Struct {
254-
255- static from ( array ) {
256- const copy = Uint8Array . from ( array ) ;
257- const group = NamedGroup . from ( copy . subarray ( 0 , 2 ) ) ;
258- const key_exchange = KeyExchange . from ( copy . subarray ( 2 ) ) ;
259- return new KeyShareEntry_0 ( group , key_exchange ) ;
260- }
261-
262- constructor ( group , key_exchange ) {
263- super ( group . Uint16 , key_exchange ) ;
264- this . group = group ;
265- this . key_exchange = key_exchange . key_exchange ;
266- }
267- }
268-
269- export class KeyShareEntry extends Uint8Array {
270- #group;
271- #key_exchange;
272- static fromGroupKey ( group , key_exchange ) {
273- return KeyShareEntry . from (
274- safeuint8array ( group . byte , KeyExchange . fromKey ( key_exchange ) )
275- )
276- }
277- static from ( array ) { return new KeyShareEntry ( array ) }
278- constructor ( ...args ) {
279- super ( ...args )
280- }
281- get group ( ) {
282- if ( this . #group) return this . #group;
283- this . #group ||= NamedGroup . from ( this . subarray ( 0 , 2 ) ) ;
284- return this . #group;
285- }
286- get key_exchange ( ) {
287- if ( this . #key_exchange) return this . #key_exchange;
288- this . #key_exchange ||= KeyExchange . from ( this . subarray ( 2 ) ) . key_exchange ;
289- return this . #key_exchange;
290- }
291- }
292-
293- async function generateKey ( algo ) {
294- return await crypto . subtle . generateKey (
295- algo ,
296- true ,
297- [ "deriveKey" , "deriveBits" ]
298- ) ;
299- }
300-
301- const isUint8Array = v => v instanceof Uint8Array
302-
303170// npx -p typescript tsc ./src/namedgroup.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist
0 commit comments