2
0

kdf_lib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  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
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/engine.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/kdf.h>
  17. #include <openssl/core.h>
  18. #include <openssl/core_names.h>
  19. #include "crypto/asn1.h"
  20. #include "crypto/evp.h"
  21. #include "internal/numbers.h"
  22. #include "internal/provider.h"
  23. #include "evp_local.h"
  24. EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
  25. {
  26. EVP_KDF_CTX *ctx = NULL;
  27. if (kdf == NULL)
  28. return NULL;
  29. ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
  30. if (ctx == NULL
  31. || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
  32. || !EVP_KDF_up_ref(kdf)) {
  33. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  34. if (ctx != NULL)
  35. kdf->freectx(ctx->algctx);
  36. OPENSSL_free(ctx);
  37. ctx = NULL;
  38. } else {
  39. ctx->meth = kdf;
  40. }
  41. return ctx;
  42. }
  43. void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
  44. {
  45. if (ctx == NULL)
  46. return;
  47. ctx->meth->freectx(ctx->algctx);
  48. ctx->algctx = NULL;
  49. EVP_KDF_free(ctx->meth);
  50. OPENSSL_free(ctx);
  51. }
  52. EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
  53. {
  54. EVP_KDF_CTX *dst;
  55. if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
  56. return NULL;
  57. dst = OPENSSL_malloc(sizeof(*dst));
  58. if (dst == NULL) {
  59. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  60. return NULL;
  61. }
  62. memcpy(dst, src, sizeof(*dst));
  63. if (!EVP_KDF_up_ref(dst->meth)) {
  64. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  65. OPENSSL_free(dst);
  66. return NULL;
  67. }
  68. dst->algctx = src->meth->dupctx(src->algctx);
  69. if (dst->algctx == NULL) {
  70. EVP_KDF_CTX_free(dst);
  71. return NULL;
  72. }
  73. return dst;
  74. }
  75. int evp_kdf_get_number(const EVP_KDF *kdf)
  76. {
  77. return kdf->name_id;
  78. }
  79. const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
  80. {
  81. return kdf->type_name;
  82. }
  83. const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
  84. {
  85. return kdf->description;
  86. }
  87. int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
  88. {
  89. return evp_is_a(kdf->prov, kdf->name_id, NULL, name);
  90. }
  91. const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
  92. {
  93. return kdf->prov;
  94. }
  95. const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
  96. {
  97. return ctx->meth;
  98. }
  99. void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
  100. {
  101. if (ctx == NULL)
  102. return;
  103. if (ctx->meth->reset != NULL)
  104. ctx->meth->reset(ctx->algctx);
  105. }
  106. size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
  107. {
  108. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  109. size_t s = 0;
  110. if (ctx == NULL)
  111. return 0;
  112. *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
  113. if (ctx->meth->get_ctx_params != NULL
  114. && ctx->meth->get_ctx_params(ctx->algctx, params))
  115. return s;
  116. if (ctx->meth->get_params != NULL
  117. && ctx->meth->get_params(params))
  118. return s;
  119. return 0;
  120. }
  121. int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
  122. const OSSL_PARAM params[])
  123. {
  124. if (ctx == NULL)
  125. return 0;
  126. return ctx->meth->derive(ctx->algctx, key, keylen, params);
  127. }
  128. /*
  129. * The {get,set}_params functions return 1 if there is no corresponding
  130. * function in the implementation. This is the same as if there was one,
  131. * but it didn't recognise any of the given params, i.e. nothing in the
  132. * bag of parameters was useful.
  133. */
  134. int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
  135. {
  136. if (kdf->get_params != NULL)
  137. return kdf->get_params(params);
  138. return 1;
  139. }
  140. int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
  141. {
  142. if (ctx->meth->get_ctx_params != NULL)
  143. return ctx->meth->get_ctx_params(ctx->algctx, params);
  144. return 1;
  145. }
  146. int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
  147. {
  148. if (ctx->meth->set_ctx_params != NULL)
  149. return ctx->meth->set_ctx_params(ctx->algctx, params);
  150. return 1;
  151. }
  152. int EVP_KDF_names_do_all(const EVP_KDF *kdf,
  153. void (*fn)(const char *name, void *data),
  154. void *data)
  155. {
  156. if (kdf->prov != NULL)
  157. return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
  158. return 1;
  159. }