Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/src/proto/pace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions test/pace_aes_256_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}