]> git.cryptolib.org Git - avr-crypto-lib.git/blob - present/present.c
some fixes, mainly at rsaes-pkcs1v15
[avr-crypto-lib.git] / present / present.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.h"
31
32 static uint8_t sbox(uint8_t b){
33         uint8_t sb[]={0xC, 0x5, 0x6, 0xB, 
34                                   0x9, 0x0, 0xA, 0xD, 
35                                   0x3, 0xE, 0xF, 0x8, 
36                                   0x4, 0x7, 0x1, 0x2 };
37         return (((sb[b>>4])<<4)|(sb[b&0xf]));
38 }
39
40 static uint8_t sbox_inv(uint8_t b){
41         uint8_t sb[]={0x5, 0xE, 0xF, 0x8, 
42                                   0xC, 0x1, 0x2, 0xD, 
43                                   0xB, 0x4, 0x6, 0x3, 
44                                   0x0, 0x7, 0x9, 0xA };
45         return (((sb[b>>4])<<4)|(sb[b&0xf]));
46 }
47
48 #define SHR_O(a) c=(a)&1; (a)>>=1;
49 #define SHR_I(a) (a)=(c?0x8000:0x0000) | ((a)>>1);
50
51 static void p(uint16_t* o, uint8_t* i){
52         uint8_t c;
53         uint8_t m,n;
54         for(m=0; m<8; ++m){
55                 for(n=0; n<2; ++n){
56                         SHR_O(i[m]);
57                         SHR_I(o[0]);
58                         SHR_O(i[m]);
59                         SHR_I(o[1]);
60                         SHR_O(i[m]);
61                         SHR_I(o[2]);
62                         SHR_O(i[m]);
63                         SHR_I(o[3]);
64                 }
65         }
66 }
67
68 static void p_inv(uint8_t* o, uint8_t* i){
69         uint8_t tmp[8];
70         p((uint16_t*)tmp, i);
71         p((uint16_t*)o, tmp);
72 }
73
74 void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
75         uint8_t tmp[2];
76         union __attribute__((packed)) {
77                 uint8_t  v8[10];
78                 uint16_t v16[5];
79                 uint64_t v64;
80                 struct __attribute__((packed)) {
81                         uint8_t padding;
82                         union {
83                                 uint64_t v64;
84                                 uint16_t v16[4];
85                         } y;
86                 } x;    
87         } b;
88         uint8_t i;
89         memcpy(b.v8, key, 10);
90         memcpy(&(ctx->k[0]), b.v8+2, 8);
91         for(i=1; i<32; ++i){
92                 /* rotate buffer 19 right */
93                 memcpy(tmp, b.v8, 2);
94                 memmove(b.v8, b.v8+2, 8);
95                 memcpy(b.v8+8, tmp, 2);
96                  /* three shifts to do*/
97                 tmp[1]=b.v8[0];
98                 b.v64 >>= 3;
99                 b.v16[4] >>= 3;
100                 b.v8[9] |= tmp[1]<<5;
101                 b.v8[7] |= tmp[0]<<5;
102                 /* rotating done now substitution */
103                 b.v8[9] = (sbox(b.v8[9])&0xF0) | ((b.v8[9])&0x0F);
104                 /* xor with round counter */
105                 b.x.y.v16[0] ^= (uint16_t)i<<7;
106                 memcpy(&(ctx->k[i]), b.v8+2, 8);
107         }
108 }
109
110 void present_enc(void* buffer, present_ctx_t* ctx){
111         uint8_t i,j,tmp[8];
112         for(i=0; i<31; ++i){
113                 *((uint64_t*)buffer) ^= ctx->k[i];
114                  for(j=0; j<8; ++j){
115                         tmp[j] = sbox(((uint8_t*)buffer)[j]);
116                  }
117                  p((uint16_t*)buffer, tmp);
118         }
119         *((uint64_t*)buffer) ^= ctx->k[31];
120 }
121
122
123 void present_dec(void* buffer, present_ctx_t* ctx){
124         uint8_t j,tmp[8];
125         int8_t i;
126         *((uint64_t*)buffer) ^= ctx->k[31];
127
128         for(i=30; i>=0; --i){ 
129                 p_inv(tmp, (uint8_t*)buffer);
130                 for(j=0; j<8; ++j){
131                         ((uint8_t*)buffer)[j] = sbox_inv(tmp[j]);
132                 }
133                 *((uint64_t*)buffer) ^= ctx->k[i];
134         }
135 }