]> git.cryptolib.org Git - avr-crypto-lib.git/blob - present/present128.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / present / present128.c
1 /* present128.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  * present128.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 "memxor.h"
32 #include "present_common.h"
33 #include "present128.h"
34
35 static
36 void key_update_128(uint8_t *buffer, uint8_t round){
37         uint8_t j;
38         uint8_t t8;
39         union __attribute__((packed)){
40                 uint8_t   v8[2];
41                 uint16_t v16;
42         } tmp;
43         /* rotate buffer 67 right */
44         for(j=0; j<8; ++j){
45                 tmp.v8[0] = buffer[j];
46                 buffer[j] = buffer[j + 8];
47                 buffer[j + 8] = tmp.v8[0];
48         }
49         j=0;
50         t8 = (uint16_t)buffer[15] << (5);
51         do{
52                 tmp.v8[1] = buffer[j];
53                 tmp.v16 >>= 3;
54                 buffer[j] = tmp.v8[1] | t8;
55                 t8 = tmp.v8[0] & 0xe0;
56         }while(++j<16);
57         /* rotating done now substitution */
58         buffer[0] = present_sbox(buffer[0]);
59         /* xor with round counter */
60         buffer[8] ^= round << 6;
61         buffer[7] ^= round >> 2;
62 }
63
64
65 static
66 void key_update_128_inv(uint8_t *buffer, uint8_t round){
67         uint8_t j;
68         uint8_t t8;
69         union __attribute__((packed)){
70                 uint8_t   v8[2];
71                 uint16_t v16;
72         } tmp;
73         /* xor with round counter */
74         buffer[8] ^= round << 6;
75         buffer[7] ^= round >> 2;
76
77         /* rotating done now substitution */
78         buffer[0] = present_sbox_inv(buffer[0]);
79
80         /* rotate buffer 67 left */
81         for(j=0; j<8; ++j){
82                 tmp.v8[0] = buffer[j];
83                 buffer[j] = buffer[j + 8];
84                 buffer[j + 8] = tmp.v8[0];
85         }
86         j=15;
87         t8 = (uint16_t)buffer[0] >> (5);
88         do{
89                 tmp.v8[0] = buffer[j];
90                 tmp.v16 <<= 3;
91                 buffer[j] = tmp.v8[0] | t8;
92                 t8 = tmp.v8[1] & 0x07;
93         }while(j--);
94 }
95
96 void present128_init(const uint8_t *key, uint8_t keysize_b, present128_ctx_t *ctx){
97         uint8_t i;
98         memcpy(ctx->fwd_key, key, 16);
99         memcpy(ctx->rev_key, key, 16);
100         for(i=1; i<32; ++i){
101                 key_update_128(ctx->rev_key, i);
102         }
103 }
104
105 void present128_enc(void *buffer, present128_ctx_t *ctx){
106         present_generic_enc(buffer, (uint8_t*)ctx, 16, key_update_128);
107 }
108
109 void present128_dec(void *buffer, present128_ctx_t *ctx){
110         present_generic_dec(buffer, (uint8_t*)ctx, 16, key_update_128_inv);
111 }
112
113 /*
114 void present128_enc(void *buffer, present128_ctx_t *ctx){
115         uint8_t i,j,tmp[8], k[16];
116         memcpy(k, ctx->fwd_key, 16);
117         memxor(buffer, k, 8);
118         for(i=1; i<32; ++i){
119                 j = 7;
120                 do{
121                         tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
122                 }while(j--);
123                 present_p(buffer, tmp);
124                 key_update_128(k, i);
125                 memxor(buffer, k, 8);
126         }
127 }
128
129 void present128_dec(void *buffer, present128_ctx_t *ctx){
130         uint8_t j,tmp[8], k[16];
131         uint8_t i;
132         memcpy(k, ctx->rev_key, 16);
133         memxor(buffer, k, 8);
134         i = 31;
135         do{
136                 present_p(tmp, buffer);
137                 present_p(buffer, tmp);
138                 j = 7;
139                 do{
140                         ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
141                 }while(j--);
142                 key_update_128_inv(k, i);
143                 memxor(buffer, k, 8);
144         }while(--i);
145 }
146 */