]> git.cryptolib.org Git - arm-crypto-lib.git/blob - present/present_speed.c
improving present
[arm-crypto-lib.git] / present / present_speed.c
1 /* present.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /**
20  * present.c
21  * a implementation of the PRESENT block-cipher
22  * author: Daniel Otte
23  * email:  daniel.otte@rub.de
24  * license: GPLv3
25  * 
26  * */
27  
28 #include <string.h>
29 #include <stdint.h> 
30 #include "present_speed.h"
31
32 static
33 void key_update(uint8_t* buffer, uint8_t round){
34         uint8_t j;
35         union __attribute__((packed)){
36                 uint8_t   v8[2];
37                 uint16_t v16;
38         } tmp;
39         /* rotate buffer 19 right */
40         tmp.v16 = ((uint16_t*)buffer)[4];
41         j=4;
42         do{
43                 ((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
44         }while(--j);
45         ((uint16_t*)buffer)[0] = tmp.v16;
46         uint8_t t8;
47         j=0;
48         t8 = (uint16_t)buffer[9] << (5);
49         do{
50                 tmp.v8[1] = buffer[j];
51                 tmp.v16 >>= 3;
52                 buffer[j] = tmp.v8[1] | t8;
53                 t8 = tmp.v8[0] & 0xe0;
54         }while(++j<10);
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;
60 }
61
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);
66         for(i=1; i<32; ++i){
67                 key_update(key_buffer, i);
68                 memcpy(&(ctx->k[i]), key_buffer, 8);
69         }
70 }
71
72
73 void present_enc(void* buffer, present_ctx_t* ctx){
74         uint8_t i,j,tmp[8];
75         for(i=0; i<31; ++i){
76                 *((uint64_t*)buffer) ^= ctx->k[i];
77                 memxor(buffer, &ctx->k[i], 8);
78                 j = 7;
79                 do{
80                         tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
81                 }while(j--);
82                 present_p(buffer, tmp);
83         }
84         memxor(buffer, &ctx->k[31], 8);
85 }
86
87
88 void present_dec(void* buffer, present_ctx_t* ctx){
89         uint8_t j,tmp[8];
90         uint8_t i;
91         memxor(buffer, &ctx->k[31], 8);
92         i = 30;
93         do{
94                 present_p(tmp, buffer);
95                 present_p(buffer, tmp);
96                 j = 7;
97                 do{
98                         ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
99                 }while(j--);
100                 memxor(buffer, &ctx->k[i], 8);
101         }while(i--);
102 }