From 5f891d28d69d073f2595e479fd8e4badbdab028e Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 9 Apr 2018 14:46:13 +0200 Subject: [PATCH] Switch cipher/aes.js to Uint8Arrays --- src/crypto/cipher/aes.js | 8 +++----- test/crypto/cipher/aes.js | 23 ++++++++--------------- test/general/packet.js | 2 +- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/crypto/cipher/aes.js b/src/crypto/cipher/aes.js index 55505cfd..dcd2bd03 100644 --- a/src/crypto/cipher/aes.js +++ b/src/crypto/cipher/aes.js @@ -7,16 +7,14 @@ import { AES_ECB } from 'asmcrypto.js/src/aes/ecb/exports'; // TODO use webCrypto or nodeCrypto when possible. function aes(length) { const c = function(key) { - this.key = Uint8Array.from(key); + this.key = key; this.encrypt = function(block) { - block = Uint8Array.from(block); - return Array.from(AES_ECB.encrypt(block, this.key, false)); + return AES_ECB.encrypt(block, this.key, false); }; this.decrypt = function(block) { - block = Uint8Array.from(block); - return Array.from(AES_ECB.decrypt(block, this.key, false)); + return AES_ECB.decrypt(block, this.key, false); }; }; diff --git a/test/crypto/cipher/aes.js b/test/crypto/cipher/aes.js index 806f7114..c381befc 100644 --- a/test/crypto/cipher/aes.js +++ b/test/crypto/cipher/aes.js @@ -7,11 +7,13 @@ const { expect } = chai; describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() { function test_aes(input, key, output) { - const aes = new openpgp.crypto.cipher.aes128(key); + const aes = new openpgp.crypto.cipher.aes128(new Uint8Array(key)); - const result = util.Uint8Array_to_str(aes.encrypt(new Uint8Array(input))); + const encrypted = aes.encrypt(new Uint8Array(input)); + expect(encrypted).to.deep.equal(new Uint8Array(output)); - return util.str_to_hex(result) === util.str_to_hex(util.Uint8Array_to_str(output)); + const decrypted = aes.decrypt(new Uint8Array(output)); + expect(decrypted).to.deep.equal(new Uint8Array(input)); } const testvectors128 = [[[0x00,0x01,0x02,0x03,0x05,0x06,0x07,0x08,0x0A,0x0B,0x0C,0x0D,0x0F,0x10,0x11,0x12],[0x50,0x68,0x12,0xA4,0x5F,0x08,0xC8,0x89,0xB9,0x7F,0x59,0x80,0x03,0x8B,0x83,0x59],[0xD8,0xF5,0x32,0x53,0x82,0x89,0xEF,0x7D,0x06,0xB5,0x06,0xA4,0xFD,0x5B,0xE9,0xC9]], @@ -64,30 +66,21 @@ describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function it('128 bit key', function (done) { for (let i = 0; i < testvectors128.length; i++) { - const res = test_aes(testvectors128[i][1],testvectors128[i][0],testvectors128[i][2]); - expect(res, 'block ' + util.Uint8Array_to_hex(testvectors128[i][1]) + - ' and key '+util.Uint8Array_to_hex(testvectors128[i][0]) + - ' should be '+util.Uint8Array_to_hex(testvectors128[i][2])).to.be.true; + test_aes(testvectors128[i][1],testvectors128[i][0],testvectors128[i][2]); } done(); }); it('192 bit key', function (done) { for (let i = 0; i < testvectors192.length; i++) { - const res = test_aes(testvectors192[i][1],testvectors192[i][0],testvectors192[i][2]); - expect(res, 'block ' + util.Uint8Array_to_hex(testvectors192[i][1]) + - ' and key ' + util.Uint8Array_to_hex(testvectors192[i][0])+ - ' should be ' + util.Uint8Array_to_hex(testvectors192[i][2])).to.be.true; + test_aes(testvectors192[i][1],testvectors192[i][0],testvectors192[i][2]); } done(); }); it('256 bit key', function (done) { for (let i = 0; i < testvectors256.length; i++) { - const res = test_aes(testvectors256[i][1],testvectors256[i][0],testvectors256[i][2]); - expect(res, 'block ' + util.Uint8Array_to_hex(testvectors256[i][1]) + - ' and key ' + util.Uint8Array_to_hex(testvectors256[i][0]) + - ' should be ' + util.Uint8Array_to_hex(testvectors256[i][2])).to.be.true; + test_aes(testvectors256[i][1],testvectors256[i][0],testvectors256[i][2]); } done(); }); diff --git a/test/general/packet.js b/test/general/packet.js index 3759a6fa..c6dc7144 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -89,7 +89,7 @@ describe("Packet", function() { message.push(enc); await enc.packets.push(literal); - const key = '12345678901234567890123456789012'; + const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]); const algo = 'aes256'; await enc.encrypt(algo, key);