Use the correct counter values with encryption/decryption

This commit is contained in:
Juhani Krekelä 2021-04-09 20:49:21 +03:00
parent 138cc5d2f5
commit 0cb02aaf14
1 changed files with 7 additions and 5 deletions

12
puer.c
View File

@ -394,15 +394,15 @@ void ccm_encrypt(unsigned char key[16], uint32_t messageindex, unsigned char mes
// Xor full blocks
size_t index = 0;
uint32_t counter = 1;
for (; index + 16 <= length; index += 16) {
// Message blocks are numbered from index 1 onwards
ccm_xor_block(&message[index], key_words, messageindex, index + 1);
ccm_xor_block(&message[index], key_words, messageindex, counter++);
}
// Xor partial block, if any
if (index < length) {
unsigned char fullblock[16];
memcpy(fullblock, &message[index], length - index);
ccm_xor_block(fullblock, key_words, messageindex, index + 1);
ccm_xor_block(fullblock, key_words, messageindex, counter++);
memcpy(&message[index], fullblock, length - index);
}
}
@ -417,15 +417,17 @@ bool ccm_decrypt(unsigned char key[16], uint32_t messageindex, unsigned char mes
// Xor full blocks
size_t index = 0;
uint32_t counter = 1;
for (; index + 16 <= length; index += 16) {
// Message blocks are numbered from index 1 onwards
ccm_xor_block(&message[index], key_words, messageindex, index + 1);
ccm_xor_block(&message[index], key_words, messageindex, counter++);
}
// Xor partial block, if any
if (index < length) {
unsigned char fullblock[16];
memset(fullblock, 0, 16);
memcpy(fullblock, &message[index], length - index);
ccm_xor_block(fullblock, key_words, messageindex, index + 1);
ccm_xor_block(fullblock, key_words, messageindex, counter++);
memcpy(&message[index], fullblock, length - index);
}