diff options
| author | jsing <> | 2023-07-28 11:06:28 +0000 |
|---|---|---|
| committer | jsing <> | 2023-07-28 11:06:28 +0000 |
| commit | 38c8cccc10e393239fb7a405be8df30901552b2d (patch) | |
| tree | 1ae3e387553f664435534c79a465abaebc2a4a27 /src/lib/libcrypto/md5/md5.c | |
| parent | cc6047a4cf022212107fe26aebe50cd2ca78b787 (diff) | |
| download | openbsd-38c8cccc10e393239fb7a405be8df30901552b2d.tar.gz openbsd-38c8cccc10e393239fb7a405be8df30901552b2d.tar.bz2 openbsd-38c8cccc10e393239fb7a405be8df30901552b2d.zip | |
Combine md5 into a single C file.
Diffstat (limited to 'src/lib/libcrypto/md5/md5.c')
| -rw-r--r-- | src/lib/libcrypto/md5/md5.c | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/src/lib/libcrypto/md5/md5.c b/src/lib/libcrypto/md5/md5.c new file mode 100644 index 0000000000..06516781a1 --- /dev/null +++ b/src/lib/libcrypto/md5/md5.c | |||
| @@ -0,0 +1,303 @@ | |||
| 1 | /* $OpenBSD: md5.c,v 1.6 2023/07/28 11:06:28 jsing Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <openssl/opensslv.h> | ||
| 61 | #include <openssl/crypto.h> | ||
| 62 | |||
| 63 | #include <stdlib.h> | ||
| 64 | #include <string.h> | ||
| 65 | |||
| 66 | #include <openssl/opensslconf.h> | ||
| 67 | |||
| 68 | #include <openssl/md5.h> | ||
| 69 | |||
| 70 | #ifdef MD5_ASM | ||
| 71 | # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__) || \ | ||
| 72 | defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) | ||
| 73 | # define md5_block_data_order md5_block_asm_data_order | ||
| 74 | # elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) | ||
| 75 | # define md5_block_data_order md5_block_asm_data_order | ||
| 76 | # endif | ||
| 77 | #endif | ||
| 78 | |||
| 79 | __BEGIN_HIDDEN_DECLS | ||
| 80 | |||
| 81 | void md5_block_data_order (MD5_CTX *c, const void *p, size_t num); | ||
| 82 | |||
| 83 | __END_HIDDEN_DECLS | ||
| 84 | |||
| 85 | #define DATA_ORDER_IS_LITTLE_ENDIAN | ||
| 86 | |||
| 87 | #define HASH_LONG MD5_LONG | ||
| 88 | #define HASH_CTX MD5_CTX | ||
| 89 | #define HASH_CBLOCK MD5_CBLOCK | ||
| 90 | #define HASH_UPDATE MD5_Update | ||
| 91 | #define HASH_TRANSFORM MD5_Transform | ||
| 92 | #define HASH_FINAL MD5_Final | ||
| 93 | #define HASH_MAKE_STRING(c,s) do { \ | ||
| 94 | unsigned long ll; \ | ||
| 95 | ll=(c)->A; HOST_l2c(ll,(s)); \ | ||
| 96 | ll=(c)->B; HOST_l2c(ll,(s)); \ | ||
| 97 | ll=(c)->C; HOST_l2c(ll,(s)); \ | ||
| 98 | ll=(c)->D; HOST_l2c(ll,(s)); \ | ||
| 99 | } while (0) | ||
| 100 | #define HASH_BLOCK_DATA_ORDER md5_block_data_order | ||
| 101 | |||
| 102 | #include "md32_common.h" | ||
| 103 | LCRYPTO_ALIAS(MD5_Update); | ||
| 104 | LCRYPTO_ALIAS(MD5_Transform); | ||
| 105 | LCRYPTO_ALIAS(MD5_Final); | ||
| 106 | |||
| 107 | /* | ||
| 108 | #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z))) | ||
| 109 | #define G(x,y,z) (((x) & (z)) | ((y) & (~(z)))) | ||
| 110 | */ | ||
| 111 | |||
| 112 | /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be | ||
| 113 | * simplified to the code below. Wei attributes these optimizations | ||
| 114 | * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. | ||
| 115 | */ | ||
| 116 | #define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) | ||
| 117 | #define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c)) | ||
| 118 | #define H(b,c,d) ((b) ^ (c) ^ (d)) | ||
| 119 | #define I(b,c,d) (((~(d)) | (b)) ^ (c)) | ||
| 120 | |||
| 121 | #define R0(a,b,c,d,k,s,t) { \ | ||
| 122 | a+=((k)+(t)+F((b),(c),(d))); \ | ||
| 123 | a=ROTATE(a,s); \ | ||
| 124 | a+=b; };\ | ||
| 125 | |||
| 126 | #define R1(a,b,c,d,k,s,t) { \ | ||
| 127 | a+=((k)+(t)+G((b),(c),(d))); \ | ||
| 128 | a=ROTATE(a,s); \ | ||
| 129 | a+=b; }; | ||
| 130 | |||
| 131 | #define R2(a,b,c,d,k,s,t) { \ | ||
| 132 | a+=((k)+(t)+H((b),(c),(d))); \ | ||
| 133 | a=ROTATE(a,s); \ | ||
| 134 | a+=b; }; | ||
| 135 | |||
| 136 | #define R3(a,b,c,d,k,s,t) { \ | ||
| 137 | a+=((k)+(t)+I((b),(c),(d))); \ | ||
| 138 | a=ROTATE(a,s); \ | ||
| 139 | a+=b; }; | ||
| 140 | |||
| 141 | /* Implemented from RFC1321 The MD5 Message-Digest Algorithm | ||
| 142 | */ | ||
| 143 | |||
| 144 | #define INIT_DATA_A (unsigned long)0x67452301L | ||
| 145 | #define INIT_DATA_B (unsigned long)0xefcdab89L | ||
| 146 | #define INIT_DATA_C (unsigned long)0x98badcfeL | ||
| 147 | #define INIT_DATA_D (unsigned long)0x10325476L | ||
| 148 | |||
| 149 | int | ||
| 150 | MD5_Init(MD5_CTX *c) | ||
| 151 | { | ||
| 152 | memset (c, 0, sizeof(*c)); | ||
| 153 | c->A = INIT_DATA_A; | ||
| 154 | c->B = INIT_DATA_B; | ||
| 155 | c->C = INIT_DATA_C; | ||
| 156 | c->D = INIT_DATA_D; | ||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | LCRYPTO_ALIAS(MD5_Init); | ||
| 160 | |||
| 161 | #ifndef md5_block_data_order | ||
| 162 | #ifdef X | ||
| 163 | #undef X | ||
| 164 | #endif | ||
| 165 | void | ||
| 166 | md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) | ||
| 167 | { | ||
| 168 | const unsigned char *data = data_; | ||
| 169 | unsigned MD32_REG_T A, B, C, D, l; | ||
| 170 | unsigned MD32_REG_T X0, X1, X2, X3, X4, X5, X6, X7, | ||
| 171 | X8, X9, X10, X11, X12, X13, X14, X15; | ||
| 172 | |||
| 173 | A = c->A; | ||
| 174 | B = c->B; | ||
| 175 | C = c->C; | ||
| 176 | D = c->D; | ||
| 177 | |||
| 178 | for (; num--; ) { | ||
| 179 | HOST_c2l(data, l); | ||
| 180 | X0 = l; | ||
| 181 | HOST_c2l(data, l); | ||
| 182 | X1 = l; | ||
| 183 | /* Round 0 */ | ||
| 184 | R0(A, B, C, D, X0, 7, 0xd76aa478L); | ||
| 185 | HOST_c2l(data, l); | ||
| 186 | X2 = l; | ||
| 187 | R0(D, A, B, C, X1, 12, 0xe8c7b756L); | ||
| 188 | HOST_c2l(data, l); | ||
| 189 | X3 = l; | ||
| 190 | R0(C, D, A, B, X2, 17, 0x242070dbL); | ||
| 191 | HOST_c2l(data, l); | ||
| 192 | X4 = l; | ||
| 193 | R0(B, C, D, A, X3, 22, 0xc1bdceeeL); | ||
| 194 | HOST_c2l(data, l); | ||
| 195 | X5 = l; | ||
| 196 | R0(A, B, C, D, X4, 7, 0xf57c0fafL); | ||
| 197 | HOST_c2l(data, l); | ||
| 198 | X6 = l; | ||
| 199 | R0(D, A, B, C, X5, 12, 0x4787c62aL); | ||
| 200 | HOST_c2l(data, l); | ||
| 201 | X7 = l; | ||
| 202 | R0(C, D, A, B, X6, 17, 0xa8304613L); | ||
| 203 | HOST_c2l(data, l); | ||
| 204 | X8 = l; | ||
| 205 | R0(B, C, D, A, X7, 22, 0xfd469501L); | ||
| 206 | HOST_c2l(data, l); | ||
| 207 | X9 = l; | ||
| 208 | R0(A, B, C, D, X8, 7, 0x698098d8L); | ||
| 209 | HOST_c2l(data, l); | ||
| 210 | X10 = l; | ||
| 211 | R0(D, A, B, C, X9, 12, 0x8b44f7afL); | ||
| 212 | HOST_c2l(data, l); | ||
| 213 | X11 = l; | ||
| 214 | R0(C, D, A, B, X10, 17, 0xffff5bb1L); | ||
| 215 | HOST_c2l(data, l); | ||
| 216 | X12 = l; | ||
| 217 | R0(B, C, D, A, X11, 22, 0x895cd7beL); | ||
| 218 | HOST_c2l(data, l); | ||
| 219 | X13 = l; | ||
| 220 | R0(A, B, C, D, X12, 7, 0x6b901122L); | ||
| 221 | HOST_c2l(data, l); | ||
| 222 | X14 = l; | ||
| 223 | R0(D, A, B, C, X13, 12, 0xfd987193L); | ||
| 224 | HOST_c2l(data, l); | ||
| 225 | X15 = l; | ||
| 226 | R0(C, D, A, B, X14, 17, 0xa679438eL); | ||
| 227 | R0(B, C, D, A, X15, 22, 0x49b40821L); | ||
| 228 | /* Round 1 */ | ||
| 229 | R1(A, B, C, D, X1, 5, 0xf61e2562L); | ||
| 230 | R1(D, A, B, C, X6, 9, 0xc040b340L); | ||
| 231 | R1(C, D, A, B, X11, 14, 0x265e5a51L); | ||
| 232 | R1(B, C, D, A, X0, 20, 0xe9b6c7aaL); | ||
| 233 | R1(A, B, C, D, X5, 5, 0xd62f105dL); | ||
| 234 | R1(D, A, B, C, X10, 9, 0x02441453L); | ||
| 235 | R1(C, D, A, B, X15, 14, 0xd8a1e681L); | ||
| 236 | R1(B, C, D, A, X4, 20, 0xe7d3fbc8L); | ||
| 237 | R1(A, B, C, D, X9, 5, 0x21e1cde6L); | ||
| 238 | R1(D, A, B, C, X14, 9, 0xc33707d6L); | ||
| 239 | R1(C, D, A, B, X3, 14, 0xf4d50d87L); | ||
| 240 | R1(B, C, D, A, X8, 20, 0x455a14edL); | ||
| 241 | R1(A, B, C, D, X13, 5, 0xa9e3e905L); | ||
| 242 | R1(D, A, B, C, X2, 9, 0xfcefa3f8L); | ||
| 243 | R1(C, D, A, B, X7, 14, 0x676f02d9L); | ||
| 244 | R1(B, C, D, A, X12, 20, 0x8d2a4c8aL); | ||
| 245 | /* Round 2 */ | ||
| 246 | R2(A, B, C, D, X5, 4, 0xfffa3942L); | ||
| 247 | R2(D, A, B, C, X8, 11, 0x8771f681L); | ||
| 248 | R2(C, D, A, B, X11, 16, 0x6d9d6122L); | ||
| 249 | R2(B, C, D, A, X14, 23, 0xfde5380cL); | ||
| 250 | R2(A, B, C, D, X1, 4, 0xa4beea44L); | ||
| 251 | R2(D, A, B, C, X4, 11, 0x4bdecfa9L); | ||
| 252 | R2(C, D, A, B, X7, 16, 0xf6bb4b60L); | ||
| 253 | R2(B, C, D, A, X10, 23, 0xbebfbc70L); | ||
| 254 | R2(A, B, C, D, X13, 4, 0x289b7ec6L); | ||
| 255 | R2(D, A, B, C, X0, 11, 0xeaa127faL); | ||
| 256 | R2(C, D, A, B, X3, 16, 0xd4ef3085L); | ||
| 257 | R2(B, C, D, A, X6, 23, 0x04881d05L); | ||
| 258 | R2(A, B, C, D, X9, 4, 0xd9d4d039L); | ||
| 259 | R2(D, A, B, C, X12, 11, 0xe6db99e5L); | ||
| 260 | R2(C, D, A, B, X15, 16, 0x1fa27cf8L); | ||
| 261 | R2(B, C, D, A, X2, 23, 0xc4ac5665L); | ||
| 262 | /* Round 3 */ | ||
| 263 | R3(A, B, C, D, X0, 6, 0xf4292244L); | ||
| 264 | R3(D, A, B, C, X7, 10, 0x432aff97L); | ||
| 265 | R3(C, D, A, B, X14, 15, 0xab9423a7L); | ||
| 266 | R3(B, C, D, A, X5, 21, 0xfc93a039L); | ||
| 267 | R3(A, B, C, D, X12, 6, 0x655b59c3L); | ||
| 268 | R3(D, A, B, C, X3, 10, 0x8f0ccc92L); | ||
| 269 | R3(C, D, A, B, X10, 15, 0xffeff47dL); | ||
| 270 | R3(B, C, D, A, X1, 21, 0x85845dd1L); | ||
| 271 | R3(A, B, C, D, X8, 6, 0x6fa87e4fL); | ||
| 272 | R3(D, A, B, C, X15, 10, 0xfe2ce6e0L); | ||
| 273 | R3(C, D, A, B, X6, 15, 0xa3014314L); | ||
| 274 | R3(B, C, D, A, X13, 21, 0x4e0811a1L); | ||
| 275 | R3(A, B, C, D, X4, 6, 0xf7537e82L); | ||
| 276 | R3(D, A, B, C, X11, 10, 0xbd3af235L); | ||
| 277 | R3(C, D, A, B, X2, 15, 0x2ad7d2bbL); | ||
| 278 | R3(B, C, D, A, X9, 21, 0xeb86d391L); | ||
| 279 | |||
| 280 | A = c->A += A; | ||
| 281 | B = c->B += B; | ||
| 282 | C = c->C += C; | ||
| 283 | D = c->D += D; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | #endif | ||
| 287 | |||
| 288 | unsigned char * | ||
| 289 | MD5(const unsigned char *d, size_t n, unsigned char *md) | ||
| 290 | { | ||
| 291 | MD5_CTX c; | ||
| 292 | static unsigned char m[MD5_DIGEST_LENGTH]; | ||
| 293 | |||
| 294 | if (md == NULL) | ||
| 295 | md = m; | ||
| 296 | if (!MD5_Init(&c)) | ||
| 297 | return NULL; | ||
| 298 | MD5_Update(&c, d, n); | ||
| 299 | MD5_Final(md, &c); | ||
| 300 | explicit_bzero(&c, sizeof(c)); | ||
| 301 | return (md); | ||
| 302 | } | ||
| 303 | LCRYPTO_ALIAS(MD5); | ||
