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 "present128.h"
35 void key_update_128(uint8_t* buffer, uint8_t round){
38 union __attribute__((packed)){
42 /* rotate buffer 67 right */
44 tmp.v8[0] = buffer[j];
45 buffer[j] = buffer[j + 8];
46 buffer[j + 8] = tmp.v8[0];
49 t8 = (uint16_t)buffer[15] << (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]);
58 /* xor with round counter */
59 buffer[8] ^= round << 6;
60 buffer[7] ^= round >> 2;
65 void key_update_128_inv(uint8_t* buffer, uint8_t round){
68 union __attribute__((packed)){
72 /* xor with round counter */
73 buffer[8] ^= round << 6;
74 buffer[7] ^= round >> 2;
76 /* rotating done now substitution */
77 buffer[0] = present_sbox_inv(buffer[0]);
79 /* rotate buffer 67 left */
81 tmp.v8[0] = buffer[j];
82 buffer[j] = buffer[j + 8];
83 buffer[j + 8] = tmp.v8[0];
86 t8 = (uint16_t)buffer[0] >> (5);
88 tmp.v8[0] = buffer[j];
90 buffer[j] = tmp.v8[0] | t8;
91 t8 = tmp.v8[1] & 0x07;
95 void present128_init(const uint8_t* key, uint8_t keysize_b, present128_ctx_t* ctx){
97 memcpy(ctx->fwd_key, key, 16);
98 memcpy(ctx->rev_key, key, 16);
100 key_update_128(ctx->rev_key, i);
104 void present128_enc(void* buffer, present128_ctx_t* ctx){
105 present_generic_enc(buffer, (uint8_t*)ctx, 16, key_update_128);
108 void present128_dec(void* buffer, present128_ctx_t* ctx){
109 present_generic_dec(buffer, (uint8_t*)ctx, 16, key_update_128_inv);
113 void present128_enc(void* buffer, present128_ctx_t* ctx){
114 uint8_t i,j,tmp[8], k[16];
115 memcpy(k, ctx->fwd_key, 16);
116 memxor(buffer, k, 8);
120 tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
122 present_p(buffer, tmp);
123 key_update_128(k, i);
124 memxor(buffer, k, 8);
128 void present128_dec(void* buffer, present128_ctx_t* ctx){
129 uint8_t j,tmp[8], k[16];
131 memcpy(k, ctx->rev_key, 16);
132 memxor(buffer, k, 8);
135 present_p(tmp, buffer);
136 present_p(buffer, tmp);
139 ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
141 key_update_128_inv(k, i);
142 memxor(buffer, k, 8);