ec_oct.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (point == NULL) {
  70. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  71. return 0;
  72. }
  73. if (group->meth->point2oct == 0
  74. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  75. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  76. return 0;
  77. }
  78. if (!ec_point_is_compat(point, group)) {
  79. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  80. return 0;
  81. }
  82. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  83. if (group->meth->field_type == NID_X9_62_prime_field)
  84. return ossl_ec_GFp_simple_point2oct(group, point, form, buf, len,
  85. ctx);
  86. else
  87. #ifdef OPENSSL_NO_EC2M
  88. {
  89. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  90. return 0;
  91. }
  92. #else
  93. return ossl_ec_GF2m_simple_point2oct(group, point,
  94. form, buf, len, ctx);
  95. #endif
  96. }
  97. return group->meth->point2oct(group, point, form, buf, len, ctx);
  98. }
  99. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  100. const unsigned char *buf, size_t len, BN_CTX *ctx)
  101. {
  102. if (group->meth->oct2point == 0
  103. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  104. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  105. return 0;
  106. }
  107. if (!ec_point_is_compat(point, group)) {
  108. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  109. return 0;
  110. }
  111. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  112. if (group->meth->field_type == NID_X9_62_prime_field)
  113. return ossl_ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  114. else
  115. #ifdef OPENSSL_NO_EC2M
  116. {
  117. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  118. return 0;
  119. }
  120. #else
  121. return ossl_ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
  122. #endif
  123. }
  124. return group->meth->oct2point(group, point, buf, len, ctx);
  125. }
  126. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  127. point_conversion_form_t form,
  128. unsigned char **pbuf, BN_CTX *ctx)
  129. {
  130. size_t len;
  131. unsigned char *buf;
  132. len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
  133. if (len == 0)
  134. return 0;
  135. if ((buf = OPENSSL_malloc(len)) == NULL)
  136. return 0;
  137. len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
  138. if (len == 0) {
  139. OPENSSL_free(buf);
  140. return 0;
  141. }
  142. *pbuf = buf;
  143. return len;
  144. }