diff --git a/test/aes_pcbc_test.dart b/test/aes_pcbc_test.dart index 80dc6ae..f2ef061 100644 --- a/test/aes_pcbc_test.dart +++ b/test/aes_pcbc_test.dart @@ -72,6 +72,77 @@ void main() { } }); + // No published standard vectors exist for PCBC. These expected values come + // from an independent reference: the AES block primitive from Python's + // pyca/cryptography 50.0.1 (OpenSSL) with PCBC chaining written per + // https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Propagating_cipher_block_chaining_(PCBC) + // C_i = E_K(P_i ^ P_(i-1) ^ C_(i-1)), P_0 ^ C_0 = IV + // Key, IV and plaintext are from NIST SP 800-38A, Appendix F.2. The first + // block must equal the CBC first block of F.2.1/F.2.3/F.2.5. + group('known inputs (independent reference)', () { + final iv = fromHex('000102030405060708090a0b0c0d0e0f'); + final plain = fromHex( + '6bc1bee22e409f96e93d7e117393172a' + 'ae2d8a571e03ac9c9eb76fac45af8e51' + '30c81c46a35ce411e5fbc1191a0a52ef' + 'f69f2445df4f9b17ad2b417be66c3710', + ); + final vectors = { + 'AES128': [ + '2b7e151628aed2a6abf7158809cf4f3c', + '7649abac8119b246cee98e9b12e9197d' + '9e8baff12ad5270a0d1eef93d7037994' + '5700b39803779fa35a3c600a49a163c0' + '33ae199f27379f21be6dd57d295cc87d', + ], + 'AES192': [ + '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b', + '4f021db243bc633d7178183a9fa071e8' + '1acaac3d3dcad4c817a0d242174e1459' + '3981c337de3e3fad8743ee4c8c6c86dd' + 'bc1cbb499b805e0956a2f58bc3289059', + ], + 'AES256': [ + '603deb1015ca71be2b73aef0857d7781' + '1f352c073b6108d72d9810a30914dff4', + 'f58c4c04d6e5f1ba779eabfb5f7bfbd6' + 'e9b49d86e04ec7b4c4e3ddd45dec395a' + '189ce8209083344cce2e02f3b6d8edae' + 'b135724f867f99dc38420d4621f59b2d', + ], + }; + vectors.forEach((name, v) { + final key = fromHex(v[0]); + final cipher = fromHex(v[1]); + group(name, () { + test('encrypt', () { + final aes = AES.noPadding(key).pcbc(iv); + expect(toHex(aes.encrypt(plain)), equals(toHex(cipher))); + }); + test('decrypt', () { + final aes = AES.noPadding(key).pcbc(iv); + expect(toHex(aes.decrypt(cipher)), equals(toHex(plain))); + }); + }); + }); + group('AES128 PKCS#7 (30-byte message)', () { + final key = fromHex('2b7e151628aed2a6abf7158809cf4f3c'); + final message = plain.sublist(0, 30); + final cipher = fromHex( + '7649abac8119b246cee98e9b12e9197d' + 'd220d44bffd51f1ffb4a684586c12f89', + ); + test('encrypt', () { + final aes = AES.pkcs7(key).pcbc(iv); + expect(toHex(aes.encrypt(message)), equals(toHex(cipher))); + }); + test('decrypt', () { + final aes = AES.pkcs7(key).pcbc(iv); + expect(toHex(aes.decrypt(cipher)), equals(toHex(message))); + }); + }); + }); + group('stream cipher', () { test('encryptor bind matches convert with chunked input', () async { final key = fromHex('2b7e151628aed2a6abf7158809cf4f3c');