kdf_lib.c 4.4 KB

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