3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 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/>.
20 * a non-bitsliced implementation of the serpent sboxes
22 * email: daniel.otte@rub.de
27 #include <string.h> /* memset() */
28 #include <avr/pgmspace.h>
29 #include "serpent-sboxes.h"
31 uint8_t sbox[] PROGMEM = {
32 0x38, 0xF1, 0xA6, 0x5B, 0xED, 0x42, 0x70, 0x9C,
33 0xFC, 0x27, 0x90, 0x5A, 0x1B, 0xE8, 0x6D, 0x34,
34 0x86, 0x79, 0x3C, 0xAF, 0xD1, 0xE4, 0x0B, 0x52,
35 0x0F, 0xB8, 0xC9, 0x63, 0xD1, 0x24, 0xA7, 0x5E,
36 0x1F, 0x83, 0xC0, 0xB6, 0x25, 0x4A, 0x9E, 0x7D,
37 0xF5, 0x2B, 0x4A, 0x9C, 0x03, 0xE8, 0xD6, 0x71,
38 0x72, 0xC5, 0x84, 0x6B, 0xE9, 0x1F, 0xD3, 0xA0,
39 0x1D, 0xF0, 0xE8, 0x2B, 0x74, 0xCA, 0x93, 0x56,
40 /* now the inverted sboxes */
41 0xD3, 0xB0, 0xA6, 0x5C, 0x1E, 0x47, 0xF9, 0x82,
42 0x58, 0x2E, 0xF6, 0xC3, 0xB4, 0x79, 0x1D, 0xA0,
43 0xC9, 0xF4, 0xBE, 0x12, 0x03, 0x6D, 0x58, 0xA7,
44 0x09, 0xA7, 0xBE, 0x6D, 0x35, 0xC2, 0x48, 0xF1,
45 0x50, 0x83, 0xA9, 0x7E, 0x2C, 0xB6, 0x4F, 0xD1,
46 0x8F, 0x29, 0x41, 0xDE, 0xB6, 0x53, 0x7C, 0xA0,
47 0xFA, 0x1D, 0x53, 0x60, 0x49, 0xE7, 0x2C, 0x8B,
48 0x30, 0x6D, 0x9E, 0xF8, 0x5C, 0xB7, 0xA1, 0x42
52 #define SHR_O(a) c=(a)&1; ((a) = (a)>>1)
53 #define SHR_I(a) ((a) = (c?0x80:0x00)| ((a)>>1))
55 static void serpent_ip(uint32_t *i, uint8_t *o){
74 #define SHR_I(a) ((a) = (c?0x80000000L:0x00L)| ((a)>>1)) /* we use 32-bit words here */
76 static void serpent_fp(uint32_t *i, uint32_t *o){
94 /******************************************************************************/
95 static void sbox128x(uint8_t box, void* w){
101 t = pgm_read_byte(sbox + box*8 + i);
109 t = ((uint8_t*)o)[i];
113 ((uint8_t*)o)[i] = x;
115 serpent_fp((uint32_t*)o, w);
118 void sbox128(void * w, uint8_t box){
119 sbox128x(box&0x7, w);
123 void inv_sbox128(void * w, uint8_t box){
124 sbox128x(((box&0x7)|0x8), w);