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