| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:38:44 +08:00
										 |  |  |  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:17:34 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:51:34 +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
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include "internal/cryptlib.h"
 | 
					
						
							|  |  |  | #include <openssl/asn1t.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  | #include <openssl/core_names.h>
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/evp.h>
 | 
					
						
							|  |  |  | #include <openssl/x509.h>
 | 
					
						
							|  |  |  | #include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  | #include "crypto/evp.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 01:07:10 +08:00
										 |  |  | #ifndef OPENSSL_NO_SCRYPT
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | /* PKCS#5 scrypt password based encryption structures */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASN1_SEQUENCE(SCRYPT_PARAMS) = { | 
					
						
							|  |  |  |         ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING), | 
					
						
							|  |  |  |         ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER), | 
					
						
							|  |  |  |         ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER), | 
					
						
							|  |  |  |         ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER), | 
					
						
							|  |  |  |         ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER), | 
					
						
							| 
									
										
										
										
											2017-07-26 21:05:59 +08:00
										 |  |  | } ASN1_SEQUENCE_END(SCRYPT_PARAMS) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:05:59 +08:00
										 |  |  | IMPLEMENT_ASN1_FUNCTIONS(SCRYPT_PARAMS) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen, | 
					
						
							|  |  |  |                                     size_t keylen, uint64_t N, uint64_t r, | 
					
						
							|  |  |  |                                     uint64_t p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher, | 
					
						
							|  |  |  |                                   const unsigned char *salt, int saltlen, | 
					
						
							|  |  |  |                                   unsigned char *aiv, uint64_t N, uint64_t r, | 
					
						
							|  |  |  |                                   uint64_t p) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-11 06:28:10 +08:00
										 |  |  |     X509_ALGOR *scheme = NULL, *ret = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     int alg_nid; | 
					
						
							|  |  |  |     size_t keylen = 0; | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     EVP_CIPHER_CTX *ctx = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     unsigned char iv[EVP_MAX_IV_LENGTH]; | 
					
						
							|  |  |  |     PBE2PARAM *pbe2 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!cipher) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SCRYPT_PARAMETERS); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												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
										 |  |  |     alg_nid = EVP_CIPHER_get_type(cipher); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     if (alg_nid == NID_undef) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-11 06:28:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     pbe2 = PBE2PARAM_new(); | 
					
						
							|  |  |  |     if (pbe2 == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Setup the AlgorithmIdentifier for the encryption scheme */ | 
					
						
							|  |  |  |     scheme = pbe2->encryption; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-11 06:28:10 +08:00
										 |  |  |     scheme->algorithm = OBJ_nid2obj(alg_nid); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     scheme->parameter = ASN1_TYPE_new(); | 
					
						
							|  |  |  |     if (scheme->parameter == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Create random IV */ | 
					
						
							| 
									
										
										
											
												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 (EVP_CIPHER_get_iv_length(cipher)) { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         if (aiv) | 
					
						
							| 
									
										
										
											
												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
										 |  |  |             memcpy(iv, aiv, EVP_CIPHER_get_iv_length(cipher)); | 
					
						
							|  |  |  |         else if (RAND_bytes(iv, EVP_CIPHER_get_iv_length(cipher)) <= 0) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     ctx = EVP_CIPHER_CTX_new(); | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Dummy cipherinit to just setup the IV */ | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2018-05-11 03:14:12 +08:00
										 |  |  |     if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     EVP_CIPHER_CTX_free(ctx); | 
					
						
							|  |  |  |     ctx = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* If its RC2 then we'd better setup the key length */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (alg_nid == NID_rc2_cbc) | 
					
						
							| 
									
										
										
											
												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
										 |  |  |         keylen = EVP_CIPHER_get_key_length(cipher); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Setup keyfunc */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     X509_ALGOR_free(pbe2->keyfunc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pbe2->keyfunc == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Now set up top level AlgorithmIdentifier */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = X509_ALGOR_new(); | 
					
						
							|  |  |  |     if (ret == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret->algorithm = OBJ_nid2obj(NID_pbes2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Encode PBE2PARAM into parameter */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2, | 
					
						
							|  |  |  |                                 &ret->parameter) == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PBE2PARAM_free(pbe2); | 
					
						
							|  |  |  |     pbe2 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  merr: | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |     ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     PBE2PARAM_free(pbe2); | 
					
						
							|  |  |  |     X509_ALGOR_free(ret); | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     EVP_CIPHER_CTX_free(ctx); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen, | 
					
						
							|  |  |  |                                     size_t keylen, uint64_t N, uint64_t r, | 
					
						
							|  |  |  |                                     uint64_t p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     X509_ALGOR *keyfunc = NULL; | 
					
						
							| 
									
										
										
										
											2016-11-11 06:28:10 +08:00
										 |  |  |     SCRYPT_PARAMS *sparam = SCRYPT_PARAMS_new(); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (sparam == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!saltlen) | 
					
						
							|  |  |  |         saltlen = PKCS5_SALT_LEN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* This will either copy salt or grow the buffer */ | 
					
						
							|  |  |  |     if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If have a key len set it up */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (keylen > 0) { | 
					
						
							|  |  |  |         sparam->keyLength = ASN1_INTEGER_new(); | 
					
						
							|  |  |  |         if (sparam->keyLength == NULL) | 
					
						
							|  |  |  |             goto merr; | 
					
						
							|  |  |  |         if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0) | 
					
						
							|  |  |  |             goto merr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Finally setup the keyfunc structure */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     keyfunc = X509_ALGOR_new(); | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |     if (keyfunc == NULL) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Encode SCRYPT_PARAMS into parameter of pbe2 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam, | 
					
						
							|  |  |  |                                 &keyfunc->parameter) == NULL) | 
					
						
							|  |  |  |         goto merr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SCRYPT_PARAMS_free(sparam); | 
					
						
							|  |  |  |     return keyfunc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  merr: | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |     ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |  err: | 
					
						
							|  |  |  |     SCRYPT_PARAMS_free(sparam); | 
					
						
							|  |  |  |     X509_ALGOR_free(keyfunc); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  | int PKCS5_v2_scrypt_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, | 
					
						
							|  |  |  |                                 int passlen, ASN1_TYPE *param, | 
					
						
							|  |  |  |                                 const EVP_CIPHER *c, const EVP_MD *md, int en_de, | 
					
						
							|  |  |  |                                 OSSL_LIB_CTX *libctx, const char *propq) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char *salt, key[EVP_MAX_KEY_LENGTH]; | 
					
						
							|  |  |  |     uint64_t p, r, N; | 
					
						
							|  |  |  |     size_t saltlen; | 
					
						
							|  |  |  |     size_t keylen = 0; | 
					
						
							| 
									
										
										
										
											2019-05-07 08:45:57 +08:00
										 |  |  |     int t, rv = 0; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |     SCRYPT_PARAMS *sparam = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |     if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Decode parameter */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sparam == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												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
										 |  |  |     t = EVP_CIPHER_CTX_get_key_length(ctx); | 
					
						
							| 
									
										
										
										
											2019-05-07 08:45:57 +08:00
										 |  |  |     if (t < 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); | 
					
						
							| 
									
										
										
										
											2019-05-07 08:45:57 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     keylen = t; | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Now check the parameters of sparam */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sparam->keyLength) { | 
					
						
							|  |  |  |         uint64_t spkeylen; | 
					
						
							|  |  |  |         if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0) | 
					
						
							|  |  |  |             || (spkeylen != keylen)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* Check all parameters fit in uint64_t and are acceptable to scrypt */ | 
					
						
							|  |  |  |     if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0 | 
					
						
							|  |  |  |         || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0 | 
					
						
							|  |  |  |         || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0 | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  |         || EVP_PBE_scrypt_ex(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0, | 
					
						
							|  |  |  |                              libctx, propq) == 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EVP, EVP_R_ILLEGAL_SCRYPT_PARAMETERS); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* it seems that its all OK */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     salt = sparam->salt->data; | 
					
						
							|  |  |  |     saltlen = sparam->salt->length; | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  |     if (EVP_PBE_scrypt_ex(pass, passlen, salt, saltlen, N, r, p, 0, key, | 
					
						
							|  |  |  |                           keylen, libctx, propq) == 0) | 
					
						
							| 
									
										
										
										
											2015-05-20 20:23:06 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de); | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     if (keylen) | 
					
						
							|  |  |  |         OPENSSL_cleanse(key, keylen); | 
					
						
							|  |  |  |     SCRYPT_PARAMS_free(sparam); | 
					
						
							|  |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-02-17 15:56:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, | 
					
						
							|  |  |  |                              int passlen, ASN1_TYPE *param, | 
					
						
							|  |  |  |                              const EVP_CIPHER *c, const EVP_MD *md, int en_de) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PKCS5_v2_scrypt_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 01:07:10 +08:00
										 |  |  | #endif /* OPENSSL_NO_SCRYPT */
 |