ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Speck.cpp
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "Speck.h"
24 #include "Crypto.h"
25 #include "utility/RotateUtil.h"
26 #include "utility/EndianUtil.h"
27 #include <string.h>
28 
56 // The "avr-gcc" compiler doesn't do a very good job of compiling
57 // code involving 64-bit values. So we have to use inline assembly.
58 // It also helps to break the state up into 32-bit quantities
59 // because "asm" supports register names like %A0, %B0, %C0, %D0
60 // for the bytes in a 32-bit quantity, but it does not support
61 // %E0, %F0, %G0, %H0 for the high bytes of a 64-bit quantity.
62 #if defined(__AVR__)
63 #define USE_AVR_INLINE_ASM 1
64 #endif
65 
73  : rounds(32)
74 {
75 }
76 
77 Speck::~Speck()
78 {
79  clean(k);
80 }
81 
82 size_t Speck::blockSize() const
83 {
84  return 16;
85 }
86 
87 size_t Speck::keySize() const
88 {
89  // Also supports 128-bit and 192-bit, but we only report 256-bit.
90  return 32;
91 }
92 
93 // Pack/unpack byte-aligned big-endian 64-bit quantities.
94 #define pack64(data, value) \
95  do { \
96  uint64_t v = htobe64((value)); \
97  memcpy((data), &v, sizeof(uint64_t)); \
98  } while (0)
99 #define unpack64(value, data) \
100  do { \
101  memcpy(&(value), (data), sizeof(uint64_t)); \
102  (value) = be64toh((value)); \
103  } while (0)
104 
105 bool Speck::setKey(const uint8_t *key, size_t len)
106 {
107 #if USE_AVR_INLINE_ASM
108  uint64_t l[4];
109  uint8_t m, mb;
110  if (len == 32) {
111  m = 4;
112  mb = 3 * 8;
113  } else if (len == 24) {
114  m = 3;
115  mb = 2 * 8;
116  } else if (len == 16) {
117  m = 2;
118  mb = 8;
119  } else {
120  return false;
121  }
122  rounds = 30 + m;
123 
124  // Copy the first (m - 1) * 8 bytes of the key into the "l" array
125  // in reverse order to convert big endian into little-endian.
126  __asm__ __volatile__ (
127  "1:\n"
128  "ld __tmp_reg__,-Z\n"
129  "st X+,__tmp_reg__\n"
130  "dec %2\n"
131  "brne 1b\n"
132  : : "x"(l), "z"(key + len - 8), "r"(mb)
133  );
134 
135  // Copy the final 8 bytes of the key into k[0] in reverse order.
136  __asm__ __volatile__ (
137  "1:\n"
138  "ld __tmp_reg__,-Z\n"
139  "st X+,__tmp_reg__\n"
140  "dec %2\n"
141  "brne 1b\n"
142  : : "x"(k), "z"(key + len), "r"(8)
143  );
144 
145  // Expand the key to the full key schedule.
146  __asm__ __volatile__ (
147  "1:\n"
148  // l[li_out] = (k[i] + rightRotate8_64(l[li_in])) ^ i;
149  "add %A1,%2\n" // X = &(l[li_in])
150  "adc %B1,__zero_reg__\n"
151  "ld r15,X+\n" // x = rightRotate8_64(l[li_in])
152  "ld r8,X+\n"
153  "ld r9,X+\n"
154  "ld r10,X+\n"
155  "ld r11,X+\n"
156  "ld r12,X+\n"
157  "ld r13,X+\n"
158  "ld r14,X+\n"
159 
160  "ld r16,Z+\n" // y = k[i]
161  "ld r17,Z+\n"
162  "ld r18,Z+\n"
163  "ld r19,Z+\n"
164  "ld r20,Z+\n"
165  "ld r21,Z+\n"
166  "ld r22,Z+\n"
167  "ld r23,Z+\n"
168 
169  "add r8,r16\n" // x += y
170  "adc r9,r17\n"
171  "adc r10,r18\n"
172  "adc r11,r19\n"
173  "adc r12,r20\n"
174  "adc r13,r21\n"
175  "adc r14,r22\n"
176  "adc r15,r23\n"
177 
178  "eor r8,%4\n" // x ^= i
179 
180  // X = X - li_in + li_out
181  "ldi r24,8\n" // li_in = li_in + 1
182  "add %2,r24\n"
183  "sub %A1,%2\n" // return X to its initial value
184  "sbc %B1,__zero_reg__\n"
185  "ldi r25,0x1f\n"
186  "and %2,r25\n" // li_in = li_in % 4
187  "add %A1,%3\n" // X = &(l[li_out])
188  "adc %B1,__zero_reg__\n"
189 
190  "st X+,r8\n" // l[li_out] = x
191  "st X+,r9\n"
192  "st X+,r10\n"
193  "st X+,r11\n"
194  "st X+,r12\n"
195  "st X+,r13\n"
196  "st X+,r14\n"
197  "st X+,r15\n"
198 
199  "add %3,r24\n" // li_out = li_out + 1
200  "sub %A1,%3\n" // return X to its initial value
201  "sbc %B1,__zero_reg__\n"
202  "and %3,r25\n" // li_out = li_out % 4
203 
204  // k[i + 1] = leftRotate3_64(k[i]) ^ l[li_out];
205  "lsl r16\n" // y = leftRotate1_64(y)
206  "rol r17\n"
207  "rol r18\n"
208  "rol r19\n"
209  "rol r20\n"
210  "rol r21\n"
211  "rol r22\n"
212  "rol r23\n"
213  "adc r16,__zero_reg__\n"
214 
215  "lsl r16\n" // y = leftRotate1_64(y)
216  "rol r17\n"
217  "rol r18\n"
218  "rol r19\n"
219  "rol r20\n"
220  "rol r21\n"
221  "rol r22\n"
222  "rol r23\n"
223  "adc r16,__zero_reg__\n"
224 
225  "lsl r16\n" // y = leftRotate1_64(y)
226  "rol r17\n"
227  "rol r18\n"
228  "rol r19\n"
229  "rol r20\n"
230  "rol r21\n"
231  "rol r22\n"
232  "rol r23\n"
233  "adc r16,__zero_reg__\n"
234 
235  "eor r16,r8\n" // y ^= x
236  "eor r17,r9\n"
237  "eor r18,r10\n"
238  "eor r19,r11\n"
239  "eor r20,r12\n"
240  "eor r21,r13\n"
241  "eor r22,r14\n"
242  "eor r23,r15\n"
243 
244  "st Z,r16\n" // k[i + 1] = y
245  "std Z+1,r17\n"
246  "std Z+2,r18\n"
247  "std Z+3,r19\n"
248  "std Z+4,r20\n"
249  "std Z+5,r21\n"
250  "std Z+6,r22\n"
251  "std Z+7,r23\n"
252 
253  // Loop
254  "inc %4\n" // ++i
255  "dec %5\n" // --rounds
256  "breq 2f\n"
257  "rjmp 1b\n"
258  "2:\n"
259 
260  : : "z"(k), "x"(l),
261  "r"((uint8_t)0), // initial value of li_in
262  "r"((uint8_t)((m - 1) * 8)), // initial value of li_out
263  "r"(0), // initial value of i
264  "r"(rounds - 1)
265  : "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
266  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
267  "r24", "r25"
268  );
269 #else
270  uint64_t l[4];
271  uint8_t m;
272  if (len == 32) {
273  m = 4;
274  unpack64(l[2], key);
275  unpack64(l[1], key + 8);
276  unpack64(l[0], key + 16);
277  unpack64(k[0], key + 24);
278  } else if (len == 24) {
279  m = 3;
280  unpack64(l[1], key);
281  unpack64(l[0], key + 8);
282  unpack64(k[0], key + 16);
283  } else if (len == 16) {
284  m = 2;
285  unpack64(l[0], key);
286  unpack64(k[0], key + 8);
287  } else {
288  return false;
289  }
290  rounds = 30 + m;
291  uint8_t li_in = 0;
292  uint8_t li_out = m - 1;
293  for (uint8_t i = 0; i < (rounds - 1); ++i) {
294  l[li_out] = (k[i] + rightRotate8_64(l[li_in])) ^ i;
295  k[i + 1] = leftRotate3_64(k[i]) ^ l[li_out];
296  if ((++li_in) >= m)
297  li_in = 0;
298  if ((++li_out) >= m)
299  li_out = 0;
300  }
301 #endif
302  clean(l);
303  return true;
304 }
305 
306 void Speck::encryptBlock(uint8_t *output, const uint8_t *input)
307 {
308 #if USE_AVR_INLINE_ASM
309  uint32_t xlow, xhigh, ylow, yhigh;
310 
311  // Unpack the input into the x and y variables, converting
312  // from big-endian into little-endian in the process.
313  __asm__ __volatile__ (
314  "ld %D1,Z\n"
315  "ldd %C1,Z+1\n"
316  "ldd %B1,Z+2\n"
317  "ldd %A1,Z+3\n"
318  "ldd %D0,Z+4\n"
319  "ldd %C0,Z+5\n"
320  "ldd %B0,Z+6\n"
321  "ldd %A0,Z+7\n"
322  "ldd %D3,Z+8\n"
323  "ldd %C3,Z+9\n"
324  "ldd %B3,Z+10\n"
325  "ldd %A3,Z+11\n"
326  "ldd %D2,Z+12\n"
327  "ldd %C2,Z+13\n"
328  "ldd %B2,Z+14\n"
329  "ldd %A2,Z+15\n"
330  : "=r"(xlow), "=r"(xhigh), "=r"(ylow), "=r"(yhigh)
331  : "z"(input)
332  );
333 
334  // Perform all encryption rounds. Z points to the key schedule.
335  __asm__ __volatile__ (
336  "1:\n"
337  // x = (rightRotate8_64(x) + y) ^ *s++;
338  "mov __tmp_reg__,%A0\n" // x = rightRotate8_64(x)
339  "mov %A0,%B0\n"
340  "mov %B0,%C0\n"
341  "mov %C0,%D0\n"
342  "mov %D0,%A1\n"
343  "mov %A1,%B1\n"
344  "mov %B1,%C1\n"
345  "mov %C1,%D1\n"
346  "mov %D1,__tmp_reg__\n"
347 
348  "add %A0,%A2\n" // x += y
349  "adc %B0,%B2\n"
350  "adc %C0,%C2\n"
351  "adc %D0,%D2\n"
352  "adc %A1,%A3\n"
353  "adc %B1,%B3\n"
354  "adc %C1,%C3\n"
355  "adc %D1,%D3\n"
356 
357  "ld __tmp_reg__,Z+\n" // x ^= *s++
358  "eor %A0,__tmp_reg__\n"
359  "ld __tmp_reg__,Z+\n"
360  "eor %B0,__tmp_reg__\n"
361  "ld __tmp_reg__,Z+\n"
362  "eor %C0,__tmp_reg__\n"
363  "ld __tmp_reg__,Z+\n"
364  "eor %D0,__tmp_reg__\n"
365  "ld __tmp_reg__,Z+\n"
366  "eor %A1,__tmp_reg__\n"
367  "ld __tmp_reg__,Z+\n"
368  "eor %B1,__tmp_reg__\n"
369  "ld __tmp_reg__,Z+\n"
370  "eor %C1,__tmp_reg__\n"
371  "ld __tmp_reg__,Z+\n"
372  "eor %D1,__tmp_reg__\n"
373 
374  // y = leftRotate3_64(y) ^ x;
375  "lsl %A2\n" // y = leftRotate1_64(y)
376  "rol %B2\n"
377  "rol %C2\n"
378  "rol %D2\n"
379  "rol %A3\n"
380  "rol %B3\n"
381  "rol %C3\n"
382  "rol %D3\n"
383  "adc %A2,__zero_reg__\n"
384 
385  "lsl %A2\n" // y = leftRotate1_64(y)
386  "rol %B2\n"
387  "rol %C2\n"
388  "rol %D2\n"
389  "rol %A3\n"
390  "rol %B3\n"
391  "rol %C3\n"
392  "rol %D3\n"
393  "adc %A2,__zero_reg__\n"
394 
395  "lsl %A2\n" // y = leftRotate1_64(y)
396  "rol %B2\n"
397  "rol %C2\n"
398  "rol %D2\n"
399  "rol %A3\n"
400  "rol %B3\n"
401  "rol %C3\n"
402  "rol %D3\n"
403  "adc %A2,__zero_reg__\n"
404 
405  "eor %A2,%A0\n" // y ^= x
406  "eor %B2,%B0\n"
407  "eor %C2,%C0\n"
408  "eor %D2,%D0\n"
409  "eor %A3,%A1\n"
410  "eor %B3,%B1\n"
411  "eor %C3,%C1\n"
412  "eor %D3,%D1\n"
413 
414  // Loop
415  "dec %5\n" // --round
416  "breq 2f\n"
417  "rjmp 1b\n"
418  "2:\n"
419  : "+r"(xlow), "+r"(xhigh), "+r"(ylow), "+r"(yhigh)
420  : "z"(k), "r"(rounds)
421  );
422 
423  // Pack the results into the output and convert back to big-endian.
424  __asm__ __volatile__ (
425  "st Z,%D1\n"
426  "std Z+1,%C1\n"
427  "std Z+2,%B1\n"
428  "std Z+3,%A1\n"
429  "std Z+4,%D0\n"
430  "std Z+5,%C0\n"
431  "std Z+6,%B0\n"
432  "std Z+7,%A0\n"
433  "std Z+8,%D3\n"
434  "std Z+9,%C3\n"
435  "std Z+10,%B3\n"
436  "std Z+11,%A3\n"
437  "std Z+12,%D2\n"
438  "std Z+13,%C2\n"
439  "std Z+14,%B2\n"
440  "std Z+15,%A2\n"
441  : : "r"(xlow), "r"(xhigh), "r"(ylow), "r"(yhigh), "z"(output)
442  );
443 #else
444  uint64_t x, y;
445  const uint64_t *s = k;
446  unpack64(x, input);
447  unpack64(y, input + 8);
448  for (uint8_t round = rounds; round > 0; --round, ++s) {
449  x = (rightRotate8_64(x) + y) ^ s[0];
450  y = leftRotate3_64(y) ^ x;
451  }
452  pack64(output, x);
453  pack64(output + 8, y);
454 #endif
455 }
456 
457 void Speck::decryptBlock(uint8_t *output, const uint8_t *input)
458 {
459 #if USE_AVR_INLINE_ASM
460  uint32_t xlow, xhigh, ylow, yhigh;
461 
462  // Unpack the input into the x and y variables, converting
463  // from big-endian into little-endian in the process.
464  __asm__ __volatile__ (
465  "ld %D1,Z\n"
466  "ldd %C1,Z+1\n"
467  "ldd %B1,Z+2\n"
468  "ldd %A1,Z+3\n"
469  "ldd %D0,Z+4\n"
470  "ldd %C0,Z+5\n"
471  "ldd %B0,Z+6\n"
472  "ldd %A0,Z+7\n"
473  "ldd %D3,Z+8\n"
474  "ldd %C3,Z+9\n"
475  "ldd %B3,Z+10\n"
476  "ldd %A3,Z+11\n"
477  "ldd %D2,Z+12\n"
478  "ldd %C2,Z+13\n"
479  "ldd %B2,Z+14\n"
480  "ldd %A2,Z+15\n"
481  : "=r"(xlow), "=r"(xhigh), "=r"(ylow), "=r"(yhigh)
482  : "z"(input)
483  );
484 
485  // Perform all decryption rounds. Z points to the end of key schedule.
486  __asm__ __volatile__ (
487  "1:\n"
488  // y = rightRotate3_64(x ^ y);
489  "eor %A2,%A0\n" // y ^= x
490  "eor %B2,%B0\n"
491  "eor %C2,%C0\n"
492  "eor %D2,%D0\n"
493  "eor %A3,%A1\n"
494  "eor %B3,%B1\n"
495  "eor %C3,%C1\n"
496  "eor %D3,%D1\n"
497 
498  "bst %A2,0\n" // y = rightRotate1_64(y)
499  "ror %D3\n"
500  "ror %C3\n"
501  "ror %B3\n"
502  "ror %A3\n"
503  "ror %D2\n"
504  "ror %C2\n"
505  "ror %B2\n"
506  "ror %A2\n"
507  "bld %D3,7\n"
508 
509  "bst %A2,0\n" // y = rightRotate1_64(y)
510  "ror %D3\n"
511  "ror %C3\n"
512  "ror %B3\n"
513  "ror %A3\n"
514  "ror %D2\n"
515  "ror %C2\n"
516  "ror %B2\n"
517  "ror %A2\n"
518  "bld %D3,7\n"
519 
520  "bst %A2,0\n" // y = rightRotate1_64(y)
521  "ror %D3\n"
522  "ror %C3\n"
523  "ror %B3\n"
524  "ror %A3\n"
525  "ror %D2\n"
526  "ror %C2\n"
527  "ror %B2\n"
528  "ror %A2\n"
529  "bld %D3,7\n"
530 
531  // x = leftRotate8_64((x ^ *s--) - y);
532  "ld __tmp_reg__,-Z\n" // x ^= *s--
533  "eor %D1,__tmp_reg__\n"
534  "ld __tmp_reg__,-Z\n"
535  "eor %C1,__tmp_reg__\n"
536  "ld __tmp_reg__,-Z\n"
537  "eor %B1,__tmp_reg__\n"
538  "ld __tmp_reg__,-Z\n"
539  "eor %A1,__tmp_reg__\n"
540  "ld __tmp_reg__,-Z\n"
541  "eor %D0,__tmp_reg__\n"
542  "ld __tmp_reg__,-Z\n"
543  "eor %C0,__tmp_reg__\n"
544  "ld __tmp_reg__,-Z\n"
545  "eor %B0,__tmp_reg__\n"
546  "ld __tmp_reg__,-Z\n"
547  "eor %A0,__tmp_reg__\n"
548 
549  "sub %A0,%A2\n" // x -= y
550  "sbc %B0,%B2\n"
551  "sbc %C0,%C2\n"
552  "sbc %D0,%D2\n"
553  "sbc %A1,%A3\n"
554  "sbc %B1,%B3\n"
555  "sbc %C1,%C3\n"
556  "sbc %D1,%D3\n"
557 
558  "mov __tmp_reg__,%D1\n" // x = lefRotate8_64(x)
559  "mov %D1,%C1\n"
560  "mov %C1,%B1\n"
561  "mov %B1,%A1\n"
562  "mov %A1,%D0\n"
563  "mov %D0,%C0\n"
564  "mov %C0,%B0\n"
565  "mov %B0,%A0\n"
566  "mov %A0,__tmp_reg__\n"
567 
568  // Loop
569  "dec %5\n" // --round
570  "breq 2f\n"
571  "rjmp 1b\n"
572  "2:\n"
573  : "+r"(xlow), "+r"(xhigh), "+r"(ylow), "+r"(yhigh)
574  : "z"(k + rounds), "r"(rounds)
575  );
576 
577  // Pack the results into the output and convert back to big-endian.
578  __asm__ __volatile__ (
579  "st Z,%D1\n"
580  "std Z+1,%C1\n"
581  "std Z+2,%B1\n"
582  "std Z+3,%A1\n"
583  "std Z+4,%D0\n"
584  "std Z+5,%C0\n"
585  "std Z+6,%B0\n"
586  "std Z+7,%A0\n"
587  "std Z+8,%D3\n"
588  "std Z+9,%C3\n"
589  "std Z+10,%B3\n"
590  "std Z+11,%A3\n"
591  "std Z+12,%D2\n"
592  "std Z+13,%C2\n"
593  "std Z+14,%B2\n"
594  "std Z+15,%A2\n"
595  : : "r"(xlow), "r"(xhigh), "r"(ylow), "r"(yhigh), "z"(output)
596  );
597 #else
598  uint64_t x, y;
599  const uint64_t *s = k + rounds - 1;
600  unpack64(x, input);
601  unpack64(y, input + 8);
602  for (uint8_t round = rounds; round > 0; --round, --s) {
603  y = rightRotate3_64(x ^ y);
604  x = leftRotate8_64((x ^ s[0]) - y);
605  }
606  pack64(output, x);
607  pack64(output + 8, y);
608 #endif
609 }
610 
612 {
613  clean(k);
614 }
size_t keySize() const
Default size of the key for this block cipher, in bytes.
Definition: Speck.cpp:87
size_t blockSize() const
Size of a single block processed by this cipher, in bytes.
Definition: Speck.cpp:82
Speck()
Constructs a Speck block cipher with no initial key.
Definition: Speck.cpp:72
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: Speck.cpp:306
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: Speck.cpp:457
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: Speck.cpp:105
void clear()
Clears all security-sensitive state from this block cipher.
Definition: Speck.cpp:611