| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |  * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							|  |  |  |  * Copyright (c) 2018-2019, Oracle and/or its affiliates.  All rights reserved. | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include "internal/cryptlib.h"
 | 
					
						
							|  |  |  | #include <openssl/engine.h>
 | 
					
						
							|  |  |  | #include <openssl/evp.h>
 | 
					
						
							|  |  |  | #include <openssl/x509v3.h>
 | 
					
						
							|  |  |  | #include <openssl/kdf.h>
 | 
					
						
							|  |  |  | #include "internal/asn1_int.h"
 | 
					
						
							|  |  |  | #include "internal/evp_int.h"
 | 
					
						
							|  |  |  | #include "internal/numbers.h"
 | 
					
						
							|  |  |  | #include "evp_locl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  | EVP_KDF_CTX *EVP_KDF_CTX_new(const EVP_KDF *kdf) | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-16 09:43:41 +08:00
										 |  |  |     EVP_KDF_CTX *ctx = NULL; | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 09:43:41 +08:00
										 |  |  |     if (kdf == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     if (ctx == NULL || (ctx->impl = kdf->new()) == NULL) { | 
					
						
							|  |  |  |         EVPerr(EVP_F_EVP_KDF_CTX_NEW, ERR_R_MALLOC_FAILURE); | 
					
						
							|  |  |  |         OPENSSL_free(ctx); | 
					
						
							|  |  |  |         ctx = NULL; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ctx->meth = kdf; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ctx; | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  | EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id) | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     const EVP_KDF *kdf = EVP_get_kdfbynid(id); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     return EVP_KDF_CTX_new(kdf); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  | int EVP_KDF_nid(const EVP_KDF *kdf) | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     return kdf->type; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  | const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ctx->meth; | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     ctx->meth->free(ctx->impl); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  |     OPENSSL_free(ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void EVP_KDF_reset(EVP_KDF_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     if (ctx->meth->reset != NULL) | 
					
						
							|  |  |  |         ctx->meth->reset(ctx->impl); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     va_list args; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     va_start(args, cmd); | 
					
						
							|  |  |  |     ret = EVP_KDF_vctrl(ctx, cmd, args); | 
					
						
							|  |  |  |     va_end(args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ret == -2) | 
					
						
							|  |  |  |         EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     return ctx->meth->ctrl(ctx->impl, cmd, args); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     if (ctx->meth->ctrl_str == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  |         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | 
					
						
							|  |  |  |         return -2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     ret = ctx->meth->ctrl_str(ctx->impl, type, value); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  |     if (ret == -2) | 
					
						
							|  |  |  |         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t EVP_KDF_size(EVP_KDF_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     if (ctx->meth->size == NULL) | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  |         return SIZE_MAX; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     return ctx->meth->size(ctx->impl); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:18:56 +08:00
										 |  |  |     return ctx->meth->derive(ctx->impl, key, keylen); | 
					
						
							| 
									
										
										
										
											2018-06-22 05:16:18 +08:00
										 |  |  | } |