1
0
Fork 0

Adds CMAC KDF extract, preliminary expand (counter mode) present but not passing vectors

This commit is contained in:
Shawn Nock 2016-08-26 12:37:16 -04:00
parent a26746c9e8
commit d3a0f92b84
4 changed files with 170 additions and 1 deletions

57
ckdf.c Normal file
View File

@ -0,0 +1,57 @@
/* CMAC Key Derivation Function
HKDF, but with CMAC
Based on ietf draft-agl-ckdf-00
Shawn Nock - <nock@nocko.se>
*/
#include <stdint.h>
#include <stdio.h>
#include "cmac.h"
void ckdf_extract(uint8_t *prk, uint8_t *salt, uint8_t *ikm,
uint32_t ikm_len) {
uint8_t zeros[16] = {0};
if (salt == NULL) {
cmac_aes128_init(zeros);
} else {
cmac_aes128_init(salt);
}
cmac_aes128(prk, ikm, ikm_len, 16);
}
void ckdf_expand(uint8_t *okm, uint8_t *prk, uint8_t *info,
uint32_t info_len, uint32_t okm_len) {
uint32_t num_blocks = okm_len / 16;
/* Simulate ceiling division instead of C std floor */
uint8_t remainder = okm_len % 16;
if (remainder) {
num_blocks++;
}
cmac_aes128_init(prk);
uint8_t t[16+info_len+1];
uint8_t *initial = &t[16];
memset(t, 0, 16);
for (uint32_t i = 0; i < num_blocks; i++) {
*(initial+info_len) = i+1;
if (i == 0) {
memcpy(initial, info, info_len);
cmac_aes128(t, initial, info_len+1, 16);
} else {
block_print_bytes("IN", t, sizeof(t));
cmac_aes128(t, t, sizeof(t), 16);
block_print_bytes("OUT", t, sizeof(t));
}
printf("T%d: ", i+1);
block_print(NULL, t);
if (i == num_blocks - 1 && remainder) {
memcpy(okm+16*i, t, remainder);
} else {
memcpy(okm+16*i, t, 16);
}
}
return;
}

12
ckdf.h Normal file
View File

@ -0,0 +1,12 @@
/* CMAC Key Derivation Function
HKDF, but with CMAC
Based on ietf draft-agl-ckdf-00
Shawn Nock - <nock@nocko.se>
*/
void ckdf_extract(uint8_t *, uint8_t *, uint8_t *, uint32_t);
void ckdf_expand(uint8_t *, uint8_t *, uint8_t *, uint32_t, uint32_t);

View File

@ -78,7 +78,7 @@ debug-gdbinit:
.PHONY: flash flash-softdevice erase-all startdebug
TEST_CFLAGS=-std=gnu99 -g -Wall -Werror -I.. -DHOST_BUILD -fsanitize=address -Os
TEST_CFLAGS=-std=gnu99 -g -Wall -Werror -I.. -DHOST_BUILD -Os
test:
$(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c cmac.c tests/test_cmac.c -o tests/test_cmac
@ -87,3 +87,5 @@ test:
./tests/test_ctr
$(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c ctr.c cmac.c eax.c tests/test_eax.c -o tests/test_eax
./tests/test_eax
$(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c cmac.c ckdf.c tests/test_ckdf.c -o tests/test_ckdf
./tests/test_ckdf

98
tests/test_ckdf.c Normal file
View File

@ -0,0 +1,98 @@
/* Test vectors for CKDF */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "../ckdf.h"
#include "../block.h"
uint8_t zeros[16] = {0};
int main(void) {
printf("Testing CKDF-AES128-Extract: ");
fflush(stdout);
uint8_t salt[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
uint8_t prk[16];
/* Test Null IKM */
uint8_t case1[16] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46};
ckdf_extract(prk, salt, NULL, 0);
printf("%s", block_eq(prk, case1) ? "." : "F");
/* Vector #2 */
uint8_t ikm2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t case2[16] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c};
ckdf_extract(prk, salt, ikm2, sizeof(ikm2));
printf("%s", block_eq(prk, case2) ? "." : "F");
/* Null salt */
uint8_t ikm3[] = {0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x20, 0x6b,
0x65, 0x79};
uint8_t case3[] = {0x6f, 0x79, 0xb4, 0x01, 0xea, 0x76, 0x1a, 0x01,
0x00, 0xb7, 0xca, 0x60, 0xc1, 0x78, 0xb6, 0x9d};
ckdf_extract(prk, NULL, ikm3, sizeof(ikm3));
printf("%s\n", block_eq(prk, case3) ? "." : "F");
/* printf("Testing CKDF-AES128-Extract: "); */
/* fflush(stdout); */
/* /\* Example 4 *\/ */
/* uint8_t prk4[] = {0x6f, 0x79, 0xb4, 0x01, 0xea, 0x76, 0x1a, 0x01, */
/* 0x00, 0xb7, 0xca, 0x60, 0xc1, 0x78, 0xb6, 0x9d}; */
/* uint8_t *info4 = NULL; */
/* uint8_t info4_len = 0; */
/* uint32_t okm4_len = 32; */
/* uint8_t case4[] = {0x92, 0x2d, 0xa3, 0x1d, 0x7e, 0x19, 0x55, 0xf0, */
/* 0x6a, 0x56, 0x46, 0x4b, 0x5f, 0xeb, 0x70, 0x32, */
/* 0xf3, 0xe9, 0x96, 0x29, 0x51, 0x65, 0xf6, 0xc6, */
/* 0x0e, 0x08, 0xba, 0x43, 0x2d, 0xd9, 0x05, 0x8b}; */
/* uint8_t okm4[okm4_len]; */
/* ckdf_expand(okm4, prk4, info4, info4_len, okm4_len); */
/* printf("%s", !memcmp(okm4, case4, okm4_len) ? "." : "F"); */
/* /\* Example 5 *\/ */
/* uint8_t info5[] = {0x69, 0x6e, 0x66, 0x6f, 0x20, 0x73, 0x74, 0x72, */
/* 0x69, 0x6e, 0x67}; */
/* uint32_t okm5_len = 256; */
/* uint8_t okm5[okm5_len]; */
/* uint8_t case5[] = {0x61, 0x74, 0xe6, 0x72, 0x12, 0xe1, 0x23, 0x4b, */
/* 0x6e, 0x05, 0xbf, 0xd3, 0x10, 0x43, 0x42, 0x2c, */
/* 0x7a, 0xb6, 0xdc, 0x31, 0x5d, 0xb7, 0xd9, 0x8d, */
/* 0x01, 0x3a, 0xb3, 0x32, 0x92, 0x4b, 0x7f, 0xe9, */
/* 0x0a, 0xe9, 0xa8, 0x9d, 0x09, 0xc9, 0x3b, 0xe4, */
/* 0x0c, 0xe5, 0x25, 0xe0, 0xb6, 0xf0, 0xd3, 0x7d, */
/* 0xf3, 0x81, 0x81, 0x91, 0x3a, 0xa3, 0xd5, 0x88, */
/* 0xf7, 0x5a, 0x35, 0x94, 0xef, 0x7a, 0x93, 0xac, */
/* 0xd7, 0x91, 0x33, 0x1e, 0x79, 0x29, 0xde, 0x8b, */
/* 0xc8, 0xc8, 0xa6, 0xee, 0x2d, 0xd9, 0x96, 0x0e, */
/* 0xc5, 0x7f, 0xe1, 0x59, 0x61, 0x06, 0x76, 0xa7, */
/* 0xc1, 0x18, 0xc4, 0xaa, 0xc2, 0xd3, 0x4a, 0x89, */
/* 0x6e, 0xdd, 0x36, 0x91, 0xf0, 0xe9, 0x22, 0xa3, */
/* 0x0e, 0xec, 0xc7, 0xb3, 0xec, 0x3e, 0xaa, 0x91, */
/* 0x13, 0xd4, 0xee, 0x51, 0x8b, 0x0a, 0x4c, 0x7e, */
/* 0xd0, 0xb4, 0x75, 0xdf, 0xbd, 0x07, 0xee, 0x02, */
/* 0xa3, 0x47, 0x08, 0x32, 0xda, 0x24, 0x7e, 0xf3, */
/* 0xb0, 0x7f, 0x9a, 0xcd, 0x8d, 0xdb, 0xb7, 0x65, */
/* 0x73, 0x69, 0xe1, 0xc5, 0x29, 0x42, 0xfa, 0xb2, */
/* 0x11, 0xd4, 0x7c, 0x44, 0x0d, 0x68, 0x18, 0xf8, */
/* 0x29, 0xcd, 0xd8, 0xda, 0xd8, 0x4b, 0x82, 0x5e, */
/* 0x11, 0x66, 0xcb, 0xdc, 0xdb, 0xb1, 0x39, 0x04, */
/* 0xd6, 0x75, 0x3d, 0xe7, 0x60, 0x70, 0xa1, 0x45, */
/* 0xa8, 0x57, 0x24, 0x96, 0xc2, 0x80, 0x85, 0x67, */
/* 0x94, 0x59, 0xd8, 0x01, 0xf1, 0x44, 0x49, 0xfb, */
/* 0xf3, 0x43, 0x0a, 0x83, 0x68, 0x5a, 0x4b, 0x8d, */
/* 0x09, 0x1d, 0xc2, 0xfc, 0x85, 0xb8, 0x20, 0x9d, */
/* 0x7c, 0xfd, 0x5d, 0xbd, 0x39, 0xd7, 0x9a, 0x8d, */
/* 0xd7, 0xc6, 0xf9, 0x81, 0xaf, 0x06, 0x4c, 0xe6, */
/* 0x9e, 0x58, 0xa9, 0x9f, 0xbd, 0x9f, 0xfd, 0x58, */
/* 0xa2, 0xd9, 0x3d, 0x60, 0x97, 0x2e, 0xc8, 0x73, */
/* 0xf2, 0x7f, 0xea, 0xed, 0xee, 0xd7, 0x3f, 0x0a}; */
/* ckdf_expand(okm5, prk4, info5, sizeof(info5), okm5_len); */
/* printf("%s\n", !memcmp(okm5, case5, okm5_len) ? "." : "F"); */
return 0;
}