kdf_lib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_EVP_LIB);
  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. return NULL;
  57. memcpy(dst, src, sizeof(*dst));
  58. if (!EVP_KDF_up_ref(dst->meth)) {
  59. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  60. OPENSSL_free(dst);
  61. return NULL;
  62. }
  63. dst->algctx = src->meth->dupctx(src->algctx);
  64. if (dst->algctx == NULL) {
  65. EVP_KDF_CTX_free(dst);
  66. return NULL;
  67. }
  68. return dst;
  69. }
  70. int evp_kdf_get_number(const EVP_KDF *kdf)
  71. {
  72. return kdf->name_id;
  73. }
  74. const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
  75. {
  76. return kdf->type_name;
  77. }
  78. const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
  79. {
  80. return kdf->description;
  81. }
  82. int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
  83. {
  84. return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name);
  85. }
  86. const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
  87. {
  88. return kdf->prov;
  89. }
  90. const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
  91. {
  92. return ctx->meth;
  93. }
  94. void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
  95. {
  96. if (ctx == NULL)
  97. return;
  98. if (ctx->meth->reset != NULL)
  99. ctx->meth->reset(ctx->algctx);
  100. }
  101. size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
  102. {
  103. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  104. size_t s = 0;
  105. if (ctx == NULL)
  106. return 0;
  107. *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
  108. if (ctx->meth->get_ctx_params != NULL
  109. && ctx->meth->get_ctx_params(ctx->algctx, params))
  110. return s;
  111. if (ctx->meth->get_params != NULL
  112. && ctx->meth->get_params(params))
  113. return s;
  114. return 0;
  115. }
  116. int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
  117. const OSSL_PARAM params[])
  118. {
  119. if (ctx == NULL)
  120. return 0;
  121. return ctx->meth->derive(ctx->algctx, key, keylen, params);
  122. }
  123. /*
  124. * The {get,set}_params functions return 1 if there is no corresponding
  125. * function in the implementation. This is the same as if there was one,
  126. * but it didn't recognise any of the given params, i.e. nothing in the
  127. * bag of parameters was useful.
  128. */
  129. int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
  130. {
  131. if (kdf->get_params != NULL)
  132. return kdf->get_params(params);
  133. return 1;
  134. }
  135. int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
  136. {
  137. if (ctx->meth->get_ctx_params != NULL)
  138. return ctx->meth->get_ctx_params(ctx->algctx, params);
  139. return 1;
  140. }
  141. int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
  142. {
  143. if (ctx->meth->set_ctx_params != NULL)
  144. return ctx->meth->set_ctx_params(ctx->algctx, params);
  145. return 1;
  146. }
  147. int EVP_KDF_names_do_all(const EVP_KDF *kdf,
  148. void (*fn)(const char *name, void *data),
  149. void *data)
  150. {
  151. if (kdf->prov != NULL)
  152. return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
  153. return 1;
  154. }