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