ec_oct.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2011-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. /*
  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. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
  25. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  26. return 0;
  27. }
  28. if (!ec_point_is_compat(point, group)) {
  29. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
  30. EC_R_INCOMPATIBLE_OBJECTS);
  31. return 0;
  32. }
  33. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  34. if (group->meth->field_type == NID_X9_62_prime_field)
  35. return ec_GFp_simple_set_compressed_coordinates(group, point, x,
  36. y_bit, ctx);
  37. else
  38. #ifdef OPENSSL_NO_EC2M
  39. {
  40. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
  41. EC_R_GF2M_NOT_SUPPORTED);
  42. return 0;
  43. }
  44. #else
  45. return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
  46. y_bit, ctx);
  47. #endif
  48. }
  49. return group->meth->point_set_compressed_coordinates(group, point, x,
  50. y_bit, ctx);
  51. }
  52. #ifndef OPENSSL_NO_DEPRECATED_3_0
  53. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  54. EC_POINT *point, const BIGNUM *x,
  55. int y_bit, BN_CTX *ctx)
  56. {
  57. return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
  58. }
  59. # ifndef OPENSSL_NO_EC2M
  60. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  61. EC_POINT *point, const BIGNUM *x,
  62. int y_bit, BN_CTX *ctx)
  63. {
  64. return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
  65. }
  66. # endif
  67. #endif
  68. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  69. point_conversion_form_t form, unsigned char *buf,
  70. size_t len, BN_CTX *ctx)
  71. {
  72. if (group->meth->point2oct == 0
  73. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  74. ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  75. return 0;
  76. }
  77. if (!ec_point_is_compat(point, group)) {
  78. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
  79. return 0;
  80. }
  81. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  82. if (group->meth->field_type == NID_X9_62_prime_field)
  83. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  84. else
  85. #ifdef OPENSSL_NO_EC2M
  86. {
  87. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
  88. return 0;
  89. }
  90. #else
  91. return ec_GF2m_simple_point2oct(group, point,
  92. form, buf, len, ctx);
  93. #endif
  94. }
  95. return group->meth->point2oct(group, point, form, buf, len, ctx);
  96. }
  97. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  98. const unsigned char *buf, size_t len, BN_CTX *ctx)
  99. {
  100. if (group->meth->oct2point == 0
  101. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  102. ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  103. return 0;
  104. }
  105. if (!ec_point_is_compat(point, group)) {
  106. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
  107. return 0;
  108. }
  109. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  110. if (group->meth->field_type == NID_X9_62_prime_field)
  111. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  112. else
  113. #ifdef OPENSSL_NO_EC2M
  114. {
  115. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
  116. return 0;
  117. }
  118. #else
  119. return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
  120. #endif
  121. }
  122. return group->meth->oct2point(group, point, buf, len, ctx);
  123. }
  124. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  125. point_conversion_form_t form,
  126. unsigned char **pbuf, BN_CTX *ctx)
  127. {
  128. size_t len;
  129. unsigned char *buf;
  130. len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
  131. if (len == 0)
  132. return 0;
  133. if ((buf = OPENSSL_malloc(len)) == NULL) {
  134. ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
  135. return 0;
  136. }
  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. }