25 #include "utility/EndianUtil.h"
26 #include "utility/RotateUtil.h"
27 #include "utility/ProgMemUtil.h"
58 #define USE_AVR_INLINE_ASM 1
61 #ifndef CRYPTO_LITTLE_ENDIAN
62 #error "Arduino platforms are assumed to be little-endian"
66 #define RC_EXTRACT_ROW(x,shift) \
67 (((((uint32_t)((x) >> ((shift) + 24))) & 0xFF)) | \
68 ((((uint32_t)((x) >> ((shift) + 16))) & 0xFF) << 8) | \
69 ((((uint32_t)((x) >> ((shift) + 8))) & 0xFF) << 16) | \
70 ((((uint32_t)((x) >> ((shift)))) & 0xFF) << 24))
73 #define ALPHA 0x243F6A8885A308D3ULL
74 #define ALPHA_ROW0 (RC_EXTRACT_ROW(ALPHA, 32))
75 #define ALPHA_ROW1 (RC_EXTRACT_ROW(ALPHA, 0))
77 #ifndef USE_AVR_INLINE_ASM
81 {RC_EXTRACT_ROW((x), 32), RC_EXTRACT_ROW((x), 0)}
84 static uint32_t
const rc[8][2] = {
85 RC(0x13198A2E03707344ULL),
86 RC(0xA4093822299F31D0ULL),
87 RC(0x082EFA98EC4E6C89ULL),
88 RC(0x452821E638D01377ULL),
89 RC(0xBE5466CF34E90C6CULL),
90 RC(0xC0AC29B7C97C50DDULL),
91 RC(0x3F84D5B5B5470917ULL),
92 RC(0x9216D5D98979FB1BULL)
95 #endif // !USE_AVR_INLINE_ASM
131 #ifndef USE_AVR_INLINE_ASM
133 inline void mantis_unpack_block(uint32_t *block,
const uint8_t *buf)
135 block[0] = ((uint32_t)(buf[0])) |
136 (((uint32_t)(buf[1])) << 8) |
137 (((uint32_t)(buf[2])) << 16) |
138 (((uint32_t)(buf[3])) << 24);
139 block[1] = ((uint32_t)(buf[4])) |
140 (((uint32_t)(buf[5])) << 8) |
141 (((uint32_t)(buf[6])) << 16) |
142 (((uint32_t)(buf[7])) << 24);
145 static void mantis_unpack_rotated_block(uint32_t *block,
const uint8_t *buf)
150 uint8_t carry = buf[7];
151 for (index = 0; index < 8; ++index) {
153 rotated[index] = (carry << 7) | (next >> 1);
156 rotated[7] ^= (buf[0] >> 7);
157 mantis_unpack_block(block, rotated);
161 #endif // !USE_AVR_INLINE_ASM
167 #if USE_AVR_INLINE_ASM
168 __asm__ __volatile__ (
198 "mov __tmp_reg__,__zero_reg__\n"
199 "bld __tmp_reg__,0\n"
200 "eor r15,__tmp_reg__\n"
211 "ld __tmp_reg__,X+\n"
212 "std Z+16,__tmp_reg__\n"
213 "ld __tmp_reg__,X+\n"
214 "std Z+17,__tmp_reg__\n"
215 "ld __tmp_reg__,X+\n"
216 "std Z+18,__tmp_reg__\n"
217 "ld __tmp_reg__,X+\n"
218 "std Z+19,__tmp_reg__\n"
219 "ld __tmp_reg__,X+\n"
220 "std Z+20,__tmp_reg__\n"
221 "ld __tmp_reg__,X+\n"
222 "std Z+21,__tmp_reg__\n"
223 "ld __tmp_reg__,X+\n"
224 "std Z+22,__tmp_reg__\n"
225 "ld __tmp_reg__,X+\n"
226 "std Z+23,__tmp_reg__\n"
229 "std Z+24,__zero_reg__\n"
230 "std Z+25,__zero_reg__\n"
231 "std Z+26,__zero_reg__\n"
232 "std Z+27,__zero_reg__\n"
233 "std Z+28,__zero_reg__\n"
234 "std Z+29,__zero_reg__\n"
235 "std Z+30,__zero_reg__\n"
236 "std Z+31,__zero_reg__\n"
238 : :
"z"(&st),
"x"(key)
239 :
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
"memory"
242 mantis_unpack_block(st.k0, key);
243 mantis_unpack_block(st.k1, key + 8);
244 mantis_unpack_rotated_block(st.k0prime, key);
270 #if USE_AVR_INLINE_ASM
271 __asm__ __volatile__ (
272 "mov __tmp_reg__,r26\n"
273 "or __tmp_reg__,r27\n"
275 "st Z+,__zero_reg__\n"
276 "st Z+,__zero_reg__\n"
277 "st Z+,__zero_reg__\n"
278 "st Z+,__zero_reg__\n"
279 "st Z+,__zero_reg__\n"
280 "st Z+,__zero_reg__\n"
281 "st Z+,__zero_reg__\n"
282 "st Z+,__zero_reg__\n"
285 "ld __tmp_reg__,X+\n"
286 "st Z+,__tmp_reg__\n"
287 "ld __tmp_reg__,X+\n"
288 "st Z+,__tmp_reg__\n"
289 "ld __tmp_reg__,X+\n"
290 "st Z+,__tmp_reg__\n"
291 "ld __tmp_reg__,X+\n"
292 "st Z+,__tmp_reg__\n"
293 "ld __tmp_reg__,X+\n"
294 "st Z+,__tmp_reg__\n"
295 "ld __tmp_reg__,X+\n"
296 "st Z+,__tmp_reg__\n"
297 "ld __tmp_reg__,X+\n"
298 "st Z+,__tmp_reg__\n"
299 "ld __tmp_reg__,X+\n"
300 "st Z+,__tmp_reg__\n"
302 : :
"z"(st.tweak),
"x"(tweak)
306 mantis_unpack_block(st.tweak, tweak);
325 uint32_t temp = st.k0[0];
326 st.k0[0] = st.k0prime[0];
327 st.k0prime[0] = temp;
329 st.k0[1] = st.k0prime[1];
330 st.k0prime[1] = temp;
333 st.k1[0] ^= ALPHA_ROW0;
334 st.k1[1] ^= ALPHA_ROW1;
337 #if USE_AVR_INLINE_ASM
340 #define RC_EXTRACT_BYTE(x,shift) ((uint8_t)((x) >> (shift)))
342 RC_EXTRACT_BYTE((x), 56), \
343 RC_EXTRACT_BYTE((x), 48), \
344 RC_EXTRACT_BYTE((x), 40), \
345 RC_EXTRACT_BYTE((x), 32), \
346 RC_EXTRACT_BYTE((x), 24), \
347 RC_EXTRACT_BYTE((x), 16), \
348 RC_EXTRACT_BYTE((x), 8), \
349 RC_EXTRACT_BYTE((x), 0)
353 #define ALIGN256 __attribute__((aligned(256)))
360 static uint8_t
const sbox[256 + 64] PROGMEM ALIGN256 = {
361 0xcc, 0xca, 0xcd, 0xc3, 0xce, 0xcb, 0xcf, 0xc7, 0xc8, 0xc9, 0xc1, 0xc5,
362 0xc0, 0xc2, 0xc4, 0xc6, 0xac, 0xaa, 0xad, 0xa3, 0xae, 0xab, 0xaf, 0xa7,
363 0xa8, 0xa9, 0xa1, 0xa5, 0xa0, 0xa2, 0xa4, 0xa6, 0xdc, 0xda, 0xdd, 0xd3,
364 0xde, 0xdb, 0xdf, 0xd7, 0xd8, 0xd9, 0xd1, 0xd5, 0xd0, 0xd2, 0xd4, 0xd6,
365 0x3c, 0x3a, 0x3d, 0x33, 0x3e, 0x3b, 0x3f, 0x37, 0x38, 0x39, 0x31, 0x35,
366 0x30, 0x32, 0x34, 0x36, 0xec, 0xea, 0xed, 0xe3, 0xee, 0xeb, 0xef, 0xe7,
367 0xe8, 0xe9, 0xe1, 0xe5, 0xe0, 0xe2, 0xe4, 0xe6, 0xbc, 0xba, 0xbd, 0xb3,
368 0xbe, 0xbb, 0xbf, 0xb7, 0xb8, 0xb9, 0xb1, 0xb5, 0xb0, 0xb2, 0xb4, 0xb6,
369 0xfc, 0xfa, 0xfd, 0xf3, 0xfe, 0xfb, 0xff, 0xf7, 0xf8, 0xf9, 0xf1, 0xf5,
370 0xf0, 0xf2, 0xf4, 0xf6, 0x7c, 0x7a, 0x7d, 0x73, 0x7e, 0x7b, 0x7f, 0x77,
371 0x78, 0x79, 0x71, 0x75, 0x70, 0x72, 0x74, 0x76, 0x8c, 0x8a, 0x8d, 0x83,
372 0x8e, 0x8b, 0x8f, 0x87, 0x88, 0x89, 0x81, 0x85, 0x80, 0x82, 0x84, 0x86,
373 0x9c, 0x9a, 0x9d, 0x93, 0x9e, 0x9b, 0x9f, 0x97, 0x98, 0x99, 0x91, 0x95,
374 0x90, 0x92, 0x94, 0x96, 0x1c, 0x1a, 0x1d, 0x13, 0x1e, 0x1b, 0x1f, 0x17,
375 0x18, 0x19, 0x11, 0x15, 0x10, 0x12, 0x14, 0x16, 0x5c, 0x5a, 0x5d, 0x53,
376 0x5e, 0x5b, 0x5f, 0x57, 0x58, 0x59, 0x51, 0x55, 0x50, 0x52, 0x54, 0x56,
377 0x0c, 0x0a, 0x0d, 0x03, 0x0e, 0x0b, 0x0f, 0x07, 0x08, 0x09, 0x01, 0x05,
378 0x00, 0x02, 0x04, 0x06, 0x2c, 0x2a, 0x2d, 0x23, 0x2e, 0x2b, 0x2f, 0x27,
379 0x28, 0x29, 0x21, 0x25, 0x20, 0x22, 0x24, 0x26, 0x4c, 0x4a, 0x4d, 0x43,
380 0x4e, 0x4b, 0x4f, 0x47, 0x48, 0x49, 0x41, 0x45, 0x40, 0x42, 0x44, 0x46,
381 0x6c, 0x6a, 0x6d, 0x63, 0x6e, 0x6b, 0x6f, 0x67, 0x68, 0x69, 0x61, 0x65,
382 0x60, 0x62, 0x64, 0x66,
386 RC(0x13198A2E03707344ULL),
387 RC(0xA4093822299F31D0ULL),
388 RC(0x082EFA98EC4E6C89ULL),
389 RC(0x452821E638D01377ULL),
390 RC(0xBE5466CF34E90C6CULL),
391 RC(0xC0AC29B7C97C50DDULL),
392 RC(0x3F84D5B5B5470917ULL),
393 RC(0x9216D5D98979FB1BULL)
399 "mov r30," reg "\n" \
402 #define RC_SETUP(reg) \
404 "mov r30," reg "\n" \
406 "adc r24,__zero_reg__\n" \
408 #define RC_CLEANUP(reg) \
410 "sbc r24,__zero_reg__\n" \
412 "sbc r24,__zero_reg__\n"
413 #define RC_ADD(reg) \
416 #elif defined(__AVR_HAVE_LPMX__)
418 "mov r30," reg "\n" \
420 #define RC_SETUP(reg) \
422 "mov r30," reg "\n" \
424 #define RC_CLEANUP(reg) \
427 #define RC_ADD(reg) \
430 #elif defined(__AVR_TINY__)
432 "mov r30," reg "\n" \
434 #define RC_SETUP(reg) \
436 "mov r30," reg "\n" \
438 #define RC_CLEANUP(reg) \
441 #define RC_ADD(reg) \
446 "mov r30," reg "\n" \
449 #define RC_SETUP(reg) \
451 "mov r30," reg "\n" \
453 #define RC_CLEANUP(reg) \
456 #define RC_ADD(reg) \
463 #define MIX_COLUMNS(row0, row1, row2, row3) \
464 "mov __tmp_reg__," row0 "\n" \
465 "mov r25," row2 "\n" \
466 "mov " row0 "," row1 "\n" \
467 "mov " row1 ",__tmp_reg__\n" \
468 "mov " row2 "," row3 "\n" \
469 "mov " row3 ",r25\n" \
470 "eor __tmp_reg__," row0 "\n" \
471 "eor r25," row2 "\n" \
472 "eor " row0 ",r25\n" \
473 "eor " row1 ",r25\n" \
474 "eor " row2 ",__tmp_reg__\n" \
475 "eor " row3 ",__tmp_reg__\n" \
477 #else // !USE_AVR_INLINE_ASM
486 inline uint32_t mantis_sbox(uint32_t d)
498 uint32_t a = (d >> 3);
499 uint32_t b = (d >> 2);
500 uint32_t c = (d >> 1);
502 uint32_t ab = not_a | (~b);
503 uint32_t ad = not_a & (~d);
504 uint32_t aout = (((~c) & ab) | ad);
505 uint32_t bout = ad | (b & c) | (a & c & d);
506 uint32_t cout = (b & d) | ((b | d) & not_a);
507 uint32_t dout = (a | b | c) & ab & (c | d);
508 return ((aout & 0x11111111U) << 3) | ((bout & 0x11111111U) << 2) |
509 ((cout & 0x11111111U) << 1) | (dout & 0x11111111U);
515 uint16_t row1 = tweak->
row[1];
516 uint16_t row3 = tweak->
row[3];
517 tweak->
row[1] = tweak->
row[0];
518 tweak->
row[3] = tweak->
row[2];
519 tweak->
row[0] = ((row1 >> 8) & 0x00F0U) |
522 tweak->
row[2] = ((row1 << 4) & 0x0F00U) |
523 ((row1 >> 4) & 0x00F0U) |
524 ((row3 >> 4) & 0x000FU) |
525 ((row3 << 12) & 0xF000U);
528 inline void mantis_update_tweak_inverse(
MantisCells_t *tweak)
531 uint16_t row0 = tweak->
row[0];
532 uint16_t row2 = tweak->
row[2];
533 tweak->
row[0] = tweak->
row[1];
534 tweak->
row[2] = tweak->
row[3];
535 tweak->
row[1] = ((row2 >> 4) & 0x00F0U) |
536 ((row2 << 4) & 0x0F00U) |
538 ((row0 << 8) & 0xF000U);
539 tweak->
row[3] = (row0 & 0xFF00U) |
540 ((row2 << 4) & 0x00F0U) |
541 ((row2 >> 12) & 0x000FU);
547 uint16_t row0 = state->
row[0];
548 uint16_t row1 = state->
row[1];
549 uint16_t row2 = state->
row[2];
550 uint16_t row3 = state->
row[3];
551 state->
row[0] = (row0 & 0x00F0U) |
553 ((row2 >> 8) & 0x000FU) |
554 ((row3 << 8) & 0x0F00U);
555 state->
row[1] = (row0 & 0x000FU) |
557 ((row2 >> 8) & 0x00F0U) |
558 ((row3 << 8) & 0xF000U);
559 state->
row[2] = ((row0 << 4) & 0xF000U) |
560 ((row1 << 4) & 0x00F0U) |
561 ((row2 << 4) & 0x0F00U) |
562 ((row3 >> 12) & 0x000FU);
563 state->
row[3] = ((row0 >> 4) & 0x0F00U) |
564 ((row1 >> 4) & 0x000FU) |
565 ((row2 << 12) & 0xF000U) |
566 ((row3 >> 4) & 0x00F0U);
572 uint16_t row0 = state->
row[0];
573 uint16_t row1 = state->
row[1];
574 uint16_t row2 = state->
row[2];
575 uint16_t row3 = state->
row[3];
576 state->
row[0] = (row0 & 0x00F0U) |
578 ((row2 >> 4) & 0x0F00U) |
579 ((row3 << 4) & 0xF000U);
580 state->
row[1] = (row0 & 0xF000U) |
582 ((row2 >> 4) & 0x000FU) |
583 ((row3 << 4) & 0x00F0U);
584 state->
row[2] = ((row0 << 8) & 0x0F00U) |
585 ((row1 << 8) & 0xF000U) |
586 ((row2 >> 4) & 0x00F0U) |
587 ((row3 >> 12) & 0x000FU);
588 state->
row[3] = ((row0 >> 8) & 0x000FU) |
589 ((row1 >> 8) & 0x00F0U) |
590 ((row2 << 12) & 0xF000U) |
591 ((row3 << 4) & 0x0F00U);
596 uint16_t t0 = state->
row[0];
597 uint16_t t1 = state->
row[1];
598 uint16_t t2 = state->
row[2];
599 uint16_t t3 = state->
row[3];
600 state->
row[0] = t1 ^ t2 ^ t3;
601 state->
row[1] = t0 ^ t2 ^ t3;
602 state->
row[2] = t0 ^ t1 ^ t3;
603 state->
row[3] = t0 ^ t1 ^ t2;
606 #endif // !USE_AVR_INLINE_ASM
610 #if USE_AVR_INLINE_ASM
612 uint32_t sbox_addr = (uint32_t)sbox;
614 uint16_t sbox_addr = (uint16_t)sbox;
617 __asm__ __volatile__ (
669 "eor r8,__tmp_reg__\n"
671 "ldd __tmp_reg__,Z+1\n"
672 "eor r9,__tmp_reg__\n"
674 "ldd __tmp_reg__,Z+2\n"
675 "eor r10,__tmp_reg__\n"
677 "ldd __tmp_reg__,Z+3\n"
678 "eor r11,__tmp_reg__\n"
680 "ldd __tmp_reg__,Z+4\n"
681 "eor r12,__tmp_reg__\n"
683 "ldd __tmp_reg__,Z+5\n"
684 "eor r13,__tmp_reg__\n"
686 "ldd __tmp_reg__,Z+6\n"
687 "eor r14,__tmp_reg__\n"
689 "ldd __tmp_reg__,Z+7\n"
690 "eor r15,__tmp_reg__\n"
698 "in __tmp_reg__,%6\n"
764 "ldd __tmp_reg__,%A4\n"
765 "eor r8,__tmp_reg__\n"
767 "ldd __tmp_reg__,%B4\n"
768 "eor r9,__tmp_reg__\n"
770 "ldd __tmp_reg__,%C4\n"
771 "eor r10,__tmp_reg__\n"
773 "ldd __tmp_reg__,%D4\n"
774 "eor r11,__tmp_reg__\n"
776 "ldd __tmp_reg__,%A5\n"
777 "eor r12,__tmp_reg__\n"
779 "ldd __tmp_reg__,%B5\n"
780 "eor r13,__tmp_reg__\n"
782 "ldd __tmp_reg__,%C5\n"
783 "eor r14,__tmp_reg__\n"
785 "ldd __tmp_reg__,%D5\n"
786 "eor r15,__tmp_reg__\n"
793 "mov __tmp_reg__,r8\n"
798 "and __tmp_reg__,r25\n"
799 "or r10,__tmp_reg__\n"
806 "mov __tmp_reg__,r12\n"
807 "and __tmp_reg__,r25\n"
808 "or r13,__tmp_reg__\n"
814 "mov __tmp_reg__,r14\n"
815 "and __tmp_reg__,r25\n"
816 "or r9,__tmp_reg__\n"
822 "mov __tmp_reg__,r14\n"
828 "and __tmp_reg__,r25\n"
829 "or r12,__tmp_reg__\n"
834 MIX_COLUMNS(
"r8",
"r10",
"r12",
"r14")
835 MIX_COLUMNS(
"r9",
"r11",
"r13",
"r15")
858 MIX_COLUMNS(
"r8",
"r10",
"r12",
"r14")
859 MIX_COLUMNS(
"r9",
"r11",
"r13",
"r15")
871 #define ALPHA_ADJUST(reg, c) \
873 "ldd __tmp_reg__," reg
"\n" \
874 "eor __tmp_reg__,r24\n" \
875 "std " reg
",__tmp_reg__\n"
876 ALPHA_ADJUST(
"%A4",
"0x24")
877 ALPHA_ADJUST(
"%B4",
"0x3F")
878 ALPHA_ADJUST(
"%C4",
"0x6A")
879 ALPHA_ADJUST(
"%D4",
"0x88")
880 ALPHA_ADJUST(
"%A5",
"0x85")
881 ALPHA_ADJUST(
"%B5",
"0xA3")
882 ALPHA_ADJUST(
"%C5",
"0x08")
883 ALPHA_ADJUST(
"%D5",
"0xD3")
891 MIX_COLUMNS(
"r8",
"r10",
"r12",
"r14")
892 MIX_COLUMNS(
"r9",
"r11",
"r13",
"r15")
898 "mov __tmp_reg__,r8\n"
903 "and __tmp_reg__,r25\n"
905 "or r10,__tmp_reg__\n"
906 "mov __tmp_reg__,r13\n"
910 "mov r9,__tmp_reg__\n"
917 "and __tmp_reg__,r24\n"
918 "or r15,__tmp_reg__\n"
919 "mov __tmp_reg__,r11\n"
921 "and __tmp_reg__,r24\n"
926 "or r6,__tmp_reg__\n"
931 "mov __tmp_reg__,r14\n"
932 "and __tmp_reg__,r24\n"
933 "or r10,__tmp_reg__\n"
944 "ldd __tmp_reg__,%A4\n"
945 "eor r8,__tmp_reg__\n"
947 "ldd __tmp_reg__,%B4\n"
948 "eor r9,__tmp_reg__\n"
950 "ldd __tmp_reg__,%C4\n"
951 "eor r10,__tmp_reg__\n"
953 "ldd __tmp_reg__,%D4\n"
954 "eor r11,__tmp_reg__\n"
956 "ldd __tmp_reg__,%A5\n"
957 "eor r12,__tmp_reg__\n"
959 "ldd __tmp_reg__,%B5\n"
960 "eor r13,__tmp_reg__\n"
962 "ldd __tmp_reg__,%C5\n"
963 "eor r14,__tmp_reg__\n"
965 "ldd __tmp_reg__,%D5\n"
966 "eor r15,__tmp_reg__\n"
1030 "out %6,__tmp_reg__\n"
1040 "ldd __tmp_reg__,Z+8\n"
1041 "eor r8,__tmp_reg__\n"
1042 "ldd __tmp_reg__,%A4\n"
1043 "eor r8,__tmp_reg__\n"
1045 "ldd __tmp_reg__,Z+9\n"
1046 "eor r9,__tmp_reg__\n"
1047 "ldd __tmp_reg__,%B4\n"
1048 "eor r9,__tmp_reg__\n"
1050 "ldd __tmp_reg__,Z+10\n"
1051 "eor r10,__tmp_reg__\n"
1052 "ldd __tmp_reg__,%C4\n"
1053 "eor r10,__tmp_reg__\n"
1055 "ldd __tmp_reg__,Z+11\n"
1056 "eor r11,__tmp_reg__\n"
1057 "ldd __tmp_reg__,%D4\n"
1058 "eor r11,__tmp_reg__\n"
1060 "ldd __tmp_reg__,Z+12\n"
1061 "eor r12,__tmp_reg__\n"
1062 "ldd __tmp_reg__,%A5\n"
1063 "eor r12,__tmp_reg__\n"
1065 "ldd __tmp_reg__,Z+13\n"
1066 "eor r13,__tmp_reg__\n"
1067 "ldd __tmp_reg__,%B5\n"
1068 "eor r13,__tmp_reg__\n"
1070 "ldd __tmp_reg__,Z+14\n"
1071 "eor r14,__tmp_reg__\n"
1072 "ldd __tmp_reg__,%C5\n"
1073 "eor r14,__tmp_reg__\n"
1075 "ldd __tmp_reg__,Z+15\n"
1076 "eor r15,__tmp_reg__\n"
1077 "ldd __tmp_reg__,%D5\n"
1078 "eor r15,__tmp_reg__\n"
1092 : :
"x"(input),
"z"(&st),
"Q"(output),
"Q"(sbox_addr),
1093 "Q"(k1_0),
"Q"(k1_1)
1095 ,
"I" (_SFR_IO_ADDR(RAMPZ))
1097 :
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
1098 "r16",
"r17",
"r18",
"r19",
"r20",
"r21",
"r22",
"r23",
1099 "r24",
"r25",
"r6",
"r7",
"memory"
1101 #else // !USE_AVR_INLINE_ASM
1102 const uint32_t *r = rc[0];
1109 tweak.
lrow[0] = st.tweak[0];
1110 tweak.
lrow[1] = st.tweak[1];
1111 k1.
lrow[0] = st.k1[0];
1112 k1.
lrow[1] = st.k1[1];
1115 mantis_unpack_block(state.
lrow, input);
1119 state.
lrow[0] ^= st.k0[0] ^ k1.
lrow[0] ^ tweak.
lrow[0];
1120 state.
lrow[1] ^= st.k0[1] ^ k1.
lrow[1] ^ tweak.
lrow[1];
1123 for (index = 8; index > 0; --index) {
1125 mantis_update_tweak(&tweak);
1128 state.
lrow[0] = mantis_sbox(state.
lrow[0]);
1129 state.
lrow[1] = mantis_sbox(state.
lrow[1]);
1132 state.
lrow[0] ^= r[0];
1133 state.
lrow[1] ^= r[1];
1141 mantis_shift_rows(&state);
1144 mantis_mix_columns(&state);
1148 state.
lrow[0] = mantis_sbox(state.
lrow[0]);
1149 state.
lrow[1] = mantis_sbox(state.
lrow[1]);
1150 mantis_mix_columns(&state);
1151 state.
lrow[0] = mantis_sbox(state.
lrow[0]);
1152 state.
lrow[1] = mantis_sbox(state.
lrow[1]);
1155 k1.
lrow[0] ^= ALPHA_ROW0;
1156 k1.
lrow[1] ^= ALPHA_ROW1;
1159 for (index = 8; index > 0; --index) {
1161 mantis_mix_columns(&state);
1164 mantis_shift_rows_inverse(&state);
1172 state.
lrow[0] ^= r[0];
1173 state.
lrow[1] ^= r[1];
1176 state.
lrow[0] = mantis_sbox(state.
lrow[0]);
1177 state.
lrow[1] = mantis_sbox(state.
lrow[1]);
1180 mantis_update_tweak_inverse(&tweak);
1185 state.
lrow[0] ^= st.k0prime[0] ^ k1.
lrow[0] ^ tweak.
lrow[0];
1186 state.
lrow[1] ^= st.k0prime[1] ^ k1.
lrow[1] ^ tweak.
lrow[1];
1189 uint32_t x = state.
lrow[0];
1190 output[0] = (uint8_t)x;
1191 output[1] = (uint8_t)(x >> 8);
1192 output[2] = (uint8_t)(x >> 16);
1193 output[3] = (uint8_t)(x >> 24);
1195 output[4] = (uint8_t)x;
1196 output[5] = (uint8_t)(x >> 8);
1197 output[6] = (uint8_t)(x >> 16);
1198 output[7] = (uint8_t)(x >> 24);
1199 #endif // !USE_AVR_INLINE_ASM
void swapModes()
Swaps the encryption/decryption mode for this Mantis block cipher.
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
size_t blockSize() const
Size of a Mantis-8 block in bytes.
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
virtual ~Mantis8()
Destroys this Mantis-8 block cipher object after clearing sensitive information.
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
void clear()
Clears all security-sensitive state from this block cipher.
bool setTweak(const uint8_t *tweak, size_t len)
Sets the 64-bit tweak value for this Mantis-8 block cipher.
size_t keySize() const
Size of a Mantis-8 key in bytes.
Mantis8()
Constructs a new Mantis-8 tweakable block cipher instance.
Union that describes a 64-bit 4x4 array of cells.