]> git.cryptolib.org Git - avr-crypto-lib.git/blob - present.c
8e6dc78a7c84f1bb80417dbab854e2b1073fc155
[avr-crypto-lib.git] / present.c
1 /**
2  * present.c
3  * a implementation of the PRESENT block-cipher
4  * author: Daniel Otte
5  * email:  daniel.otte@rub.de
6  * license: GPLv3
7  * 
8  * */
9  
10 #include <string.h>
11 #include <stdint.h> 
12 #include "present.h"
13
14 static uint8_t sbox(uint8_t b){
15         uint8_t sb[]={0xC, 0x5, 0x6, 0xB, 
16                                   0x9, 0x0, 0xA, 0xD, 
17                                   0x3, 0xE, 0xF, 0x8, 
18                                   0x4, 0x7, 0x1, 0x2 };
19         return (((sb[b>>4])<<4)|(sb[b&0xf]));
20 }
21
22 static uint8_t sbox_inv(uint8_t b){
23         uint8_t sb[]={0x5, 0xE, 0xF, 0x8, 
24                                   0xC, 0x1, 0x2, 0xD, 
25                                   0xB, 0x4, 0x6, 0x3, 
26                                   0x0, 0x7, 0x9, 0xA };
27         return (((sb[b>>4])<<4)|(sb[b&0xf]));
28 }
29
30 #define SHR_O(a) c=(a)&1; (a)>>=1;
31 #define SHR_I(a) (a)=(c?0x8000:0x0000) | ((a)>>1);
32
33 static void p(uint16_t* o, uint8_t* i){
34         uint8_t c;
35         uint8_t m,n;
36         for(m=0; m<8; ++m){
37                 for(n=0; n<2; ++n){
38                         SHR_O(i[m]);
39                         SHR_I(o[0]);
40                         SHR_O(i[m]);
41                         SHR_I(o[1]);
42                         SHR_O(i[m]);
43                         SHR_I(o[2]);
44                         SHR_O(i[m]);
45                         SHR_I(o[3]);
46                 }
47         }
48 }
49
50 static void p_inv(uint8_t* o, uint8_t* i){
51         uint8_t tmp[8];
52         p((uint16_t*)tmp, i);
53         p((uint16_t*)o, tmp);
54 }
55
56 void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
57         uint8_t buffer[10], tmp[2];
58         uint8_t i;
59         memcpy(buffer, key, 10);
60         memcpy(&(ctx->k[0]), buffer+2, 8);
61         for(i=1; i<32; ++i){
62                 /* rotate buffer 19 right */
63                 memcpy(tmp, buffer, 2);
64                 memmove(buffer, buffer+2, 8);
65                 memcpy(buffer+8, tmp, 2);
66                  /* three shifts to do*/
67                 tmp[1]=buffer[0];
68                 *((uint64_t*)buffer)>>=3;
69                 *((uint16_t*)(buffer+8))>>=3;
70                 buffer[9] |= tmp[1]<<5;
71                 buffer[7] |= tmp[0]<<5;
72                 /* rotating done now substitution */
73                 buffer[9] = (sbox(buffer[9])&0xF0) | ((buffer[9])&0x0F);
74                 /* xor with round counter */
75                 *((uint16_t*)(buffer+1)) ^= (uint16_t)i<<7;
76                 memcpy(&(ctx->k[i]), buffer+2, 8);
77         }
78 }
79
80 void present_enc(void* buffer, present_ctx_t* ctx){
81         uint8_t i,j,tmp[8];
82         for(i=0; i<31; ++i){
83                 *((uint64_t*)buffer) ^= ctx->k[i];
84                  for(j=0; j<8; ++j){
85                         tmp[j] = sbox(((uint8_t*)buffer)[j]);
86                  }
87                  p((uint16_t*)buffer, tmp);
88         }
89         *((uint64_t*)buffer) ^= ctx->k[31];
90 }
91
92
93 void present_dec(void* buffer, present_ctx_t* ctx){
94         uint8_t j,tmp[8];
95         int8_t i;
96         *((uint64_t*)buffer) ^= ctx->k[31];
97
98         for(i=30; i>=0; --i){ 
99                 p_inv(tmp, (uint8_t*)buffer);
100                 for(j=0; j<8; ++j){
101                         ((uint8_t*)buffer)[j] = sbox_inv(tmp[j]);
102                 }
103                 *((uint64_t*)buffer) ^= ctx->k[i];
104         }
105 }