3 This file is part of the ARM-Crypto-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/>.
22 * email: daniel.otte@rub.de
32 #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
33 #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
34 uint8_t trivium_enc(trivium_ctx_t* ctx){
41 t1 ^= (G(90) & G(91)) ^ G(170);
42 t2 ^= (G(174) & G(175)) ^ G(263);
43 t3 ^= (G(285) & G(286)) ^ G(68);
45 /* shift whole state and insert ts later */
49 (*ctx)[i] = (((*ctx)[i])<<1)|c1;
60 uint8_t trivium_getbyte(trivium_ctx_t *ctx){
64 r |= trivium_enc(ctx);
69 #define KEYSIZE_B ((keysize_b+7)/8)
70 #define IVSIZE_B ((ivsize_b +7)/8)
72 static const uint8_t rev_table[16] = {
73 0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
74 0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
75 0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
76 0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
79 void trivium_init(const void* key, uint16_t keysize_b,
80 const void* iv, uint16_t ivsize_b,
85 memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
89 t1 = ((uint8_t*)key)[--c1];
90 t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
97 t1 = ((uint8_t*)iv)[--c1];
98 t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
102 for(i=12+IVSIZE_B; i>10; --i){
104 (*ctx)[i] = (((*ctx)[i])>>3)|c1;
110 for(i=0; i<4*288; ++i){