-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.c
98 lines (80 loc) · 2.19 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#define BENCH_BUFSIZE 1024 * 1024 * 5
#define BENCH_ITER 5
#include "kuznyechik.h"
/*
* Test vectors are from the reference document.
*/
ALIGN(16) static const unsigned char key[32] = {
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
};
ALIGN(16) static const unsigned char plaintext[16] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88
};
ALIGN(16) static const unsigned char ciphertext[16] = {
0x7f, 0x67, 0x9d, 0x90, 0xbe, 0xbc, 0x24, 0x30,
0x5a, 0x46, 0x8d, 0x42, 0xb9, 0xd4, 0xed, 0xcd
};
static double do_benchmark()
{
ALIGN(16) struct kuznyechik_subkeys subkeys;
ALIGN(16) unsigned char buffer[BENCH_BUFSIZE];
unsigned char *ptr, *eptr;
clock_t stime;
ptr = buffer;
eptr = buffer + sizeof(buffer);
/* Randomize the buffer */
while (ptr < eptr)
*ptr++ = rand();
ptr = buffer;
kuznyechik_set_key(&subkeys, key);
/* Encrypt whole buffer */
stime = clock();
while (ptr < eptr) {
kuznyechik_encrypt(&subkeys, ptr, ptr);
ptr += 16;
}
return (double) (clock() - stime) / CLOCKS_PER_SEC;
}
static void print_block(const unsigned char *blk, const char *prefix)
{
unsigned int i;
printf("%s ", prefix);
for (i = 0; i < 16; i++)
printf("%02x", blk[i]);
putchar('\n');
}
int main(int argc, const char **argv)
{
ALIGN(16) unsigned char buffer[16];
ALIGN(16) struct kuznyechik_subkeys subkeys;
double elapsed, mbs;
unsigned int i;
int retval = 0;
srand(time(NULL));
/* Print test vectors */
kuznyechik_set_key(&subkeys, key);
print_block(plaintext, "P:");
kuznyechik_encrypt(&subkeys, buffer, plaintext);
print_block(buffer, "C:");
kuznyechik_decrypt(&subkeys, buffer, buffer);
print_block(buffer, "P:");
/* Do benchmark */
putchar('\n');
for (i = 0; i < BENCH_ITER; i++) {
elapsed = do_benchmark();
mbs = (double) (BENCH_BUFSIZE) / (double) elapsed / 1024 / 1024;
fprintf(stdout, "[%d/%d] Encryption speed: %f MB/s\n",
i + 1, BENCH_ITER, mbs);
}
putchar('\n');
return retval;
}