forked from ANSSI-FR/libdrbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdc2.c
313 lines (258 loc) · 7.65 KB
/
mdc2.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2022 - This file is part of libdrbg project
*
* Author: Ryad BENADJILA <ryad.benadjila@ssi.gouv.fr>
* Contributor: Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "libhash_config.h"
#ifdef WITH_HASH_MDC2
#include "mdc2.h"
/* Include DES helpers */
#include "tdes.h"
int mdc2_set_padding_type(mdc2_context *ctx,
padding_type p)
{
int ret;
MDC2_HASH_CHECK_INITIALIZED(ctx, ret, err);
/* We cannot change the padding type after the first update */
MUST_HAVE((ctx->mdc2_total == 0), ret, err);
if((p != ISOIEC10118_TYPE1) && (p != ISOIEC10118_TYPE2)){
ret = -1;
goto err;
}
ctx->padding = p;
ret = 0;
err:
return ret;
}
/* MDC-2 core processing. Returns 0 on success, -1 on error. */
static inline int mdc2_process(mdc2_context *ctx,
const uint8_t data[MDC2_BLOCK_SIZE])
{
int ret;
unsigned int j;
uint8_t V[8], W[8];
uint8_t *A, *B;
des_context des_ctx;
/* Get the current internal state in A and B */
A = (uint8_t*)&(ctx->mdc2_state[0]);
B = (uint8_t*)&(ctx->mdc2_state[8]);
A[0] = (uint8_t)((A[0] & 0x9f) | 0x40);
B[0] = (uint8_t)((B[0] & 0x9f) | 0x20);
/* Set odd parity */
for(j = 0; j < 8; j++){
A[j] = odd_parity[A[j]];
B[j] = odd_parity[B[j]];
}
/* Compute V_i = M_i + E(M_i, A_i) */
memset(&des_ctx, 0, sizeof(des_context));
ret = des_set_key(&des_ctx, A, DES_ENCRYPTION); EG(ret, err);
ret = des(&des_ctx, &data[0], V); EG(ret, err);
for(j = 0; j < 8; j++){
V[j] = (V[j] ^ data[j]);
}
/* Compute W_i = M_i + E(M_i, B_i) */
memset(&des_ctx, 0, sizeof(des_context));
ret = des_set_key(&des_ctx, B, DES_ENCRYPTION); EG(ret, err);
ret = des(&des_ctx, &data[0], W); EG(ret, err);
for(j = 0; j < 8; j++){
W[j] = (W[j] ^ data[j]);
}
/* Cross the results */
/* In A */
memcpy(&A[0], &V[0], 4);
memcpy(&A[4], &W[4], 4);
/* In B */
memcpy(&B[0], &W[0], 4);
memcpy(&B[4], &V[4], 4);
err:
return ret;
}
/* Init hash function. Returns 0 on success, -1 on error. */
int mdc2_init(mdc2_context *ctx)
{
int ret;
MUST_HAVE((ctx != NULL), ret, err);
ctx->mdc2_total = 0;
/* Initialize A1 */
memset(&(ctx->mdc2_state[0]), 0x52, 8);
/* Initialize B1 */
memset(&(ctx->mdc2_state[8]), 0x25, 8);
/* Initialize default padding type */
ctx->padding = ISOIEC10118_TYPE1;
/* Tell that we are initialized */
ctx->magic = MDC2_HASH_MAGIC;
ret = 0;
err:
return ret;
}
int mdc2_update(mdc2_context *ctx, const uint8_t *input, uint32_t ilen)
{
const uint8_t *data_ptr = input;
uint32_t remain_ilen = ilen;
uint16_t fill;
uint8_t left;
int ret;
MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
MDC2_HASH_CHECK_INITIALIZED(ctx, ret, err);
/* Nothing to process, return */
if (ilen == 0) {
ret = 0;
goto err;
}
/* Get what's left in our local buffer */
left = (ctx->mdc2_total & 0xF);
fill = (uint16_t)(MDC2_BLOCK_SIZE - left);
ctx->mdc2_total += ilen;
if ((left > 0) && (remain_ilen >= fill)) {
/* Copy data at the end of the buffer */
memcpy(ctx->mdc2_buffer + left, data_ptr, fill);
ret = mdc2_process(ctx, ctx->mdc2_buffer); EG(ret, err);
data_ptr += fill;
remain_ilen -= fill;
left = 0;
}
while (remain_ilen >= MDC2_BLOCK_SIZE) {
ret = mdc2_process(ctx, data_ptr); EG(ret, err);
data_ptr += MDC2_BLOCK_SIZE;
remain_ilen -= MDC2_BLOCK_SIZE;
}
if (remain_ilen > 0) {
memcpy(ctx->mdc2_buffer + left, data_ptr, remain_ilen);
}
ret = 0;
err:
return ret;
}
/* Finalize. Returns 0 on success, -1 on error.*/
int mdc2_final(mdc2_context *ctx, uint8_t output[MDC2_DIGEST_SIZE])
{
int ret;
unsigned int i;
uint8_t pad_byte;
MUST_HAVE((output != NULL), ret, err);
MDC2_HASH_CHECK_INITIALIZED(ctx, ret, err);
if(ctx->padding == ISOIEC10118_TYPE1){
/* "Padding method 1" in ISO-IEC-10118 */
/* This is our final step, so we proceed with the padding: the last block
* is padded with zeroes.
*/
pad_byte = 0x00;
if((ctx->mdc2_total % MDC2_BLOCK_SIZE) != 0){
for(i = (ctx->mdc2_total % MDC2_BLOCK_SIZE); i < MDC2_BLOCK_SIZE; i++){
ctx->mdc2_buffer[i] = pad_byte;
}
/* And process the block */
ret = mdc2_process(ctx, ctx->mdc2_buffer); EG(ret, err);
}
}
else if(ctx->padding == ISOIEC10118_TYPE2){
/* "Padding method 2" in ISO-IEC-10118 */
/* This is our final step, so we proceed with the padding: the last block
* is appended 0x80 and then padded with zeroes.
*/
ctx->mdc2_buffer[(ctx->mdc2_total % MDC2_BLOCK_SIZE)] = 0x80;
pad_byte = 0x00;
for(i = ((unsigned int)(ctx->mdc2_total % MDC2_BLOCK_SIZE) + 1); i < MDC2_BLOCK_SIZE; i++){
ctx->mdc2_buffer[i] = pad_byte;
}
/* And process the block */
ret = mdc2_process(ctx, ctx->mdc2_buffer); EG(ret, err);
}
else{
/* Unkown padding */
ret = -1;
goto err;
}
/* Output the hash result */
memcpy(output, ctx->mdc2_state, MDC2_DIGEST_SIZE);
/* Tell that we are uninitialized */
ctx->magic = (uint64_t)0;
ret = 0;
err:
return ret;
}
/*
* Scattered version performing init/update/finalize on a vector of buffers
* 'inputs' with the length of each buffer passed via 'ilens'. The function
* loops on pointers in 'inputs' until it finds a NULL pointer. The function
* returns 0 on success, -1 on error.
*/
int mdc2_scattered(const uint8_t **inputs, const uint32_t *ilens,
uint8_t output[MDC2_DIGEST_SIZE], padding_type p)
{
mdc2_context ctx;
int ret, pos = 0;
MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
ret = mdc2_init(&ctx); EG(ret, err);
ret = mdc2_set_padding_type(&ctx, p); EG(ret, err);
while (inputs[pos] != NULL) {
ret = mdc2_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
pos += 1;
}
ret = mdc2_final(&ctx, output);
err:
return ret;
}
/*
* Scattered version performing init/update/finalize on a vector of buffers
* 'inputs' with the length of each buffer passed via 'ilens'. The function
* loops on pointers in 'inputs' until it finds a NULL pointer. The function
* returns 0 on success, -1 on error.
*/
int mdc2_scattered_padding1(const uint8_t **inputs, const uint32_t *ilens,
uint8_t output[MDC2_DIGEST_SIZE])
{
return mdc2_scattered(inputs, ilens, output, ISOIEC10118_TYPE1);
}
/*
* Scattered version performing init/update/finalize on a vector of buffers
* 'inputs' with the length of each buffer passed via 'ilens'. The function
* loops on pointers in 'inputs' until it finds a NULL pointer. The function
* returns 0 on success, -1 on error.
*/
int mdc2_scattered_padding2(const uint8_t **inputs, const uint32_t *ilens,
uint8_t output[MDC2_DIGEST_SIZE])
{
return mdc2_scattered(inputs, ilens, output, ISOIEC10118_TYPE2);
}
/*
* Single call version performing init/update/final on given input.
* Returns 0 on success, -1 on error.
*/
int mdc2(const uint8_t *input, uint32_t ilen, uint8_t output[MDC2_DIGEST_SIZE], padding_type p)
{
mdc2_context ctx;
int ret;
ret = mdc2_init(&ctx); EG(ret, err);
ret = mdc2_set_padding_type(&ctx, p); EG(ret, err);
ret = mdc2_update(&ctx, input, ilen); EG(ret, err);
ret = mdc2_final(&ctx, output);
err:
return ret;
}
/*
* Single call version performing init/update/final on given input.
* Returns 0 on success, -1 on error.
*/
int mdc2_padding1(const uint8_t *input, uint32_t ilen, uint8_t output[MDC2_DIGEST_SIZE])
{
return mdc2(input, ilen, output, ISOIEC10118_TYPE1);
}
/*
* Single call version performing init/update/final on given input.
* Returns 0 on success, -1 on error.
*/
int mdc2_padding2(const uint8_t *input, uint32_t ilen, uint8_t output[MDC2_DIGEST_SIZE])
{
return mdc2(input, ilen, output, ISOIEC10118_TYPE2);
}
#else
/*
* Dummy definition to avoid the empty translation unit ISO C warning
*/
typedef int dummy;
#endif