-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes-ctr-sample.c
173 lines (142 loc) · 3.4 KB
/
aes-ctr-sample.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <linux/socket.h>
#include "common.h"
int aes_ctr_afalg(char *input_buf, char *key_buf, char *iv_buf, char *alafg_buf)
{
int i;
int opfd;
int tfmfd;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "skcipher",
.salg_name = "ctr(aes)"
};
struct msghdr msg = {};
struct cmsghdr *cmsg;
char cbuf[CMSG_SPACE(4) + CMSG_SPACE(20)] = {0};
struct af_alg_iv *iv;
struct iovec iov;
tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));
setsockopt(tfmfd, SOL_ALG, ALG_SET_KEY, key_buf, 16);
opfd = accept(tfmfd, NULL, 0);
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_OP;
cmsg->cmsg_len = CMSG_LEN(4);
*(__u32 *)CMSG_DATA(cmsg) = ALG_OP_ENCRYPT;
cmsg = CMSG_NXTHDR(&msg, cmsg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_IV;
cmsg->cmsg_len = CMSG_LEN(20);
iv = (void *)CMSG_DATA(cmsg);
iv->ivlen = 16;
memcpy(iv->iv, iv_buf, 16);
iov.iov_base = input_buf;
iov.iov_len = 64;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
sendmsg(opfd, &msg, 0);
read(opfd, alafg_buf, 64);
#ifdef PRINT
printf("output:\n");
for (i = 0; i < 64; i++) {
printf("%02x ", (unsigned char)alafg_buf[i]);
}
printf("\n");
#endif
close(opfd);
close(tfmfd);
return 0;
}
int aes_ctr_afalg_de(char *input_buf, char *key_buf, char *iv_buf, char *alafg_buf)
{
int i;
int opfd;
int tfmfd;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "skcipher",
.salg_name = "ctr(aes)"
};
struct msghdr msg = {};
struct cmsghdr *cmsg;
char cbuf[CMSG_SPACE(4) + CMSG_SPACE(20)] = {0};
struct af_alg_iv *iv;
struct iovec iov;
tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));
setsockopt(tfmfd, SOL_ALG, ALG_SET_KEY, key_buf, 16);
opfd = accept(tfmfd, NULL, 0);
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_OP;
cmsg->cmsg_len = CMSG_LEN(4);
*(__u32 *)CMSG_DATA(cmsg) = ALG_OP_DECRYPT;
cmsg = CMSG_NXTHDR(&msg, cmsg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_IV;
cmsg->cmsg_len = CMSG_LEN(20);
iv = (void *)CMSG_DATA(cmsg);
iv->ivlen = 16;
memcpy(iv->iv, iv_buf, 16);
iov.iov_base = input_buf;
iov.iov_len = 64;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
sendmsg(opfd, &msg, 0);
read(opfd, alafg_buf, 64);
#ifdef PRINT
printf("output:\n");
for (i = 0; i < 64; i++) {
printf("%02x ", (unsigned char)alafg_buf[i]);
}
printf("\n");
#endif
close(opfd);
close(tfmfd);
return 0;
}
int aes_ctr_test()
{
int ret;
char alafg_buf[64];
char de_buf[64];
char key_buf[16];
char iv_buf[16];
char input_buf[64];
for (int i = 0; i < 100; ++i)
{
srand((unsigned int)time(0));
printf("TEST %d\n", i+1);
printf("key:\n");
rand_array(key_buf, 16);
printf("iv:\n");
rand_array(iv_buf, 16);
printf("input:\n");
rand_array(input_buf, 64);
aes_ctr_afalg(input_buf, key_buf, iv_buf, alafg_buf);
/* aes_ctr_afalg_de(alafg_buf, key_buf, iv_buf, de_buf);
ret = array_equal(input_buf, de_buf, 64);
if (ret < 0)
{
printf("AES-CTR ERROR: AFALG AND OPENSSL RESULTS DIFFER\n");
return -1;
}
*/ }
return 0;
}
int main()
{
aes_ctr_test();
}