-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmagma_calc.c
More file actions
177 lines (164 loc) · 5.52 KB
/
Copy pathmagma_calc.c
File metadata and controls
177 lines (164 loc) · 5.52 KB
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
177
#include "magma_calc.h"
#ifdef DEBUG_MODE
static void magma_print_debug_4_bytes(const uint8_t* state) {
for (int i = 0; i < 4; i++) {
printf("%02x", state[i]);
}
printf("\n");
}
static void magma_print_debug_8_bytes(const uint8_t* state) {
for (int i = 0; i < 8; i++) {
printf("%02x", state[i]);
}
printf("\n");
}
#endif
static void magma_t_transformation(const uint8_t* in_data, uint8_t* out_data) {
uint8_t first_part_byte, sec_part_byte;
for (int i = 3; i >= 0; i--) {
first_part_byte = (in_data[i] & 0x0f);
sec_part_byte = (in_data[i] & 0xf0) >> 4;
first_part_byte = pi[(3 - i) * 2] [first_part_byte];
sec_part_byte = pi[(3 - i) * 2 + 1] [sec_part_byte];
out_data[i] = (sec_part_byte << 4) | first_part_byte;
}
}
static void magma_xor(const uint8_t* a, const uint8_t* b, uint8_t* c) {
for (int i = 0; i < 4; i++) {
c[i] = a[i] ^ b[i];
}
}
static void magma_xor_ring_32(const uint8_t* a, const uint8_t* b, uint8_t* c) {
uint32_t internal = 0;
for (int i = 3; i >= 0; i--) {
internal = a[i] + b[i] + (internal >> 8);
c[i] = internal & 0xff;
}
}
static void magma_g_transformation(const uint8_t* k, const uint8_t* a, uint8_t* out_data) {
uint8_t internal[4];
uint32_t out_data_32;
magma_xor_ring_32(a, k, internal);
magma_t_transformation(internal, internal);
out_data_32 = internal[0];
out_data_32 = (out_data_32 << 8) + internal[1];
out_data_32 = (out_data_32 << 8) + internal[2];
out_data_32 = (out_data_32 << 8) + internal[3];
out_data_32 = (out_data_32 << 11)|(out_data_32 >> 21);
out_data[3] = out_data_32 & 0xff;
out_data[2] = (out_data_32 >> 8) & 0xff;
out_data[1] = (out_data_32 >> 16) & 0xff;
out_data[0] = (out_data_32 >> 24) & 0xff;
}
static void magma_g_round_transformation(const uint8_t* k, const uint8_t* a, uint8_t* out_data) {
uint8_t a_0[4];
uint8_t a_1[4];
uint8_t g[4];
for(int i = 0; i < 4; i++) {
a_1[i] = a[i];
a_0[i] = a[4 + i];
}
magma_g_transformation(k, a_0, g);
magma_xor(a_1, g, g);
for(int i = 0; i < 4; i++) {
a_1[i] = a_0[i];
a_0[i] = g[i];
}
for(int i = 0; i < 4; i++) {
out_data[i] = a_1[i];
out_data[4 + i] = a_0[i];
}
}
static void magma_g_final_transformation(const uint8_t* k, const uint8_t* a, uint8_t* out_data) {
uint8_t a_0[4];
uint8_t a_1[4];
uint8_t g[4];
for(int i = 0; i < 4; i++) {
a_1[i] = a[i];
a_0[i] = a[4 + i];
}
magma_g_transformation(k, a_0, g);
magma_xor(a_1, g, g);
for(int i = 0; i < 4; i++) {
a_1[i] = g[i];
}
for(int i = 0; i < 4; i++) {
out_data[i] = a_1[i];
out_data[4 + i] = a_0[i];
}
}
void magma_init(magma_ctx *ctx, const uint8_t *key)
{
memcpy(ctx->iter_key[0], key, 4);
memcpy(ctx->iter_key[1], key + 4, 4);
memcpy(ctx->iter_key[2], key + 8, 4);
memcpy(ctx->iter_key[3], key + 12, 4);
memcpy(ctx->iter_key[4], key + 16, 4);
memcpy(ctx->iter_key[5], key + 20, 4);
memcpy(ctx->iter_key[6], key + 24, 4);
memcpy(ctx->iter_key[7], key + 28, 4);
memcpy(ctx->iter_key[8], key, 4);
memcpy(ctx->iter_key[9], key + 4, 4);
memcpy(ctx->iter_key[10], key + 8, 4);
memcpy(ctx->iter_key[11], key + 12, 4);
memcpy(ctx->iter_key[12], key + 16, 4);
memcpy(ctx->iter_key[13], key + 20, 4);
memcpy(ctx->iter_key[14], key + 24, 4);
memcpy(ctx->iter_key[15], key + 28, 4);
memcpy(ctx->iter_key[16], key, 4);
memcpy(ctx->iter_key[17], key + 4, 4);
memcpy(ctx->iter_key[18], key + 8, 4);
memcpy(ctx->iter_key[19], key + 12, 4);
memcpy(ctx->iter_key[20], key + 16, 4);
memcpy(ctx->iter_key[21], key + 20, 4);
memcpy(ctx->iter_key[22], key + 24, 4);
memcpy(ctx->iter_key[23], key + 28, 4);
memcpy(ctx->iter_key[24], key + 28, 4);
memcpy(ctx->iter_key[25], key + 24, 4);
memcpy(ctx->iter_key[26], key + 20, 4);
memcpy(ctx->iter_key[27], key + 16, 4);
memcpy(ctx->iter_key[28], key + 12, 4);
memcpy(ctx->iter_key[29], key + 8, 4);
memcpy(ctx->iter_key[30], key + 4, 4);
memcpy(ctx->iter_key[31], key, 4);
#ifdef DEBUG_MODE
printf("Iteration cipher keys:\n");
int i;
for (i = 0; i < 32; i++) {
printf("K%d=", i+1);
magma_print_debug_4_bytes(ctx->iter_key[i]);
}
#endif
}
void magma_encrypt(magma_ctx* ctx, const uint8_t *blk, uint8_t *out_blk)
{
#ifdef DEBUG_MODE
printf("Text:\n");
magma_print_debug_8_bytes(blk);
#endif
magma_g_round_transformation(ctx->iter_key[0], blk, out_blk);
for(int i = 1; i < 31; i++) {
magma_g_round_transformation(ctx->iter_key[i], out_blk, out_blk);
}
magma_g_final_transformation(ctx->iter_key[31], out_blk, out_blk);
#ifdef DEBUG_MODE
printf("Encrypted text:\n");
magma_print_debug_8_bytes(out_blk);
#endif
}
void magma_decrypt(magma_ctx *ctx, const uint8_t *blk, uint8_t *out_blk)
{
#ifdef DEBUG_MODE
printf("Gipher text:\n");
magma_print_debug_8_bytes(blk);
#endif
magma_g_round_transformation(ctx->iter_key[31], blk, out_blk);
for(int i = 30; i > 0; i--) {
magma_g_round_transformation(ctx->iter_key[i], out_blk, out_blk);
}
magma_g_final_transformation(ctx->iter_key[0], out_blk, out_blk);
#ifdef DEBUG_MODE
printf("Decrypted text:\n");
magma_print_debug_8_bytes(out_blk);
#endif
}