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
31 #include "present_common.h"
32 #include "present80.h"
35 void key_update(uint8_t* buffer, uint8_t round){
37 union __attribute__((packed)){
41 /* rotate buffer 19 right */
42 tmp.v16 = ((uint16_t*)buffer)[4];
45 ((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
47 ((uint16_t*)buffer)[0] = tmp.v16;
50 t8 = (uint16_t)buffer[9] << (5);
52 tmp.v8[1] = buffer[j];
54 buffer[j] = tmp.v8[1] | t8;
55 t8 = tmp.v8[0] & 0xe0;
57 /* rotating done now substitution */
58 buffer[0] = (present_sbox(buffer[0])&0xF0) | ((buffer[0])&0x0F);
59 /* xor with round counter */
60 buffer[8] ^= round << 7;
61 buffer[7] ^= round >> 1;
65 void key_update_inv(uint8_t* buffer, uint8_t round){
67 union __attribute__((packed)){
71 /* xor with round counter */
72 buffer[8] ^= round << 7;
73 buffer[7] ^= round >> 1;
74 /* rotating done now substitution */
75 buffer[0] = (present_sbox_inv(buffer[0])&0xF0) | ((buffer[0])&0x0F);
76 /* rotate buffer 19 left */
77 tmp.v16 = ((uint16_t*)buffer)[0];
80 ((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j+1];
82 ((uint16_t*)buffer)[4] = tmp.v16;
85 t8 = (uint16_t)buffer[0] >> (5);
87 tmp.v8[0] = buffer[j];
89 buffer[j] = tmp.v8[0] | t8;
90 t8 = tmp.v8[1] & 0x07;
94 void present80_init(const uint8_t* key, uint8_t keysize_b, present80_ctx_t* ctx){
96 memcpy(ctx->fwd_key, key, 10);
97 memcpy(ctx->rev_key, key, 10);
99 key_update(ctx->rev_key, i);
103 void present80_enc(void* buffer, present80_ctx_t* ctx){
104 present_generic_enc(buffer, (uint8_t*)ctx, 10, key_update);
107 void present80_dec(void* buffer, present80_ctx_t* ctx){
108 present_generic_dec(buffer, (uint8_t*)ctx, 10, key_update_inv);
112 void present80_enc(void* buffer, present80_ctx_t* ctx){
113 uint8_t i,j,tmp[8], k[10];
114 memcpy(k, ctx->fwd_key, 10);
115 memxor(buffer, k, 8);
119 tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
121 present_p(buffer, tmp);
123 memxor(buffer, k, 8);
127 void present80_dec(void* buffer, present80_ctx_t* ctx){
128 uint8_t j,tmp[8], k[10];
130 memcpy(k, ctx->rev_key, 10);
131 memxor(buffer, k, 8);
134 present_p(tmp, buffer);
135 present_p(buffer, tmp);
138 ((uint8_t*)buffer)[j] = sbox_inv(((uint8_t*)buffer)[j]);
140 key_update_inv(k, i);
141 memxor(buffer, k, 8);