ecdh_ossl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, 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 <string.h>
  11. #include <limits.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/err.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/ec.h>
  17. #include "ec_local.h"
  18. int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
  19. const EC_POINT *pub_key, const EC_KEY *ecdh)
  20. {
  21. if (ecdh->group->meth->ecdh_compute_key == NULL) {
  22. ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
  23. return 0;
  24. }
  25. return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
  26. }
  27. /*-
  28. * This implementation is based on the following primitives in the IEEE 1363 standard:
  29. * - ECKAS-DH1
  30. * - ECSVDP-DH
  31. */
  32. int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
  33. const EC_POINT *pub_key, const EC_KEY *ecdh)
  34. {
  35. BN_CTX *ctx;
  36. EC_POINT *tmp = NULL;
  37. BIGNUM *x = NULL;
  38. const BIGNUM *priv_key;
  39. const EC_GROUP *group;
  40. int ret = 0;
  41. size_t buflen, len;
  42. unsigned char *buf = NULL;
  43. if ((ctx = BN_CTX_new_ex(ecdh->libctx)) == NULL)
  44. goto err;
  45. BN_CTX_start(ctx);
  46. x = BN_CTX_get(ctx);
  47. if (x == NULL) {
  48. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
  49. goto err;
  50. }
  51. priv_key = EC_KEY_get0_private_key(ecdh);
  52. if (priv_key == NULL) {
  53. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_MISSING_PRIVATE_KEY);
  54. goto err;
  55. }
  56. group = EC_KEY_get0_group(ecdh);
  57. if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
  58. if (!EC_GROUP_get_cofactor(group, x, NULL) ||
  59. !BN_mul(x, x, priv_key, ctx)) {
  60. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
  61. goto err;
  62. }
  63. priv_key = x;
  64. }
  65. if ((tmp = EC_POINT_new(group)) == NULL) {
  66. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
  67. goto err;
  68. }
  69. if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
  70. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
  71. goto err;
  72. }
  73. if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) {
  74. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
  75. goto err;
  76. }
  77. buflen = (EC_GROUP_get_degree(group) + 7) / 8;
  78. len = BN_num_bytes(x);
  79. if (len > buflen) {
  80. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
  81. goto err;
  82. }
  83. if ((buf = OPENSSL_malloc(buflen)) == NULL) {
  84. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
  85. goto err;
  86. }
  87. memset(buf, 0, buflen - len);
  88. if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
  89. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
  90. goto err;
  91. }
  92. *pout = buf;
  93. *poutlen = buflen;
  94. buf = NULL;
  95. ret = 1;
  96. err:
  97. EC_POINT_clear_free(tmp);
  98. BN_CTX_end(ctx);
  99. BN_CTX_free(ctx);
  100. OPENSSL_free(buf);
  101. return ret;
  102. }