Switch cipher/aes.js to Uint8Arrays
This commit is contained in:
parent
c2f898279b
commit
5f891d28d6
|
@ -7,16 +7,14 @@ import { AES_ECB } from 'asmcrypto.js/src/aes/ecb/exports';
|
||||||
// TODO use webCrypto or nodeCrypto when possible.
|
// TODO use webCrypto or nodeCrypto when possible.
|
||||||
function aes(length) {
|
function aes(length) {
|
||||||
const c = function(key) {
|
const c = function(key) {
|
||||||
this.key = Uint8Array.from(key);
|
this.key = key;
|
||||||
|
|
||||||
this.encrypt = function(block) {
|
this.encrypt = function(block) {
|
||||||
block = Uint8Array.from(block);
|
return AES_ECB.encrypt(block, this.key, false);
|
||||||
return Array.from(AES_ECB.encrypt(block, this.key, false));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.decrypt = function(block) {
|
this.decrypt = function(block) {
|
||||||
block = Uint8Array.from(block);
|
return AES_ECB.decrypt(block, this.key, false);
|
||||||
return Array.from(AES_ECB.decrypt(block, this.key, false));
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,13 @@ const { expect } = chai;
|
||||||
|
|
||||||
describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() {
|
describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() {
|
||||||
function test_aes(input, key, output) {
|
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]],
|
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) {
|
it('128 bit key', function (done) {
|
||||||
for (let i = 0; i < testvectors128.length; i++) {
|
for (let i = 0; i < testvectors128.length; i++) {
|
||||||
const res = test_aes(testvectors128[i][1],testvectors128[i][0],testvectors128[i][2]);
|
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;
|
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('192 bit key', function (done) {
|
it('192 bit key', function (done) {
|
||||||
for (let i = 0; i < testvectors192.length; i++) {
|
for (let i = 0; i < testvectors192.length; i++) {
|
||||||
const res = test_aes(testvectors192[i][1],testvectors192[i][0],testvectors192[i][2]);
|
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;
|
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('256 bit key', function (done) {
|
it('256 bit key', function (done) {
|
||||||
for (let i = 0; i < testvectors256.length; i++) {
|
for (let i = 0; i < testvectors256.length; i++) {
|
||||||
const res = test_aes(testvectors256[i][1],testvectors256[i][0],testvectors256[i][2]);
|
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;
|
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
|
@ -89,7 +89,7 @@ describe("Packet", function() {
|
||||||
message.push(enc);
|
message.push(enc);
|
||||||
await enc.packets.push(literal);
|
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';
|
const algo = 'aes256';
|
||||||
|
|
||||||
await enc.encrypt(algo, key);
|
await enc.encrypt(algo, key);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user