Skip to content

Commit ef82d5b

Browse files
authored
Merge pull request #3027 from aaronjamt/patch-1
[hf seos] Correct command padding size
2 parents 4fa8f27 + 7d198c9 commit ef82d5b

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

client/src/cmdhfseos.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ static void increment_command_wrapper(uint8_t *input, int input_len) {
163163
input[input_len - 1]++; // Increment the last element of the header by 1
164164
}
165165

166-
static void padToBlockSize(const uint8_t *input, int inputSize, int blockSize, uint8_t *output) {
167-
int paddingSize = blockSize - (inputSize % blockSize);
168-
memcpy(output, input, inputSize);
169-
memset(output + inputSize, 0x80, 1);
170-
memset(output + inputSize + 1, 0x00, paddingSize - 1);
166+
static void padToBlockSize(const uint8_t *input, int *inputSize, int blockSize, uint8_t *output) {
167+
int paddingSize = blockSize - (*inputSize % blockSize);
168+
memcpy(output, input, *inputSize);
169+
memset(output + *inputSize, 0x80, 1);
170+
memset(output + *inputSize + 1, 0x00, paddingSize - 1);
171+
*inputSize += paddingSize;
171172
}
172173

173174
static void generate_command_wrapping(uint8_t *command_Header, int command_header_len, uint8_t *unencrypted_Command, int unencrypted_command_len, uint8_t *rndICC, uint8_t *rndIFD, uint8_t *diversified_enc_key, uint8_t *diversified_mac_key, int encryption_algorithm, uint8_t *command, int *command_len) {
@@ -188,29 +189,32 @@ static void generate_command_wrapping(uint8_t *command_Header, int command_heade
188189
increment_command_wrapper(rndCounter, block_size);
189190

190191
// Command Header is for the APDU Command to be sent
191-
uint8_t padded_Command_Header[block_size];
192-
padToBlockSize(command_Header, command_header_len, block_size, padded_Command_Header);
192+
int padded_Command_Header_len = command_header_len;
193+
uint8_t padded_Command_Header[padded_Command_Header_len+block_size];
194+
padToBlockSize(command_Header, &padded_Command_Header_len, block_size, padded_Command_Header);
193195

194196
// Unencrypted Command is our actual command data
195-
uint8_t padded_unencrypted_Command[block_size];
196-
padToBlockSize(unencrypted_Command, unencrypted_command_len, block_size, padded_unencrypted_Command);
197+
int padded_unencrypted_Command_len = unencrypted_command_len;
198+
uint8_t padded_unencrypted_Command[padded_unencrypted_Command_len+block_size];
199+
padToBlockSize(unencrypted_Command, &padded_unencrypted_Command_len, block_size, padded_unencrypted_Command);
197200

198-
uint8_t padded_encrypted_Command[block_size];
199-
create_cryptogram(diversified_enc_key, padded_unencrypted_Command, padded_encrypted_Command, sizeof(padded_unencrypted_Command), encryption_algorithm);
201+
uint8_t padded_encrypted_Command[padded_unencrypted_Command_len];
202+
create_cryptogram(diversified_enc_key, padded_unencrypted_Command, padded_encrypted_Command, padded_unencrypted_Command_len, encryption_algorithm);
200203

201204
uint8_t asn1_tag_cryptograph[2] = {0x85, ARRAYLEN(padded_encrypted_Command)};
202205
uint8_t asn1_tag_mac[2] = {0x8e, 0x08};
203206
uint8_t command_trailer[2] = {0x97, 0x00};
204-
uint8_t padded_command_trailer[block_size - ARRAYLEN(command_trailer)];
205-
padToBlockSize(command_trailer, sizeof(command_trailer), sizeof(padded_command_trailer), padded_command_trailer);
207+
int padded_command_trailer_len = ARRAYLEN(command_trailer);
208+
uint8_t padded_command_trailer[padded_command_trailer_len+block_size];
209+
padToBlockSize(command_trailer, &padded_command_trailer_len, block_size, padded_command_trailer);
206210

207-
uint8_t toEncrypt[ARRAYLEN(rndCounter) + ARRAYLEN(padded_Command_Header) + ARRAYLEN(asn1_tag_cryptograph) + ARRAYLEN(padded_encrypted_Command) + ARRAYLEN(padded_command_trailer)];
211+
uint8_t toEncrypt[ARRAYLEN(rndCounter) + padded_Command_Header_len + ARRAYLEN(asn1_tag_cryptograph) + ARRAYLEN(padded_encrypted_Command) + padded_command_trailer_len];
208212

209213
memcpy(toEncrypt, rndCounter, ARRAYLEN(rndCounter));
210-
memcpy(toEncrypt + ARRAYLEN(rndCounter), padded_Command_Header, ARRAYLEN(padded_Command_Header));
211-
memcpy(toEncrypt + ARRAYLEN(rndCounter) + ARRAYLEN(padded_Command_Header), asn1_tag_cryptograph, ARRAYLEN(asn1_tag_cryptograph));
212-
memcpy(toEncrypt + ARRAYLEN(rndCounter) + ARRAYLEN(padded_Command_Header) + ARRAYLEN(asn1_tag_cryptograph), padded_encrypted_Command, ARRAYLEN(padded_encrypted_Command));
213-
memcpy(toEncrypt + ARRAYLEN(rndCounter) + ARRAYLEN(padded_Command_Header) + ARRAYLEN(asn1_tag_cryptograph) + ARRAYLEN(padded_encrypted_Command), padded_command_trailer, ARRAYLEN(padded_command_trailer));
214+
memcpy(toEncrypt + ARRAYLEN(rndCounter), padded_Command_Header, padded_Command_Header_len);
215+
memcpy(toEncrypt + ARRAYLEN(rndCounter) + padded_Command_Header_len, asn1_tag_cryptograph, ARRAYLEN(asn1_tag_cryptograph));
216+
memcpy(toEncrypt + ARRAYLEN(rndCounter) + padded_Command_Header_len + ARRAYLEN(asn1_tag_cryptograph), padded_encrypted_Command, ARRAYLEN(padded_encrypted_Command));
217+
memcpy(toEncrypt + ARRAYLEN(rndCounter) + padded_Command_Header_len + ARRAYLEN(asn1_tag_cryptograph) + ARRAYLEN(padded_encrypted_Command), padded_command_trailer, padded_command_trailer_len);
214218

215219
// Breakdown
216220
// 0181e43801010201 + 0000000000000001 + 0CCB3FFF800000000000000000000000 + 8510EB54DA90CB43AEE7FBFE816ECA25A10D + 9700 + 800000000000000000000000

0 commit comments

Comments
 (0)