gem5  v20.1.0.0
qarma.cc
Go to the documentation of this file.
1 // -*- mode:c++ -*-
2 
3 // Copyright (c) 2020 Metempsy Technology Consulting
4 // All rights reserved
5 //
6 // The license below extends only to copyright in the software and shall
7 // not be construed as granting a license to any other intellectual
8 // property including but not limited to intellectual property relating
9 // to a hardware implementation of the functionality of the software
10 // licensed hereunder. You may use the software subject to the license
11 // terms below provided that you ensure that this notice is replicated
12 // unmodified and in its entirety in all distributions of the software,
13 // modified or unmodified, in source code or in binary form.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions are
17 // met: redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer;
19 // redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution;
22 // neither the name of the copyright holders nor the names of its
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 #include "arch/arm/qarma.hh"
39 
40 #include <array>
41 
42 #include "base/bitfield.hh"
43 
44 using namespace QARMA;
45 using namespace std;
46 
47 
48 uint8_t
49 QARMA::rotCell(uint8_t incell, int amount)
50 {
51  uint8_t res = ((incell << amount) | (incell >> (4-amount)))& 0xF;
52  return res;
53 }
54 
55 uint8_t
56 QARMA::tweakCellInvRot(uint8_t incell)
57 {
58  uint8_t outcell = 0x0;
59  outcell = incell << 1;
60  uint8_t t = 0x1 & (incell ^ (incell>>3));
61  outcell |= t;
62  return outcell & 0xF;
63 }
64 
65 uint8_t
66 QARMA::tweakCellRot(uint8_t incell)
67 {
68  uint8_t outcell = 0x0;
69  outcell = incell >> 1;
70  uint8_t t = 0x1 & (incell ^ (incell>>1));
71  outcell |= t<<3;
72  return outcell & 0xF;
73 }
74 
75 BIT64
77 {
78  BIT64 outdata = 0x0;
79  outdata.b0 = tweakCellInvRot(indata.b12);
80  outdata.b1 = indata.b13;
81  outdata.b2 = indata.b5;
82  outdata.b3 = indata.b6;
83  outdata.b4 = indata.b0;
84  outdata.b5 = indata.b1;
85  outdata.b6 = tweakCellInvRot(indata.b2);
86  outdata.b7 = indata.b3;
87  outdata.b8 = tweakCellInvRot(indata.b7);
88  outdata.b9 = tweakCellInvRot(indata.b15);
89  outdata.b10 = tweakCellInvRot(indata.b14);
90  outdata.b11 = tweakCellInvRot(indata.b4);
91  outdata.b12 = indata.b8;
92  outdata.b13 = indata.b9;
93  outdata.b14 = indata.b10;
94  outdata.b15 = tweakCellInvRot(indata.b11);
95  return outdata;
96 }
97 
98 BIT64
99 QARMA::tweakShuffle(BIT64 indata)
100 {
101  BIT64 outdata = 0x0;
102  outdata.b0 = indata.b4;
103  outdata.b1 = indata.b5;
104  outdata.b2 = tweakCellRot(indata.b6);
105  outdata.b3 = indata.b7;
106  outdata.b4 = tweakCellRot(indata.b11);
107  outdata.b5 = indata.b2;
108  outdata.b6 = indata.b3;
109  outdata.b7 = tweakCellRot(indata.b8);
110  outdata.b8 = indata.b12;
111  outdata.b9 = indata.b13;
112  outdata.b10 = indata.b14;
113  outdata.b11 = tweakCellRot(indata.b15);
114  outdata.b12 = tweakCellRot(indata.b0);
115  outdata.b13 = indata.b1;
116  outdata.b14 = tweakCellRot(indata.b10);
117  outdata.b15 = tweakCellRot(indata.b9);
118  return outdata;
119 }
120 
121 
122 BIT64
124 {
125  BIT64 outdata = 0x0;
126  outdata.b0 = indata.b3;
127  outdata.b1 = indata.b6;
128  outdata.b2 = indata.b12;
129  outdata.b3 = indata.b9;
130  outdata.b4 = indata.b14;
131  outdata.b5 = indata.b11;
132  outdata.b6 = indata.b1;
133  outdata.b7 = indata.b4;
134  outdata.b8 = indata.b8;
135  outdata.b9 = indata.b13;
136  outdata.b10 = indata.b7;
137  outdata.b11 = indata.b2;
138  outdata.b12 = indata.b5;
139  outdata.b13 = indata.b0;
140  outdata.b14 = indata.b10;
141  outdata.b15 = indata.b15;
142  return outdata;
143 }
144 
145 BIT64
147 {
148  BIT64 outdata = 0x0;
149  outdata.b0 = indata.b13;
150  outdata.b1 = indata.b6;
151  outdata.b2 = indata.b11;
152  outdata.b3 = indata.b0;
153  outdata.b4 = indata.b7;
154  outdata.b5 = indata.b12;
155  outdata.b6 = indata.b1;
156  outdata.b7 = indata.b10;
157  outdata.b8 = indata.b8;
158  outdata.b9 = indata.b3;
159  outdata.b10 = indata.b14;
160  outdata.b11 = indata.b5;
161  outdata.b12 = indata.b2;
162  outdata.b13 = indata.b9;
163  outdata.b14 = indata.b4;
164  outdata.b15 = indata.b15;
165  return outdata;
166 }
167 
168 
169 uint64_t
170 QARMA::PACInvSub(uint64_t tInput)
171 {
172  // This is a 4-bit substitution from the PRINCE-family cipher
173  uint64_t t_output = 0x0;
174  for (int i=15; i>=0; i--) {
175  t_output = t_output << 4;
176  uint8_t b = (tInput >> i*4 ) & 0xF;
177  switch ( b ) {
178  case 0x0:
179  t_output |= 0x5;
180  break;
181  case 0x1:
182  t_output |= 0xe;
183  break;
184  case 0x2:
185  t_output |= 0xd;
186  break;
187  case 0x3:
188  t_output |= 0x8;
189  break;
190  case 0x4:
191  t_output |= 0xa;
192  break;
193  case 0x5:
194  t_output |= 0xb;
195  break;
196  case 0x6:
197  t_output |= 0x1;
198  break;
199  case 0x7:
200  t_output |= 0x9;
201  break;
202  case 0x8:
203  t_output |= 0x2;
204  break;
205  case 0x9:
206  t_output |= 0x6;
207  break;
208  case 0xa:
209  t_output |= 0xf;
210  break;
211  case 0xb:
212  t_output |= 0x0;
213  break;
214  case 0xc:
215  t_output |= 0x4;
216  break;
217  case 0xd:
218  t_output |= 0xc;
219  break;
220  case 0xe:
221  t_output |= 0x7;
222  break;
223  case 0xf:
224  t_output |= 0x3;
225  break;
226  default:
227  //unreachable
228  break;
229  }
230  }
231  return t_output;
232 }
233 
234 uint64_t
235 QARMA::PACSub(uint64_t tInput){
236  // This is a 4-bit substitution from the PRINCE-family cipher
237  uint64_t t_output = 0x0;
238  for (int i=15; i>=0; i--) {
239  t_output = t_output << 4;
240  uint8_t b = (tInput >> i*4 ) & 0xF;
241  switch ( b ) {
242  case 0x0:
243  t_output |= 0xb;
244  break;
245  case 0x1:
246  t_output |= 0x6;
247  break;
248  case 0x2:
249  t_output |= 0x8;
250  break;
251  case 0x3:
252  t_output |= 0xf;
253  break;
254  case 0x4:
255  t_output |= 0xc;
256  break;
257  case 0x5:
258  t_output |= 0x0;
259  break;
260  case 0x6:
261  t_output |= 0x9;
262  break;
263  case 0x7:
264  t_output |= 0xe;
265  break;
266  case 0x8:
267  t_output |= 0x3;
268  break;
269  case 0x9:
270  t_output |= 0x7;
271  break;
272  case 0xa:
273  t_output |= 0x4;
274  break;
275  case 0xb:
276  t_output |= 0x5;
277  break;
278  case 0xc:
279  t_output |= 0xd;
280  break;
281  case 0xd:
282  t_output |= 0x2;
283  break;
284  case 0xe:
285  t_output |= 0x1;
286  break;
287  case 0xf:
288  t_output |= 0xa;
289  break;
290  default:
291  //unreachable
292  break;
293  }
294  }
295  return t_output;
296 }
297 
298 uint64_t
299 QARMA::PACMult(uint64_t tInput)
300 {
301  uint64_t t_output = 0;
302 
303  for (int i=0;i<=3; i++) {
304  uint8_t b8 = (tInput >> (4*(i+8))) & 0xF;
305  uint8_t b4 = (tInput >> (4*(i+4))) & 0xF;
306  uint8_t b12 = (tInput >> (4*(i+12))) & 0xF;
307  uint8_t b0 = (tInput >> (4*(i))) & 0xF;
308 
309  uint64_t t0 = rotCell(b8, 1) ^ rotCell(b4, 2);
310  t0 = t0 ^ rotCell(b0, 1);
311 
312  uint64_t t1 = rotCell(b12, 1) ^ rotCell(b4, 1);
313  t1 = t1 ^ rotCell(b0, 2);
314 
315  uint64_t t2 = rotCell(b12, 2) ^ rotCell(b8, 1);
316  t2 = t2 ^ rotCell(b0, 1);
317 
318  uint64_t t3 = rotCell(b12, 1) ^ rotCell(b8, 2);
319  t3 = t3 ^ rotCell(b4, 1);
320 
321  t_output |= (t3 << (4*i));
322  t_output |= (t2 << (4*(i+4)));
323  t_output |= (t1 << (4*(i+8)));
324  t_output |= (t0 << (4*(i+12)));
325  }
326  return t_output;
327 }
328 
329 BIT64
330 QARMA::computePAC(BIT64 data, BIT64 modifier, BIT64 key0, BIT64 key1)
331 {
332  BIT64 workingval;
333  BIT64 runningmod;
334  BIT64 roundkey;
335  BIT64 modk0;
336  std::array<BIT64, 5> RC;
337  RC[0] = (BIT64) 0x0000000000000000;
338  RC[1] = (BIT64) 0x13198A2E03707344;
339  RC[2] = (BIT64) 0xA4093822299F31D0;
340  RC[3] = (BIT64) 0x082EFA98EC4E6C89;
341  RC[4] = (BIT64) 0x452821E638D01377;
342 
343  const BIT64 alpha = 0xC0AC29B7C97C50DD;
344  //modk0 = key0<0>:key0<63:2>:
345 
346  modk0 = (key0 & 0x1) << 63;
347  modk0 = modk0 | ((key0 & ~0x3) >> 1);
348  modk0 = modk0 | ((key0.b15>>3) ^ ((key0.b0 & 0x2)>>1));
349 
350  runningmod = modifier;
351  workingval = data^key0;
352  for (int i=0; i<=4; i++) {
353  roundkey = key1 ^ runningmod;
354  workingval = workingval ^ roundkey;
355  workingval = workingval ^ RC[i];
356 
357  if (i > 0) {
358  workingval = PACCellShuffle(workingval);
359  workingval = PACMult(workingval);
360  }
361  workingval = PACSub(workingval);
362  runningmod = tweakShuffle(runningmod);
363  }
364  roundkey = modk0 ^ runningmod;
365  workingval = workingval ^ roundkey;
366 
367  workingval = PACCellShuffle(workingval);
368  workingval = PACMult(workingval);
369  workingval = PACSub(workingval);
370  workingval = PACCellShuffle(workingval);
371  workingval = PACMult(workingval);
372  workingval = key1 ^ workingval;
373 
374  workingval = PACCellInvShuffle(workingval);
375  workingval = PACInvSub(workingval);
376  workingval = PACMult(workingval);
377  workingval = PACCellInvShuffle(workingval);
378  workingval = workingval ^ key0;
379  workingval = workingval ^ runningmod;
380 
381  for (int i=0; i<=4; i++) {
382  workingval = PACInvSub(workingval);
383  if (i < 4) {
384  workingval = PACMult(workingval);
385  workingval = PACCellInvShuffle(workingval);
386  }
387  runningmod = tweakInvShuffle(runningmod);
388  roundkey = key1 ^ runningmod;
389  workingval = workingval ^ RC[4-i];
390  workingval = workingval ^ roundkey;
391  workingval = workingval ^ alpha;
392  }
393  workingval = workingval ^ modk0;
394  return workingval;
395 }
396 
data
const char data[]
Definition: circlebuf.test.cc:42
ArmISA::t2
Bitfield< 2 > t2
Definition: miscregs_types.hh:228
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
ArmISA::t3
Bitfield< 3 > t3
Definition: miscregs_types.hh:227
ArmISA::t0
Bitfield< 0 > t0
Definition: miscregs_types.hh:230
QARMA::tweakCellInvRot
uint8_t tweakCellInvRot(uint8_t incell)
Definition: qarma.cc:56
QARMA::amount
int amount
Definition: qarma.hh:67
qarma.hh
bitfield.hh
QARMA
Definition: qarma.hh:44
QARMA::PACSub
uint64_t PACSub(uint64_t tInput)
Definition: qarma.cc:235
QARMA::tweakShuffle
BIT64 tweakShuffle(BIT64 indata)
Definition: qarma.cc:99
QARMA::PACCellInvShuffle
BIT64 PACCellInvShuffle(BIT64 indata)
Definition: qarma.cc:123
QARMA::computePAC
BIT64 computePAC(BIT64 data, BIT64 modifier, BIT64 key0, BIT64 key1)
Definition: qarma.cc:330
QARMA::b8
Bitfield< 35, 32 > b8
Definition: qarma.hh:55
ArmISA::b
Bitfield< 7 > b
Definition: miscregs_types.hh:376
QARMA::tweakInvShuffle
BIT64 tweakInvShuffle(BIT64 indata)
Definition: qarma.cc:76
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
ArmISA::t1
Bitfield< 1 > t1
Definition: miscregs_types.hh:229
QARMA::b4
Bitfield< 19, 16 > b4
Definition: qarma.hh:59
QARMA::PACInvSub
uint64_t PACInvSub(uint64_t tInput)
Definition: qarma.cc:170
QARMA::PACCellShuffle
BIT64 PACCellShuffle(BIT64 indata)
Definition: qarma.cc:146
QARMA::PACMult
uint64_t PACMult(uint64_t tInput)
Definition: qarma.cc:299
QARMA::b0
Bitfield< 3, 0 > b0
Definition: qarma.hh:63
QARMA::b12
Bitfield< 51, 48 > b12
Definition: qarma.hh:51
QARMA::tweakCellRot
uint8_t tweakCellRot(uint8_t incell)
Definition: qarma.cc:66

Generated on Wed Sep 30 2020 14:02:01 for gem5 by doxygen 1.8.17