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 movw r30, r24 ; Z = a
176 movw r26, r22 ; X = b
205 for(i = 0; i < 5; ++i){
206 for(j = 0; j < 5; ++j){
207 a[j][i] ^= b[(4 + i) % 5][0];
212 /* a[0..4][0]{0..7} ^= b[4][0]{0..7} */
216 /* a[0..4][1]{0..7} ^= b[0][0]{0..7} */
217 subi XL, lo8(4 * 5 * 8 + 8)
218 sbci XH, hi8(4 * 5 * 8 + 8)
220 /* a[0..4][2]{0..7} ^= b[1][0]{0..7} */
223 /* a[0..4][3]{0..7} ^= b[2][0]{0..7} */
226 /* a[0..4][4]{0..7} ^= b[3][0]{0..7} */
230 for(i = 0; i < 5; ++i){
231 for(j = 0; j < 5; ++j){
232 a[j][i] ^= rotate64_1bit_left(b[(i + 1) % 5][0]);
236 /* a[0..4][0]{0..7} ^= rol(b[1][0]{0..7}) */
237 subi r24, lo8(5 * 8 - 1)
238 sbci r25, hi8(5 * 8 - 1)
239 subi XL, lo8(2 * 5 * 8 + 8)
240 sbci XH, hi8(2 * 5 * 8 + 8)
242 /* a[0..4][1]{0..7} ^= rol(b[2][0]{0..7}) */
245 /* a[0..4][21]{0..7} ^= rol(b[3][0]{0..7}) */
248 /* a[0..4][3]{0..7} ^= rol(b[4][0]{0..7}) */
251 /* a[0..4][4]{0..7} ^= rol(b[0][0]{0..7}) */
252 subi XL, lo8(4 * 5 * 8 + 8)
253 sbci XH, hi8(4 * 5 * 8 + 8)
259 for(i = 0; i < 5; ++i){
260 for(j = 0; j < 5; ++j){
261 b[(2 * i + 3 * j) % 5][j] =
262 rotate64left_code(a[j][i], pgm_read_byte(&(keccak_rotate_codes[i][j])));
268 const uint8_t* rot_code = (const uint8_t*)keccak_rotate_codes;
269 const uint8_t* idx_idx = (const uint8_t*)rho_pi_idx_table;
270 uint64_t *a_tmp = (uint64_t*)a;
271 for(i = 0; i < 25; ++i){
272 *((uint64_t*)(((uint8_t*)b) + pgm_read_byte(idx_idx++))) =
273 rotate64left_code(*a_tmp++, pgm_read_byte(rot_code++));
282 ldi r18, lo8(keccak_rotate_codes)
283 ldi r19, hi8(keccak_rotate_codes)
285 ldi r18, lo8(rho_pi_idx_table)
286 ldi r19, hi8(rho_pi_idx_table)
308 call rotate64left_code
328 for(i = 0; i < 5; ++i){
329 a[i][0] ^= ((~(b[i][1])) & (b[i][2]));
330 a[i][1] ^= ((~(b[i][2])) & (b[i][3]));
331 a[i][2] ^= ((~(b[i][3])) & (b[i][4]));
332 a[i][3] ^= ((~(b[i][4])) & (b[i][0]));
333 a[i][4] ^= ((~(b[i][0])) & (b[i][1]));
338 ; X points at b + 32 + 8 = b + 40 = b[1][0] has to point to b[0][0]
342 subi YL, lo8(5 * 5 * 8)
343 sbci YH, hi8(5 * 5 * 8)