Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/md4/md4.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/md4.h>
58
59
#include <stdlib.h>
60
#include <string.h>
61
62
#include "../internal.h"
63
#include "../crypto/fipsmodule/digest/md32_common.h"
64
65
66
0
uint8_t *MD4(const uint8_t *data, size_t len, uint8_t out[MD4_DIGEST_LENGTH]) {
67
0
  MD4_CTX ctx;
68
0
  MD4_Init(&ctx);
69
0
  MD4_Update(&ctx, data, len);
70
0
  MD4_Final(out, &ctx);
71
72
0
  return out;
73
0
}
74
75
// Implemented from RFC 1186 The MD4 Message-Digest Algorithm.
76
77
576
int MD4_Init(MD4_CTX *md4) {
78
576
  OPENSSL_memset(md4, 0, sizeof(MD4_CTX));
79
576
  md4->h[0] = 0x67452301UL;
80
576
  md4->h[1] = 0xefcdab89UL;
81
576
  md4->h[2] = 0x98badcfeUL;
82
576
  md4->h[3] = 0x10325476UL;
83
576
  return 1;
84
576
}
85
86
void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num);
87
88
0
void MD4_Transform(MD4_CTX *c, const uint8_t data[MD4_CBLOCK]) {
89
0
  md4_block_data_order(c->h, data, 1);
90
0
}
91
92
14.8k
int MD4_Update(MD4_CTX *c, const void *data, size_t len) {
93
14.8k
  crypto_md32_update(&md4_block_data_order, c->h, c->data, MD4_CBLOCK, &c->num,
94
14.8k
                     &c->Nh, &c->Nl, data, len);
95
14.8k
  return 1;
96
14.8k
}
97
98
2.97k
int MD4_Final(uint8_t out[MD4_DIGEST_LENGTH], MD4_CTX *c) {
99
2.97k
  crypto_md32_final(&md4_block_data_order, c->h, c->data, MD4_CBLOCK, &c->num,
100
2.97k
                    c->Nh, c->Nl, /*is_big_endian=*/0);
101
102
2.97k
  CRYPTO_store_u32_le(out, c->h[0]);
103
2.97k
  CRYPTO_store_u32_le(out + 4, c->h[1]);
104
2.97k
  CRYPTO_store_u32_le(out + 8, c->h[2]);
105
2.97k
  CRYPTO_store_u32_le(out + 12, c->h[3]);
106
2.97k
  return 1;
107
2.97k
}
108
109
// As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
110
// simplified to the code below.  Wei attributes these optimizations
111
// to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
112
1.75M
#define F(b, c, d) ((((c) ^ (d)) & (b)) ^ (d))
113
1.75M
#define G(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
114
1.75M
#define H(b, c, d) ((b) ^ (c) ^ (d))
115
116
#define R0(a, b, c, d, k, s, t)            \
117
1.75M
  do {                                     \
118
1.75M
    (a) += ((k) + (t) + F((b), (c), (d))); \
119
1.75M
    (a) = CRYPTO_rotl_u32(a, s);           \
120
1.75M
  } while (0)
121
122
#define R1(a, b, c, d, k, s, t)            \
123
1.75M
  do {                                     \
124
1.75M
    (a) += ((k) + (t) + G((b), (c), (d))); \
125
1.75M
    (a) = CRYPTO_rotl_u32(a, s);           \
126
1.75M
  } while (0)
127
128
#define R2(a, b, c, d, k, s, t)            \
129
1.75M
  do {                                     \
130
1.75M
    (a) += ((k) + (t) + H((b), (c), (d))); \
131
1.75M
    (a) = CRYPTO_rotl_u32(a, s);           \
132
1.75M
  } while (0)
133
134
4.75k
void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num) {
135
4.75k
  uint32_t A, B, C, D;
136
4.75k
  uint32_t X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15;
137
138
4.75k
  A = state[0];
139
4.75k
  B = state[1];
140
4.75k
  C = state[2];
141
4.75k
  D = state[3];
142
143
114k
  for (; num--;) {
144
109k
    X0 = CRYPTO_load_u32_le(data);
145
109k
    data += 4;
146
109k
    X1 = CRYPTO_load_u32_le(data);
147
109k
    data += 4;
148
    // Round 0
149
109k
    R0(A, B, C, D, X0, 3, 0);
150
109k
    X2 = CRYPTO_load_u32_le(data);
151
109k
    data += 4;
152
109k
    R0(D, A, B, C, X1, 7, 0);
153
109k
    X3 = CRYPTO_load_u32_le(data);
154
109k
    data += 4;
155
109k
    R0(C, D, A, B, X2, 11, 0);
156
109k
    X4 = CRYPTO_load_u32_le(data);
157
109k
    data += 4;
158
109k
    R0(B, C, D, A, X3, 19, 0);
159
109k
    X5 = CRYPTO_load_u32_le(data);
160
109k
    data += 4;
161
109k
    R0(A, B, C, D, X4, 3, 0);
162
109k
    X6 = CRYPTO_load_u32_le(data);
163
109k
    data += 4;
164
109k
    R0(D, A, B, C, X5, 7, 0);
165
109k
    X7 = CRYPTO_load_u32_le(data);
166
109k
    data += 4;
167
109k
    R0(C, D, A, B, X6, 11, 0);
168
109k
    X8 = CRYPTO_load_u32_le(data);
169
109k
    data += 4;
170
109k
    R0(B, C, D, A, X7, 19, 0);
171
109k
    X9 = CRYPTO_load_u32_le(data);
172
109k
    data += 4;
173
109k
    R0(A, B, C, D, X8, 3, 0);
174
109k
    X10 = CRYPTO_load_u32_le(data);
175
109k
    data += 4;
176
109k
    R0(D, A, B, C, X9, 7, 0);
177
109k
    X11 = CRYPTO_load_u32_le(data);
178
109k
    data += 4;
179
109k
    R0(C, D, A, B, X10, 11, 0);
180
109k
    X12 = CRYPTO_load_u32_le(data);
181
109k
    data += 4;
182
109k
    R0(B, C, D, A, X11, 19, 0);
183
109k
    X13 = CRYPTO_load_u32_le(data);
184
109k
    data += 4;
185
109k
    R0(A, B, C, D, X12, 3, 0);
186
109k
    X14 = CRYPTO_load_u32_le(data);
187
109k
    data += 4;
188
109k
    R0(D, A, B, C, X13, 7, 0);
189
109k
    X15 = CRYPTO_load_u32_le(data);
190
109k
    data += 4;
191
109k
    R0(C, D, A, B, X14, 11, 0);
192
109k
    R0(B, C, D, A, X15, 19, 0);
193
    // Round 1
194
109k
    R1(A, B, C, D, X0, 3, 0x5A827999L);
195
109k
    R1(D, A, B, C, X4, 5, 0x5A827999L);
196
109k
    R1(C, D, A, B, X8, 9, 0x5A827999L);
197
109k
    R1(B, C, D, A, X12, 13, 0x5A827999L);
198
109k
    R1(A, B, C, D, X1, 3, 0x5A827999L);
199
109k
    R1(D, A, B, C, X5, 5, 0x5A827999L);
200
109k
    R1(C, D, A, B, X9, 9, 0x5A827999L);
201
109k
    R1(B, C, D, A, X13, 13, 0x5A827999L);
202
109k
    R1(A, B, C, D, X2, 3, 0x5A827999L);
203
109k
    R1(D, A, B, C, X6, 5, 0x5A827999L);
204
109k
    R1(C, D, A, B, X10, 9, 0x5A827999L);
205
109k
    R1(B, C, D, A, X14, 13, 0x5A827999L);
206
109k
    R1(A, B, C, D, X3, 3, 0x5A827999L);
207
109k
    R1(D, A, B, C, X7, 5, 0x5A827999L);
208
109k
    R1(C, D, A, B, X11, 9, 0x5A827999L);
209
109k
    R1(B, C, D, A, X15, 13, 0x5A827999L);
210
    // Round 2
211
109k
    R2(A, B, C, D, X0, 3, 0x6ED9EBA1L);
212
109k
    R2(D, A, B, C, X8, 9, 0x6ED9EBA1L);
213
109k
    R2(C, D, A, B, X4, 11, 0x6ED9EBA1L);
214
109k
    R2(B, C, D, A, X12, 15, 0x6ED9EBA1L);
215
109k
    R2(A, B, C, D, X2, 3, 0x6ED9EBA1L);
216
109k
    R2(D, A, B, C, X10, 9, 0x6ED9EBA1L);
217
109k
    R2(C, D, A, B, X6, 11, 0x6ED9EBA1L);
218
109k
    R2(B, C, D, A, X14, 15, 0x6ED9EBA1L);
219
109k
    R2(A, B, C, D, X1, 3, 0x6ED9EBA1L);
220
109k
    R2(D, A, B, C, X9, 9, 0x6ED9EBA1L);
221
109k
    R2(C, D, A, B, X5, 11, 0x6ED9EBA1L);
222
109k
    R2(B, C, D, A, X13, 15, 0x6ED9EBA1L);
223
109k
    R2(A, B, C, D, X3, 3, 0x6ED9EBA1L);
224
109k
    R2(D, A, B, C, X11, 9, 0x6ED9EBA1L);
225
109k
    R2(C, D, A, B, X7, 11, 0x6ED9EBA1L);
226
109k
    R2(B, C, D, A, X15, 15, 0x6ED9EBA1L);
227
228
109k
    A = state[0] += A;
229
109k
    B = state[1] += B;
230
109k
    C = state[2] += C;
231
109k
    D = state[3] += D;
232
109k
  }
233
4.75k
}