diff --git a/lib/src/proto/pace.dart b/lib/src/proto/pace.dart index 5ab60be..76d206e 100644 --- a/lib/src/proto/pace.dart +++ b/lib/src/proto/pace.dart @@ -544,7 +544,7 @@ class PACE { if (cipherAlgorithm == CipherAlgorithm.AES) { _log.debug("Cipher algorithm: AES."); - AESCipher aesCipher = AESChiperSelector.getChiper(size: KEY_LENGTH.s128); //size is not important + AESCipher aesCipher = AESChiperSelector.getChiper(size: keyLength); Uint8List computedAuthToken = aesCipher.calculateCMAC(data: inputData, key: macKey); _log.sdVerbose("Computed auth token: ${computedAuthToken.hex()}"); return computedAuthToken; @@ -580,8 +580,8 @@ class PACE { if (cipherAlgo == CipherAlgorithm.AES){ _log.debug("PACE.decryptNonce; Cipher algorithm: AES"); - AESCipher aesCipher128 = AESChiperSelector.getChiper(size: KEY_LENGTH.s128); - Uint8List decryptedNonce = aesCipher128.decrypt(data: nonce, key: k_pi); + AESCipher aesCipher = AESChiperSelector.getChiper(size: keyLength); + Uint8List decryptedNonce = aesCipher.decrypt(data: nonce, key: k_pi); _log.sdVerbose("PACE.decryptNonce; Decrypted nonce: ${decryptedNonce.hex()}"); return decryptedNonce; } diff --git a/test/pace_aes_256_test.dart b/test/pace_aes_256_test.dart new file mode 100644 index 0000000..ef8d967 --- /dev/null +++ b/test/pace_aes_256_test.dart @@ -0,0 +1,45 @@ +import 'dart:typed_data'; + +import 'package:test/test.dart'; +import 'package:dmrtd/extensions.dart'; + +import 'package:dmrtd/src/proto/pace.dart'; +import 'package:dmrtd/src/lds/asn1ObjectIdentifiers.dart'; +import 'package:dmrtd/src/proto/access_key.dart'; +import 'package:dmrtd/src/crypto/aes.dart'; + +class _DummyAccessKey extends AccessKey { + @override + int PACE_REF_KEY_TAG = 0x00; + + final Uint8List _kpi; + _DummyAccessKey(this._kpi); + + @override + Uint8List Kpi(CipherAlgorithm cipherAlgorithm, KEY_LENGTH keyLength) => _kpi; + + @override + String toString() => 'DummyAccessKey{Kpi:${_kpi.hex()}}'; +} + +void main() { + test('decryptNonce accepts AES key length different from block size', () { + final paceProtocolMap = customOIDS.firstWhere( + (e) => e['readableName'] == 'id-PACE-ECDH-GM-AES-CBC-CMAC-256'); + final paceProtocol = OIEPaceProtocol.fromMap(item: paceProtocolMap); + + final kpi = + '00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF' + .parseHex(); + final nonce = 'A1A2A3A4A5A6A7A8A9AAABACADAEAFB0'.parseHex(); + + final aes = AESChiperSelector.getChiper(size: KEY_LENGTH.s256); + final encrypted = aes.encrypt(data: nonce, key: kpi); + + final accessKey = _DummyAccessKey(kpi); + final decrypted = PACE.decryptNonce( + paceProtocol: paceProtocol, nonce: encrypted, accessKey: accessKey); + + expect(decrypted, nonce); + }); +} \ No newline at end of file