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 <avr/pgmspace.h>
31 #include "present_speed.h"
34 void key_update(uint8_t *buffer, uint8_t round){
36 union __attribute__((packed)){
40 /* rotate buffer 19 right */
41 tmp.v16 = ((uint16_t*)buffer)[4];
44 ((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
46 ((uint16_t*)buffer)[0] = tmp.v16;
49 t8 = (uint16_t)buffer[9] << (5);
51 tmp.v8[1] = buffer[j];
53 buffer[j] = tmp.v8[1] | t8;
54 t8 = tmp.v8[0] & 0xe0;
56 /* rotating done now substitution */
57 buffer[0] = (present_sbox(buffer[0])&0xF0) | ((buffer[0])&0x0F);
58 /* xor with round counter */
59 buffer[8] ^= round << 7;
60 buffer[7] ^= round >> 1;
63 void present_init(const uint8_t *key, uint8_t keysize_b, present_ctx_t *ctx){
64 uint8_t i,key_buffer[10];
65 memcpy(key_buffer, key, 10);
66 memcpy(&(ctx->k[0]), key_buffer, 8);
68 key_update(key_buffer, i);
69 memcpy(&(ctx->k[i]), key_buffer, 8);
74 void present_enc(void *buffer, present_ctx_t *ctx){
77 *((uint64_t*)buffer) ^= ctx->k[i];
78 memxor(buffer, &ctx->k[i], 8);
81 tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
83 present_p(buffer, tmp);
85 memxor(buffer, &ctx->k[31], 8);
89 void present_dec(void *buffer, present_ctx_t *ctx){
92 memxor(buffer, &ctx->k[31], 8);
95 present_p(tmp, buffer);
96 present_p(buffer, tmp);
99 ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
101 memxor(buffer, &ctx->k[i], 8);