3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2012 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
29 #include "avr-asm-macros.S"
34 .global rho_pi_idx_table
38 .byte (((2 * \j + 3 * \i) % 5) * 5 + \i) * 8
44 void keccak_theta (uint64_t *a, uint64_t *b){
46 for(i = 0; i < 5; ++i){
47 b[i][0] = a[0][i] ^ a[1][i] ^ a[2][i] ^ a[3][i] ^ a[4][i];
52 /*********************************************
54 *********************************************
56 r24:r25 = a ; uint64_t a[5][5]
65 Z = r24:r25 + 7 + 4 * 40
85 /*********************************************
87 *********************************************
89 r24:r25 = a+1 ; uint64_t a[5][5]
92 a[0..4][0] ^= rol(b,1)
99 Z = r24:r25 + 7 + 4 * 40
112 .irp r, 0, 1, 2, 3, 4
123 add r19, __zero_reg__
126 .irp r, 0, 1, 2, 3, 4
138 ; a[i][j] = b[i][j] ^ ((~(b[i][(j + 1) % 5])) & (b[i][(j + 2) % 5]));
140 /*********************************************
142 *********************************************
148 a[0..7] ^= ~b[0..7] & c[0..7]
175 stack_alloc_large 200, r26, r27
180 movw r30, r24 ; Z = a
209 for(i = 0; i < 5; ++i){
210 for(j = 0; j < 5; ++j){
211 a[j][i] ^= b[(4 + i) % 5][0];
216 /* a[0..4][0]{0..7} ^= b[4][0]{0..7} */
220 /* a[0..4][1]{0..7} ^= b[0][0]{0..7} */
221 subi XL, lo8(4 * 5 * 8 + 8)
222 sbci XH, hi8(4 * 5 * 8 + 8)
224 /* a[0..4][2]{0..7} ^= b[1][0]{0..7} */
227 /* a[0..4][3]{0..7} ^= b[2][0]{0..7} */
230 /* a[0..4][4]{0..7} ^= b[3][0]{0..7} */
234 for(i = 0; i < 5; ++i){
235 for(j = 0; j < 5; ++j){
236 a[j][i] ^= rotate64_1bit_left(b[(i + 1) % 5][0]);
240 /* a[0..4][0]{0..7} ^= rol(b[1][0]{0..7}) */
241 subi r24, lo8(5 * 8 - 1)
242 sbci r25, hi8(5 * 8 - 1)
243 subi XL, lo8(2 * 5 * 8 + 8)
244 sbci XH, hi8(2 * 5 * 8 + 8)
246 /* a[0..4][1]{0..7} ^= rol(b[2][0]{0..7}) */
249 /* a[0..4][21]{0..7} ^= rol(b[3][0]{0..7}) */
252 /* a[0..4][3]{0..7} ^= rol(b[4][0]{0..7}) */
255 /* a[0..4][4]{0..7} ^= rol(b[0][0]{0..7}) */
256 subi XL, lo8(4 * 5 * 8 + 8)
257 sbci XH, hi8(4 * 5 * 8 + 8)
263 for(i = 0; i < 5; ++i){
264 for(j = 0; j < 5; ++j){
265 b[(2 * i + 3 * j) % 5][j] =
266 rotate64left_code(a[j][i], pgm_read_byte(&(keccak_rotate_codes[i][j])));
272 const uint8_t* rot_code = (const uint8_t*)keccak_rotate_codes;
273 const uint8_t* idx_idx = (const uint8_t*)rho_pi_idx_table;
274 uint64_t *a_tmp = (uint64_t*)a;
275 for(i = 0; i < 25; ++i){
276 *((uint64_t*)(((uint8_t*)b) + pgm_read_byte(idx_idx++))) =
277 rotate64left_code(*a_tmp++, pgm_read_byte(rot_code++));
286 ldi r18, lo8(keccak_rotate_codes)
287 ldi r19, hi8(keccak_rotate_codes)
289 ldi r18, lo8(rho_pi_idx_table)
290 ldi r19, hi8(rho_pi_idx_table)
312 call rotate64left_code
332 for(i = 0; i < 5; ++i){
333 a[i][0] ^= ((~(b[i][1])) & (b[i][2]));
334 a[i][1] ^= ((~(b[i][2])) & (b[i][3]));
335 a[i][2] ^= ((~(b[i][3])) & (b[i][4]));
336 a[i][3] ^= ((~(b[i][4])) & (b[i][0]));
337 a[i][4] ^= ((~(b[i][0])) & (b[i][1]));
342 ; X points at b + 32 + 8 = b + 40 = b[1][0] has to point to b[0][0]
346 subi YL, lo8(5 * 5 * 8)
347 sbci YH, hi8(5 * 5 * 8)
376 ldi r30, lo8(keccak_rc_comp)
377 ldi r31, hi8(keccak_rc_comp)
379 adc r31, __zero_reg__
415 stack_free_large3 200