fb11e647
vrobic
reseau statique a...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright (C) 2013 Freie Universitรคt Berlin, Computer Systems & Telematics
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file
* @brief implementation of the RC5 cipher-algorithm
*
* @author Nicolai Schmittberger <nicolai.schmittberger@fu-berlin.de>
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @author Naveen Sastry
*
* @}
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "crypto/rc5.h"
#include "crypto/ciphers.h"
/**
* Define a fixed blocksize of 8 bytes
*/
#define BLOCK_SIZE (8U)
/**
* @brief Interface to the rc5 cipher
*/
static const cipher_interface_t rc5_interface = {
BLOCK_SIZE,
CIPHERS_MAX_KEY_SIZE,
rc5_init,
rc5_encrypt,
rc5_decrypt
};
const cipher_id_t CIPHER_RC5 = &rc5_interface;
int rc5_encrypt(const cipher_context_t *context, const uint8_t *block,
uint8_t *cipherBlock)
{
register uint32_t l;
register uint32_t r;
rc5_context_t *rc5_context = (rc5_context_t *) context->context;
register uint32_t *s = rc5_context->skey;
uint8_t i;
c2l(block, l);
block += 4;
c2l(block, r);
l += *s++;
r += *s++;
for (i = RC5_ROUNDS; i > 0; i--) {
l ^= r;
uint8_t tmp = r;
tmp &= 0x1f;
rotl32(l, tmp);
l += *s++;
r ^= l;
tmp = l;
tmp &= 0x1f;
rotl32(r, tmp);
r += *s++;
}
l2c(l, cipherBlock);
cipherBlock += 4;
l2c(r, cipherBlock);
return 1;
}
int rc5_decrypt(const cipher_context_t *context, const uint8_t *cipherBlock,
uint8_t *plainBlock)
{
register uint32_t l;
register uint32_t r;
rc5_context_t *rc5_context = (rc5_context_t *) context->context;
register uint32_t *s = rc5_context->skey + (2 * RC5_ROUNDS) + 1;
uint8_t i;
c2l(cipherBlock, l);
cipherBlock += 4;
c2l(cipherBlock, r);
for (i = RC5_ROUNDS; i > 0; i--) {
r -= *s--;
uint8_t tmp = l;
tmp &= 0x1f;
rotr32(r, tmp);
r ^= l;
l -= *s--;
tmp = r;
tmp &= 0x1f;
rotr32(l, tmp);
l ^= r;
}
r -= *s--;
l -= *s;
l2c(l, plainBlock);
plainBlock += 4;
l2c(r, plainBlock);
return 1;
}
int rc5_init(cipher_context_t *context, const uint8_t *key, uint8_t keySize)
{
(void) keySize;
uint32_t *L, l, A, B, *S;
uint8_t ii, jj;
int8_t i;
uint8_t tmp[8];
// Make sure that context is large enough. If this is not the case,
// you should build with -DRC5.
if(CIPHER_MAX_CONTEXT_SIZE < RC5_CONTEXT_SIZE) {
return 0;
}
rc5_context_t *rc5_context = (rc5_context_t *) context->context;
S = rc5_context->skey;
//dumpBuffer ("RC5M:setupKey K", (uint8_t *)key, 8);
c2l(key, l);
L = (uint32_t *) tmp;
L[0] = l;
key += 4;
c2l(key, l);
L[1] = l;
S[0] = RC5_32_P;
//dumpBuffer ("RC5M:setupKey L", (uint8_t *)L, 8);
for (i = 1; i < 2 * RC5_ROUNDS + 2; i++) {
S[i] = (S[i - 1] + RC5_32_Q);
/* sum =(*S+RC5_32_Q)&RC5_32_MASK;
* S++;
* S = sum;
*/
}
//dumpBuffer ("RC5M: setupKey S", (uint8_t *)S, 2 * (RC5_ROUNDS +1) * 4);
ii = jj = 0;
A = B = 0;
S = rc5_context->skey;
for (i = 3 * (2 * RC5_ROUNDS + 2) - 1; i >= 0; i--) {
uint32_t k = (*S + A + B)&RC5_32_MASK;
rotl32((k), (3));
A = *S = k;
S++;
uint8_t m = ((char)(A + B)) & 0x1f;
k = (*L + A + B)&RC5_32_MASK;
rotl32((k), (m));
B = *L = k;
if (++ii >= 2 * RC5_ROUNDS + 2) {
ii = 0;
S = rc5_context->skey;
}
jj ^= 4;
L = (uint32_t *)(&tmp[jj]);
}
return 1;
}
|