-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpa.cpp
More file actions
239 lines (209 loc) · 9.66 KB
/
Copy pathcpa.cpp
File metadata and controls
239 lines (209 loc) · 9.66 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* ===================================================================== */
/* This file is part of Daredevil */
/* Daredevil is a side-channel analysis tool */
/* Copyright (C) 2016 */
/* Original author: Paul Bottinelli <paulbottinelli@hotmail.com> */
/* Contributors: Joppe Bos <joppe_bos@hotmail.com> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* ===================================================================== */
/* This file is used in every correlation power attack. It has functions
* like split_work or precomp_traces that are used in focpa and socpa.
* Also all the used structures in focpa and socpa can be found in cpa.h.
*/
#include "cpa.h"
#include "omp.h"
/* Given the messages stored in m, use the bytenum-th byte to construct
* the guesses for round R for algorithm alg and store the guesses in guess.
* des_switch is only used by DES
*/
template <class TypeGuess>
int construct_guess (TypeGuess ***guess, uint32_t alg, Matrix *m, uint32_t n_m, uint32_t bytenum, uint32_t R, uint32_t des_switch, uint16_t * sbox, uint32_t n_keys, int8_t bit) {
int ret;
switch (alg) {
case ALG_AES:
ret = construct_guess_AES (guess, m, n_m, bytenum, R, sbox, n_keys, bit); //construct_guess_AES can be found in aes.cpp
if (ret < 0) return -1;
break;
case ALG_DES:
ret = construct_guess_DES (guess, m, n_m, bytenum, R, des_switch, sbox, n_keys, bit); //construct_guess_DES can be found in des.cpp
if (ret < 0) return -1;
break;
default:
fprintf (stderr, "Algorithm is not supported (yet).\n");
return -1;
}
return 1;
}
/* This functions simply splits the total work (n_rows) into an equal number of
* threads, creates this amount of threads and starts them to precompute the
* distance of means for each row of the matrix trace. If the offset value is
* specified, we start splitting the work starting at offset.
*
* ! We expect a matrix where the number of traces is n_rows
*/
template <class TypeTrace, class TypeReturn>
int p_precomp_traces(TypeTrace ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset/*, int n_traces_from_offset*/)
{
int n, rc,
workload = 0,
n_traces = n_columns;
//printf("Offset: %i\n", offset);
/* If the total work by thread is smaller than 1, only the last thread would
* work, which is against the sole principle of multithreading. Thus, we
* reduce the number of threads until the workload is larger than 1.
*/
workload = ((n_rows-offset)/n_threads);
while (workload < 1) {
n_threads -= 1;
workload = ((n_rows-offset)/n_threads);
}
pthread_t threads[n_threads];
PrecompTracesNorm<TypeTrace> *ta = NULL;
ta = (PrecompTracesNorm<TypeTrace>*) malloc(n_threads * sizeof(PrecompTracesNorm<TypeTrace>));
if (ta == NULL) {
fprintf (stderr, "[ERROR] Memory alloc failed.\n");
return -1;
}
for (n = 0; n < n_threads; n++) {
//printf(" Thread_%i [%i-%i]\n",n , offset+ n*workload, offset+n*workload + workload + ((n + 1) / n_threads)*(n_rows % n_threads));
ta[n] = PrecompTracesNorm<TypeTrace>(offset + n*workload, workload + ((n + 1) / n_threads) * (n_rows % n_threads), n_traces, exponent, trace);
rc = pthread_create(&threads[n], NULL, precomp_traces_v_2<TypeTrace, TypeReturn>, (void *) &ta[n]);
if (rc != 0) {
fprintf(stderr, "[ERROR] Creating thread.\n");
free (ta);
return -1;
}
}
for (n = 0; n < n_threads; n++) {
rc = pthread_join(threads[n], NULL);
if (rc != 0) {
fprintf(stderr, "[ERROR] Joining thread.\n");
free (ta);
return -1;
}
}
free (ta);
return 0;
}
/* This function precomputes the mean for the traces and subtract this mean
* from every element of the traces. This is to be used by the newer v_5 of
* SOCPA.
*/
template <class TypeTrace, class TypeReturn>
void * precomp_traces_v_2(void * args_in)
{
int i, j;
TypeReturn mean = 0.0;
PrecompTracesNorm<TypeTrace> * G = (PrecompTracesNorm<TypeTrace> *) args_in;
for (i = G->start; i < G->start + G->end; i++) {
for (j = 0; j < G->length; j++) {
mean += G->trace[i][j];
}
mean /= G->length;
for (j = 0; j < G->length; j++) {
G->trace[i][j] -= mean;
G->trace[i][j] = pow(G->trace[i][j],G->exponent);
}
}
return NULL;
}
/* This functions simply splits the total work (n_rows) into an equal number of
* threads, creates this amount of threads and starts them to precompute the
* distance of means for each row of the matrix trace. If the offset value is
* specified, we start splitting the work starting at offset.
*
* ! We expect a matrix where the number of traces is n_rows
*/
template <class TypeTrace, class TypeReturn>
int p_precomp_traces_norm(TypeTrace ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset/*, int n_traces_from_offset*/)
{
int n, rc,
workload = 0,
n_traces = n_columns;
//printf("Offset: %i\n", offset);
/* If the total work by thread is smaller than 1, only the last thread would
* work, which is against the sole principle of multithreading. Thus, we
* reduce the number of threads until the workload is larger than 1.
*/
workload = ((n_rows-offset)/n_threads);
while (workload < 1) {
n_threads -= 1;
workload = ((n_rows-offset)/n_threads);
}
pthread_t threads[n_threads];
PrecompTracesNorm<TypeTrace> *ta = NULL;
ta = (PrecompTracesNorm<TypeTrace>*) malloc(n_threads * sizeof(PrecompTracesNorm<TypeTrace>));
if (ta == NULL) {
fprintf (stderr, "[ERROR] Memory alloc failed.\n");
return -1;
}
for (n = 0; n < n_threads; n++) {
//printf(" Thread_%i [%i-%i]\n",n , offset+ n*workload, offset+n*workload + workload + ((n + 1) / n_threads)*(n_rows % n_threads));
ta[n] = PrecompTracesNorm<TypeTrace>(offset + n*workload, workload + ((n + 1) / n_threads) * (n_rows % n_threads), n_traces, exponent, trace);
rc = pthread_create(&threads[n], NULL, precomp_traces_v_2_norm<TypeTrace, TypeReturn>, (void *) &ta[n]);
if (rc != 0) {
fprintf(stderr, "[ERROR] Creating thread.\n");
free (ta);
return -1;
}
}
for (n = 0; n < n_threads; n++) {
rc = pthread_join(threads[n], NULL);
if (rc != 0) {
fprintf(stderr, "[ERROR] Joining thread.\n");
free (ta);
return -1;
}
}
free (ta);
return 0;
}
/* This function precomputes the mean for the traces and subtract this mean
* from every element of the traces. This is to be used by the newer v_5 of
* SOCPA.
*/
template <class TypeTrace, class TypeReturn>
void * precomp_traces_v_2_norm(void * args_in)
{
int i, j;
TypeReturn mean = 0.0;
TypeReturn std = 0.0;
PrecompTracesNorm<TypeTrace> * G = (PrecompTracesNorm<TypeTrace> *) args_in;
for (i = G->start; i < G->start + G->end; i++) {
for (j = 0; j < G->length; j++) {
mean += G->trace[i][j];
std += pow(G->trace[i][j],2);
}
std = sqrt(G->length*std - mean*mean)/G->length;
mean /= G->length;
for (j = 0; j < G->length; j++) {
G->trace[i][j] -= mean;
G->trace[i][j] /= std;
G->trace[i][j] = pow(G->trace[i][j],G->exponent);
}
}
return NULL;
}
template int construct_guess (uint8_t ***guess, uint32_t alg, Matrix *m, uint32_t n_m, uint32_t bytenum, uint32_t R, uint32_t des_switch, uint16_t * sbox, uint32_t n_keys, int8_t bit);
template int p_precomp_traces<int8_t, double>(int8_t ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces<double, double>(double ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces<float, float>(float ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces<float, double>(float ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces<signed char, float>(signed char ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces_norm<int8_t, double>(int8_t ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces_norm<double, double>(double ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces_norm<float, float>(float ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces_norm<float, double>(float ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);
template int p_precomp_traces_norm<signed char, float>(signed char ** trace, int n_rows, int n_columns, int n_threads, uint8_t exponent, int offset);