Skip to content

Commit f0c9779

Browse files
authored
Merge pull request #3040 from aaronjamt/patch-1
[hf seos] Fix CMAC writing out-of-bounds
2 parents 046a26c + 950c087 commit f0c9779

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

client/src/cmdhfseos.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,26 @@ static const known_algo_t known_algorithm_map[] = {
103103
{9, "AES-128_CBC_MODE"},
104104
};
105105

106-
static int create_cmac(uint8_t *key, uint8_t *input, uint8_t *out, int input_len, int encryption_algorithm) {
106+
static int create_cmac(uint8_t *key, uint8_t *input, uint8_t *out, int input_len, int output_len, int encryption_algorithm) {
107107
uint8_t iv[16] = {0x00};
108+
uint8_t mac[16] = {0x00};
108109

109110
if (encryption_algorithm == 0x09) {
110111
// Working as expected
111-
aes_cmac(iv, key, input, out, input_len);
112+
aes_cmac(iv, key, input, mac, input_len);
112113
} else if (encryption_algorithm == 0x02) {
113114
// CMAC Requires a 24 byte key, but the 2k3DES uses the 1st part for the 3rd part of the key
114115
memcpy(&key[16], &key[0], 8);
115116

116117
const mbedtls_cipher_info_t *ctx;
117118
ctx = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
118-
mbedtls_cipher_cmac(ctx, key, 192, input, input_len, out);
119+
mbedtls_cipher_cmac(ctx, key, 192, input, input_len, mac);
119120
} else {
120121
PrintAndLogEx(ERR, _RED_("Unknown Encryption Algorithm"));
121122
return PM3_ESOFT;
122123
}
124+
// Copy only requested number of bytes into output buffer
125+
memcpy(out, mac, output_len);
123126
return PM3_SUCCESS;
124127
}
125128

@@ -221,7 +224,7 @@ static void generate_command_wrapping(uint8_t *command_Header, int command_heade
221224
// 0181e43801010201 + 0000000000000001 + 0CCB3FFF800000000000000000000000 + 8510EB54DA90CB43AEE7FBFE816ECA25A10D + 9700 + 800000000000000000000000
222225

223226
uint8_t mac[8];
224-
create_cmac(diversified_mac_key, padded_toEncrypt, mac, padded_toEncrypt_len, encryption_algorithm);
227+
create_cmac(diversified_mac_key, padded_toEncrypt, mac, padded_toEncrypt_len, sizeof(mac), encryption_algorithm);
225228

226229
// PrintAndLogEx(SUCCESS, "Encryption Key................... " _YELLOW_("%s"), sprint_hex_inrow(diversified_enc_key, 24));
227230
// PrintAndLogEx(SUCCESS, "MAC Key.......................... " _YELLOW_("%s"), sprint_hex_inrow(diversified_mac_key, 24));
@@ -587,7 +590,7 @@ static int select_DF_verify(uint8_t *response, uint8_t response_length, uint8_t
587590
uint8_t cmac[16];
588591
uint8_t MAC_key[24] = {0x00};
589592
memcpy(MAC_key, keys[key_index].privMacKey, 16);
590-
create_cmac(MAC_key, input, cmac, input_len, encryption_algorithm);
593+
create_cmac(MAC_key, input, cmac, input_len, sizeof(cmac), encryption_algorithm);
591594

592595
// PrintAndLogEx(INFO, "--- " _CYAN_("MAC") " ---------------------------");
593596
// PrintAndLogEx(SUCCESS, "MAC Key: "_YELLOW_("%s"), sprint_hex_inrow(MAC_key,sizeof(MAC_key)));
@@ -821,7 +824,7 @@ static int seos_mutual_auth(uint8_t *adfOID, size_t adfoid_len, uint8_t *randomI
821824
uint8_t mac[8];
822825
uint8_t mutual_auth_enc[32];
823826
create_cryptogram(AES_key, mutual_auth_plain, mutual_auth_enc, sizeof(mutual_auth_plain), encryption_algorithm);
824-
create_cmac(MAC_key, mutual_auth_enc, mac, sizeof(mutual_auth_enc), encryption_algorithm);
827+
create_cmac(MAC_key, mutual_auth_enc, mac, sizeof(mutual_auth_enc), sizeof(mac), encryption_algorithm);
825828

826829
uint8_t message_authenticated[40];
827830
memcpy(message_authenticated, mutual_auth_enc, sizeof(mutual_auth_enc));

0 commit comments

Comments
 (0)