chore: removing ChaChaPolyCipherState and NoisePublicKey encryption/decryption, as it is only used for tests, and not during normal usage

This commit is contained in:
Richard Ramos 2023-11-21 12:36:01 -04:00 committed by richΛrd
parent 1cacb2f548
commit 37086a523f

View File

@ -2,62 +2,6 @@ import { concat as uint8ArrayConcat } from "uint8arrays/concat";
import { equals as uint8ArrayEquals } from "uint8arrays/equals";
import { bytes32 } from "./@types/basic.js";
import { chaCha20Poly1305Decrypt, chaCha20Poly1305Encrypt } from "./crypto.js";
import { isEmptyKey } from "./noise.js";
/**
* A ChaChaPoly Cipher State containing key (k), nonce (nonce) and associated data (ad)
*/
export class ChaChaPolyCipherState {
k: bytes32;
nonce: bytes32;
ad: Uint8Array;
/**
* @param k 32-byte key
* @param nonce 12 byte little-endian nonce
* @param ad associated data
*/
constructor(k: bytes32 = new Uint8Array(), nonce: bytes32 = new Uint8Array(), ad: Uint8Array = new Uint8Array()) {
this.k = k;
this.nonce = nonce;
this.ad = ad;
}
/**
* Takes a Cipher State (with key, nonce, and associated data) and encrypts a plaintext.
* The cipher state in not changed
* @param plaintext data to encrypt
* @returns sealed ciphertext including authentication tag
*/
encrypt(plaintext: Uint8Array): Uint8Array {
// If plaintext is empty, we raise an error
if (plaintext.length == 0) {
throw new Error("tried to encrypt empty plaintext");
}
return chaCha20Poly1305Encrypt(plaintext, this.nonce, this.ad, this.k);
}
/**
* Takes a Cipher State (with key, nonce, and associated data) and decrypts a ciphertext
* The cipher state is not changed
* @param ciphertext data to decrypt
* @returns plaintext
*/
decrypt(ciphertext: Uint8Array): Uint8Array {
// If ciphertext is empty, we raise an error
if (ciphertext.length == 0) {
throw new Error("tried to decrypt empty ciphertext");
}
const plaintext = chaCha20Poly1305Decrypt(ciphertext, this.nonce, this.ad, this.k);
if (!plaintext) {
throw new Error("decryptWithAd failed");
}
return plaintext;
}
}
/**
* A Noise public key is a public key exchanged during Noise handshakes (no private part)
@ -126,42 +70,4 @@ export class NoisePublicKey {
return new NoisePublicKey(flag, pk);
}
/**
* Encrypt a NoisePublicKey using a ChaChaPolyCipherState
* @param pk NoisePublicKey to encrypt
* @param cs ChaChaPolyCipherState used to encrypt
* @returns encrypted NoisePublicKey
*/
static encrypt(pk: NoisePublicKey, cs: ChaChaPolyCipherState): NoisePublicKey {
// We proceed with encryption only if
// - a key is set in the cipher state
// - the public key is unencrypted
if (!isEmptyKey(cs.k) && pk.flag == 0) {
const encPk = cs.encrypt(pk.pk);
return new NoisePublicKey(1, encPk);
}
// Otherwise we return the public key as it is
return pk.clone();
}
/**
* Decrypts a Noise public key using a ChaChaPoly Cipher State
* @param pk NoisePublicKey to decrypt
* @param cs ChaChaPolyCipherState used to decrypt
* @returns decrypted NoisePublicKey
*/
static decrypt(pk: NoisePublicKey, cs: ChaChaPolyCipherState): NoisePublicKey {
// We proceed with decryption only if
// - a key is set in the cipher state
// - the public key is encrypted
if (!isEmptyKey(cs.k) && pk.flag == 1) {
const decrypted = cs.decrypt(pk.pk);
return new NoisePublicKey(0, decrypted);
}
// Otherwise we return the public key as it is
return pk.clone();
}
}