2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include "internal/cryptlib.h"
14 #include <openssl/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/kdf.h>
18 #include "internal/asn1_int.h"
19 #include "internal/evp_int.h"
20 #include "internal/numbers.h"
23 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
25 /* This array needs to be in order of NIDs */
26 static const EVP_KDF_METHOD *standard_methods[] = {
28 #ifndef OPENSSL_NO_SCRYPT
36 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
39 static int kmeth_cmp(const EVP_KDF_METHOD *const *a,
40 const EVP_KDF_METHOD *const *b)
42 return ((*a)->type - (*b)->type);
45 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
48 static const EVP_KDF_METHOD *kdf_meth_find(int type)
51 const EVP_KDF_METHOD *t = &tmp, **ret;
54 ret = OBJ_bsearch_kmeth(&t, standard_methods,
55 OSSL_NELEM(standard_methods));
56 if (ret == NULL || *ret == NULL)
62 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
65 const EVP_KDF_METHOD *kmeth;
67 kmeth = kdf_meth_find(id);
69 EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, EVP_R_UNSUPPORTED_ALGORITHM);
73 ret = OPENSSL_zalloc(sizeof(*ret));
75 EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, ERR_R_MALLOC_FAILURE);
79 if (kmeth->new != NULL && (ret->impl = kmeth->new()) == NULL) {
80 EVP_KDF_CTX_free(ret);
88 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
93 ctx->kmeth->free(ctx->impl);
97 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
102 if (ctx->kmeth->reset != NULL)
103 ctx->kmeth->reset(ctx->impl);
106 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
112 ret = EVP_KDF_vctrl(ctx, cmd, args);
116 EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
121 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
126 return ctx->kmeth->ctrl(ctx->impl, cmd, args);
129 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
136 if (ctx->kmeth->ctrl_str == NULL) {
137 EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
141 ret = ctx->kmeth->ctrl_str(ctx->impl, type, value);
143 EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
148 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
153 if (ctx->kmeth->size == NULL)
156 return ctx->kmeth->size(ctx->impl);
159 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
164 return ctx->kmeth->derive(ctx->impl, key, keylen);