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