@@ -3,11 +3,14 @@ import { newInstance, ChaCha20Poly1305 } from '@chainsafe/as-chacha20poly1305'
33import { digest } from '@chainsafe/as-sha256'
44import { isElectronMain } from 'wherearewe'
55import { pureJsCrypto } from './js.js'
6+ import type { KeyPair } from '../@types/libp2p.js'
67import type { ICryptoInterface } from '../crypto.js'
78
89const ctx = newInstance ( )
910const asImpl = new ChaCha20Poly1305 ( ctx )
1011const CHACHA_POLY1305 = 'chacha20-poly1305'
12+ const PKCS8_PREFIX = Buffer . from ( [ 0x30 , 0x2e , 0x02 , 0x01 , 0x00 , 0x30 , 0x05 , 0x06 , 0x03 , 0x2b , 0x65 , 0x6e , 0x04 , 0x22 , 0x04 , 0x20 ] )
13+ const X25519_PREFIX = Buffer . from ( [ 0x30 , 0x2a , 0x30 , 0x05 , 0x06 , 0x03 , 0x2b , 0x65 , 0x6e , 0x03 , 0x21 , 0x00 ] )
1114const nodeCrypto : Pick < ICryptoInterface , 'hashSHA256' | 'chaCha20Poly1305Encrypt' | 'chaCha20Poly1305Decrypt' > = {
1215 hashSHA256 ( data ) {
1316 return crypto . createHash ( 'sha256' ) . update ( data ) . digest ( )
@@ -76,6 +79,68 @@ export const defaultCrypto: ICryptoInterface = {
7679 return asCrypto . chaCha20Poly1305Decrypt ( ciphertext , nonce , ad , k , dst )
7780 }
7881 return nodeCrypto . chaCha20Poly1305Decrypt ( ciphertext , nonce , ad , k , dst )
82+ } ,
83+ generateX25519KeyPair ( ) : KeyPair {
84+ const { publicKey, privateKey } = crypto . generateKeyPairSync ( 'x25519' , {
85+ publicKeyEncoding : {
86+ type : 'spki' ,
87+ format : 'der'
88+ } ,
89+ privateKeyEncoding : {
90+ type : 'pkcs8' ,
91+ format : 'der'
92+ }
93+ } )
94+
95+ return {
96+ publicKey : publicKey . subarray ( X25519_PREFIX . length ) ,
97+ privateKey : privateKey . subarray ( PKCS8_PREFIX . length )
98+ }
99+ } ,
100+ generateX25519KeyPairFromSeed ( seed : Uint8Array ) : KeyPair {
101+ const privateKey = crypto . createPrivateKey ( {
102+ key : Buffer . concat ( [
103+ PKCS8_PREFIX ,
104+ seed
105+ ] , PKCS8_PREFIX . byteLength + seed . byteLength ) ,
106+ type : 'pkcs8' ,
107+ format : 'der'
108+ } )
109+
110+ const publicKey = crypto . createPublicKey ( privateKey )
111+ . export ( {
112+ type : 'spki' ,
113+ format : 'der'
114+ } ) . subarray ( X25519_PREFIX . length )
115+
116+ return {
117+ publicKey,
118+ privateKey : seed
119+ }
120+ } ,
121+ generateX25519SharedKey ( privateKey : Uint8Array , publicKey : Uint8Array ) : Uint8Array {
122+ publicKey = Buffer . concat ( [
123+ X25519_PREFIX ,
124+ publicKey
125+ ] , X25519_PREFIX . byteLength + publicKey . byteLength )
126+
127+ privateKey = Buffer . concat ( [
128+ PKCS8_PREFIX ,
129+ privateKey
130+ ] , PKCS8_PREFIX . byteLength + privateKey . byteLength )
131+
132+ return crypto . diffieHellman ( {
133+ publicKey : crypto . createPublicKey ( {
134+ key : Buffer . from ( publicKey , publicKey . byteOffset , publicKey . byteLength ) ,
135+ type : 'spki' ,
136+ format : 'der'
137+ } ) ,
138+ privateKey : crypto . createPrivateKey ( {
139+ key : Buffer . from ( privateKey , privateKey . byteOffset , privateKey . byteLength ) ,
140+ type : 'pkcs8' ,
141+ format : 'der'
142+ } )
143+ } )
79144 }
80145}
81146
0 commit comments