3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2011 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/>.
65 3x Fx Ex 0x 5x 4x Bx Cx Dx Ax 9x 6x 7x 8x 2x 1x
67 9x Ex 5x 6x Ax 2x 3x Cx Fx 0x 4x Dx 7x Bx 1x 8x
70 static const uint8_t pq_lut[16] = {
71 0x39, 0xFE, 0xE5, 0x06, 0x5A, 0x42, 0xB3, 0xCC,
72 0xDF, 0xA0, 0x94, 0x6D, 0x77, 0x8B, 0x21, 0x18
75 uint8_t khazad_sbox(uint8_t a){
77 b = pq_lut[a>>4] & 0xf0;
78 c = pq_lut[a&0xf] & 0x0f;
83 b = pq_lut[b>>4] << 4;
84 c = pq_lut[c&0xf] >> 4;
89 b = pq_lut[b>>4] & 0xf0;
90 c = pq_lut[c&0xf] & 0x0f;
94 static void gamma_x(uint8_t* a){
102 /******************************************************************************/
103 /* p8 (x) = x^8 + x^4 + x^3 + x^2 + 1 */
107 * 01x 03x 04x 05x 06x 08x 0Bx 07x
108 * 03x 01x 05x 04x 08x 06x 07x 0Bx
109 * 04x 05x 01x 03x 0Bx 07x 06x 08x
110 * 05x 04x 03x 01x 07x 0Bx 08x 06x
111 * 06x 08x 0Bx 07x 01x 03x 04x 05x
112 * 08x 06x 07x 0Bx 03x 01x 05x 04x
113 * 0Bx 07x 06x 08x 04x 05x 01x 03x
114 * 07x 0Bx 08x 06x 05x 04x 03x 01x
117 static const uint8_t h[8][4] = {
118 { 0x13, 0x45, 0x68, 0xB7 },
119 { 0x31, 0x54, 0x86, 0x7B },
120 { 0x45, 0x13, 0xB7, 0x68 },
121 { 0x54, 0x31, 0x7B, 0x86 },
122 { 0x68, 0xB7, 0x13, 0x45 },
123 { 0x86, 0x7B, 0x31, 0x54 },
124 { 0xB7, 0x68, 0x45, 0x13 },
125 { 0x7B, 0x86, 0x54, 0x31 }
128 static void theta(uint8_t* a){
137 accu ^= gf256mul(*a++, x>>4, POLYNOM);
138 accu ^= gf256mul(*a++, x&0xf, POLYNOM);
146 /******************************************************************************/
148 static void khazad_round(uint8_t* a, const uint8_t* k){
154 /******************************************************************************/
156 void khazad_init(const void* key, khazad_ctx_t* ctx){
160 c[i] = khazad_sbox(r*8+i);
162 memcpy(ctx->k[r], (uint8_t*)key+8, 8);
163 khazad_round(ctx->k[r], c);
164 memxor(ctx->k[r], (uint8_t*)key, 8);
167 c[i] = khazad_sbox(r*8+i);
169 memcpy(ctx->k[r], ctx->k[r-1], 8);
170 khazad_round(ctx->k[r], c);
171 memxor(ctx->k[r], (uint8_t*)key+8, 8);
174 c[i] = khazad_sbox(r*8+i);
176 memcpy(ctx->k[r], ctx->k[r-1], 8);
177 khazad_round(ctx->k[r], c);
178 memxor(ctx->k[r], ctx->k[r-2], 8);
182 /******************************************************************************/
184 void khazad_enc(void* buffer, const khazad_ctx_t* ctx){
186 memxor(buffer, ctx->k[0], 8);
188 khazad_round(buffer, ctx->k[r]);
191 memxor(buffer, ctx->k[8], 8);
194 /******************************************************************************/
196 void khazad_dec(void* buffer, const khazad_ctx_t* ctx){
198 memxor(buffer, ctx->k[8], 8);
201 memxor(buffer, ctx->k[r--], 8);
205 memxor(buffer, ctx->k[0], 8);