2
0

ecdh_ossl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 OpenSSL license (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_lcl.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()) == 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_NO_PRIVATE_VALUE);
  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_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  74. NID_X9_62_prime_field) {
  75. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, NULL, ctx)) {
  76. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
  77. goto err;
  78. }
  79. }
  80. #ifndef OPENSSL_NO_EC2M
  81. else {
  82. if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, NULL, ctx)) {
  83. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
  84. goto err;
  85. }
  86. }
  87. #endif
  88. buflen = (EC_GROUP_get_degree(group) + 7) / 8;
  89. len = BN_num_bytes(x);
  90. if (len > buflen) {
  91. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
  92. goto err;
  93. }
  94. if ((buf = OPENSSL_malloc(buflen)) == NULL) {
  95. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
  96. goto err;
  97. }
  98. memset(buf, 0, buflen - len);
  99. if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
  100. ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
  101. goto err;
  102. }
  103. *pout = buf;
  104. *poutlen = buflen;
  105. buf = NULL;
  106. ret = 1;
  107. err:
  108. EC_POINT_free(tmp);
  109. if (ctx)
  110. BN_CTX_end(ctx);
  111. BN_CTX_free(ctx);
  112. OPENSSL_free(buf);
  113. return ret;
  114. }