dh_kdf.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2013-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "e_os.h"
  10. #ifndef OPENSSL_NO_CMS
  11. # include <string.h>
  12. # include <openssl/core_names.h>
  13. # include <openssl/dh.h>
  14. # include <openssl/evp.h>
  15. # include <openssl/asn1.h>
  16. # include <openssl/kdf.h>
  17. # include <internal/provider.h>
  18. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  19. const unsigned char *Z, size_t Zlen,
  20. ASN1_OBJECT *key_oid,
  21. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
  22. {
  23. int ret = 0, nid;
  24. EVP_KDF_CTX *kctx = NULL;
  25. EVP_KDF *kdf = NULL;
  26. const char *oid_sn;
  27. OSSL_PARAM params[5], *p = params;
  28. const char *mdname = EVP_MD_name(md);
  29. const OSSL_PROVIDER *prov = EVP_MD_provider(md);
  30. OPENSSL_CTX *provctx = ossl_provider_library_context(prov);
  31. nid = OBJ_obj2nid(key_oid);
  32. if (nid == NID_undef)
  33. return 0;
  34. oid_sn = OBJ_nid2sn(nid);
  35. if (oid_sn == NULL)
  36. return 0;
  37. kdf = EVP_KDF_fetch(provctx, OSSL_KDF_NAME_X942KDF, NULL);
  38. if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL)
  39. goto err;
  40. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  41. (char *)mdname, strlen(mdname) + 1);
  42. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  43. (unsigned char *)Z, Zlen);
  44. if (ukm != NULL)
  45. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM,
  46. (unsigned char *)ukm, ukmlen);
  47. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
  48. (char *)oid_sn, strlen(oid_sn) + 1);
  49. *p = OSSL_PARAM_construct_end();
  50. ret = EVP_KDF_CTX_set_params(kctx, params) > 0
  51. && EVP_KDF_derive(kctx, out, outlen) > 0;
  52. err:
  53. EVP_KDF_CTX_free(kctx);
  54. EVP_KDF_free(kdf);
  55. return ret;
  56. }
  57. #endif /* OPENSSL_NO_CMS */