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