| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-08 20:04:41 +08:00
										 |  |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:40:06 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  |  * 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
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 07:38:09 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * RC4 low level APIs are deprecated for public use, but still ok for internal | 
					
						
							|  |  |  |  * use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2003-03-21 07:29:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef OPENSSL_NO_RC4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-24 06:13:45 +08:00
										 |  |  | # include <openssl/evp.h>
 | 
					
						
							|  |  |  | # include <openssl/objects.h>
 | 
					
						
							| 
									
										
										
										
											2001-07-31 07:57:25 +08:00
										 |  |  | # include <openssl/rc4.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | # include "crypto/evp.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-19 00:01:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-31 07:57:25 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     RC4_KEY ks;                 /* working key */ | 
					
						
							|  |  |  | } EVP_RC4_KEY; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 18:17:27 +08:00
										 |  |  | # define data(ctx) ((EVP_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-03 22:13:58 +08:00
										 |  |  | static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
					
						
							|  |  |  |                         const unsigned char *iv, int enc); | 
					
						
							| 
									
										
										
										
											2000-05-27 20:38:43 +08:00
										 |  |  | static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
					
						
							| 
									
										
										
										
											2008-11-01 03:48:25 +08:00
										 |  |  |                       const unsigned char *in, size_t inl); | 
					
						
							| 
									
										
										
										
											2001-03-09 10:51:02 +08:00
										 |  |  | static const EVP_CIPHER r4_cipher = { | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     NID_rc4, | 
					
						
							|  |  |  |     1, EVP_RC4_KEY_SIZE, 0, | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     EVP_CIPH_VARIABLE_LENGTH, | 
					
						
							| 
									
										
											  
											
												Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new.  Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field.  The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md().  Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md().  Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
        if (flags & x) --> if ((flags & x) != 0)
        if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
											
										 
											2021-02-17 06:51:56 +08:00
										 |  |  |     EVP_ORIG_GLOBAL, | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     rc4_init_key, | 
					
						
							|  |  |  |     rc4_cipher, | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2001-07-31 07:57:25 +08:00
										 |  |  |     sizeof(EVP_RC4_KEY), | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2005-08-29 06:49:57 +08:00
										 |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     NULL | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-09 10:51:02 +08:00
										 |  |  | static const EVP_CIPHER r4_40_cipher = { | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  |     NID_rc4_40, | 
					
						
							|  |  |  |     1, 5 /* 40 bit */ , 0, | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     EVP_CIPH_VARIABLE_LENGTH, | 
					
						
							| 
									
										
											  
											
												Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new.  Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field.  The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md().  Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md().  Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
        if (flags & x) --> if ((flags & x) != 0)
        if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
											
										 
											2021-02-17 06:51:56 +08:00
										 |  |  |     EVP_ORIG_GLOBAL, | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  |     rc4_init_key, | 
					
						
							|  |  |  |     rc4_cipher, | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2001-07-31 07:57:25 +08:00
										 |  |  |     sizeof(EVP_RC4_KEY), | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2005-08-29 06:49:57 +08:00
										 |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2000-05-27 07:51:35 +08:00
										 |  |  |     NULL | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-09 10:51:02 +08:00
										 |  |  | const EVP_CIPHER *EVP_rc4(void) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 22:04:09 +08:00
										 |  |  |     return &r4_cipher; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-09 10:51:02 +08:00
										 |  |  | const EVP_CIPHER *EVP_rc4_40(void) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 22:04:09 +08:00
										 |  |  |     return &r4_40_cipher; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-03 22:13:58 +08:00
										 |  |  | static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
					
						
							|  |  |  |                         const unsigned char *iv, int enc) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-19 12:50:11 +08:00
										 |  |  |     int keylen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) <= 0) | 
					
						
							| 
									
										
										
										
											2021-03-19 12:50:11 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     RC4_set_key(&data(ctx)->ks, keylen, key); | 
					
						
							| 
									
										
										
										
											2000-05-27 20:38:43 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-27 20:38:43 +08:00
										 |  |  | static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
					
						
							| 
									
										
										
										
											2008-11-01 03:48:25 +08:00
										 |  |  |                       const unsigned char *in, size_t inl) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-07-31 07:57:25 +08:00
										 |  |  |     RC4(&data(ctx)->ks, inl, in, out); | 
					
						
							| 
									
										
										
										
											2000-05-27 20:38:43 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 |