-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecb.c
134 lines (112 loc) · 4.02 KB
/
ecb.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdint.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define _GNU_SOURCE
#define __USE_GNU
#include <sched.h>
#include <arm_neon.h>
#define REPEAT 5000000
#define WARMUP REPEAT/4
uint64_t start_clk, end_clk;
double total_clk;
__inline uint64_t get_Clks(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1e6 + tv.tv_usec); // usec
}
#define MEASURE(x) for (int i=0; i< WARMUP; i++){x;} \
start_clk=get_Clks(); \
for (int i = 0; i < REPEAT; i++) \
{ \
{x;} \
} \
end_clk=get_Clks(); \
total_clk=(double)(end_clk-start_clk)*2.1e3/REPEAT;
#define TIME_IT(name, func, nbytes, MULTIPLE) \
printf("%s-%d: ", name, nbytes); \
MEASURE(func); \
printf("%g cpb\n", total_clk/(nbytes)/(MULTIPLE));
void printblock(const unsigned char* x)
{
for (int i = 0; i < 16; i++) {
if(i%4 == 0 && i != 0){
putchar(' ');
}
printf("%02x", x[i]);
}
putchar('\n');
}
uint8x16_t aes_enc_rnd(uint8x16_t block, uint8x16_t rndKey){
return vaesmcq_u8(vaeseq_u8(block, rndKey));
}
uint8x16_t aes_enc(uint8x16_t block, uint8x16_t *expkey)
{
block = aes_enc_rnd(block, expkey[0]);
block = aes_enc_rnd(block, expkey[1]);
block = aes_enc_rnd(block, expkey[2]);
block = aes_enc_rnd(block, expkey[3]);
block = aes_enc_rnd(block, expkey[4]);
block = aes_enc_rnd(block, expkey[5]);
block = aes_enc_rnd(block, expkey[6]);
block = aes_enc_rnd(block, expkey[7]);
block = aes_enc_rnd(block, expkey[8]);
block = vaeseq_u8(block, expkey[9]);
return veorq_u8(block, expkey[10]);
}
void aes_ofb(const unsigned char* in, unsigned char *out,
int length, const char *expkey, const char* iv)
{
uint8x16_t block, cipher;
block = vld1q_u8((int8_t *)iv);
for (int i = 0; i < length; i += 16){
block = aes_enc(block, (uint8x16_t *)expkey);
cipher = veorq_u8(vld1q_u8(&((int8_t *)in)[i]), block);
vst1q_u8(&((int8_t*)out)[i], cipher);
}
}
int main(void){
cpu_set_t cpuset;
CPU_ZERO(&cpuset); CPU_SET(7, &cpuset);
if (sched_setaffinity(getpid(), sizeof cpuset, &cpuset) != 0) perror("setaffinity");
int nblocks = 4;
int nbytes = nblocks * 16;
int nbits = nbytes * 8;
// NIST test key
unsigned char key[16] = {
0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
};
// corresponding expanded key
unsigned char expkey[11*16] = {
0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c,
0xa0,0xfa,0xfe,0x17,0x88,0x54,0x2c,0xb1,0x23,0xa3,0x39,0x39,0x2a,0x6c,0x76,0x05,
0xf2,0xc2,0x95,0xf2,0x7a,0x96,0xb9,0x43,0x59,0x35,0x80,0x7a,0x73,0x59,0xf6,0x7f,
0x3d,0x80,0x47,0x7d,0x47,0x16,0xfe,0x3e,0x1e,0x23,0x7e,0x44,0x6d,0x7a,0x88,0x3b,
0xef,0x44,0xa5,0x41,0xa8,0x52,0x5b,0x7f,0xb6,0x71,0x25,0x3b,0xdb,0x0b,0xad,0x00,
0xd4,0xd1,0xc6,0xf8,0x7c,0x83,0x9d,0x87,0xca,0xf2,0xb8,0xbc,0x11,0xf9,0x15,0xbc,
0x6d,0x88,0xa3,0x7a,0x11,0x0b,0x3e,0xfd,0xdb,0xf9,0x86,0x41,0xca,0x00,0x93,0xfd,
0x4e,0x54,0xf7,0x0e,0x5f,0x5f,0xc9,0xf3,0x84,0xa6,0x4f,0xb2,0x4e,0xa6,0xdc,0x4f,
0xea,0xd2,0x73,0x21,0xb5,0x8d,0xba,0xd2,0x31,0x2b,0xf5,0x60,0x7f,0x8d,0x29,0x2f,
0xac,0x77,0x66,0xf3,0x19,0xfa,0xdc,0x21,0x28,0xd1,0x29,0x41,0x57,0x5c,0x00,0x6e,
0xd0,0x14,0xf9,0xa8,0xc9,0xee,0x25,0x89,0xe1,0x3f,0x0c,0xc8,0xb6,0x63,0x0c,0xa6
};
unsigned char message[16*512] = { 0 };
unsigned char cipher[16*512] = { 0 };
srand(time(NULL));
for (int i = 0; i < 16*512; i++) {
message[i] = rand();
}
unsigned char iv[128] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
};
aes_ofb(message, cipher, 16*512, expkey, iv);
puts("timing");
TIME_IT("AES-OFB", aes_ofb(cipher, cipher, 16*512, expkey, iv), 16*512, 1);
return cipher[0] ^ cipher[(16*512)-1];
}