From f225f994ec71b6150d9f133b43069b48f263e725 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 9 Apr 2018 17:32:53 +0200 Subject: [PATCH] Add AEAD-OCB test vector --- test/general/packet.js | 80 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/test/general/packet.js b/test/general/packet.js index c6dc7144..4f951e8c 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -453,8 +453,8 @@ describe("Packet", function() { } }); - it('Sym. encrypted session key reading/writing test vector (draft04)', async function() { - // From https://gitlab.com/openpgp-wg/rfc4880bis/commit/00b20923e6233fb6ff1666ecd5acfefceb32907d + it('Sym. encrypted session key reading/writing test vector (EAX, draft04)', async function() { + // From https://gitlab.com/openpgp-wg/rfc4880bis/blob/00b20923/back.mkd#sample-aead-eax-encryption-and-decryption let aead_protectVal = openpgp.config.aead_protect; let aead_chunk_size_byteVal = openpgp.config.aead_chunk_size_byte; @@ -528,6 +528,82 @@ describe("Packet", function() { } }); + it('Sym. encrypted session key reading/writing test vector (OCB, draft04)', async function() { + // From https://gitlab.com/openpgp-wg/rfc4880bis/blob/00b20923/back.mkd#sample-aead-ocb-encryption-and-decryption + + let aead_protectVal = openpgp.config.aead_protect; + let aead_chunk_size_byteVal = openpgp.config.aead_chunk_size_byte; + let s2k_iteration_count_byteVal = openpgp.config.s2k_iteration_count_byte; + openpgp.config.aead_protect = 'draft04'; + openpgp.config.aead_chunk_size_byte = 14; + openpgp.config.s2k_iteration_count_byte = 0x90; + + let salt = openpgp.util.hex_to_Uint8Array(`9f0b7da3e5ea6477`); + let sessionKey = openpgp.util.hex_to_Uint8Array(`d1 f0 1b a3 0e 13 0a a7 d2 58 2c 16 e0 50 ae 44`.replace(/\s+/g, '')); + let sessionIV = openpgp.util.hex_to_Uint8Array(`99 e3 26 e5 40 0a 90 93 6c ef b4 e8 eb a0 8c`.replace(/\s+/g, '')); + let dataIV = openpgp.util.hex_to_Uint8Array(`5e d2 bc 1e 47 0a be 8f 1d 64 4c 7a 6c 8a 56`.replace(/\s+/g, '')); + + let randomBytesStub = stub(openpgp.crypto.random, 'getRandomBytes'); + randomBytesStub.onCall(0).returns(resolves(salt)); + randomBytesStub.onCall(1).returns(resolves(sessionKey)); + randomBytesStub.onCall(2).returns(resolves(sessionIV)); + randomBytesStub.onCall(3).returns(resolves(dataIV)); + + let packetBytes = openpgp.util.hex_to_Uint8Array(` + c3 3d 05 07 02 03 08 9f 0b 7d a3 e5 ea 64 77 90 + 99 e3 26 e5 40 0a 90 93 6c ef b4 e8 eb a0 8c 67 + 73 71 6d 1f 27 14 54 0a 38 fc ac 52 99 49 da c5 + 29 d3 de 31 e1 5b 4a eb 72 9e 33 00 33 db ed + + d4 49 01 07 02 0e 5e d2 bc 1e 47 0a be 8f 1d 64 + 4c 7a 6c 8a 56 7b 0f 77 01 19 66 11 a1 54 ba 9c + 25 74 cd 05 62 84 a8 ef 68 03 5c 62 3d 93 cc 70 + 8a 43 21 1b b6 ea f2 b2 7f 7c 18 d5 71 bc d8 3b + 20 ad d3 a0 8b 73 af 15 b9 a0 98 + `.replace(/\s+/g, '')); + + try { + const passphrase = 'password'; + const algo = 'aes128'; + + const literal = new openpgp.packet.Literal(0); + const key_enc = new openpgp.packet.SymEncryptedSessionKey(); + const enc = new openpgp.packet.SymEncryptedAEADProtected(); + const msg = new openpgp.packet.List(); + enc.aeadAlgorithm = key_enc.aeadAlgorithm = 'ocb'; + + msg.push(key_enc); + msg.push(enc); + + key_enc.sessionKeyAlgorithm = algo; + await key_enc.encrypt(passphrase); + + const key = key_enc.sessionKey; + + literal.setBytes(openpgp.util.str_to_Uint8Array('Hello, world!\n'), openpgp.enums.literal.binary); + literal.filename = ''; + enc.packets.push(literal); + await enc.encrypt(algo, key); + + const data = msg.write(); + expect(data).to.deep.equal(packetBytes); + + const msg2 = new openpgp.packet.List(); + msg2.read(data); + + await msg2[0].decrypt(passphrase); + const key2 = msg2[0].sessionKey; + await msg2[1].decrypt(msg2[0].sessionKeyAlgorithm, key2); + + expect(stringify(msg2[1].packets[0].data)).to.equal(stringify(literal.data)); + } finally { + openpgp.config.aead_protect = aead_protectVal; + openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteVal; + openpgp.config.s2k_iteration_count_byte = s2k_iteration_count_byteVal; + randomBytesStub.restore(); + } + }); + it('Secret key encryption/decryption test', async function() { const armored_msg = '-----BEGIN PGP MESSAGE-----\n' +