This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_main.c
137 lines (96 loc) · 3.34 KB
/
test_main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// test_main.c
// 2020-01-23 Markku-Juhani O. Saarinen <mjos@pqshield.com>
// Copyright (c) 2020, PQShield Ltd. All rights reserved.
// Minimal unit tests for AES-128/192/256 (FIPS 197) and SM4 (GM/T 0002-2012).
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "aes_wrap.h"
#include "saes32.h"
#include "aes_saes32.h"
#include "aes_saes64.h"
#include "aes_otf_saes64.h"
#include "gcm_wrap.h"
#include "gcm_gfmul.h"
// unit tests
int test_aes(); // aes_test.c
int test_sm4(); // sm4_test.c
int test_gcm(); // gcm_test.c
// generate "reference" hw testbench data for the instruction
// output should match with hdl/saes32_tb.v
int test_hwtb()
{
uint32_t rd, rs1, rs2, fn;
rs1 = 0x00000000;
rs2 = 0x00000000;
for (fn = 0; fn < 24; fn++) {
rd = saes32(rs1, rs2, fn);
printf("[TB] rd=%08x rs1=%08x rs2=%08x fn=%02x\n", rd, rs1, rs2, fn);
rs2 += 0x01234567;
}
return 0;
}
// stub main: run unit tests
int main(int argc, char **argv)
{
int fail = 0;
// generate hardware testbench data ?
if (argc > 1 && strcmp(argv[1], "tb") == 0) {
return test_hwtb();
}
// algorithm tests
printf("[INFO] === AES using SAES32 ===\n");
aes128_enc_key = aes128_enc_key_saes32; // set encryption key
aes192_enc_key = aes192_enc_key_saes32;
aes256_enc_key = aes256_enc_key_saes32;
aes128_enc_ecb = aes128_enc_ecb_saes32; // encrypt a block
aes192_enc_ecb = aes192_enc_ecb_saes32;
aes256_enc_ecb = aes256_enc_ecb_saes32;
aes128_dec_key = aes128_dec_key_saes32; // set decryption key
aes192_dec_key = aes192_dec_key_saes32;
aes256_dec_key = aes256_dec_key_saes32;
aes128_dec_ecb = aes128_dec_ecb_saes32; // decrypt a block
aes192_dec_ecb = aes192_dec_ecb_saes32;
aes256_dec_ecb = aes256_dec_ecb_saes32;
fail += test_aes(); // run tests with UUT = SAES32
printf("[INFO] === AES using SAES64 / On-the-fly keying ===\n");
aes128_enc_ecb = aes128_enc_otf_saes64;
aes192_enc_ecb = aes192_enc_otf_saes64;
aes256_enc_ecb = aes256_enc_otf_saes64;
fail += test_aes(); // run tests with UUT = OTF/64
printf("[INFO] === AES using SAES64 ===\n");
aes128_enc_key = aes128_enc_key_saes64; // set encryption key
aes192_enc_key = aes192_enc_key_saes64;
aes256_enc_key = aes256_enc_key_saes64;
aes128_enc_ecb = aes128_enc_ecb_saes64; // encrypt a block
aes192_enc_ecb = aes192_enc_ecb_saes64;
aes256_enc_ecb = aes256_enc_ecb_saes64;
aes128_dec_key = aes128_dec_key_saes64; // set decryption key
aes192_dec_key = aes192_dec_key_saes64;
aes256_dec_key = aes256_dec_key_saes64;
aes128_dec_ecb = aes128_dec_ecb_saes64; // decrypt a block
aes192_dec_ecb = aes192_dec_ecb_saes64;
aes256_dec_ecb = aes256_dec_ecb_saes64;
fail += test_aes(); // run tests with UUT = SAES64
printf("[INFO] === GCM using rv64_ghash_mul() ===\n");
ghash_rev = rv64_ghash_rev;
ghash_mul = rv64_ghash_mul;
fail += test_gcm();
printf("[INFO] === GCM using rv32_ghash_mul() ===\n");
ghash_rev = rv32_ghash_rev;
ghash_mul = rv32_ghash_mul;
fail += test_gcm();
printf("[INFO] === GCM using rv32_ghash_mul_kar() ===\n");
ghash_rev = rv32_ghash_rev;
ghash_mul = rv32_ghash_mul_kar;
fail += test_gcm();
printf("[INFO] === SM4 test ===\n");
fail += test_sm4();
if (fail == 0) {
printf("[PASS] all tests passed.\n");
} else {
printf("[FAIL] %d test(s) failed.\n", fail);
}
return fail;
}