xref: /openssl/crypto/md5/md5_local.h (revision 3d68e293)
1 /*
2  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdlib.h>
11 #include <string.h>
12 #include <openssl/e_os2.h>
13 #include <openssl/md5.h>
14 
15 #ifdef MD5_ASM
16 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
17      defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
18      defined(_M_X64) || defined(__aarch64__) || \
19      (defined(__loongarch__) && __loongarch_grlen == 64)
20 #  define md5_block_data_order ossl_md5_block_asm_data_order
21 # elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
22 #  define md5_block_data_order ossl_md5_block_asm_data_order
23 # elif defined(__sparc) || defined(__sparc__)
24 #  define md5_block_data_order ossl_md5_block_asm_data_order
25 # endif
26 #endif
27 
28 void md5_block_data_order(MD5_CTX *c, const void *p, size_t num);
29 
30 #define DATA_ORDER_IS_LITTLE_ENDIAN
31 
32 #define HASH_LONG               MD5_LONG
33 #define HASH_CTX                MD5_CTX
34 #define HASH_CBLOCK             MD5_CBLOCK
35 #define HASH_UPDATE             MD5_Update
36 #define HASH_TRANSFORM          MD5_Transform
37 #define HASH_FINAL              MD5_Final
38 #define HASH_MAKE_STRING(c,s)   do {    \
39         unsigned long ll;               \
40         ll=(c)->A; (void)HOST_l2c(ll,(s));      \
41         ll=(c)->B; (void)HOST_l2c(ll,(s));      \
42         ll=(c)->C; (void)HOST_l2c(ll,(s));      \
43         ll=(c)->D; (void)HOST_l2c(ll,(s));      \
44         } while (0)
45 #define HASH_BLOCK_DATA_ORDER   md5_block_data_order
46 
47 #include "crypto/md32_common.h"
48 
49 /*-
50 #define F(x,y,z)        (((x) & (y))  |  ((~(x)) & (z)))
51 #define G(x,y,z)        (((x) & (z))  |  ((y) & (~(z))))
52 */
53 
54 /*
55  * As pointed out by Wei Dai, the above can be simplified to the code
56  * below.  Wei attributes these optimizations to Peter Gutmann's
57  * SHS code, and he attributes it to Rich Schroeppel.
58  */
59 #define F(b,c,d)        ((((c) ^ (d)) & (b)) ^ (d))
60 #define G(b,c,d)        ((((b) ^ (c)) & (d)) ^ (c))
61 #define H(b,c,d)        ((b) ^ (c) ^ (d))
62 #define I(b,c,d)        (((~(d)) | (b)) ^ (c))
63 
64 #define R0(a,b,c,d,k,s,t) { \
65         a+=((k)+(t)+F((b),(c),(d))); \
66         a=ROTATE(a,s); \
67         a+=b; };
68 
69 #define R1(a,b,c,d,k,s,t) { \
70         a+=((k)+(t)+G((b),(c),(d))); \
71         a=ROTATE(a,s); \
72         a+=b; };
73 
74 #define R2(a,b,c,d,k,s,t) { \
75         a+=((k)+(t)+H((b),(c),(d))); \
76         a=ROTATE(a,s); \
77         a+=b; };
78 
79 #define R3(a,b,c,d,k,s,t) { \
80         a+=((k)+(t)+I((b),(c),(d))); \
81         a=ROTATE(a,s); \
82         a+=b; };
83