diff --git a/erts/emulator/Makefile.in b/erts/emulator/Makefile.in index 38729e91d366..1eb26e9a1381 100644 --- a/erts/emulator/Makefile.in +++ b/erts/emulator/Makefile.in @@ -379,6 +379,14 @@ else LIBS += $(RYU_LIBRARY) endif +DEPLIBS += $(OPENSSL_LIB) +ifeq ($(TARGET),win32) +LIBS += -L$(OPENSSL_OBJDIR) -l$(OPENSSL_LIB_NAME) +else +# Build on darwin fails if -l$(OPENSSL_LIB_NAME) is used +LIBS += $(OPENSSL_LIB) +endif + LIBSCTP = @LIBSCTP@ ORG_THR_LIBS=@EMU_THR_LIBS@ @@ -421,7 +429,8 @@ OBJDIR = obj/$(TTF_DIR) CREATE_DIRS += $(OBJDIR) \ pcre/obj/$(TARGET)/$(TYPE) \ $(ZLIB_OBJDIR) \ - $(RYU_OBJDIR) + $(RYU_OBJDIR) \ + $(OPENSSL_OBJDIR) ifeq ($(FLAVOR),jit) CREATE_DIRS+=$(OBJDIR)/asmjit/ $(OBJDIR)/asmjit/core $(OBJDIR)/asmjit/$(JIT_ARCH) @@ -491,6 +500,7 @@ endif include zlib/zlib.mk include pcre/pcre.mk include ryu/ryu.mk +include openssl/openssl.mk $(ERTS_LIB): $(V_at)cd $(ERTS_LIB_DIR) && $(MAKE) $(TYPE) @@ -504,6 +514,7 @@ clean: $(RM) -r pcre/obj/$(TARGET) $(PCRE_GENINC) $(RM) -r zlib/obj/$(TARGET) $(RM) -r ryu/obj/$(TARGET) + $(RM) -r openssl/obj/$(TARGET) $(RM) -r bin/$(TARGET) cd $(ERTS_LIB_DIR) && $(MAKE) clean @@ -793,6 +804,7 @@ COMMON_INCLUDES += -Izlib endif COMMON_INCLUDES += -Ipcre COMMON_INCLUDES += -Iryu +COMMON_INCLUDES += -Iopenssl/include COMMON_INCLUDES += -I../include -I../include/$(TARGET) COMMON_INCLUDES += -I../include/internal -I../include/internal/$(TARGET) @@ -1083,7 +1095,7 @@ RUN_OBJS += \ $(OBJDIR)/erl_trace.o $(OBJDIR)/copy.o \ $(OBJDIR)/utils.o $(OBJDIR)/bif.o \ $(OBJDIR)/io.o $(OBJDIR)/erl_printf_term.o\ - $(OBJDIR)/erl_debug.o $(OBJDIR)/erl_md5.o \ + $(OBJDIR)/erl_debug.o \ $(OBJDIR)/erl_message.o $(OBJDIR)/erl_proc_sig_queue.o \ $(OBJDIR)/erl_process_dict.o $(OBJDIR)/erl_process_lock.o \ $(OBJDIR)/erl_port_task.o $(OBJDIR)/erl_arith.o \ diff --git a/erts/emulator/beam/beam_code.h b/erts/emulator/beam/beam_code.h index dfe192b41e0e..720ae41b204d 100644 --- a/erts/emulator/beam/beam_code.h +++ b/erts/emulator/beam/beam_code.h @@ -23,6 +23,7 @@ #include "sys.h" #include "erl_process.h" +#include "erl_md5.h" /* Macros for manipulating locations. */ #define LINE_INVALID_LOCATION (0) @@ -44,7 +45,7 @@ # define BEAM_NATIVE_MIN_FUNC_SZ 4 #endif -#define MD5_SIZE 16 +#define MD5_SIZE MD5_DIGEST_LENGTH typedef struct BeamCodeLineTab_ BeamCodeLineTab; diff --git a/erts/emulator/beam/erl_bif_chksum.c b/erts/emulator/beam/erl_bif_chksum.c index 959f5960a1bb..66f7992c2ef3 100644 --- a/erts/emulator/beam/erl_bif_chksum.c +++ b/erts/emulator/beam/erl_bif_chksum.c @@ -30,6 +30,7 @@ #include "erl_binary.h" #include "big.h" #include "zlib.h" +#include "erl_md5.h" typedef void (*ChksumFun)(void *sum_in_out, const byte *buf, diff --git a/erts/emulator/beam/erl_bif_info.c b/erts/emulator/beam/erl_bif_info.c index eb9aaaf41702..5ed0c624ef7e 100644 --- a/erts/emulator/beam/erl_bif_info.c +++ b/erts/emulator/beam/erl_bif_info.c @@ -53,6 +53,7 @@ #include "erl_alloc_util.h" #include "erl_global_literals.h" #include "beam_load.h" +#include "erl_md5.h" #include "erl_iolist.h" #ifdef ERTS_ENABLE_LOCK_COUNT diff --git a/erts/emulator/beam/erl_md5.c b/erts/emulator/beam/erl_md5.c deleted file mode 100644 index 2a4e026d9ca1..000000000000 --- a/erts/emulator/beam/erl_md5.c +++ /dev/null @@ -1,330 +0,0 @@ -/* - * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm - */ - -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All - * rights reserved. - * - * License to copy and use this software is granted provided that it - * is identified as the "RSA Data Security, Inc. MD5 Message-Digest - * Algorithm" in all material mentioning or referencing this software - * or this function. - * - * License is also granted to make and use derivative works provided - * that such works are identified as "derived from the RSA Data - * Security, Inc. MD5 Message-Digest Algorithm" in all material - * mentioning or referencing the derived work. - * - * RSA Data Security, Inc. makes no representations concerning either - * the merchantability of this software or the suitability of this - * software for any particular purpose. It is provided "as is" - * without express or implied warranty of any kind. - * - * These notices must be retained in any copies of any part of this - * documentation and/or software. - */ - -/* %ExternalCopyright% */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "sys.h" -#include "erl_vm.h" -#include "global.h" -#include "erl_process.h" -#include "error.h" -#include "bif.h" -#include "erl_binary.h" - -typedef void *POINTER; - -/* - * Constants for MD5Transform routine. - */ - -#define S11 7 -#define S12 12 -#define S13 17 -#define S14 22 -#define S21 5 -#define S22 9 -#define S23 14 -#define S24 20 -#define S31 4 -#define S32 11 -#define S33 16 -#define S34 23 -#define S41 6 -#define S42 10 -#define S43 15 -#define S44 21 - -static void MD5Transform(Uint32 [4], unsigned char [64]); -static void Encode(unsigned char *, Uint32 *, unsigned int); -static void Decode(Uint32 *, unsigned char *, unsigned int); - -static unsigned char PADDING[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -/* - * F, G, H and I are basic MD5 functions. - */ -#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) -#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) -#define H(x, y, z) ((x) ^ (y) ^ (z)) -#define I(x, y, z) ((y) ^ ((x) | (~z))) - -/* - * ROTATE_LEFT rotates x left n bits. - */ -#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) - -/* - * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. - * Rotation is separate from addition to prevent recomputation. - */ -#define FF(a, b, c, d, x, s, ac) { \ - (a) += F ((b), (c), (d)) + (x) + (Uint32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define GG(a, b, c, d, x, s, ac) { \ - (a) += G ((b), (c), (d)) + (x) + (Uint32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define HH(a, b, c, d, x, s, ac) { \ - (a) += H ((b), (c), (d)) + (x) + (Uint32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define II(a, b, c, d, x, s, ac) { \ - (a) += I ((b), (c), (d)) + (x) + (Uint32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} - -/* - * MD5 initialization. Begins an MD5 operation, writing a new context. - */ -void MD5Init(MD5_CTX* context) -{ - context->count[0] = context->count[1] = 0; - - /* - * Load magic initialization constants. - */ - context->state[0] = 0x67452301; - context->state[1] = 0xefcdab89; - context->state[2] = 0x98badcfe; - context->state[3] = 0x10325476; -} - -/* - * MD5 block update operation. Continues an MD5 message-digest - * operation, processing another message block, and updating the - * context. - */ -void MD5Update (MD5_CTX *context, - unsigned char *input, /* input block */ - unsigned int inputLen) /* length of input block */ -{ - unsigned int i, index, partLen; - - /* - * Compute number of bytes mod 64 - */ - index = (unsigned int)((context->count[0] >> 3) & 0x3F); - - /* Update number of bits */ - if ((context->count[0] += ((Uint32)inputLen << 3)) - < ((Uint32)inputLen << 3)) - context->count[1]++; - context->count[1] += ((Uint32)inputLen >> 29); - - partLen = 64 - index; - - /* - * Transform as many times as possible. - */ - if (inputLen >= partLen) { - sys_memcpy - ((POINTER)&context->buffer[index], (POINTER)input, partLen); - MD5Transform (context->state, context->buffer); - - for (i = partLen; i + 63 < inputLen; i += 64) - MD5Transform (context->state, &input[i]); - - index = 0; - } - else - i = 0; - - /* - * Buffer remaining input - */ - sys_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i); -} - -/* - * MD5 finalization. Ends an MD5 message-digest operation, writing - the message digest and zeroizing the context. - */ -void MD5Final (unsigned char digest[16], /* message digest */ - MD5_CTX *context) /* context */ -{ - unsigned char bits[8]; - unsigned int index, padLen; - - /* - * Save number of bits - */ - Encode (bits, context->count, 8); - - /* - * Pad out to 56 mod 64. - */ - index = (unsigned int)((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); - MD5Update (context, PADDING, padLen); - - /* - * Append length (before padding) - */ - MD5Update (context, bits, 8); - - /* - * Store state in digest - */ - Encode (digest, context->state, 16); - - /* - * Zeroize sensitive information. - */ - sys_memset ((POINTER)context, 0, sizeof (*context)); -} - -/* - * MD5 basic transformation. Transforms state based on block. - */ -static void MD5Transform (Uint32 state[4], unsigned char block[64]) -{ - Uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; - - Decode (x, block, 64); - - /* Round 1 */ - FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ - FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ - FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ - FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ - FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ - FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ - FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ - FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ - FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ - FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ - FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ - FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ - FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ - FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ - FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ - FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ - - /* Round 2 */ - GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ - GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ - GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ - GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ - GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ - GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ - GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ - GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ - GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ - GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ - GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ - GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ - GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ - GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ - GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ - GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ - - /* Round 3 */ - HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ - HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ - HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ - HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ - HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ - HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ - HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ - HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ - HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ - HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ - HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ - HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ - HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ - HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ - HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ - HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ - - /* Round 4 */ - II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ - II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ - II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ - II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ - II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ - II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ - II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ - II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ - II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ - II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ - II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ - II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ - II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ - II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ - II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ - II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - - /* - * Zeroize sensitive information. - */ - sys_memset ((POINTER)x, 0, sizeof (x)); -} - -/* - * Encodes input (Uint32) into output (unsigned char). Assumes len is - * a multiple of 4. - */ -static void Encode (unsigned char *output, Uint32 *input, unsigned int len) -{ - unsigned int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); - } -} - -/* - * Decodes input (unsigned char) into output (Uint32). Assumes len is - * a multiple of 4. - */ -static void Decode (Uint32 *output, unsigned char *input, unsigned int len) -{ - unsigned int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((Uint32)input[j]) | (((Uint32)input[j+1]) << 8) | - (((Uint32)input[j+2]) << 16) | (((Uint32)input[j+3]) << 24); -} diff --git a/erts/emulator/beam/global.h b/erts/emulator/beam/global.h index d7a242457196..a92ed632bc14 100644 --- a/erts/emulator/beam/global.h +++ b/erts/emulator/beam/global.h @@ -1257,20 +1257,6 @@ void erl_start(int, char**); void erts_usage(void); Eterm erts_preloaded(Process* p); - -/* erl_md5.c */ - -typedef struct { - Uint32 state[4]; /* state (ABCD) */ - Uint32 count[2]; /* number of bits, modulo 2^64 (lsb first) */ - unsigned char buffer[64]; /* input buffer */ -} MD5_CTX; - -void MD5Init(MD5_CTX *); -void MD5Update(MD5_CTX *, unsigned char *, unsigned int); -void MD5Final(unsigned char [16], MD5_CTX *); - - /* io.c */ typedef struct { diff --git a/erts/emulator/openssl/LICENSE.txt b/erts/emulator/openssl/LICENSE.txt new file mode 100644 index 000000000000..49cc83d2ee29 --- /dev/null +++ b/erts/emulator/openssl/LICENSE.txt @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/erts/emulator/openssl/README b/erts/emulator/openssl/README new file mode 100644 index 000000000000..942d294daa2a --- /dev/null +++ b/erts/emulator/openssl/README @@ -0,0 +1,27 @@ +This directory contains a *very* small part of OpenSSL. Currently only parts +of the OpenSSL MD5 implementation. + +The files that has been taken from the 3.1.4 version of OpenSSL: + +./LICENSE.txt +./crypto/md5/md5_local.h +./crypto/md5/md5_dgst.c +./include/crypto/md32_common.h +./include/openssl/md5.h + +The openssl.diff file contains the diff between the original files and the ones +included in OTP. + +Ignoring LICENSE.txt (which contains the full Apache 2.0 license) the +copyright and license notice of the above OpenSSL files are as follows +(copyright years differ a bit): + +/* + * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + diff --git a/erts/emulator/openssl/crypto/md5/md5_dgst.c b/erts/emulator/openssl/crypto/md5/md5_dgst.c new file mode 100644 index 000000000000..155a5ac3e55a --- /dev/null +++ b/erts/emulator/openssl/crypto/md5/md5_dgst.c @@ -0,0 +1,181 @@ +/* + * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * MD5 low level APIs are deprecated for public use, but still ok for + * internal use. + */ +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include "internal/deprecated.h" +#endif + +#include +#include "md5_local.h" +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include +#endif + +/* + * Implemented from RFC1321 The MD5 Message-Digest Algorithm + */ + +#define INIT_DATA_A (unsigned long)0x67452301L +#define INIT_DATA_B (unsigned long)0xefcdab89L +#define INIT_DATA_C (unsigned long)0x98badcfeL +#define INIT_DATA_D (unsigned long)0x10325476L + +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define MD5_Init MD5_INIT_FUNCTION_NAME +#endif + +int MD5_Init(MD5_CTX *c) +{ + memset(c, 0, sizeof(*c)); + c->A = INIT_DATA_A; + c->B = INIT_DATA_B; + c->C = INIT_DATA_C; + c->D = INIT_DATA_D; + return 1; +} + +#ifndef md5_block_data_order +# ifdef X +# undef X +# endif +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#endif +void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) +{ + const unsigned char *data = data_; + register unsigned MD32_REG_T A, B, C, D, l; +# ifndef MD32_XARRAY + /* See comment in crypto/sha/sha_local.h for details. */ + unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7, + XX8, XX9, XX10, XX11, XX12, XX13, XX14, XX15; +# define X(i) XX##i +# else + MD5_LONG XX[MD5_LBLOCK]; +# define X(i) XX[i] +# endif + + A = c->A; + B = c->B; + C = c->C; + D = c->D; + + for (; num--;) { + (void)HOST_c2l(data, l); + X(0) = l; + (void)HOST_c2l(data, l); + X(1) = l; + /* Round 0 */ + R0(A, B, C, D, X(0), 7, 0xd76aa478L); + (void)HOST_c2l(data, l); + X(2) = l; + R0(D, A, B, C, X(1), 12, 0xe8c7b756L); + (void)HOST_c2l(data, l); + X(3) = l; + R0(C, D, A, B, X(2), 17, 0x242070dbL); + (void)HOST_c2l(data, l); + X(4) = l; + R0(B, C, D, A, X(3), 22, 0xc1bdceeeL); + (void)HOST_c2l(data, l); + X(5) = l; + R0(A, B, C, D, X(4), 7, 0xf57c0fafL); + (void)HOST_c2l(data, l); + X(6) = l; + R0(D, A, B, C, X(5), 12, 0x4787c62aL); + (void)HOST_c2l(data, l); + X(7) = l; + R0(C, D, A, B, X(6), 17, 0xa8304613L); + (void)HOST_c2l(data, l); + X(8) = l; + R0(B, C, D, A, X(7), 22, 0xfd469501L); + (void)HOST_c2l(data, l); + X(9) = l; + R0(A, B, C, D, X(8), 7, 0x698098d8L); + (void)HOST_c2l(data, l); + X(10) = l; + R0(D, A, B, C, X(9), 12, 0x8b44f7afL); + (void)HOST_c2l(data, l); + X(11) = l; + R0(C, D, A, B, X(10), 17, 0xffff5bb1L); + (void)HOST_c2l(data, l); + X(12) = l; + R0(B, C, D, A, X(11), 22, 0x895cd7beL); + (void)HOST_c2l(data, l); + X(13) = l; + R0(A, B, C, D, X(12), 7, 0x6b901122L); + (void)HOST_c2l(data, l); + X(14) = l; + R0(D, A, B, C, X(13), 12, 0xfd987193L); + (void)HOST_c2l(data, l); + X(15) = l; + R0(C, D, A, B, X(14), 17, 0xa679438eL); + R0(B, C, D, A, X(15), 22, 0x49b40821L); + /* Round 1 */ + R1(A, B, C, D, X(1), 5, 0xf61e2562L); + R1(D, A, B, C, X(6), 9, 0xc040b340L); + R1(C, D, A, B, X(11), 14, 0x265e5a51L); + R1(B, C, D, A, X(0), 20, 0xe9b6c7aaL); + R1(A, B, C, D, X(5), 5, 0xd62f105dL); + R1(D, A, B, C, X(10), 9, 0x02441453L); + R1(C, D, A, B, X(15), 14, 0xd8a1e681L); + R1(B, C, D, A, X(4), 20, 0xe7d3fbc8L); + R1(A, B, C, D, X(9), 5, 0x21e1cde6L); + R1(D, A, B, C, X(14), 9, 0xc33707d6L); + R1(C, D, A, B, X(3), 14, 0xf4d50d87L); + R1(B, C, D, A, X(8), 20, 0x455a14edL); + R1(A, B, C, D, X(13), 5, 0xa9e3e905L); + R1(D, A, B, C, X(2), 9, 0xfcefa3f8L); + R1(C, D, A, B, X(7), 14, 0x676f02d9L); + R1(B, C, D, A, X(12), 20, 0x8d2a4c8aL); + /* Round 2 */ + R2(A, B, C, D, X(5), 4, 0xfffa3942L); + R2(D, A, B, C, X(8), 11, 0x8771f681L); + R2(C, D, A, B, X(11), 16, 0x6d9d6122L); + R2(B, C, D, A, X(14), 23, 0xfde5380cL); + R2(A, B, C, D, X(1), 4, 0xa4beea44L); + R2(D, A, B, C, X(4), 11, 0x4bdecfa9L); + R2(C, D, A, B, X(7), 16, 0xf6bb4b60L); + R2(B, C, D, A, X(10), 23, 0xbebfbc70L); + R2(A, B, C, D, X(13), 4, 0x289b7ec6L); + R2(D, A, B, C, X(0), 11, 0xeaa127faL); + R2(C, D, A, B, X(3), 16, 0xd4ef3085L); + R2(B, C, D, A, X(6), 23, 0x04881d05L); + R2(A, B, C, D, X(9), 4, 0xd9d4d039L); + R2(D, A, B, C, X(12), 11, 0xe6db99e5L); + R2(C, D, A, B, X(15), 16, 0x1fa27cf8L); + R2(B, C, D, A, X(2), 23, 0xc4ac5665L); + /* Round 3 */ + R3(A, B, C, D, X(0), 6, 0xf4292244L); + R3(D, A, B, C, X(7), 10, 0x432aff97L); + R3(C, D, A, B, X(14), 15, 0xab9423a7L); + R3(B, C, D, A, X(5), 21, 0xfc93a039L); + R3(A, B, C, D, X(12), 6, 0x655b59c3L); + R3(D, A, B, C, X(3), 10, 0x8f0ccc92L); + R3(C, D, A, B, X(10), 15, 0xffeff47dL); + R3(B, C, D, A, X(1), 21, 0x85845dd1L); + R3(A, B, C, D, X(8), 6, 0x6fa87e4fL); + R3(D, A, B, C, X(15), 10, 0xfe2ce6e0L); + R3(C, D, A, B, X(6), 15, 0xa3014314L); + R3(B, C, D, A, X(13), 21, 0x4e0811a1L); + R3(A, B, C, D, X(4), 6, 0xf7537e82L); + R3(D, A, B, C, X(11), 10, 0xbd3af235L); + R3(C, D, A, B, X(2), 15, 0x2ad7d2bbL); + R3(B, C, D, A, X(9), 21, 0xeb86d391L); + + A = c->A += A; + B = c->B += B; + C = c->C += C; + D = c->D += D; + } +} +#endif diff --git a/erts/emulator/openssl/crypto/md5/md5_local.h b/erts/emulator/openssl/crypto/md5/md5_local.h new file mode 100644 index 000000000000..7ec6b3595eae --- /dev/null +++ b/erts/emulator/openssl/crypto/md5/md5_local.h @@ -0,0 +1,98 @@ +/* + * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#if defined(ERLANG_OPENSSL_INTEGRATION) +#include "openssl/md5.h" +#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#undef MD5_ASM +#else +#include +#include +#endif + +#ifdef MD5_ASM +# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \ + defined(_M_X64) || defined(__aarch64__) +# define md5_block_data_order ossl_md5_block_asm_data_order +# elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) +# define md5_block_data_order ossl_md5_block_asm_data_order +# elif defined(__sparc) || defined(__sparc__) +# define md5_block_data_order ossl_md5_block_asm_data_order +# endif +#endif + +void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); + +#define DATA_ORDER_IS_LITTLE_ENDIAN + +#define HASH_LONG MD5_LONG +#define HASH_CTX MD5_CTX +#define HASH_CBLOCK MD5_CBLOCK +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define HASH_UPDATE MD5_UPDATE_FUNCTION_NAME +#define HASH_TRANSFORM MD5_TRANSFORM_FUNCTION_NAME +#define HASH_FINAL MD5_FINAL_FUNCTION_NAME +#else +#define HASH_UPDATE MD5_Update +#define HASH_TRANSFORM MD5_Transform +#define HASH_FINAL MD5_Final +#endif +#define HASH_MAKE_STRING(c,s) do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll,(s)); \ + ll=(c)->B; (void)HOST_l2c(ll,(s)); \ + ll=(c)->C; (void)HOST_l2c(ll,(s)); \ + ll=(c)->D; (void)HOST_l2c(ll,(s)); \ + } while (0) +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define HASH_BLOCK_DATA_ORDER MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#undef md5_block_data_order +#else +#define HASH_BLOCK_DATA_ORDER md5_block_data_order +#endif + +#include "crypto/md32_common.h" + +/*- +#define F(x,y,z) (((x) & (y)) | ((~(x)) & (z))) +#define G(x,y,z) (((x) & (z)) | ((y) & (~(z)))) +*/ + +/* + * As pointed out by Wei Dai, the above can be simplified to the code + * below. Wei attributes these optimizations to Peter Gutmann's + * SHS code, and he attributes it to Rich Schroeppel. + */ +#define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) +#define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c)) +#define H(b,c,d) ((b) ^ (c) ^ (d)) +#define I(b,c,d) (((~(d)) | (b)) ^ (c)) + +#define R0(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+F((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R1(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+G((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R2(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+H((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R3(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+I((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; diff --git a/erts/emulator/openssl/include/crypto/md32_common.h b/erts/emulator/openssl/include/crypto/md32_common.h new file mode 100644 index 000000000000..1f68008c41e0 --- /dev/null +++ b/erts/emulator/openssl/include/crypto/md32_common.h @@ -0,0 +1,288 @@ +/* + * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/*- + * This is a generic 32 bit "collector" for message digest algorithms. + * Whenever needed it collects input character stream into chunks of + * 32 bit values and invokes a block function that performs actual hash + * calculations. + * + * Porting guide. + * + * Obligatory macros: + * + * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN + * this macro defines byte order of input stream. + * HASH_CBLOCK + * size of a unit chunk HASH_BLOCK operates on. + * HASH_LONG + * has to be at least 32 bit wide. + * HASH_CTX + * context structure that at least contains following + * members: + * typedef struct { + * ... + * HASH_LONG Nl,Nh; + * either { + * HASH_LONG data[HASH_LBLOCK]; + * unsigned char data[HASH_CBLOCK]; + * }; + * unsigned int num; + * ... + * } HASH_CTX; + * data[] vector is expected to be zeroed upon first call to + * HASH_UPDATE. + * HASH_UPDATE + * name of "Update" function, implemented here. + * HASH_TRANSFORM + * name of "Transform" function, implemented here. + * HASH_FINAL + * name of "Final" function, implemented here. + * HASH_BLOCK_DATA_ORDER + * name of "block" function capable of treating *unaligned* input + * message in original (data) byte order, implemented externally. + * HASH_MAKE_STRING + * macro converting context variables to an ASCII hash string. + * + * MD5 example: + * + * #define DATA_ORDER_IS_LITTLE_ENDIAN + * + * #define HASH_LONG MD5_LONG + * #define HASH_CTX MD5_CTX + * #define HASH_CBLOCK MD5_CBLOCK + * #define HASH_UPDATE MD5_Update + * #define HASH_TRANSFORM MD5_Transform + * #define HASH_FINAL MD5_Final + * #define HASH_BLOCK_DATA_ORDER md5_block_data_order + */ + +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include +#endif + +#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) +# error "DATA_ORDER must be defined!" +#endif + +#ifndef HASH_CBLOCK +# error "HASH_CBLOCK must be defined!" +#endif +#ifndef HASH_LONG +# error "HASH_LONG must be defined!" +#endif +#ifndef HASH_CTX +# error "HASH_CTX must be defined!" +#endif + +#ifndef HASH_UPDATE +# error "HASH_UPDATE must be defined!" +#endif +#ifndef HASH_TRANSFORM +# error "HASH_TRANSFORM must be defined!" +#endif +#ifndef HASH_FINAL +# error "HASH_FINAL must be defined!" +#endif + +#ifndef HASH_BLOCK_DATA_ORDER +# error "HASH_BLOCK_DATA_ORDER must be defined!" +#endif + +#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) + +#ifndef PEDANTIC +# if defined(__GNUC__) && __GNUC__>=2 && \ + !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) +# if defined(__riscv_zbb) || defined(__riscv_zbkb) +# if __riscv_xlen == 64 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("roriw %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# if __riscv_xlen == 32 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("rori %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# endif +# endif +#endif + +#if defined(DATA_ORDER_IS_BIG_ENDIAN) + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<<24) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>>24)&0xff), \ + l) + +#endif + +/* + * Time for some action :-) + */ + +int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) +{ + const unsigned char *data = data_; + unsigned char *p; + HASH_LONG l; + size_t n; + + if (len == 0) + return 1; + + l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL; + if (l < c->Nl) /* overflow */ + c->Nh++; + c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on + * 16-bit */ + c->Nl = l; + + n = c->num; + if (n != 0) { + p = (unsigned char *)c->data; + + if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { + memcpy(p + n, data, HASH_CBLOCK - n); + HASH_BLOCK_DATA_ORDER(c, p, 1); + n = HASH_CBLOCK - n; + data += n; + len -= n; + c->num = 0; + /* + * We use memset rather than OPENSSL_cleanse() here deliberately. + * Using OPENSSL_cleanse() here could be a performance issue. It + * will get properly cleansed on finalisation so this isn't a + * security problem. + */ + memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ + } else { + memcpy(p + n, data, len); + c->num += (unsigned int)len; + return 1; + } + } + + n = len / HASH_CBLOCK; + if (n > 0) { + HASH_BLOCK_DATA_ORDER(c, data, n); + n *= HASH_CBLOCK; + data += n; + len -= n; + } + + if (len != 0) { + p = (unsigned char *)c->data; + c->num = (unsigned int)len; + memcpy(p, data, len); + } + return 1; +} + +void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) +{ + HASH_BLOCK_DATA_ORDER(c, data, 1); +} + +int HASH_FINAL(unsigned char *md, HASH_CTX *c) +{ + unsigned char *p = (unsigned char *)c->data; + size_t n = c->num; + + p[n] = 0x80; /* there is always room for one */ + n++; + + if (n > (HASH_CBLOCK - 8)) { + memset(p + n, 0, HASH_CBLOCK - n); + n = 0; + HASH_BLOCK_DATA_ORDER(c, p, 1); + } + memset(p + n, 0, HASH_CBLOCK - 8 - n); + + p += HASH_CBLOCK - 8; +#if defined(DATA_ORDER_IS_BIG_ENDIAN) + (void)HOST_l2c(c->Nh, p); + (void)HOST_l2c(c->Nl, p); +#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) + (void)HOST_l2c(c->Nl, p); + (void)HOST_l2c(c->Nh, p); +#endif + p -= HASH_CBLOCK; + HASH_BLOCK_DATA_ORDER(c, p, 1); + c->num = 0; +#if defined(ERLANG_OPENSSL_INTEGRATION) + /* + * We are not using this anywhere where we consider + * this to be a security issue. + */ + memset(p, 0, HASH_CBLOCK); +#else + OPENSSL_cleanse(p, HASH_CBLOCK); +#endif + +#ifndef HASH_MAKE_STRING +# error "HASH_MAKE_STRING must be defined!" +#else + HASH_MAKE_STRING(c, md); +#endif + + return 1; +} + +#ifndef MD32_REG_T +# if defined(__alpha) || defined(__sparcv9) || defined(__mips) +# define MD32_REG_T long +/* + * This comment was originally written for MD5, which is why it + * discusses A-D. But it basically applies to all 32-bit digests, + * which is why it was moved to common header file. + * + * In case you wonder why A-D are declared as long and not + * as MD5_LONG. Doing so results in slight performance + * boost on LP64 architectures. The catch is we don't + * really care if 32 MSBs of a 64-bit register get polluted + * with eventual overflows as we *save* only 32 LSBs in + * *either* case. Now declaring 'em long excuses the compiler + * from keeping 32 MSBs zeroed resulting in 13% performance + * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. + * Well, to be honest it should say that this *prevents* + * performance degradation. + */ +# else +/* + * Above is not absolute and there are LP64 compilers that + * generate better code if MD32_REG_T is defined int. The above + * pre-processor condition reflects the circumstances under which + * the conclusion was made and is subject to further extension. + */ +# define MD32_REG_T int +# endif +#endif diff --git a/erts/emulator/openssl/include/erl_md5.h b/erts/emulator/openssl/include/erl_md5.h new file mode 100644 index 000000000000..ad3d5dd9d595 --- /dev/null +++ b/erts/emulator/openssl/include/erl_md5.h @@ -0,0 +1,35 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2023. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * %CopyrightEnd% + */ + +#ifndef ERL_MD5_H__ +#define ERL_MD5_H__ + +#undef ERLANG_OPENSSL_INTEGRATION +#define ERLANG_OPENSSL_INTEGRATION + +#define MD5_INIT_FUNCTION_NAME MD5Init +#define MD5_UPDATE_FUNCTION_NAME MD5Update +#define MD5_FINAL_FUNCTION_NAME MD5Final +#define MD5_TRANSFORM_FUNCTION_NAME MD5Transform +#define MD5_BLOCK_DATA_ORDER_FUNCTION_NAME MD5BlockDataOrder + +#include "openssl/md5.h" + +#endif diff --git a/erts/emulator/openssl/include/openssl/md5.h b/erts/emulator/openssl/include/openssl/md5.h new file mode 100644 index 000000000000..c9b42ecb115c --- /dev/null +++ b/erts/emulator/openssl/include/openssl/md5.h @@ -0,0 +1,82 @@ +/* + * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OPENSSL_MD5_H +# define OPENSSL_MD5_H +# pragma once + +#if defined(ERLANG_OPENSSL_INTEGRATION) + +#include "erl_md5.h" + +#undef OPENSSL_NO_MD5 +#undef OPENSSL_NO_DEPRECATED_3_0 + +#else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + +# include +# ifndef OPENSSL_NO_DEPRECATED_3_0 +# define HEADER_MD5_H +# endif + +# include + +#endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + +# ifndef OPENSSL_NO_MD5 +#if !defined(ERLANG_OPENSSL_INTEGRATION) +# include +#endif +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define MD5_DIGEST_LENGTH 16 + +# if !defined(OPENSSL_NO_DEPRECATED_3_0) +/* + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! MD5_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define MD5_LONG unsigned int + +# define MD5_CBLOCK 64 +# define MD5_LBLOCK (MD5_CBLOCK/4) + +typedef struct MD5state_st { + MD5_LONG A, B, C, D; + MD5_LONG Nl, Nh; + MD5_LONG data[MD5_LBLOCK]; + unsigned int num; +} MD5_CTX; +# endif +# ifndef OPENSSL_NO_DEPRECATED_3_0 +# if defined(ERLANG_OPENSSL_INTEGRATION) +int MD5_INIT_FUNCTION_NAME(MD5_CTX *c); +int MD5_UPDATE_FUNCTION_NAME(MD5_CTX *c, const void *data, size_t len); +int MD5_FINAL_FUNCTION_NAME(unsigned char *md, MD5_CTX *c); +void MD5_TRANSFORM_FUNCTION_NAME(MD5_CTX *c, const unsigned char *b); +# else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ +OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c); +OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len); +OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c); +OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n, + unsigned char *md); +OSSL_DEPRECATEDIN_3_0 void MD5_Transform(MD5_CTX *c, const unsigned char *b); +# endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ +# endif + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/erts/emulator/openssl/openssl.diff b/erts/emulator/openssl/openssl.diff new file mode 100644 index 000000000000..0e1d1545820f --- /dev/null +++ b/erts/emulator/openssl/openssl.diff @@ -0,0 +1,168 @@ +diff -u original/crypto/md5/md5_dgst.c crypto/md5/md5_dgst.c +--- original/crypto/md5/md5_dgst.c 2023-11-24 15:57:21.151284250 +0100 ++++ crypto/md5/md5_dgst.c 2023-11-24 23:08:56.359410118 +0100 +@@ -11,11 +11,15 @@ + * MD5 low level APIs are deprecated for public use, but still ok for + * internal use. + */ ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include "internal/deprecated.h" ++#endif + + #include + #include "md5_local.h" ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include ++#endif + + /* + * Implemented from RFC1321 The MD5 Message-Digest Algorithm +@@ -26,6 +30,10 @@ + #define INIT_DATA_C (unsigned long)0x98badcfeL + #define INIT_DATA_D (unsigned long)0x10325476L + ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define MD5_Init MD5_INIT_FUNCTION_NAME ++#endif ++ + int MD5_Init(MD5_CTX *c) + { + memset(c, 0, sizeof(*c)); +@@ -40,6 +48,9 @@ + # ifdef X + # undef X + # endif ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#endif + void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) + { + const unsigned char *data = data_; +diff -u original/crypto/md5/md5_local.h crypto/md5/md5_local.h +--- original/crypto/md5/md5_local.h 2023-11-24 15:57:11.999228660 +0100 ++++ crypto/md5/md5_local.h 2023-11-24 22:46:24.091772692 +0100 +@@ -9,8 +9,14 @@ + + #include + #include ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#include "openssl/md5.h" ++#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#undef MD5_ASM ++#else + #include + #include ++#endif + + #ifdef MD5_ASM + # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ +@@ -31,9 +37,15 @@ + #define HASH_LONG MD5_LONG + #define HASH_CTX MD5_CTX + #define HASH_CBLOCK MD5_CBLOCK ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define HASH_UPDATE MD5_UPDATE_FUNCTION_NAME ++#define HASH_TRANSFORM MD5_TRANSFORM_FUNCTION_NAME ++#define HASH_FINAL MD5_FINAL_FUNCTION_NAME ++#else + #define HASH_UPDATE MD5_Update + #define HASH_TRANSFORM MD5_Transform + #define HASH_FINAL MD5_Final ++#endif + #define HASH_MAKE_STRING(c,s) do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll,(s)); \ +@@ -41,7 +53,12 @@ + ll=(c)->C; (void)HOST_l2c(ll,(s)); \ + ll=(c)->D; (void)HOST_l2c(ll,(s)); \ + } while (0) ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define HASH_BLOCK_DATA_ORDER MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#undef md5_block_data_order ++#else + #define HASH_BLOCK_DATA_ORDER md5_block_data_order ++#endif + + #include "crypto/md32_common.h" + +diff -u original/include/crypto/md32_common.h include/crypto/md32_common.h +--- original/include/crypto/md32_common.h 2023-11-24 15:58:01.910884892 +0100 ++++ include/crypto/md32_common.h 2023-11-24 22:49:19.072662368 +0100 +@@ -63,7 +63,9 @@ + * #define HASH_BLOCK_DATA_ORDER md5_block_data_order + */ + ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include ++#endif + + #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) + # error "DATA_ORDER must be defined!" +@@ -236,7 +238,15 @@ + p -= HASH_CBLOCK; + HASH_BLOCK_DATA_ORDER(c, p, 1); + c->num = 0; ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++ /* ++ * We are not using this anywhere where we consider ++ * this to be a security issue. ++ */ ++ memset(p, 0, HASH_CBLOCK); ++#else + OPENSSL_cleanse(p, HASH_CBLOCK); ++#endif + + #ifndef HASH_MAKE_STRING + # error "HASH_MAKE_STRING must be defined!" +diff -u original/include/openssl/md5.h include/openssl/md5.h +--- original/include/openssl/md5.h 2023-11-24 15:58:28.610171865 +0100 ++++ include/openssl/md5.h 2023-11-24 23:10:52.570854593 +0100 +@@ -11,6 +11,15 @@ + # define OPENSSL_MD5_H + # pragma once + ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++ ++#include "erl_md5.h" ++ ++#undef OPENSSL_NO_MD5 ++#undef OPENSSL_NO_DEPRECATED_3_0 ++ ++#else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ ++ + # include + # ifndef OPENSSL_NO_DEPRECATED_3_0 + # define HEADER_MD5_H +@@ -18,8 +27,12 @@ + + # include + ++#endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ ++ + # ifndef OPENSSL_NO_MD5 ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + # include ++#endif + # include + # ifdef __cplusplus + extern "C" { +@@ -46,12 +59,19 @@ + } MD5_CTX; + # endif + # ifndef OPENSSL_NO_DEPRECATED_3_0 ++# if defined(ERLANG_OPENSSL_INTEGRATION) ++int MD5_INIT_FUNCTION_NAME(MD5_CTX *c); ++int MD5_UPDATE_FUNCTION_NAME(MD5_CTX *c, const void *data, size_t len); ++int MD5_FINAL_FUNCTION_NAME(unsigned char *md, MD5_CTX *c); ++void MD5_TRANSFORM_FUNCTION_NAME(MD5_CTX *c, const unsigned char *b); ++# else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c); + OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len); + OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c); + OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n, + unsigned char *md); + OSSL_DEPRECATEDIN_3_0 void MD5_Transform(MD5_CTX *c, const unsigned char *b); ++# endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + # endif + + # ifdef __cplusplus diff --git a/erts/emulator/openssl/openssl.mk b/erts/emulator/openssl/openssl.mk new file mode 100644 index 000000000000..a084a4272077 --- /dev/null +++ b/erts/emulator/openssl/openssl.mk @@ -0,0 +1,53 @@ +# +# %CopyrightBegin% +# +# Copyright Ericsson AB 2023. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# %CopyrightEnd% +# + +OPENSSL_O = md5_dgst.o + +OPENSSL_LIB_NAME = micro-openssl +OPENSSL_DIR = $(ERL_TOP)/erts/emulator/openssl +OPENSSL_INCLUDE_DIR = $(OPENSSL_DIR)/include +OPENSSL_OBJDIR = $(OPENSSL_DIR)/obj/$(TARGET)/$(TYPE) +OPENSSL_OBJS = $(OPENSSL_O:%=$(OPENSSL_OBJDIR)/%) +OPENSSL_CFLAGS = $(filter-out -DDEBUG,$(CFLAGS)) -I$(OPENSSL_INCLUDE_DIR) \ + -DERLANG_OPENSSL_INTEGRATION + +ifeq ($(TARGET), win32) +OPENSSL_LIB = $(OPENSSL_OBJDIR)/$(OPENSSL_LIB_NAME).lib + +$(OPENSSL_LIB): $(OPENSSL_OBJS) + $(V_AR) -out:$@ $(OPENSSL_OBJS) +else + +OPENSSL_LIB = $(OPENSSL_OBJDIR)/$(OPENSSL_LIB_NAME).a + +$(OPENSSL_LIB): $(OPENSSL_OBJS) + $(V_AR) $(ARFLAGS) $@ $(OPENSSL_OBJS) + -@ ($(RANLIB) $@ || true) 2>/dev/null +endif + +$(OPENSSL_OBJDIR)/%.o: $(OPENSSL_DIR)/crypto/md5/%.c + $(V_CC) -c $(OPENSSL_CFLAGS) -o $@ $< + +# Dependencies. + +$(OPENSSL_OBJDIR)/md5_dgst.o: $(OPENSSL_DIR)/crypto/md5/md5_dgst.c \ + $(OPENSSL_DIR)/crypto/md5/md5_local.h \ + $(OPENSSL_DIR)/include/crypto/md32_common.h \ + $(OPENSSL_DIR)/include/openssl/md5.h diff --git a/lib/erl_interface/src/Makefile.in b/lib/erl_interface/src/Makefile.in index 0a5ae800be5f..282250279826 100644 --- a/lib/erl_interface/src/Makefile.in +++ b/lib/erl_interface/src/Makefile.in @@ -42,6 +42,8 @@ include $(TARGET)/eidefs.mk include $(ERL_TOP)/make/output.mk +include openssl/openssl.mk + EBINDIR=../ebin APP_FILE= erl_interface.app @@ -96,7 +98,7 @@ AR_FLAGS=rcv endif INCFLAGS = -I. -I../include -Iconnect -Iencode -Idecode -Imisc -Iepmd \ - -I$(TARGET) + -Iopenssl/include -I$(TARGET) ifeq ($(USING_VC),yes) WARNFLAGS = -Wall @@ -106,7 +108,8 @@ endif WARNFLAGS += -DEI_NO_DEPR_WARN -CFLAGS = @DED_CFLAGS@ $(WARNFLAGS) $(INCFLAGS) $(TYPE_FLAGS) +CFLAGS = @DED_CFLAGS@ $(WARNFLAGS) $(INCFLAGS) $(TYPE_FLAGS) \ + -DERLANG_OPENSSL_INTEGRATION DED_LD = @DED_LD@ DED_LDFLAGS = @DED_LDFLAGS@ DED_LD_FLAG_RUNTIME_LIBRARY_PATH = @DED_LD_FLAG_RUNTIME_LIBRARY_PATH@ @@ -137,7 +140,7 @@ BINDIR = $(ERL_TOP)/lib/erl_interface/bin/$(TARGET) # -Wno-char-subscripts # -Wshadow -vpath %.c connect:encode:decode:misc:epmd:global +vpath %.c connect:encode:decode:misc:epmd:global:$(OPENSSL_VPATH) ########################################################################### # List targets @@ -286,7 +289,8 @@ EISOURCES = \ $(ENCODESRC) \ $(EPMDSRC) \ $(MISCSRC) \ - $(GLOBALSOURCES) + $(GLOBALSOURCES) \ + $(OPENSSL_SOURCES) CONNECTSRC = \ connect/ei_connect.c \ @@ -365,7 +369,6 @@ MISCSRC = \ misc/ei_pthreads.c \ misc/ei_trace.c \ misc/ei_x_encode.c \ - misc/eimd5.c \ misc/get_type.c \ misc/show_msg.c \ misc/ei_compat.c \ diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index 3f2becde5a97..f95895a7e4d1 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -65,7 +65,7 @@ #include "ei_locking.h" #include "eisend.h" #include "eirecv.h" -#include "eimd5.h" +#include "erl_md5.h" #include "putget.h" #include "ei_resolve.h" #include "ei_epmd.h" diff --git a/lib/erl_interface/src/misc/eimd5.c b/lib/erl_interface/src/misc/eimd5.c deleted file mode 100644 index 3d7dd960e736..000000000000 --- a/lib/erl_interface/src/misc/eimd5.c +++ /dev/null @@ -1,319 +0,0 @@ -/* - * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm - */ - -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All - * rights reserved. - * - * License to copy and use this software is granted provided that it - * is identified as the "RSA Data Security, Inc. MD5 Message-Digest - * Algorithm" in all material mentioning or referencing this software - * or this function. - * - * License is also granted to make and use derivative works provided - * that such works are identified as "derived from the RSA Data - * Security, Inc. MD5 Message-Digest Algorithm" in all material - * mentioning or referencing the derived work. - * - * RSA Data Security, Inc. makes no representations concerning either - * the merchantability of this software or the suitability of this - * software for any particular purpose. It is provided "as is" - * without express or implied warranty of any kind. - * - * These notices must be retained in any copies of any part of this - * documentation and/or software. - */ - -#include "eidef.h" - -#include -#include "eimd5.h" - -/* - * Constants for MD5Transform routine. - */ - -#define S11 7 -#define S12 12 -#define S13 17 -#define S14 22 -#define S21 5 -#define S22 9 -#define S23 14 -#define S24 20 -#define S31 4 -#define S32 11 -#define S33 16 -#define S34 23 -#define S41 6 -#define S42 10 -#define S43 15 -#define S44 21 - -static void MD5Transform(UINT4 [4], unsigned char [64]); -static void Encode(unsigned char *, UINT4 *, unsigned int); -static void Decode(UINT4 *, unsigned char *, unsigned int); - -static unsigned char PADDING[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -/* - * F, G, H and I are basic MD5 functions. - */ -#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) -#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) -#define H(x, y, z) ((x) ^ (y) ^ (z)) -#define I(x, y, z) ((y) ^ ((x) | (~z))) - -/* - * ROTATE_LEFT rotates x left n bits. - */ -#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) - -/* - * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. - * Rotation is separate from addition to prevent recomputation. - */ -#define FF(a, b, c, d, x, s, ac) { \ - (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define GG(a, b, c, d, x, s, ac) { \ - (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define HH(a, b, c, d, x, s, ac) { \ - (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define II(a, b, c, d, x, s, ac) { \ - (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} - - -/* - * MD5 initialization. Begins an MD5 operation, writing a new context. - */ -void ei_MD5Init(MD5_CTX* context) -{ - context->count[0] = context->count[1] = 0; - - /* - * Load magic initialization constants. - */ - context->state[0] = 0x67452301; - context->state[1] = 0xefcdab89; - context->state[2] = 0x98badcfe; - context->state[3] = 0x10325476; -} - -/* - * MD5 block update operation. Continues an MD5 message-digest - * operation, processing another message block, and updating the - * context. - */ -void ei_MD5Update (MD5_CTX *context, unsigned char *input, - unsigned int inputLen) -{ - unsigned int i, index, partLen; - - /* - * Compute number of bytes mod 64 - */ - index = (unsigned int)((context->count[0] >> 3) & 0x3F); - - /* Update number of bits */ - if ((context->count[0] += ((UINT4)inputLen << 3)) - < ((UINT4)inputLen << 3)) - context->count[1]++; - context->count[1] += ((UINT4)inputLen >> 29); - - partLen = 64 - index; - - /* - * Transform as many times as possible. - */ - if (inputLen >= partLen) { - memcpy - ((POINTER)&context->buffer[index], (POINTER)input, partLen); - MD5Transform (context->state, context->buffer); - - for (i = partLen; i + 63 < inputLen; i += 64) - MD5Transform (context->state, &input[i]); - - index = 0; - } - else - i = 0; - - /* - * Buffer remaining input - */ - memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i); -} - -/* - * MD5 finalization. Ends an MD5 message-digest operation, writing - the message digest and zeroizing the context. - */ -void ei_MD5Final (unsigned char digest[16], MD5_CTX *context) -{ - unsigned char bits[8]; - unsigned int index, padLen; - - /* - * Save number of bits - */ - Encode (bits, context->count, 8); - - /* - * Pad out to 56 mod 64. - */ - index = (unsigned int)((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); - ei_MD5Update (context, PADDING, padLen); - - /* - * Append length (before padding) - */ - ei_MD5Update (context, bits, 8); - - /* - * Store state in digest - */ - Encode (digest, context->state, 16); - - /* - * Zeroize sensitive information. - */ - memset ((POINTER)context, 0, sizeof (*context)); -} - -/* - * MD5 basic transformation. Transforms state based on block. - */ -static void MD5Transform (UINT4 state[4], unsigned char block[64]) -{ - UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; - - Decode (x, block, 64); - - /* Round 1 */ - FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ - FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ - FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ - FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ - FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ - FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ - FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ - FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ - FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ - FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ - FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ - FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ - FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ - FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ - FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ - FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ - - /* Round 2 */ - GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ - GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ - GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ - GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ - GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ - GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ - GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ - GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ - GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ - GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ - GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ - GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ - GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ - GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ - GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ - GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ - - /* Round 3 */ - HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ - HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ - HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ - HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ - HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ - HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ - HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ - HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ - HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ - HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ - HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ - HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ - HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ - HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ - HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ - HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ - - /* Round 4 */ - II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ - II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ - II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ - II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ - II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ - II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ - II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ - II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ - II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ - II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ - II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ - II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ - II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ - II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ - II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ - II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - - /* - * Zeroize sensitive information. - */ - memset ((POINTER)x, 0, sizeof (x)); -} - -/* - * Encodes input (UINT4) into output (unsigned char). Assumes len is - * a multiple of 4. - */ -static void Encode (unsigned char *output, UINT4 *input, unsigned int len) -{ - unsigned int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); - } -} - -/* - * Decodes input (unsigned char) into output (UINT4). Assumes len is - * a multiple of 4. - */ -static void Decode (UINT4 *output, unsigned char *input, unsigned int len) -{ - unsigned int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | - (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); -} diff --git a/lib/erl_interface/src/misc/eimd5.h b/lib/erl_interface/src/misc/eimd5.h deleted file mode 100644 index 746f06e23654..000000000000 --- a/lib/erl_interface/src/misc/eimd5.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm - */ - -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All - * rights reserved. - * - * License to copy and use this software is granted provided that it - * is identified as the "RSA Data Security, Inc. MD5 Message-Digest - * Algorithm" in all material mentioning or referencing this software - * or this function. - * - * License is also granted to make and use derivative works provided - * that such works are identified as "derived from the RSA Data - * Security, Inc. MD5 Message-Digest Algorithm" in all material - * mentioning or referencing the derived work. - * - * RSA Data Security, Inc. makes no representations concerning either - * the merchantability of this software or the suitability of this - * software for any particular purpose. It is provided "as is" - * without express or implied warranty of any kind. - * - * These notices must be retained in any copies of any part of this - * documentation and/or software. - */ - -#ifndef _EIMD5_H -#define _EIMD5_H - -typedef unsigned UINT4; /* Should be 32 bits. */ -typedef void *POINTER; - - -/* - * MD5 context. - */ - -typedef struct { - UINT4 state[4]; /* state (ABCD) */ - UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ - unsigned char buffer[64]; /* input buffer */ -} MD5_CTX; - -void ei_MD5Init(MD5_CTX *); -void ei_MD5Update(MD5_CTX *, unsigned char *, unsigned int); -void ei_MD5Final(unsigned char [16], MD5_CTX *); - -#endif /* _EIMD5_H */ diff --git a/lib/erl_interface/src/openssl/LICENSE.txt b/lib/erl_interface/src/openssl/LICENSE.txt new file mode 100644 index 000000000000..49cc83d2ee29 --- /dev/null +++ b/lib/erl_interface/src/openssl/LICENSE.txt @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/lib/erl_interface/src/openssl/README b/lib/erl_interface/src/openssl/README new file mode 100644 index 000000000000..942d294daa2a --- /dev/null +++ b/lib/erl_interface/src/openssl/README @@ -0,0 +1,27 @@ +This directory contains a *very* small part of OpenSSL. Currently only parts +of the OpenSSL MD5 implementation. + +The files that has been taken from the 3.1.4 version of OpenSSL: + +./LICENSE.txt +./crypto/md5/md5_local.h +./crypto/md5/md5_dgst.c +./include/crypto/md32_common.h +./include/openssl/md5.h + +The openssl.diff file contains the diff between the original files and the ones +included in OTP. + +Ignoring LICENSE.txt (which contains the full Apache 2.0 license) the +copyright and license notice of the above OpenSSL files are as follows +(copyright years differ a bit): + +/* + * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + diff --git a/lib/erl_interface/src/openssl/crypto/md5/md5_dgst.c b/lib/erl_interface/src/openssl/crypto/md5/md5_dgst.c new file mode 100644 index 000000000000..155a5ac3e55a --- /dev/null +++ b/lib/erl_interface/src/openssl/crypto/md5/md5_dgst.c @@ -0,0 +1,181 @@ +/* + * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * MD5 low level APIs are deprecated for public use, but still ok for + * internal use. + */ +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include "internal/deprecated.h" +#endif + +#include +#include "md5_local.h" +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include +#endif + +/* + * Implemented from RFC1321 The MD5 Message-Digest Algorithm + */ + +#define INIT_DATA_A (unsigned long)0x67452301L +#define INIT_DATA_B (unsigned long)0xefcdab89L +#define INIT_DATA_C (unsigned long)0x98badcfeL +#define INIT_DATA_D (unsigned long)0x10325476L + +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define MD5_Init MD5_INIT_FUNCTION_NAME +#endif + +int MD5_Init(MD5_CTX *c) +{ + memset(c, 0, sizeof(*c)); + c->A = INIT_DATA_A; + c->B = INIT_DATA_B; + c->C = INIT_DATA_C; + c->D = INIT_DATA_D; + return 1; +} + +#ifndef md5_block_data_order +# ifdef X +# undef X +# endif +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#endif +void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) +{ + const unsigned char *data = data_; + register unsigned MD32_REG_T A, B, C, D, l; +# ifndef MD32_XARRAY + /* See comment in crypto/sha/sha_local.h for details. */ + unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7, + XX8, XX9, XX10, XX11, XX12, XX13, XX14, XX15; +# define X(i) XX##i +# else + MD5_LONG XX[MD5_LBLOCK]; +# define X(i) XX[i] +# endif + + A = c->A; + B = c->B; + C = c->C; + D = c->D; + + for (; num--;) { + (void)HOST_c2l(data, l); + X(0) = l; + (void)HOST_c2l(data, l); + X(1) = l; + /* Round 0 */ + R0(A, B, C, D, X(0), 7, 0xd76aa478L); + (void)HOST_c2l(data, l); + X(2) = l; + R0(D, A, B, C, X(1), 12, 0xe8c7b756L); + (void)HOST_c2l(data, l); + X(3) = l; + R0(C, D, A, B, X(2), 17, 0x242070dbL); + (void)HOST_c2l(data, l); + X(4) = l; + R0(B, C, D, A, X(3), 22, 0xc1bdceeeL); + (void)HOST_c2l(data, l); + X(5) = l; + R0(A, B, C, D, X(4), 7, 0xf57c0fafL); + (void)HOST_c2l(data, l); + X(6) = l; + R0(D, A, B, C, X(5), 12, 0x4787c62aL); + (void)HOST_c2l(data, l); + X(7) = l; + R0(C, D, A, B, X(6), 17, 0xa8304613L); + (void)HOST_c2l(data, l); + X(8) = l; + R0(B, C, D, A, X(7), 22, 0xfd469501L); + (void)HOST_c2l(data, l); + X(9) = l; + R0(A, B, C, D, X(8), 7, 0x698098d8L); + (void)HOST_c2l(data, l); + X(10) = l; + R0(D, A, B, C, X(9), 12, 0x8b44f7afL); + (void)HOST_c2l(data, l); + X(11) = l; + R0(C, D, A, B, X(10), 17, 0xffff5bb1L); + (void)HOST_c2l(data, l); + X(12) = l; + R0(B, C, D, A, X(11), 22, 0x895cd7beL); + (void)HOST_c2l(data, l); + X(13) = l; + R0(A, B, C, D, X(12), 7, 0x6b901122L); + (void)HOST_c2l(data, l); + X(14) = l; + R0(D, A, B, C, X(13), 12, 0xfd987193L); + (void)HOST_c2l(data, l); + X(15) = l; + R0(C, D, A, B, X(14), 17, 0xa679438eL); + R0(B, C, D, A, X(15), 22, 0x49b40821L); + /* Round 1 */ + R1(A, B, C, D, X(1), 5, 0xf61e2562L); + R1(D, A, B, C, X(6), 9, 0xc040b340L); + R1(C, D, A, B, X(11), 14, 0x265e5a51L); + R1(B, C, D, A, X(0), 20, 0xe9b6c7aaL); + R1(A, B, C, D, X(5), 5, 0xd62f105dL); + R1(D, A, B, C, X(10), 9, 0x02441453L); + R1(C, D, A, B, X(15), 14, 0xd8a1e681L); + R1(B, C, D, A, X(4), 20, 0xe7d3fbc8L); + R1(A, B, C, D, X(9), 5, 0x21e1cde6L); + R1(D, A, B, C, X(14), 9, 0xc33707d6L); + R1(C, D, A, B, X(3), 14, 0xf4d50d87L); + R1(B, C, D, A, X(8), 20, 0x455a14edL); + R1(A, B, C, D, X(13), 5, 0xa9e3e905L); + R1(D, A, B, C, X(2), 9, 0xfcefa3f8L); + R1(C, D, A, B, X(7), 14, 0x676f02d9L); + R1(B, C, D, A, X(12), 20, 0x8d2a4c8aL); + /* Round 2 */ + R2(A, B, C, D, X(5), 4, 0xfffa3942L); + R2(D, A, B, C, X(8), 11, 0x8771f681L); + R2(C, D, A, B, X(11), 16, 0x6d9d6122L); + R2(B, C, D, A, X(14), 23, 0xfde5380cL); + R2(A, B, C, D, X(1), 4, 0xa4beea44L); + R2(D, A, B, C, X(4), 11, 0x4bdecfa9L); + R2(C, D, A, B, X(7), 16, 0xf6bb4b60L); + R2(B, C, D, A, X(10), 23, 0xbebfbc70L); + R2(A, B, C, D, X(13), 4, 0x289b7ec6L); + R2(D, A, B, C, X(0), 11, 0xeaa127faL); + R2(C, D, A, B, X(3), 16, 0xd4ef3085L); + R2(B, C, D, A, X(6), 23, 0x04881d05L); + R2(A, B, C, D, X(9), 4, 0xd9d4d039L); + R2(D, A, B, C, X(12), 11, 0xe6db99e5L); + R2(C, D, A, B, X(15), 16, 0x1fa27cf8L); + R2(B, C, D, A, X(2), 23, 0xc4ac5665L); + /* Round 3 */ + R3(A, B, C, D, X(0), 6, 0xf4292244L); + R3(D, A, B, C, X(7), 10, 0x432aff97L); + R3(C, D, A, B, X(14), 15, 0xab9423a7L); + R3(B, C, D, A, X(5), 21, 0xfc93a039L); + R3(A, B, C, D, X(12), 6, 0x655b59c3L); + R3(D, A, B, C, X(3), 10, 0x8f0ccc92L); + R3(C, D, A, B, X(10), 15, 0xffeff47dL); + R3(B, C, D, A, X(1), 21, 0x85845dd1L); + R3(A, B, C, D, X(8), 6, 0x6fa87e4fL); + R3(D, A, B, C, X(15), 10, 0xfe2ce6e0L); + R3(C, D, A, B, X(6), 15, 0xa3014314L); + R3(B, C, D, A, X(13), 21, 0x4e0811a1L); + R3(A, B, C, D, X(4), 6, 0xf7537e82L); + R3(D, A, B, C, X(11), 10, 0xbd3af235L); + R3(C, D, A, B, X(2), 15, 0x2ad7d2bbL); + R3(B, C, D, A, X(9), 21, 0xeb86d391L); + + A = c->A += A; + B = c->B += B; + C = c->C += C; + D = c->D += D; + } +} +#endif diff --git a/lib/erl_interface/src/openssl/crypto/md5/md5_local.h b/lib/erl_interface/src/openssl/crypto/md5/md5_local.h new file mode 100644 index 000000000000..7ec6b3595eae --- /dev/null +++ b/lib/erl_interface/src/openssl/crypto/md5/md5_local.h @@ -0,0 +1,98 @@ +/* + * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#if defined(ERLANG_OPENSSL_INTEGRATION) +#include "openssl/md5.h" +#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#undef MD5_ASM +#else +#include +#include +#endif + +#ifdef MD5_ASM +# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \ + defined(_M_X64) || defined(__aarch64__) +# define md5_block_data_order ossl_md5_block_asm_data_order +# elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) +# define md5_block_data_order ossl_md5_block_asm_data_order +# elif defined(__sparc) || defined(__sparc__) +# define md5_block_data_order ossl_md5_block_asm_data_order +# endif +#endif + +void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); + +#define DATA_ORDER_IS_LITTLE_ENDIAN + +#define HASH_LONG MD5_LONG +#define HASH_CTX MD5_CTX +#define HASH_CBLOCK MD5_CBLOCK +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define HASH_UPDATE MD5_UPDATE_FUNCTION_NAME +#define HASH_TRANSFORM MD5_TRANSFORM_FUNCTION_NAME +#define HASH_FINAL MD5_FINAL_FUNCTION_NAME +#else +#define HASH_UPDATE MD5_Update +#define HASH_TRANSFORM MD5_Transform +#define HASH_FINAL MD5_Final +#endif +#define HASH_MAKE_STRING(c,s) do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll,(s)); \ + ll=(c)->B; (void)HOST_l2c(ll,(s)); \ + ll=(c)->C; (void)HOST_l2c(ll,(s)); \ + ll=(c)->D; (void)HOST_l2c(ll,(s)); \ + } while (0) +#if defined(ERLANG_OPENSSL_INTEGRATION) +#define HASH_BLOCK_DATA_ORDER MD5_BLOCK_DATA_ORDER_FUNCTION_NAME +#undef md5_block_data_order +#else +#define HASH_BLOCK_DATA_ORDER md5_block_data_order +#endif + +#include "crypto/md32_common.h" + +/*- +#define F(x,y,z) (((x) & (y)) | ((~(x)) & (z))) +#define G(x,y,z) (((x) & (z)) | ((y) & (~(z)))) +*/ + +/* + * As pointed out by Wei Dai, the above can be simplified to the code + * below. Wei attributes these optimizations to Peter Gutmann's + * SHS code, and he attributes it to Rich Schroeppel. + */ +#define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) +#define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c)) +#define H(b,c,d) ((b) ^ (c) ^ (d)) +#define I(b,c,d) (((~(d)) | (b)) ^ (c)) + +#define R0(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+F((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R1(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+G((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R2(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+H((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; + +#define R3(a,b,c,d,k,s,t) { \ + a+=((k)+(t)+I((b),(c),(d))); \ + a=ROTATE(a,s); \ + a+=b; }; diff --git a/lib/erl_interface/src/openssl/include/crypto/md32_common.h b/lib/erl_interface/src/openssl/include/crypto/md32_common.h new file mode 100644 index 000000000000..1f68008c41e0 --- /dev/null +++ b/lib/erl_interface/src/openssl/include/crypto/md32_common.h @@ -0,0 +1,288 @@ +/* + * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/*- + * This is a generic 32 bit "collector" for message digest algorithms. + * Whenever needed it collects input character stream into chunks of + * 32 bit values and invokes a block function that performs actual hash + * calculations. + * + * Porting guide. + * + * Obligatory macros: + * + * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN + * this macro defines byte order of input stream. + * HASH_CBLOCK + * size of a unit chunk HASH_BLOCK operates on. + * HASH_LONG + * has to be at least 32 bit wide. + * HASH_CTX + * context structure that at least contains following + * members: + * typedef struct { + * ... + * HASH_LONG Nl,Nh; + * either { + * HASH_LONG data[HASH_LBLOCK]; + * unsigned char data[HASH_CBLOCK]; + * }; + * unsigned int num; + * ... + * } HASH_CTX; + * data[] vector is expected to be zeroed upon first call to + * HASH_UPDATE. + * HASH_UPDATE + * name of "Update" function, implemented here. + * HASH_TRANSFORM + * name of "Transform" function, implemented here. + * HASH_FINAL + * name of "Final" function, implemented here. + * HASH_BLOCK_DATA_ORDER + * name of "block" function capable of treating *unaligned* input + * message in original (data) byte order, implemented externally. + * HASH_MAKE_STRING + * macro converting context variables to an ASCII hash string. + * + * MD5 example: + * + * #define DATA_ORDER_IS_LITTLE_ENDIAN + * + * #define HASH_LONG MD5_LONG + * #define HASH_CTX MD5_CTX + * #define HASH_CBLOCK MD5_CBLOCK + * #define HASH_UPDATE MD5_Update + * #define HASH_TRANSFORM MD5_Transform + * #define HASH_FINAL MD5_Final + * #define HASH_BLOCK_DATA_ORDER md5_block_data_order + */ + +#if !defined(ERLANG_OPENSSL_INTEGRATION) +#include +#endif + +#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) +# error "DATA_ORDER must be defined!" +#endif + +#ifndef HASH_CBLOCK +# error "HASH_CBLOCK must be defined!" +#endif +#ifndef HASH_LONG +# error "HASH_LONG must be defined!" +#endif +#ifndef HASH_CTX +# error "HASH_CTX must be defined!" +#endif + +#ifndef HASH_UPDATE +# error "HASH_UPDATE must be defined!" +#endif +#ifndef HASH_TRANSFORM +# error "HASH_TRANSFORM must be defined!" +#endif +#ifndef HASH_FINAL +# error "HASH_FINAL must be defined!" +#endif + +#ifndef HASH_BLOCK_DATA_ORDER +# error "HASH_BLOCK_DATA_ORDER must be defined!" +#endif + +#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) + +#ifndef PEDANTIC +# if defined(__GNUC__) && __GNUC__>=2 && \ + !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) +# if defined(__riscv_zbb) || defined(__riscv_zbkb) +# if __riscv_xlen == 64 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("roriw %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# if __riscv_xlen == 32 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("rori %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# endif +# endif +#endif + +#if defined(DATA_ORDER_IS_BIG_ENDIAN) + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<<24) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>>24)&0xff), \ + l) + +#endif + +/* + * Time for some action :-) + */ + +int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) +{ + const unsigned char *data = data_; + unsigned char *p; + HASH_LONG l; + size_t n; + + if (len == 0) + return 1; + + l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL; + if (l < c->Nl) /* overflow */ + c->Nh++; + c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on + * 16-bit */ + c->Nl = l; + + n = c->num; + if (n != 0) { + p = (unsigned char *)c->data; + + if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { + memcpy(p + n, data, HASH_CBLOCK - n); + HASH_BLOCK_DATA_ORDER(c, p, 1); + n = HASH_CBLOCK - n; + data += n; + len -= n; + c->num = 0; + /* + * We use memset rather than OPENSSL_cleanse() here deliberately. + * Using OPENSSL_cleanse() here could be a performance issue. It + * will get properly cleansed on finalisation so this isn't a + * security problem. + */ + memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ + } else { + memcpy(p + n, data, len); + c->num += (unsigned int)len; + return 1; + } + } + + n = len / HASH_CBLOCK; + if (n > 0) { + HASH_BLOCK_DATA_ORDER(c, data, n); + n *= HASH_CBLOCK; + data += n; + len -= n; + } + + if (len != 0) { + p = (unsigned char *)c->data; + c->num = (unsigned int)len; + memcpy(p, data, len); + } + return 1; +} + +void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) +{ + HASH_BLOCK_DATA_ORDER(c, data, 1); +} + +int HASH_FINAL(unsigned char *md, HASH_CTX *c) +{ + unsigned char *p = (unsigned char *)c->data; + size_t n = c->num; + + p[n] = 0x80; /* there is always room for one */ + n++; + + if (n > (HASH_CBLOCK - 8)) { + memset(p + n, 0, HASH_CBLOCK - n); + n = 0; + HASH_BLOCK_DATA_ORDER(c, p, 1); + } + memset(p + n, 0, HASH_CBLOCK - 8 - n); + + p += HASH_CBLOCK - 8; +#if defined(DATA_ORDER_IS_BIG_ENDIAN) + (void)HOST_l2c(c->Nh, p); + (void)HOST_l2c(c->Nl, p); +#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) + (void)HOST_l2c(c->Nl, p); + (void)HOST_l2c(c->Nh, p); +#endif + p -= HASH_CBLOCK; + HASH_BLOCK_DATA_ORDER(c, p, 1); + c->num = 0; +#if defined(ERLANG_OPENSSL_INTEGRATION) + /* + * We are not using this anywhere where we consider + * this to be a security issue. + */ + memset(p, 0, HASH_CBLOCK); +#else + OPENSSL_cleanse(p, HASH_CBLOCK); +#endif + +#ifndef HASH_MAKE_STRING +# error "HASH_MAKE_STRING must be defined!" +#else + HASH_MAKE_STRING(c, md); +#endif + + return 1; +} + +#ifndef MD32_REG_T +# if defined(__alpha) || defined(__sparcv9) || defined(__mips) +# define MD32_REG_T long +/* + * This comment was originally written for MD5, which is why it + * discusses A-D. But it basically applies to all 32-bit digests, + * which is why it was moved to common header file. + * + * In case you wonder why A-D are declared as long and not + * as MD5_LONG. Doing so results in slight performance + * boost on LP64 architectures. The catch is we don't + * really care if 32 MSBs of a 64-bit register get polluted + * with eventual overflows as we *save* only 32 LSBs in + * *either* case. Now declaring 'em long excuses the compiler + * from keeping 32 MSBs zeroed resulting in 13% performance + * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. + * Well, to be honest it should say that this *prevents* + * performance degradation. + */ +# else +/* + * Above is not absolute and there are LP64 compilers that + * generate better code if MD32_REG_T is defined int. The above + * pre-processor condition reflects the circumstances under which + * the conclusion was made and is subject to further extension. + */ +# define MD32_REG_T int +# endif +#endif diff --git a/lib/erl_interface/src/openssl/include/erl_md5.h b/lib/erl_interface/src/openssl/include/erl_md5.h new file mode 100644 index 000000000000..4f8df4870f4d --- /dev/null +++ b/lib/erl_interface/src/openssl/include/erl_md5.h @@ -0,0 +1,35 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2023. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * %CopyrightEnd% + */ + +#ifndef ERL_MD5_H__ +#define ERL_MD5_H__ + +#undef ERLANG_OPENSSL_INTEGRATION +#define ERLANG_OPENSSL_INTEGRATION + +#define MD5_INIT_FUNCTION_NAME ei_MD5Init +#define MD5_UPDATE_FUNCTION_NAME ei_MD5Update +#define MD5_FINAL_FUNCTION_NAME ei_MD5Final +#define MD5_TRANSFORM_FUNCTION_NAME ei_MD5Transform +#define MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ei_MD5BlockDataOrder + +#include "openssl/md5.h" + +#endif diff --git a/lib/erl_interface/src/openssl/include/openssl/md5.h b/lib/erl_interface/src/openssl/include/openssl/md5.h new file mode 100644 index 000000000000..c9b42ecb115c --- /dev/null +++ b/lib/erl_interface/src/openssl/include/openssl/md5.h @@ -0,0 +1,82 @@ +/* + * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OPENSSL_MD5_H +# define OPENSSL_MD5_H +# pragma once + +#if defined(ERLANG_OPENSSL_INTEGRATION) + +#include "erl_md5.h" + +#undef OPENSSL_NO_MD5 +#undef OPENSSL_NO_DEPRECATED_3_0 + +#else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + +# include +# ifndef OPENSSL_NO_DEPRECATED_3_0 +# define HEADER_MD5_H +# endif + +# include + +#endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + +# ifndef OPENSSL_NO_MD5 +#if !defined(ERLANG_OPENSSL_INTEGRATION) +# include +#endif +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define MD5_DIGEST_LENGTH 16 + +# if !defined(OPENSSL_NO_DEPRECATED_3_0) +/* + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! MD5_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define MD5_LONG unsigned int + +# define MD5_CBLOCK 64 +# define MD5_LBLOCK (MD5_CBLOCK/4) + +typedef struct MD5state_st { + MD5_LONG A, B, C, D; + MD5_LONG Nl, Nh; + MD5_LONG data[MD5_LBLOCK]; + unsigned int num; +} MD5_CTX; +# endif +# ifndef OPENSSL_NO_DEPRECATED_3_0 +# if defined(ERLANG_OPENSSL_INTEGRATION) +int MD5_INIT_FUNCTION_NAME(MD5_CTX *c); +int MD5_UPDATE_FUNCTION_NAME(MD5_CTX *c, const void *data, size_t len); +int MD5_FINAL_FUNCTION_NAME(unsigned char *md, MD5_CTX *c); +void MD5_TRANSFORM_FUNCTION_NAME(MD5_CTX *c, const unsigned char *b); +# else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ +OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c); +OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len); +OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c); +OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n, + unsigned char *md); +OSSL_DEPRECATEDIN_3_0 void MD5_Transform(MD5_CTX *c, const unsigned char *b); +# endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ +# endif + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/lib/erl_interface/src/openssl/openssl.diff b/lib/erl_interface/src/openssl/openssl.diff new file mode 100644 index 000000000000..0e1d1545820f --- /dev/null +++ b/lib/erl_interface/src/openssl/openssl.diff @@ -0,0 +1,168 @@ +diff -u original/crypto/md5/md5_dgst.c crypto/md5/md5_dgst.c +--- original/crypto/md5/md5_dgst.c 2023-11-24 15:57:21.151284250 +0100 ++++ crypto/md5/md5_dgst.c 2023-11-24 23:08:56.359410118 +0100 +@@ -11,11 +11,15 @@ + * MD5 low level APIs are deprecated for public use, but still ok for + * internal use. + */ ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include "internal/deprecated.h" ++#endif + + #include + #include "md5_local.h" ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include ++#endif + + /* + * Implemented from RFC1321 The MD5 Message-Digest Algorithm +@@ -26,6 +30,10 @@ + #define INIT_DATA_C (unsigned long)0x98badcfeL + #define INIT_DATA_D (unsigned long)0x10325476L + ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define MD5_Init MD5_INIT_FUNCTION_NAME ++#endif ++ + int MD5_Init(MD5_CTX *c) + { + memset(c, 0, sizeof(*c)); +@@ -40,6 +48,9 @@ + # ifdef X + # undef X + # endif ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#endif + void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) + { + const unsigned char *data = data_; +diff -u original/crypto/md5/md5_local.h crypto/md5/md5_local.h +--- original/crypto/md5/md5_local.h 2023-11-24 15:57:11.999228660 +0100 ++++ crypto/md5/md5_local.h 2023-11-24 22:46:24.091772692 +0100 +@@ -9,8 +9,14 @@ + + #include + #include ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#include "openssl/md5.h" ++#define md5_block_data_order MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#undef MD5_ASM ++#else + #include + #include ++#endif + + #ifdef MD5_ASM + # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ +@@ -31,9 +37,15 @@ + #define HASH_LONG MD5_LONG + #define HASH_CTX MD5_CTX + #define HASH_CBLOCK MD5_CBLOCK ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define HASH_UPDATE MD5_UPDATE_FUNCTION_NAME ++#define HASH_TRANSFORM MD5_TRANSFORM_FUNCTION_NAME ++#define HASH_FINAL MD5_FINAL_FUNCTION_NAME ++#else + #define HASH_UPDATE MD5_Update + #define HASH_TRANSFORM MD5_Transform + #define HASH_FINAL MD5_Final ++#endif + #define HASH_MAKE_STRING(c,s) do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll,(s)); \ +@@ -41,7 +53,12 @@ + ll=(c)->C; (void)HOST_l2c(ll,(s)); \ + ll=(c)->D; (void)HOST_l2c(ll,(s)); \ + } while (0) ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++#define HASH_BLOCK_DATA_ORDER MD5_BLOCK_DATA_ORDER_FUNCTION_NAME ++#undef md5_block_data_order ++#else + #define HASH_BLOCK_DATA_ORDER md5_block_data_order ++#endif + + #include "crypto/md32_common.h" + +diff -u original/include/crypto/md32_common.h include/crypto/md32_common.h +--- original/include/crypto/md32_common.h 2023-11-24 15:58:01.910884892 +0100 ++++ include/crypto/md32_common.h 2023-11-24 22:49:19.072662368 +0100 +@@ -63,7 +63,9 @@ + * #define HASH_BLOCK_DATA_ORDER md5_block_data_order + */ + ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + #include ++#endif + + #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) + # error "DATA_ORDER must be defined!" +@@ -236,7 +238,15 @@ + p -= HASH_CBLOCK; + HASH_BLOCK_DATA_ORDER(c, p, 1); + c->num = 0; ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++ /* ++ * We are not using this anywhere where we consider ++ * this to be a security issue. ++ */ ++ memset(p, 0, HASH_CBLOCK); ++#else + OPENSSL_cleanse(p, HASH_CBLOCK); ++#endif + + #ifndef HASH_MAKE_STRING + # error "HASH_MAKE_STRING must be defined!" +diff -u original/include/openssl/md5.h include/openssl/md5.h +--- original/include/openssl/md5.h 2023-11-24 15:58:28.610171865 +0100 ++++ include/openssl/md5.h 2023-11-24 23:10:52.570854593 +0100 +@@ -11,6 +11,15 @@ + # define OPENSSL_MD5_H + # pragma once + ++#if defined(ERLANG_OPENSSL_INTEGRATION) ++ ++#include "erl_md5.h" ++ ++#undef OPENSSL_NO_MD5 ++#undef OPENSSL_NO_DEPRECATED_3_0 ++ ++#else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ ++ + # include + # ifndef OPENSSL_NO_DEPRECATED_3_0 + # define HEADER_MD5_H +@@ -18,8 +27,12 @@ + + # include + ++#endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ ++ + # ifndef OPENSSL_NO_MD5 ++#if !defined(ERLANG_OPENSSL_INTEGRATION) + # include ++#endif + # include + # ifdef __cplusplus + extern "C" { +@@ -46,12 +59,19 @@ + } MD5_CTX; + # endif + # ifndef OPENSSL_NO_DEPRECATED_3_0 ++# if defined(ERLANG_OPENSSL_INTEGRATION) ++int MD5_INIT_FUNCTION_NAME(MD5_CTX *c); ++int MD5_UPDATE_FUNCTION_NAME(MD5_CTX *c, const void *data, size_t len); ++int MD5_FINAL_FUNCTION_NAME(unsigned char *md, MD5_CTX *c); ++void MD5_TRANSFORM_FUNCTION_NAME(MD5_CTX *c, const unsigned char *b); ++# else /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c); + OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len); + OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c); + OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n, + unsigned char *md); + OSSL_DEPRECATEDIN_3_0 void MD5_Transform(MD5_CTX *c, const unsigned char *b); ++# endif /* !defined(ERLANG_OPENSSL_INTEGRATION) */ + # endif + + # ifdef __cplusplus diff --git a/lib/erl_interface/src/openssl/openssl.mk b/lib/erl_interface/src/openssl/openssl.mk new file mode 100644 index 000000000000..c4162f3e0691 --- /dev/null +++ b/lib/erl_interface/src/openssl/openssl.mk @@ -0,0 +1,23 @@ +# +# %CopyrightBegin% +# +# Copyright Ericsson AB 2023. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# %CopyrightEnd% +# + +OPENSSL_MD5_SOURCE_DIR = openssl/crypto/md5 +OPENSSL_SOURCES = $(OPENSSL_MD5_SOURCE_DIR)/md5_dgst.c +OPENSSL_VPATH = $(OPENSSL_MD5_SOURCE_DIR)