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
35 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
38 static int kmeth_cmp(const EVP_KDF_METHOD *const *a,
39 const EVP_KDF_METHOD *const *b)
41 return ((*a)->type - (*b)->type);
44 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
47 static const EVP_KDF_METHOD *kdf_meth_find(int type)
50 const EVP_KDF_METHOD *t = &tmp, **ret;
53 ret = OBJ_bsearch_kmeth(&t, standard_methods,
54 OSSL_NELEM(standard_methods));
55 if (ret == NULL || *ret == NULL)
61 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
64 const EVP_KDF_METHOD *kmeth;
66 kmeth = kdf_meth_find(id);
68 EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, EVP_R_UNSUPPORTED_ALGORITHM);
72 ret = OPENSSL_zalloc(sizeof(*ret));
74 EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, ERR_R_MALLOC_FAILURE);
78 if (kmeth->new != NULL && (ret->impl = kmeth->new()) == NULL) {
79 EVP_KDF_CTX_free(ret);
87 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
92 ctx->kmeth->free(ctx->impl);
96 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
101 if (ctx->kmeth->reset != NULL)
102 ctx->kmeth->reset(ctx->impl);
105 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
111 ret = EVP_KDF_vctrl(ctx, cmd, args);
115 EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
120 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
125 return ctx->kmeth->ctrl(ctx->impl, cmd, args);
128 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
135 if (ctx->kmeth->ctrl_str == NULL) {
136 EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
140 ret = ctx->kmeth->ctrl_str(ctx->impl, type, value);
142 EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
147 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
152 if (ctx->kmeth->size == NULL)
155 return ctx->kmeth->size(ctx->impl);
158 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
163 return ctx->kmeth->derive(ctx->impl, key, keylen);