]> git.cryptolib.org Git - arm-crypto-lib.git/blob - present/present_common.c
improving present
[arm-crypto-lib.git] / present / present_common.c
1 /* present_common.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_common.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
32 uint8_t present_sbox(uint8_t b){
33         static const uint8_t sb[] = {
34                 0xC, 0x5, 0x6, 0xB,
35                 0x9, 0x0, 0xA, 0xD,
36                 0x3, 0xE, 0xF, 0x8,
37                 0x4, 0x7, 0x1, 0x2
38         };
39         return ((sb[b >> 4]) << 4) | (sb[b & 0xf]);
40 }
41
42 uint8_t present_sbox_inv(uint8_t b){
43         static const uint8_t sb[] = {
44                 0x5, 0xE, 0xF, 0x8,
45                 0xC, 0x1, 0x2, 0xD,
46                 0xB, 0x4, 0x6, 0x3,
47                 0x0, 0x7, 0x9, 0xA
48         };
49         return ((sb[b >> 4]) << 4) | (sb[b & 0xf]);
50 }
51
52 void present_p(uint8_t* o, uint8_t* i){
53         uint8_t m,n=0,idx=0;
54         for(m=0; m<64; ++m){
55                 o[idx] <<= 1;
56                 o[idx] |= i[n] >> 7;
57                 i[n] <<= 1;
58                 idx = (idx + 2) & 7;
59                 if((m & 7) == 7){
60                         ++n;
61                 }
62                 if(m == 31){
63                         idx += 1;
64                 }
65         }
66 }
67
68
69 void present_generic_enc(void* buffer, uint8_t* ctx, uint8_t ksize_B,
70         void(*update)(uint8_t*, uint8_t)){
71         uint8_t i,j,tmp[8], k[ksize_B];
72         memcpy(k, ctx, ksize_B);
73         memxor(buffer, k, 8);
74         for(i=1; i<32; ++i){
75                 j = 7;
76                 do{
77                         tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
78                 }while(j--);
79                 present_p(buffer, tmp);
80                 update(k, i);
81                 memxor(buffer, k, 8);
82         }
83 }
84
85 void present_generic_dec(void* buffer, uint8_t* ctx, uint8_t ksize_B,
86         void(*update)(uint8_t*, uint8_t)){
87         uint8_t j,tmp[8], k[ksize_B];
88         uint8_t i;
89         memcpy(k, ctx + ksize_B, ksize_B);
90         memxor(buffer, k, 8);
91         i = 31;
92         do{
93                 present_p(tmp, buffer);
94                 present_p(buffer, tmp);
95                 j = 7;
96                 do{
97                         ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
98                 }while(j--);
99                 update(k, i);
100                 memxor(buffer, k, 8);
101         }while(--i);
102 }
103