| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#include "SP800-185.h" |
| 8 |
|
| 9 |
typedef int (*cSHAKE)(const BitSequence *input, BitLength inputBitLen, BitSequence *output, BitLength outputBitLen, const BitSequence *name, BitLength nameBitLen, const BitSequence *customization, BitLength customBitLen ); |
| 10 |
|
| 11 |
uint8_t* datahex(const char* string) { |
| 12 |
|
| 13 |
if(string == NULL) |
| 14 |
return NULL; |
| 15 |
|
| 16 |
size_t slength = strlen(string); |
| 17 |
if((slength % 2) != 0) |
| 18 |
return NULL; |
| 19 |
|
| 20 |
size_t dlength = slength / 2; |
| 21 |
|
| 22 |
uint8_t* data = malloc(dlength); |
| 23 |
memset(data, 0, dlength); |
| 24 |
|
| 25 |
size_t index = 0; |
| 26 |
while (index < slength) { |
| 27 |
char c = string[index]; |
| 28 |
int value = 0; |
| 29 |
if(c >= '0' && c <= '9') |
| 30 |
value = (c - '0'); |
| 31 |
else if (c >= 'A' && c <= 'F') |
| 32 |
value = (10 + (c - 'A')); |
| 33 |
else if (c >= 'a' && c <= 'f') |
| 34 |
value = (10 + (c - 'a')); |
| 35 |
else { |
| 36 |
free(data); |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
|
| 40 |
data[(index/2)] += value << (((index + 1) % 2) * 4); |
| 41 |
|
| 42 |
index++; |
| 43 |
} |
| 44 |
|
| 45 |
return data; |
| 46 |
} |
| 47 |
|
| 48 |
void printhex(const char *prefix, const unsigned char *b, size_t len) |
| 49 |
{ |
| 50 |
if (len == 0) { |
| 51 |
printf("%s00\n", prefix); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
printf("%s", prefix); |
| 56 |
for (; len>0; len--, b++) { |
| 57 |
printf("%02X", *b); |
| 58 |
} |
| 59 |
printf("\n"); |
| 60 |
} |
| 61 |
|
| 62 |
static unsigned index_test = 1; |
| 63 |
|
| 64 |
void make_test_vector(const char* message_hex, const char *custom_hex, const char *function_hex, unsigned md_len, cSHAKE func) |
| 65 |
{ |
| 66 |
unsigned char *message_b; |
| 67 |
unsigned char *custom_b; |
| 68 |
unsigned char *function_b; |
| 69 |
unsigned char *md; |
| 70 |
|
| 71 |
unsigned message_len; |
| 72 |
unsigned custom_len; |
| 73 |
unsigned function_len; |
| 74 |
|
| 75 |
message_b = datahex(message_hex); |
| 76 |
message_len = message_hex ? strlen(message_hex) / 2 : 0; |
| 77 |
|
| 78 |
custom_b = datahex(custom_hex); |
| 79 |
custom_len = custom_hex ? strlen(custom_hex) / 2 : 0; |
| 80 |
|
| 81 |
function_b = datahex(function_hex); |
| 82 |
function_len = function_hex ? strlen(function_hex) / 2 : 0; |
| 83 |
|
| 84 |
md = malloc(md_len); |
| 85 |
|
| 86 |
printf("\n# Sample %d\n", index_test++); |
| 87 |
printf("NLen = %d\n", function_len*8); |
| 88 |
printf("SLen = %d\n", custom_len*8); |
| 89 |
printf("Len = %d\n", message_len*8); |
| 90 |
printhex("N = ", function_b, function_len); |
| 91 |
printhex("S = ", custom_b, custom_len); |
| 92 |
printhex("Msg = ", message_b, message_len); |
| 93 |
|
| 94 |
func(message_b, message_len*8, md, md_len*8, function_b, function_len*8, custom_b, custom_len*8); |
| 95 |
|
| 96 |
printhex("MD = ", md, md_len); |
| 97 |
printf("\n"); |
| 98 |
|
| 99 |
free(message_b); |
| 100 |
free(custom_b); |
| 101 |
free(function_b); |
| 102 |
free(md); |
| 103 |
} |
| 104 |
|
| 105 |
void make_tv(unsigned msg_len, unsigned custom_len, unsigned md_len, cSHAKE func) |
| 106 |
{ |
| 107 |
char *message; |
| 108 |
char *custom; |
| 109 |
|
| 110 |
message = malloc(msg_len*2 + 1); |
| 111 |
custom = malloc(custom_len*2 + 1); |
| 112 |
|
| 113 |
for (unsigned i=0; i<msg_len; i++) |
| 114 |
sprintf(message + i*2, "%02X", i & 0xFF); |
| 115 |
message[msg_len*2] = 0; |
| 116 |
|
| 117 |
for (unsigned i=0; i<custom_len; i++) |
| 118 |
sprintf(custom + i*2, "%02X", i & 0xFF); |
| 119 |
custom[custom_len*2] = 0; |
| 120 |
|
| 121 |
make_test_vector(message, custom, "", md_len, func); |
| 122 |
|
| 123 |
free(message); |
| 124 |
free(custom); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
int main(int argc, char *argv[]) |
| 129 |
{ |
| 130 |
cSHAKE func; |
| 131 |
|
| 132 |
if (argc != 2) { |
| 133 |
fprintf(stderr, "%s [128|256]\n", argv[0]); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
if (0 == strcmp(argv[1], "128")) |
| 138 |
func = cSHAKE128; |
| 139 |
else { |
| 140 |
if (0 == strcmp(argv[1], "256")) |
| 141 |
func = cSHAKE256; |
| 142 |
else { |
| 143 |
fprintf(stderr, "%s [128|256]\n", argv[0]); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
make_tv(0, 0, 32, func); |
| 149 |
make_tv(0, 16, 32, func); |
| 150 |
make_tv(16, 0, 32, func); |
| 151 |
|
| 152 |
make_tv(255, 15, 32, func); |
| 153 |
make_tv(1024, 15, 32, func); |
| 154 |
|
| 155 |
make_tv(256, 0, 32, func); |
| 156 |
make_tv(256, 255, 32, func); |
| 157 |
make_tv(256, 256, 32, func); |
| 158 |
make_tv(256, 257, 32, func); |
| 159 |
make_tv(256, 258, 32, func); |
| 160 |
make_tv(256, 500, 32, func); |
| 161 |
make_tv(256, 1024, 32, func); |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |