-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkupyna_kmac.c
152 lines (128 loc) · 5.55 KB
/
kupyna_kmac.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
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
#include <string.h>
#include "kupyna_kmac.h"
static const uint8_t dpad[128] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t kpad32[32] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t kpad48[80] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t kpad64[64] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void kupyna256_kmac(const uint8_t* key, const uint8_t* data, size_t len, uint8_t* mac)
{
struct kupyna256_kmac_ctx_t ctx;
kupyna256_kmac_init(&ctx, key);
kupyna256_kmac_update(&ctx, data, len);
kupyna256_kmac_final(&ctx, mac);
}
void kupyna384_kmac(const uint8_t* key, const uint8_t* data, size_t len, uint8_t* mac)
{
struct kupyna384_kmac_ctx_t ctx;
kupyna384_kmac_init(&ctx, key);
kupyna384_kmac_update(&ctx, data, len);
kupyna384_kmac_final(&ctx, mac);
}
void kupyna512_kmac(const uint8_t* key, const uint8_t* data, size_t len, uint8_t* mac)
{
struct kupyna512_kmac_ctx_t ctx;
kupyna512_kmac_init(&ctx, key);
kupyna512_kmac_update(&ctx, data, len);
kupyna512_kmac_final(&ctx, mac);
}
void kupyna256_kmac_init(struct kupyna256_kmac_ctx_t* ctx, const uint8_t* key)
{
kupyna256_init(&ctx->hctx);
kupyna256_update(&ctx->hctx, key, 32);
kupyna256_update(&ctx->hctx, kpad32, sizeof(kpad32));
ctx->len = 0;
for (size_t i=0; i<32; ++i) {
ctx->ik[i] = ~key[i];
}
}
void kupyna256_kmac_update(struct kupyna256_kmac_ctx_t* ctx, const uint8_t* data, size_t len)
{
ctx->len += len;
kupyna256_update(&ctx->hctx, data, len);
}
void kupyna256_kmac_final(struct kupyna256_kmac_ctx_t* ctx, uint8_t* mac)
{
uint64_t n = ctx->len;
size_t pad_size = n < 52 ? (51 - n) : (63 - ((n - 52) % 64));
n = n * 8;
kupyna256_update(&ctx->hctx, dpad, pad_size + 1);
kupyna256_update(&ctx->hctx, (uint8_t*)&n, sizeof(n));
kupyna256_update(&ctx->hctx, dpad + 16, 4);
kupyna256_update(&ctx->hctx, ctx->ik, sizeof(ctx->ik));
kupyna256_final(&ctx->hctx, mac);
}
void kupyna384_kmac_init(struct kupyna384_kmac_ctx_t* ctx, const uint8_t* key)
{
kupyna512_init(&ctx->hctx);
kupyna512_update(&ctx->hctx, key, 48);
kupyna512_update(&ctx->hctx, kpad48, sizeof(kpad48));
ctx->len = 0;
for (size_t i=0; i<48; ++i) {
ctx->ik[i] = ~key[i];
}
}
void kupyna384_kmac_update(struct kupyna384_kmac_ctx_t* ctx, const uint8_t* data, size_t len)
{
ctx->len += len;
kupyna512_update(&ctx->hctx, data, len);
}
void kupyna384_kmac_final(struct kupyna384_kmac_ctx_t* ctx, uint8_t* mac)
{
uint64_t n = ctx->len;
size_t pad_size = n < 116 ? (115 - n) : (127 - ((n - 116) % 128));
n = n * 8;
kupyna512_update(&ctx->hctx, dpad, pad_size + 1);
kupyna512_update(&ctx->hctx, (uint8_t*)&n, sizeof(n));
kupyna512_update(&ctx->hctx, dpad + 16, 4);
kupyna512_update(&ctx->hctx, ctx->ik, sizeof(ctx->ik));
kupyna512_final2(&ctx->hctx, mac, 384);
}
void kupyna512_kmac_init(struct kupyna512_kmac_ctx_t* ctx, const uint8_t* key)
{
kupyna512_init(&ctx->hctx);
kupyna512_update(&ctx->hctx, key, 64);
kupyna512_update(&ctx->hctx, kpad64, sizeof(kpad64));
ctx->len = 0;
for (size_t i=0; i<64; ++i) {
ctx->ik[i] = ~key[i];
}
}
void kupyna512_kmac_update(struct kupyna512_kmac_ctx_t* ctx, const uint8_t* data, size_t len)
{
ctx->len += len;
kupyna512_update(&ctx->hctx, data, len);
}
void kupyna512_kmac_final(struct kupyna512_kmac_ctx_t* ctx, uint8_t* mac)
{
uint64_t n = ctx->len;
size_t pad_size = n < 116 ? (115 - n) : (127 - ((n - 116) % 128));
n = n * 8;
kupyna512_update(&ctx->hctx, dpad, pad_size + 1);
kupyna512_update(&ctx->hctx, (uint8_t*)&n, sizeof(n));
kupyna512_update(&ctx->hctx, dpad + 16, 4);
kupyna512_update(&ctx->hctx, ctx->ik, sizeof(ctx->ik));
kupyna512_final(&ctx->hctx, mac);
}