mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-24 08:01:45 +00:00
Replace with aead from bcrypto
This commit is contained in:
17
src/xx.ts
17
src/xx.ts
@ -1,7 +1,7 @@
|
||||
import {bytes32, bytes16, uint32, uint64, bytes} from './types/basic'
|
||||
import { Buffer } from 'buffer';
|
||||
import * as crypto from 'libp2p-crypto';
|
||||
import * as sodium from 'sodium-native';
|
||||
import AEAD from 'bcrypto/aead-browser';
|
||||
|
||||
type KeyPair = {
|
||||
publicKey: bytes32,
|
||||
@ -41,7 +41,7 @@ const emptyKey = Buffer.alloc(32) as bytes32;
|
||||
const minNonce = 0;
|
||||
|
||||
class XXHandshake {
|
||||
async initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
||||
private async initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
||||
const e: KeyPair;
|
||||
const re: bytes32;
|
||||
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
||||
@ -66,13 +66,14 @@ class XXHandshake {
|
||||
}
|
||||
|
||||
encrypt(k: bytes32, n: uint32, ad: bytes, plaintext: bytes) : bytes {
|
||||
const ElongatedNonce = sodium.sodium_malloc(sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); // 12U ?
|
||||
sodium.sodium_memzero(ElongatedNonce);
|
||||
ElongatedNonce.set(n, 16);
|
||||
const nonce = Buffer.alloc(12);
|
||||
nonce.writeUInt32LE(n, 4);
|
||||
const ctx = new AEAD();
|
||||
ctx.init(k, nonce);
|
||||
ctx.aad(ad);
|
||||
ctx.encrypt(plaintext);
|
||||
|
||||
const clen = plaintext.length + sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES;
|
||||
const c = sodium.sodium_malloc(clen);
|
||||
sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, plaintext, ad, null, ElongatedNonce, k);
|
||||
return ctx.final();
|
||||
}
|
||||
|
||||
// Cipher state related
|
||||
|
Reference in New Issue
Block a user