1
1
// Closed source not for private and or corporate use.
2
2
import * as defaultCrypto from '#crypto' ;
3
- import { assign , clearBuffer , isBuffer } from '@universalweb/acid' ;
3
+ import { assign , clear , isBuffer } from '@universalweb/acid' ;
4
4
import {
5
5
clientSetSession ,
6
6
encryptionKeypair as encryptionKeypair25519 ,
@@ -19,7 +19,9 @@ const {
19
19
combineKeys,
20
20
getX25519Key,
21
21
getKyberKey,
22
- get2519KeyCopy
22
+ get25519KeyCopy,
23
+ clearBuffers,
24
+ clearBuffer
23
25
} = defaultCrypto ;
24
26
const {
25
27
generateSeed,
@@ -59,20 +61,28 @@ export const x25519_kyber768Half_xchacha20 = {
59
61
publicKey : getX25519Key ( source . publicKey ) ,
60
62
privateKey : getX25519Key ( source . privateKey )
61
63
} ;
62
- destination . publicKey = get2519KeyCopy ( cipherData ) ;
63
- const x25519SessionKeys = clientSetSession ( sourceKeypair25519 , destination , source ) ;
64
+ const {
65
+ transmitKey : oldTransmitKey ,
66
+ receiveKey : oldReceiveKey
67
+ } = source ;
68
+ destination . publicKey = get25519KeyCopy ( cipherData ) ;
69
+ const x25519SessionKeys = clientSetSession ( sourceKeypair25519 , destination , sourceKeypair25519 ) ;
64
70
const cipherText = getKyberKey ( cipherData ) ;
65
71
const kyberPrivateKey = getKyberKey ( source . privateKey ) ;
66
72
console . log ( cipherText , kyberPrivateKey ) ;
67
- const kyberSharedSecret = await decapsulate ( cipherText , kyberPrivateKey ) ;
68
- console . log ( 'clientSetSession kyberSharedSecret' , kyberSharedSecret [ 0 ] , kyberSharedSecret . length ) ;
69
- source . transmitKey = combineKeys ( source . transmitKey , kyberSharedSecret ) ;
70
- source . receiveKey = combineKeys ( source . receiveKey , kyberSharedSecret ) ;
73
+ const sharedSecret = await decapsulate ( cipherText , kyberPrivateKey ) ;
74
+ console . log ( 'clientSetSession sharedSecret' , sharedSecret [ 0 ] , sharedSecret . length ) ;
75
+ const newTransmitKey = combineKeys ( oldTransmitKey , sourceKeypair25519 . transmitKey , sharedSecret ) ;
76
+ const newReceiveKey = combineKeys ( oldReceiveKey , sourceKeypair25519 . receiveKey , sharedSecret ) ;
77
+ clearBuffers ( oldTransmitKey , x25519SessionKeys . transmitKey , sharedSecret ) ;
78
+ clearBuffers ( oldReceiveKey , x25519SessionKeys . receiveKey ) ;
79
+ source . transmitKey = newTransmitKey ;
80
+ source . receiveKey = newReceiveKey ;
71
81
console . log ( 'Keys' , source . transmitKey [ 0 ] , source . receiveKey [ 0 ] ) ;
72
82
} ,
73
83
async serverInitializeSession ( source , destination , cipherData ) {
74
84
console . log ( 'serverInitializeSession CIPHER' , toHex ( cipherData ) ) ;
75
- destination . publicKey = get2519KeyCopy ( cipherData ) ;
85
+ destination . publicKey = get25519KeyCopy ( cipherData ) ;
76
86
await serverSetSessionAttach ( source , destination ) ;
77
87
source . nextSession = await kyber768Half_x25519 . serverEphemeralKeypair ( source , destination , cipherData ) ;
78
88
clearBuffer ( cipherData ) ;
@@ -83,24 +93,29 @@ export const x25519_kyber768Half_xchacha20 = {
83
93
frame [ 3 ] = source . nextSession . publicKey ;
84
94
} ,
85
95
async serverSetSession ( source , destination ) {
86
- console . log ( 'serverSetSession' ) ;
87
- if ( source . nextSession ) {
88
- assign ( source , source . nextSession ) ;
89
- source . nextSession = null ;
90
- const sourceKeypair25519 = {
91
- publicKey : getX25519Key ( source . publicKey ) ,
92
- privateKey : getX25519Key ( source . privateKey )
93
- } ;
94
- console . log ( 'serverSetSession nextSession' , sourceKeypair25519 , destination ) ;
95
- const x25519SessionKeys = serverSetSession ( sourceKeypair25519 , destination , source ) ;
96
- const sharedSecret = source . sharedSecret ;
97
- source . transmitKey = combineKeys ( source . transmitKey , sharedSecret ) ;
98
- source . receiveKey = combineKeys ( source . receiveKey , sharedSecret ) ;
99
- console . log ( 'kyberSharedSecret' , sharedSecret [ 0 ] ) ;
100
- clearBuffer ( sharedSecret ) ;
101
- source . sharedSecret = null ;
102
- console . log ( 'Keys' , source . transmitKey [ 0 ] , source . receiveKey [ 0 ] ) ;
103
- }
96
+ console . log ( 'server Setting Session' ) ;
97
+ const {
98
+ nextSession,
99
+ transmitKey : oldTransmitKey ,
100
+ receiveKey : oldReceiveKey
101
+ } = source ;
102
+ const nextSessionKeypair25519 = {
103
+ publicKey : getX25519Key ( nextSession . publicKey ) ,
104
+ privateKey : getX25519Key ( nextSession . privateKey )
105
+ } ;
106
+ console . log ( 'serverSetSession nextSession' , nextSessionKeypair25519 , destination ) ;
107
+ const x25519SessionKeys = serverSetSession ( nextSessionKeypair25519 , destination , nextSessionKeypair25519 ) ;
108
+ const sharedSecret = nextSession . sharedSecret ;
109
+ const newTransmitKey = combineKeys ( oldTransmitKey , x25519SessionKeys . transmitKey , sharedSecret ) ;
110
+ const newReceiveKey = combineKeys ( oldReceiveKey , x25519SessionKeys . receiveKey , sharedSecret ) ;
111
+ clearBuffers ( oldTransmitKey , x25519SessionKeys . transmitKey , sharedSecret ) ;
112
+ clearBuffers ( oldReceiveKey , x25519SessionKeys . receiveKey ) ;
113
+ source . transmitKey = newTransmitKey ;
114
+ source . receiveKey = newReceiveKey ;
115
+ console . log ( 'sharedSecret' , sharedSecret [ 0 ] ) ;
116
+ source . sharedSecret = null ;
117
+ source . nextSession = null ;
118
+ console . log ( 'Keys' , source . transmitKey [ 0 ] , source . receiveKey [ 0 ] ) ;
104
119
} ,
105
120
generateSeed,
106
121
keypair,
0 commit comments