-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_testvector_256bitkey_128bitiv.c
More file actions
160 lines (118 loc) · 5.97 KB
/
Copy pathgenerate_testvector_256bitkey_128bitiv.c
File metadata and controls
160 lines (118 loc) · 5.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
This program generates test vectors for MORUS.
Note the message length is a multiple of bytes.
The authentication tag is appended to the ciphertext.
*/
#include <stdio.h>
#include "crypto_aead.h"
void print_testvectors(unsigned char* plaintext, unsigned long long msglen,
unsigned char* ad, unsigned long long adlen,
unsigned char* key, unsigned char* iv)
{
unsigned long i, success;
unsigned char ciphertext[4096 + 128]; // for generating the test vector, we assume that the input plaintext is less than 4096 bytes.
unsigned long long clen; // the length of ciphertext; for CAESAR competition, clen = msglen+adlen;
printf("\n=============================================\n=============================================\nThe test vectors: \n");
printf("\nLength of plaintext: %llu bytes;\nLength of associated data: %llu bytes; \n", msglen, adlen);
printf("\nThe key is: "); for (i = 0; i < 32; i++) printf("%x%x",key[i]>>4, key[i] & 15);
printf("\nThe iv is: "); for (i = 0; i < 16; i++) printf("%x%x",iv[i]>>4, iv[i] & 15);
printf("\nThe plaintext is: "); for (i = 0; i < msglen; i++) printf("%x%x",plaintext[i]>>4, plaintext[i] & 15);
printf("\nThe associated data is "); for (i = 0; i < adlen; i++) printf("%x%x",ad[i]>>4, ad[i] & 15);
printf("\n\nNow perform encryption ....\n");
crypto_aead_encrypt(ciphertext, &clen, plaintext, msglen, ad, adlen, 0, iv, key);
printf("\nThe ciphertext is: "); for (i = 0; i < msglen; i++) printf("%x%x",ciphertext[i]>>4, ciphertext[i] & 15);
printf("\nThe tag is: "); for (i = 0; i < 16; i++) printf("%x%x",ciphertext[i+msglen]>>4, ciphertext[i+msglen] & 15);
printf("\n\nNow perform decryption ....\n");
for (i = 0; i < msglen; i++) plaintext[i] = 0;
success = crypto_aead_decrypt(plaintext, &msglen, 0, ciphertext, clen, ad, adlen, iv, key);
if (success == 0) printf("\nThe verification is successful in decryption");
else printf("\nThe verification failed in decryption");
//printf("\nThe plaintext is: "); for (i = 0; i < msglen; i++) printf("%x%x",plaintext[i]>>4, plaintext[i] & 15);
//printf("\nThe ciphertext is: "); for (i = 0; i < clen-16; i++) printf("%x%x",ciphertext[i]>>4, ciphertext[i] & 15);
printf("\n\n\n");
}
int main()
{
unsigned char msg[4096]; // For generating test vector, we assume that the length of the input plaintext is less than 4096 bytes
unsigned char ad[4096]; // For generating test vector, we assume that the length of the input associated data is less than 4096 bytes
unsigned char key[32];
unsigned char iv[16];
unsigned long long msglen, adlen, clen; // msg, adlen, clen in bytes.
unsigned char maclen = 16;
unsigned long i;
//===============================================================
msglen = 0;
adlen = 0;
for (i = 0; i < 32; i++) key[i] = 0;
for (i = 0; i < 16; i++) iv[i] = 0;
for (i = 0; i < adlen; i++) ad[i] = 0;
for (i = 0; i < msglen; i++) msg[i] = 0;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 1;
adlen = 0;
for (i = 0; i < 32; i++) key[i] = 0;
for (i = 0; i < 16; i++) iv[i] = 0;
for (i = 0; i < adlen; i++) ad[i] = 0;
for (i = 0; i < msglen; i++) msg[i] = 1;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 0;
adlen = 1;
for (i = 0; i < 32; i++) key[i] = 0;
for (i = 0; i < 16; i++) iv[i] = 0;
for (i = 0; i < adlen; i++) ad[i] = 1;
for (i = 0; i < msglen; i++) msg[i] = 0;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 1;
adlen = 1;
for (i = 0; i < 32; i++) key[i] = 0;
for (i = 0; i < 16; i++) iv[i] = 0;
key[0] = 1;
for (i = 0; i < adlen; i++) ad[i] = 0;
for (i = 0; i < msglen; i++) msg[i] = 0;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 1;
adlen = 1;
for (i = 0; i < 32; i++) key[i] = 0;
for (i = 0; i < 16; i++) iv[i] = 0;
iv[0] = 1;
for (i = 0; i < adlen; i++) ad[i] = 0;
for (i = 0; i < msglen; i++) msg[i] = 0;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 16;
adlen = 16;
for (i = 0; i < 32; i++) key[i] = 1;
for (i = 0; i < 16; i++) iv[i] = 1;
for (i = 0; i < adlen; i++) ad[i] = 1;
for (i = 0; i < msglen; i++) msg[i] = 1;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 16;
adlen = 16;
for (i = 0; i < 32; i++) key[i] = i;
for (i = 0; i < 16; i++) iv[i] = 3*i;
for (i = 0; i < adlen; i++) ad[i] = 1;
for (i = 0; i < msglen; i++) msg[i] = 1;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 64+9;
adlen = 32+7;
for (i = 0; i < 32; i++) key[i] = i;
for (i = 0; i < 16; i++) iv[i] = i*3;
for (i = 0; i < adlen; i++) ad[i] = (i*5)%256;
for (i = 0; i < msglen; i++) msg[i] = (i*7)%256;
print_testvectors(msg, msglen, ad, adlen, key, iv);
//===============================================================
msglen = 1024+7;
adlen = 512+2;
for (i = 0; i < 32; i++) key[i] = i;
for (i = 0; i < 16; i++) iv[i] = i*3;
for (i = 0; i < adlen; i++) ad[i] = (i*5)%256;
for (i = 0; i < msglen; i++) msg[i] = (i*7)%256;
print_testvectors(msg, msglen, ad, adlen, key, iv);
return 0;
}