forked from hadipourh/KeeLoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeloq.c
executable file
·44 lines (37 loc) · 1.19 KB
/
keeloq.c
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
#include "keeloq.h"
uint8_t nlf(uint8_t d)
{
return (((uint32_t)(NLF_LOOKUP_CONSTANT) >> d) & 0x1);
}
void keeloq_encrypt(uint8_t *key, uint32_t *data, const uint16_t nrounds)
{
uint32_t x;
uint16_t loop;
uint8_t o, nlf_input,k,ki;
for (loop = 0; loop < nrounds; loop++)
{
nlf_input = (((*data >> 31) & 0x1) << 4) | (((*data >> 26) & 0x1) << 3) |
(((*data >> 20) & 0x1) << 2) | (((*data >> 9) & 0x1) << 1) | ((*data >> 1) & 0x1);
o = nlf(nlf_input);
ki=loop % 64;
k=key[ki/8]>>(ki%8);
x =k ^ (*data >> 16) ^ *data ^ o;
*data = (*data >> 1) | (x << 31);
}
}
void keeloq_decrypt(uint8_t *key, uint32_t *data,const uint16_t nrounds)
{
uint32_t x;
uint16_t loop;
uint8_t o, nlf_input,k,ki;
for (loop = 0; loop < nrounds; loop++)
{
nlf_input = (((*data >> 30) & 0x1) << 4) | (((*data >> 25) & 0x1) << 3) |
(((*data >> 19) & 0x1) << 2) | (((*data >> 8) & 0x1) << 1) | (*data & 0x1);
o = nlf(nlf_input);
ki=(uint16_t)(15 - loop) % 64;
k=key[ki/8]>>(ki%8);
x = k ^ (*data >> 31) ^ (*data >> 15) ^ o;
*data = (*data << 1) | x&1;
}
}