3 This file is part of the AVR-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/>.
21 * a implementation of the PRESENT block-cipher
23 * email: daniel.otte@rub.de
30 #include "present_speed.h"
33 void key_update(uint8_t* buffer, uint8_t round){
35 union __attribute__((packed)){
39 /* rotate buffer 19 right */
40 tmp.v16 = ((uint16_t*)buffer)[4];
43 ((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
45 ((uint16_t*)buffer)[0] = tmp.v16;
48 t8 = (uint16_t)buffer[9] << (5);
50 tmp.v8[1] = buffer[j];
52 buffer[j] = tmp.v8[1] | t8;
53 t8 = tmp.v8[0] & 0xe0;
55 /* rotating done now substitution */
56 buffer[0] = (present_sbox(buffer[0])&0xF0) | ((buffer[0])&0x0F);
57 /* xor with round counter */
58 buffer[8] ^= round << 7;
59 buffer[7] ^= round >> 1;
62 void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
63 uint8_t i,key_buffer[10];
64 memcpy(key_buffer, key, 10);
65 memcpy(&(ctx->k[0]), key_buffer, 8);
67 key_update(key_buffer, i);
68 memcpy(&(ctx->k[i]), key_buffer, 8);
73 void present_enc(void* buffer, present_ctx_t* ctx){
76 *((uint64_t*)buffer) ^= ctx->k[i];
77 memxor(buffer, &ctx->k[i], 8);
80 tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
82 present_p(buffer, tmp);
84 memxor(buffer, &ctx->k[31], 8);
88 void present_dec(void* buffer, present_ctx_t* ctx){
91 memxor(buffer, &ctx->k[31], 8);
94 present_p(tmp, buffer);
95 present_p(buffer, tmp);
98 ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
100 memxor(buffer, &ctx->k[i], 8);