-
Notifications
You must be signed in to change notification settings - Fork 0
/
kupyna_utils.cpp
50 lines (43 loc) · 1.56 KB
/
kupyna_utils.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
50
#include "kupyna_utils.h"
#include "kupyna_constants.h"
// SubBytes replaces each byte in the state with the corresponding byte from the S-Box
void SubBytes(uint8_t state[], size_t stateSize) {
for (size_t i = 0; i < stateSize; ++i) {
state[i] = KUPYNA_SBOX[state[i]];
}
}
// ShiftRows circularly shifts the bytes in each row of the state
void ShiftRows(uint8_t state[], size_t stateSize) {
// Assuming stateSize is a square matrix dimension (e.g., 64 for an 8x8 matrix)
size_t n = (size_t) sqrt(stateSize);
for (size_t row = 1; row < n; ++row) {
uint8_t tempRow[n];
for (size_t col = 0; col < n; ++col) {
tempRow[col] = state[row * n + (col + row) % n];
}
for (size_t col = 0; col < n; ++col) {
state[row * n + col] = tempRow[col];
}
}
}
// MixColumns mixes the columns of the state using the MDS matrix
void MixColumns(uint8_t state[], size_t stateSize) {
// Assuming stateSize is a square matrix dimension (e.g., 64 for an 8x8 matrix)
size_t n = (size_t) sqrt(stateSize);
uint8_t tempState[stateSize];
// Matrix multiplication
for (size_t row = 0; row < n; ++row) {
for (size_t col = 0; col < n; ++col) {
uint8_t sum = 0;
for (size_t k = 0; k < n; ++k) {
// Multiplication and addition in GF(2^8) go here
// ...
}
tempState[row * n + col] = sum;
}
}
// Copy tempState into state
for (size_t i = 0; i < stateSize; ++i) {
state[i] = tempState[i];
}
}