-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use node.js crypto for x25519 keys #389
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,14 @@ import { newInstance, ChaCha20Poly1305 } from '@chainsafe/as-chacha20poly1305' | |
import { digest } from '@chainsafe/as-sha256' | ||
import { isElectronMain } from 'wherearewe' | ||
import { pureJsCrypto } from './js.js' | ||
import type { KeyPair } from '../@types/libp2p.js' | ||
import type { ICryptoInterface } from '../crypto.js' | ||
|
||
const ctx = newInstance() | ||
const asImpl = new ChaCha20Poly1305(ctx) | ||
const CHACHA_POLY1305 = 'chacha20-poly1305' | ||
const PKCS8_PREFIX = Buffer.from([0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20]) | ||
const X25519_PREFIX = Buffer.from([0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x03, 0x21, 0x00]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node.js requires these prefixes, |
||
const nodeCrypto: Pick<ICryptoInterface, 'hashSHA256' | 'chaCha20Poly1305Encrypt' | 'chaCha20Poly1305Decrypt'> = { | ||
hashSHA256 (data) { | ||
return crypto.createHash('sha256').update(data).digest() | ||
|
@@ -76,6 +79,68 @@ export const defaultCrypto: ICryptoInterface = { | |
return asCrypto.chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) | ||
} | ||
return nodeCrypto.chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) | ||
}, | ||
generateX25519KeyPair (): KeyPair { | ||
const { publicKey, privateKey } = crypto.generateKeyPairSync('x25519', { | ||
publicKeyEncoding: { | ||
type: 'spki', | ||
format: 'der' | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs8', | ||
format: 'der' | ||
} | ||
}) | ||
|
||
return { | ||
publicKey: publicKey.subarray(X25519_PREFIX.length), | ||
privateKey: privateKey.subarray(PKCS8_PREFIX.length) | ||
} | ||
}, | ||
generateX25519KeyPairFromSeed (seed: Uint8Array): KeyPair { | ||
const privateKey = crypto.createPrivateKey({ | ||
key: Buffer.concat([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to use both Buffer.concat and uint8ArrayConcat in this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No - I used Not that I would expect it to make a difference to the overall benchmark. mind. |
||
PKCS8_PREFIX, | ||
seed | ||
], PKCS8_PREFIX.byteLength + seed.byteLength), | ||
type: 'pkcs8', | ||
format: 'der' | ||
}) | ||
|
||
const publicKey = crypto.createPublicKey(privateKey) | ||
.export({ | ||
type: 'spki', | ||
format: 'der' | ||
}).subarray(X25519_PREFIX.length) | ||
|
||
return { | ||
publicKey, | ||
privateKey: seed | ||
} | ||
}, | ||
generateX25519SharedKey (privateKey: Uint8Array, publicKey: Uint8Array): Uint8Array { | ||
publicKey = Buffer.concat([ | ||
X25519_PREFIX, | ||
publicKey | ||
], X25519_PREFIX.byteLength + publicKey.byteLength) | ||
|
||
privateKey = Buffer.concat([ | ||
PKCS8_PREFIX, | ||
privateKey | ||
], PKCS8_PREFIX.byteLength + privateKey.byteLength) | ||
|
||
return crypto.diffieHellman({ | ||
publicKey: crypto.createPublicKey({ | ||
key: Buffer.from(publicKey, publicKey.byteOffset, publicKey.byteLength), | ||
type: 'spki', | ||
format: 'der' | ||
}), | ||
privateKey: crypto.createPrivateKey({ | ||
key: Buffer.from(privateKey, privateKey.byteOffset, privateKey.byteLength), | ||
type: 'pkcs8', | ||
format: 'der' | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
import { alloc as uint8ArrayAlloc, allocUnsafe as uint8ArrayAllocUnsafe } from 'uint8arrays/alloc' | ||
import { concat as uint8ArrayConcat } from 'uint8arrays/concat' | ||
import type { bytes } from './@types/basic.js' | ||
import type { MessageBuffer } from './@types/handshake.js' | ||
import type { LengthDecoderFunction } from 'it-length-prefixed' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
const allocUnsafe = (len: number): Uint8Array => { | ||
if (globalThis.Buffer) { | ||
return globalThis.Buffer.allocUnsafe(len) | ||
} | ||
|
||
return new Uint8Array(len) | ||
} | ||
|
||
export const uint16BEEncode = (value: number): Uint8Array => { | ||
const target = allocUnsafe(2) | ||
const target = uint8ArrayAllocUnsafe(2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(0, value, false) | ||
return target | ||
} | ||
|
@@ -52,7 +45,7 @@ export function decode0 (input: bytes): MessageBuffer { | |
return { | ||
ne: input.subarray(0, 32), | ||
ciphertext: input.subarray(32, input.length), | ||
ns: new Uint8Array(0) | ||
ns: uint8ArrayAlloc(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Buffers in node wherever possible. |
||
} | ||
} | ||
|
||
|
@@ -74,7 +67,7 @@ export function decode2 (input: bytes): MessageBuffer { | |
} | ||
|
||
return { | ||
ne: new Uint8Array(0), | ||
ne: uint8ArrayAlloc(0), | ||
ns: input.subarray(0, 48), | ||
ciphertext: input.subarray(48, input.length) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to do this with protons