ec_oct.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2011-2021 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. /*
  11. * ECDSA low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include <openssl/err.h>
  17. #include <openssl/opensslv.h>
  18. #include "ec_local.h"
  19. int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
  20. const BIGNUM *x, int y_bit, BN_CTX *ctx)
  21. {
  22. if (group->meth->point_set_compressed_coordinates == NULL
  23. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  24. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  25. return 0;
  26. }
  27. if (!ec_point_is_compat(point, group)) {
  28. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  29. return 0;
  30. }
  31. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  32. if (group->meth->field_type == NID_X9_62_prime_field)
  33. return ossl_ec_GFp_simple_set_compressed_coordinates(group, point, x,
  34. y_bit, ctx);
  35. else
  36. #ifdef OPENSSL_NO_EC2M
  37. {
  38. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  39. return 0;
  40. }
  41. #else
  42. return ossl_ec_GF2m_simple_set_compressed_coordinates(group, point,
  43. x, y_bit, ctx);
  44. #endif
  45. }
  46. return group->meth->point_set_compressed_coordinates(group, point, x,
  47. y_bit, ctx);
  48. }
  49. #ifndef OPENSSL_NO_DEPRECATED_3_0
  50. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  51. EC_POINT *point, const BIGNUM *x,
  52. int y_bit, BN_CTX *ctx)
  53. {
  54. return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
  55. }
  56. # ifndef OPENSSL_NO_EC2M
  57. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  58. EC_POINT *point, const BIGNUM *x,
  59. int y_bit, BN_CTX *ctx)
  60. {
  61. return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
  62. }
  63. # endif
  64. #endif
  65. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  66. point_conversion_form_t form, unsigned char *buf,
  67. size_t len, BN_CTX *ctx)
  68. {
  69. if (group->meth->point2oct == 0
  70. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  71. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  72. return 0;
  73. }
  74. if (!ec_point_is_compat(point, group)) {
  75. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  76. return 0;
  77. }
  78. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  79. if (group->meth->field_type == NID_X9_62_prime_field)
  80. return ossl_ec_GFp_simple_point2oct(group, point, form, buf, len,
  81. ctx);
  82. else
  83. #ifdef OPENSSL_NO_EC2M
  84. {
  85. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  86. return 0;
  87. }
  88. #else
  89. return ossl_ec_GF2m_simple_point2oct(group, point,
  90. form, buf, len, ctx);
  91. #endif
  92. }
  93. return group->meth->point2oct(group, point, form, buf, len, ctx);
  94. }
  95. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  96. const unsigned char *buf, size_t len, BN_CTX *ctx)
  97. {
  98. if (group->meth->oct2point == 0
  99. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  100. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  101. return 0;
  102. }
  103. if (!ec_point_is_compat(point, group)) {
  104. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  105. return 0;
  106. }
  107. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  108. if (group->meth->field_type == NID_X9_62_prime_field)
  109. return ossl_ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  110. else
  111. #ifdef OPENSSL_NO_EC2M
  112. {
  113. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  114. return 0;
  115. }
  116. #else
  117. return ossl_ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
  118. #endif
  119. }
  120. return group->meth->oct2point(group, point, buf, len, ctx);
  121. }
  122. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  123. point_conversion_form_t form,
  124. unsigned char **pbuf, BN_CTX *ctx)
  125. {
  126. size_t len;
  127. unsigned char *buf;
  128. len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
  129. if (len == 0)
  130. return 0;
  131. if ((buf = OPENSSL_malloc(len)) == NULL)
  132. return 0;
  133. len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
  134. if (len == 0) {
  135. OPENSSL_free(buf);
  136. return 0;
  137. }
  138. *pbuf = buf;
  139. return len;
  140. }