-
Notifications
You must be signed in to change notification settings - Fork 3
/
speck_32_64.cpp
49 lines (44 loc) · 1.24 KB
/
speck_32_64.cpp
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
// Speck 32/64
#include <stdio.h>
#include <inttypes.h>
#define ROUNDS 22
static inline void speck_round(uint16_t& x, uint16_t& y, const uint16_t k)
{
x = (x >> 7) | (x << (8 * sizeof(x) - 7)); // x = ROTR(x, 7)
x += y;
x ^= k;
y = (y << 2) | (y >> (8 * sizeof(y) - 2)); // y = ROTL(y, 2)
y ^= x;
}
void speck_block( const uint16_t plaintext[2]
, const uint16_t key[4]
, uint16_t ciphertext[2]
)
{
ciphertext[0] = plaintext[0];
ciphertext[1] = plaintext[1];
uint16_t b = key[0];
uint16_t a0 = key[1];
uint16_t a1 = key[2];
uint16_t a2 = key[3];
for (unsigned i = 0; i < ROUNDS; i++) {
speck_round(ciphertext[1], ciphertext[0], b);
uint16_t a = a0;
speck_round(a, b, i);
a0 = a1;
a1 = a2;
a2 = a;
}
}
int main(void)
{
uint16_t plaintext[2] = {0x694c, 0x6574};
uint16_t key[4] = {0x0100, 0x0908, 0x1110, 0x1918};
uint16_t ciphertext[2];
speck_block(plaintext, key, ciphertext);
printf("Plaintext: 0x%04x 0x%04x\n", plaintext[0], plaintext[1]);
printf("Key: 0x%04x 0x%04x 0x%04x 0x%04x\n", key[0], key[1], key[2], key[3]);
printf("Ciphertext: 0x%04x 0x%04x\n", ciphertext[0], ciphertext[1]);
printf("Expected: 0x42f2 0xa868\n");
return 0;
}