mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 13:32:19 +00:00
Add encryption methods
This commit is contained in:
parent
e55fa206fd
commit
175d8940be
@ -39,6 +39,7 @@
|
||||
"dependencies": {
|
||||
"bn.js": "^5.0.0",
|
||||
"buffer": "^5.4.3",
|
||||
"libp2p-crypto": "^0.17.1"
|
||||
"libp2p-crypto": "^0.17.1",
|
||||
"sodium-native": "^2.4.6"
|
||||
}
|
||||
}
|
||||
|
28
src/xx.ts
28
src/xx.ts
@ -1,6 +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';
|
||||
|
||||
type KeyPair = {
|
||||
publicKey: bytes32,
|
||||
@ -60,11 +61,38 @@ class XXHandshake {
|
||||
return {ss, s, e, rs, re, psk};
|
||||
}
|
||||
|
||||
incrementNonce(n: uint32) : uint32 {
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
// Cipher state related
|
||||
initializeKey(k: bytes32) : CipherState {
|
||||
const n = minNonce;
|
||||
return { k, n };
|
||||
}
|
||||
|
||||
setNonce(cs: CipherState, nonce: uint32) {
|
||||
cs.n = nonce;
|
||||
}
|
||||
|
||||
encryptWithAd(cs: CipherState, ad: bytes, plaintext: bytes) : bytes {
|
||||
const e = this.encrypt(cs.k, cs.n, ad, plaintext);
|
||||
this.setNonce(cs, this.incrementNonce(cs.n));
|
||||
return e;
|
||||
}
|
||||
|
||||
// Symmetric state related
|
||||
|
||||
async initializeSymmetric(protocolName: string) : Promise<SymmetricState> {
|
||||
const h = await this.hashProtocolName(protocolName);
|
||||
const ck = h;
|
||||
|
16
yarn.lock
16
yarn.lock
@ -1984,7 +1984,7 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.3:
|
||||
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
|
||||
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
|
||||
|
||||
ini@~1.3.0:
|
||||
ini@^1.3.5, ini@~1.3.0:
|
||||
version "1.3.5"
|
||||
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"
|
||||
integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==
|
||||
@ -2616,6 +2616,11 @@ node-forge@~0.9.1:
|
||||
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.1.tgz#775368e6846558ab6676858a4d8c6e8d16c677b5"
|
||||
integrity sha512-G6RlQt5Sb4GMBzXvhfkeFmbqR6MzhtnT7VTHuLadjkii3rdYHNdw0m8zA4BTxVIh68FicCQ2NSUANpsqkr9jvQ==
|
||||
|
||||
node-gyp-build@^4.1.0:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.1.1.tgz#d7270b5d86717068d114cc57fff352f96d745feb"
|
||||
integrity sha512-dSq1xmcPDKPZ2EED2S6zw/b9NKsqzXRE6dVr8TVQnI3FJOTteUMuqF3Qqs6LZg+mLGYJWqQzMbIjMtJqTv87nQ==
|
||||
|
||||
node-modules-regexp@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40"
|
||||
@ -3305,6 +3310,15 @@ snapdragon@^0.8.1:
|
||||
source-map-resolve "^0.5.0"
|
||||
use "^3.1.0"
|
||||
|
||||
sodium-native@^2.4.6:
|
||||
version "2.4.6"
|
||||
resolved "https://registry.yarnpkg.com/sodium-native/-/sodium-native-2.4.6.tgz#8a8173095e8cf4f997de393a2ba106c34870cac2"
|
||||
integrity sha512-Ro9lhTjot8M01nwKLXiqLSmjR7B8o+Wg4HmJUjEShw/q6XPlNMzjPkA1VJKaMH8SO8fJ/sggAKVwreTaFszS2Q==
|
||||
dependencies:
|
||||
ini "^1.3.5"
|
||||
nan "^2.14.0"
|
||||
node-gyp-build "^4.1.0"
|
||||
|
||||
source-map-resolve@^0.5.0:
|
||||
version "0.5.2"
|
||||
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.2.tgz#72e2cc34095543e43b2c62b2c4c10d4a9054f259"
|
||||
|
Loading…
x
Reference in New Issue
Block a user