1 /*
2 * Copyright (C) 2021 - This file is part of libecc project
3 *
4 * Authors:
5 * Ryad BENADJILA <ryadbenadjila@gmail.com>
6 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7 *
8 * This software is licensed under a dual BSD and GPL v2 license.
9 * See LICENSE file at the root folder of the project.
10 */
11 #include "md2.h"
12
13 /* All the inner MD-2 operations */
14 static const u8 PI_SUBST[256] = {
15 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
16 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
17 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
18 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
19 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
20 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
21 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
22 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
23 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
24 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
25 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
26 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
27 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
28 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
29 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
30 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
31 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
32 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
33 };
34
35 /* MD-2 core processing. Returns 0 on success, -1 on error. */
md2_process(md2_context * ctx,const u8 data[MD2_BLOCK_SIZE])36 ATTRIBUTE_WARN_UNUSED_RET static inline int md2_process(md2_context *ctx,
37 const u8 data[MD2_BLOCK_SIZE])
38 {
39 int ret;
40 unsigned int i, j, t;
41 u8 x[3 * MD2_BLOCK_SIZE];
42
43 MUST_HAVE((data != NULL), ret, err);
44 MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
45
46 /* Form state, block, state ^ block */
47 ret = local_memcpy(&x[0], ctx->md2_state, MD2_BLOCK_SIZE); EG(ret, err);
48 ret = local_memcpy(&x[MD2_BLOCK_SIZE], data, MD2_BLOCK_SIZE); EG(ret, err);
49 for(i = 0; i < MD2_BLOCK_SIZE; i++){
50 x[(2 * MD2_BLOCK_SIZE) + i] = (ctx->md2_state[i] ^ data[i]);
51 }
52 /* Encrypt the block during 18 rounds */
53 t = 0;
54 for(i = 0; i < 18; i++){
55 for (j = 0; j < (3 * MD2_BLOCK_SIZE); j++){
56 x[j] ^= PI_SUBST[t];
57 t = x[j];
58 }
59 t = (t + i) & 0xff;
60 }
61 /* Save the new state */
62 ret = local_memcpy(ctx->md2_state, &x[0], MD2_BLOCK_SIZE); EG(ret, err);
63 /* Update the checksum */
64 t = ctx->md2_checksum[MD2_BLOCK_SIZE - 1];
65 for(i = 0; i < MD2_BLOCK_SIZE; i++){
66 ctx->md2_checksum[i] ^= PI_SUBST[data[i] ^ t];
67 t = ctx->md2_checksum[i];
68 }
69
70 /* Zeroize x as not needed anymore */
71 ret = local_memset(x, 0, sizeof(x)); EG(ret, err);
72
73 ret = 0;
74
75 err:
76 return ret;
77 }
78
79 /* Init hash function. Returns 0 on success, -1 on error. */
md2_init(md2_context * ctx)80 ATTRIBUTE_WARN_UNUSED_RET int md2_init(md2_context *ctx)
81 {
82 int ret;
83
84 MUST_HAVE((ctx != NULL), ret, err);
85
86 /* Sanity check on size */
87 MUST_HAVE((MD2_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);
88
89 ctx->md2_total = 0;
90 /* Zeroize the state */
91 ret = local_memset(ctx->md2_state, 0, sizeof(ctx->md2_state)); EG(ret, err);
92 /* Zeroize the checksum */
93 ret = local_memset(ctx->md2_checksum, 0, sizeof(ctx->md2_checksum)); EG(ret, err);
94
95 /* Tell that we are initialized */
96 ctx->magic = MD2_HASH_MAGIC;
97
98 ret = 0;
99
100 err:
101 return ret;
102 }
103
md2_update(md2_context * ctx,const u8 * input,u32 ilen)104 ATTRIBUTE_WARN_UNUSED_RET int md2_update(md2_context *ctx, const u8 *input, u32 ilen)
105 {
106 const u8 *data_ptr = input;
107 u32 remain_ilen = ilen;
108 u16 fill;
109 u8 left;
110 int ret;
111
112 MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
113 MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
114
115 /* Nothing to process, return */
116 if (ilen == 0) {
117 ret = 0;
118 goto err;
119 }
120
121 /* Get what's left in our local buffer */
122 left = (ctx->md2_total & 0xF);
123 fill = (u16)(MD2_BLOCK_SIZE - left);
124
125 ctx->md2_total += ilen;
126
127 if ((left > 0) && (remain_ilen >= fill)) {
128 /* Copy data at the end of the buffer */
129 ret = local_memcpy(ctx->md2_buffer + left, data_ptr, fill); EG(ret, err);
130 ret = md2_process(ctx, ctx->md2_buffer); EG(ret, err);
131 data_ptr += fill;
132 remain_ilen -= fill;
133 left = 0;
134 }
135
136 while (remain_ilen >= MD2_BLOCK_SIZE) {
137 ret = md2_process(ctx, data_ptr); EG(ret, err);
138 data_ptr += MD2_BLOCK_SIZE;
139 remain_ilen -= MD2_BLOCK_SIZE;
140 }
141
142 if (remain_ilen > 0) {
143 ret = local_memcpy(ctx->md2_buffer + left, data_ptr, remain_ilen); EG(ret, err);
144 }
145
146 ret = 0;
147
148 err:
149 return ret;
150 }
151
152 /* Finalize. Returns 0 on success, -1 on error.*/
md2_final(md2_context * ctx,u8 output[MD2_DIGEST_SIZE])153 ATTRIBUTE_WARN_UNUSED_RET int md2_final(md2_context *ctx, u8 output[MD2_DIGEST_SIZE])
154 {
155 int ret;
156 unsigned int i;
157 u8 pad_byte;
158
159 MUST_HAVE((output != NULL), ret, err);
160 MD2_HASH_CHECK_INITIALIZED(ctx, ret, err);
161
162 /* This is our final step, so we proceed with the padding */
163 pad_byte = (u8)(MD2_BLOCK_SIZE - (ctx->md2_total % MD2_BLOCK_SIZE));
164 for(i = (ctx->md2_total % MD2_BLOCK_SIZE); i < MD2_BLOCK_SIZE; i++){
165 ctx->md2_buffer[i] = pad_byte;
166 }
167 /* And process the block */
168 ret = md2_process(ctx, ctx->md2_buffer); EG(ret, err);
169
170 /* Also process the checksum */
171 ret = md2_process(ctx, ctx->md2_checksum); EG(ret, err);
172
173 /* Output the hash result */
174 ret = local_memcpy(output, ctx->md2_state, MD2_DIGEST_SIZE); EG(ret, err);
175
176 /* Tell that we are uninitialized */
177 ctx->magic = WORD(0);
178
179 ret = 0;
180
181 err:
182 return ret;
183 }
184
185
186 /*
187 * Scattered version performing init/update/finalize on a vector of buffers
188 * 'inputs' with the length of each buffer passed via 'ilens'. The function
189 * loops on pointers in 'inputs' until it finds a NULL pointer. The function
190 * returns 0 on success, -1 on error.
191 */
md2_scattered(const u8 ** inputs,const u32 * ilens,u8 output[MD2_DIGEST_SIZE])192 ATTRIBUTE_WARN_UNUSED_RET int md2_scattered(const u8 **inputs, const u32 *ilens,
193 u8 output[MD2_DIGEST_SIZE])
194 {
195 md2_context ctx;
196 int ret, pos = 0;
197
198 MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
199
200 ret = md2_init(&ctx); EG(ret, err);
201
202 while (inputs[pos] != NULL) {
203 ret = md2_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
204 pos += 1;
205 }
206
207 ret = md2_final(&ctx, output);
208
209 err:
210 return ret;
211 }
212
213 /*
214 * Single call version performing init/update/final on given input.
215 * Returns 0 on success, -1 on error.
216 */
md2(const u8 * input,u32 ilen,u8 output[MD2_DIGEST_SIZE])217 ATTRIBUTE_WARN_UNUSED_RET int md2(const u8 *input, u32 ilen, u8 output[MD2_DIGEST_SIZE])
218 {
219 md2_context ctx;
220 int ret;
221
222 ret = md2_init(&ctx); EG(ret, err);
223 ret = md2_update(&ctx, input, ilen); EG(ret, err);
224 ret = md2_final(&ctx, output);
225
226 err:
227 return ret;
228 }
229