Merge pull request #2 from NodeFactoryIo/morrigan/xx-symmetric-encryption

xx symmetric encryption
This commit is contained in:
Marin Petrunić 2019-11-08 10:11:36 +01:00 committed by GitHub
commit 94a990a479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -90,11 +90,13 @@ export class XXHandshake {
private encrypt(k: bytes32, n: uint32, ad: bytes, plaintext: bytes) : bytes {
const nonce = this.nonceToBytes(n);
const ctx = new AEAD();
ctx.init(k, nonce);
ctx.aad(ad);
ctx.encrypt(plaintext);
return ctx.final();
// Encryption is done on the sent reference
return plaintext;
}
private decrypt(k: bytes32, n: uint32, ad: bytes, ciphertext: bytes) : bytes {
@ -105,7 +107,8 @@ export class XXHandshake {
ctx.aad(ad);
ctx.decrypt(ciphertext);
return ctx.final();
// Decryption is done on the sent reference
return ciphertext;
}
private isEmptyKey(k: bytes32) : boolean {

View File

@ -1,6 +1,5 @@
import { expect, assert } from "chai";
import { Buffer } from 'buffer';
import { ed25519 } from 'bcrypto';
import { XXHandshake, KeyPair } from "../src/xx";
import { loadPayloadProto, generateEd25519Keys } from "./utils";
@ -108,4 +107,32 @@ describe("Index", () => {
await doHandshake(xx);
});
it("Test symmetric encrypt and decrypt", async () => {
const xx = new XXHandshake();
const { nsInit, nsResp } = await doHandshake(xx);
const ad = Buffer.from("authenticated");
const message = Buffer.from("HelloCrypto");
xx.encryptWithAd(nsInit.cs1, ad, message);
assert(!Buffer.from("HelloCrypto").equals(message), "Encrypted message should not be same as plaintext.");
const decrypted = xx.decryptWithAd(nsResp.cs1, ad, message);
assert(Buffer.from("HelloCrypto").equals(decrypted), "Decrypted text not equal to original message.");
});
it("Test multiple messages encryption and decryption", async () => {
const xx = new XXHandshake();
const { nsInit, nsResp } = await doHandshake(xx);
const ad = Buffer.from("authenticated");
const message = Buffer.from("ethereum1");
xx.encryptWithAd(nsInit.cs1, ad, message);
const decrypted = xx.decryptWithAd(nsResp.cs1, ad, message);
assert(Buffer.from("ethereum1").equals(decrypted), "Decrypted text not equal to original message.");
const message2 = Buffer.from("ethereum2");
xx.encryptWithAd(nsInit.cs1, ad, message2);
const decrypted2 = xx.decryptWithAd(nsResp.cs1, ad, message2);
assert(Buffer.from("ethereum2").equals(decrypted2), "Decrypted text not equal to original message.");
});
});