1 |
5 |
fanatid |
#include <stdio.h>
|
2 |
|
|
#include <string.h>
|
3 |
|
|
#include <stdint.h>
|
4 |
|
|
|
5 |
|
|
#include <openssl/rand.h>
|
6 |
|
|
|
7 |
|
|
#include "gost89.h"
|
8 |
|
|
|
9 |
|
|
gost_subst_block gost_sbox = {
|
10 |
|
|
{0x4,0xA,0x9,0x2,0xD,0x8,0x0,0xE,0x6,0xB,0x1,0xC,0x7,0xF,0x5,0x3},
|
11 |
|
|
{0xE,0xB,0x4,0xC,0x6,0xD,0xF,0xA,0x2,0x3,0x8,0x1,0x0,0x7,0x5,0x9},
|
12 |
|
|
{0x5,0x8,0x1,0xD,0xA,0x3,0x4,0x2,0xE,0xF,0xC,0x7,0x6,0x0,0x9,0xB},
|
13 |
|
|
{0x7,0xD,0xA,0x1,0x0,0x8,0x9,0xF,0xE,0x4,0x6,0xC,0xB,0x2,0x5,0x3},
|
14 |
|
|
{0x6,0xC,0x7,0x1,0x5,0xF,0xD,0x8,0x4,0xA,0x9,0xE,0x0,0x3,0xB,0x2},
|
15 |
|
|
{0x4,0xB,0xA,0x0,0x7,0x2,0x1,0xD,0x3,0x6,0x8,0x5,0x9,0xC,0xF,0xE},
|
16 |
|
|
{0xD,0xB,0x4,0x1,0x3,0xF,0x5,0x9,0x0,0xA,0xE,0x7,0x6,0x8,0x2,0xC},
|
17 |
|
|
{0x1,0xF,0xD,0x0,0x5,0x7,0xA,0x4,0x9,0x2,0x3,0xE,0x6,0xB,0x8,0xC}
|
18 |
|
|
};
|
19 |
|
|
|
20 |
|
|
uint8_t gost_key[] = {
|
21 |
|
|
0x04, 0x75, 0xF6, 0xE0, 0x50, 0x38, 0xFB, 0xFA,
|
22 |
|
|
0xD2, 0xC7, 0xC3, 0x90, 0xED, 0xB3, 0xCA, 0x3D,
|
23 |
|
|
0x15, 0x47, 0x12, 0x42, 0x91, 0xAE, 0x1E, 0x8A,
|
24 |
|
|
0x2F, 0x79, 0xCD, 0x9E, 0xD2, 0xBC, 0xEF, 0xBD
|
25 |
|
|
};
|
26 |
|
|
|
27 |
|
|
void print8bytes(const uint8_t* data) {
|
28 |
|
|
for (int i = 0; i < 8; ++i)
|
29 |
|
|
printf("%02x", data[i]);
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
void printNblocks(const uint8_t* data, size_t blocks) {
|
33 |
|
|
while (blocks--) {
|
34 |
|
|
print8bytes(&data[0]);
|
35 |
|
|
printf(" ");
|
36 |
|
|
data += 8;
|
37 |
|
|
}
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
int main(int argc, char **argv) {
|
41 |
|
|
int i;
|
42 |
|
|
|
43 |
|
|
printf("SBox: 512'h ");
|
44 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k1[i]<<4) | gost_sbox.k1[i+1]);
|
45 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k2[i]<<4) | gost_sbox.k2[i+1]);
|
46 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k3[i]<<4) | gost_sbox.k3[i+1]);
|
47 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k4[i]<<4) | gost_sbox.k4[i+1]);
|
48 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k5[i]<<4) | gost_sbox.k5[i+1]);
|
49 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k6[i]<<4) | gost_sbox.k6[i+1]);
|
50 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k7[i]<<4) | gost_sbox.k7[i+1]);
|
51 |
|
|
for(i = 0; i < 16; i+=2) printf("%02x", (gost_sbox.k8[i]<<4) | gost_sbox.k8[i+1]);
|
52 |
|
|
printf("\n\n");
|
53 |
|
|
|
54 |
|
|
printf("Key: 256'h ");
|
55 |
|
|
for (i = 0; i < 32; ++i)
|
56 |
|
|
printf("%02x", gost_key[i]);
|
57 |
|
|
printf("\n\n");
|
58 |
|
|
|
59 |
|
|
gost_ctx ctx;
|
60 |
|
|
gost_init(&ctx, &gost_sbox);
|
61 |
|
|
gost_set_key(&ctx, &gost_key[0]);
|
62 |
|
|
|
63 |
|
|
uint8_t gamma[8];
|
64 |
|
|
RAND_bytes(gamma, sizeof(gamma));
|
65 |
|
|
|
66 |
|
|
uint8_t in[24];
|
67 |
|
|
RAND_bytes(in, sizeof(in));
|
68 |
|
|
uint8_t out1[sizeof(in)];
|
69 |
|
|
uint8_t out2[sizeof(in)];
|
70 |
|
|
|
71 |
|
|
/* ECB mode */
|
72 |
|
|
printf("ECB mode:\n");
|
73 |
|
|
printNblocks(&in[0], sizeof(in)/8); printf("\n");
|
74 |
|
|
gost_ecb_encrypt(&ctx, &in[0], &out1[0], sizeof(in)/8);
|
75 |
|
|
printNblocks(&out1[0], sizeof(in)/8); printf("\n");
|
76 |
|
|
gost_ecb_decrypt(&ctx, &out1[0], &out2[0], sizeof(in)/8);
|
77 |
|
|
printNblocks(&out2[0], sizeof(in)/8); printf("\n");
|
78 |
|
|
if (memcmp((const void*) &in[0], (const void*) &out2[0], sizeof(in)))
|
79 |
|
|
printf("error!\n");
|
80 |
|
|
printf("\n");
|
81 |
|
|
|
82 |
|
|
/* CFB mode */
|
83 |
|
|
printf("CFB mode (gamma: "); print8bytes(&gamma[0]); printf("):\n");
|
84 |
|
|
printNblocks(&in[0], sizeof(in)/8); printf("\n");
|
85 |
|
|
gost_cfb_encrypt(&ctx, &gamma[0], &in[0], &out1[0], sizeof(in)/8);
|
86 |
|
|
printNblocks(&out1[0], sizeof(in)/8); printf("\n");
|
87 |
|
|
gost_cfb_decrypt(&ctx, &gamma[0], &out1[0], &out2[0], sizeof(in)/8);
|
88 |
|
|
printNblocks(&out2[0], sizeof(in)/8); printf("\n");
|
89 |
|
|
if (memcmp((const void*) &in[0], (const void*) &out2[0], sizeof(in)))
|
90 |
|
|
printf("error!\n");
|
91 |
|
|
printf("\n");
|
92 |
|
|
|
93 |
|
|
/* MAC mode */
|
94 |
|
|
printf("MAC mode (length: 32bit): \n");
|
95 |
|
|
printNblocks(&in[0], sizeof(in)/8); printf("\n");
|
96 |
|
|
memset((void*) &out1[0], 0, 8);
|
97 |
|
|
gost_mac(&ctx, &in[0], sizeof(in), &out1[0], 32);
|
98 |
|
|
print8bytes(&out1[0]); printf("\n\n");
|
99 |
|
|
|
100 |
|
|
return 0;
|
101 |
|
|
}
|