ec_oct.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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 <openssl/err.h>
  12. #include <openssl/opensslv.h>
  13. #include "ec_lcl.h"
  14. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  15. EC_POINT *point, const BIGNUM *x,
  16. int y_bit, BN_CTX *ctx)
  17. {
  18. if (group->meth->point_set_compressed_coordinates == 0
  19. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  20. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  21. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  22. return 0;
  23. }
  24. if (group->meth != point->meth) {
  25. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  26. EC_R_INCOMPATIBLE_OBJECTS);
  27. return 0;
  28. }
  29. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  30. if (group->meth->field_type == NID_X9_62_prime_field)
  31. return ec_GFp_simple_set_compressed_coordinates(group, point, x,
  32. y_bit, ctx);
  33. else
  34. #ifdef OPENSSL_NO_EC2M
  35. {
  36. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  37. EC_R_GF2M_NOT_SUPPORTED);
  38. return 0;
  39. }
  40. #else
  41. return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
  42. y_bit, ctx);
  43. #endif
  44. }
  45. return group->meth->point_set_compressed_coordinates(group, point, x,
  46. y_bit, ctx);
  47. }
  48. #ifndef OPENSSL_NO_EC2M
  49. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  50. EC_POINT *point, const BIGNUM *x,
  51. int y_bit, BN_CTX *ctx)
  52. {
  53. if (group->meth->point_set_compressed_coordinates == 0
  54. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  55. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
  56. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  57. return 0;
  58. }
  59. if (group->meth != point->meth) {
  60. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
  61. EC_R_INCOMPATIBLE_OBJECTS);
  62. return 0;
  63. }
  64. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  65. if (group->meth->field_type == NID_X9_62_prime_field)
  66. return ec_GFp_simple_set_compressed_coordinates(group, point, x,
  67. y_bit, ctx);
  68. else
  69. return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
  70. y_bit, ctx);
  71. }
  72. return group->meth->point_set_compressed_coordinates(group, point, x,
  73. y_bit, ctx);
  74. }
  75. #endif
  76. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  77. point_conversion_form_t form, unsigned char *buf,
  78. size_t len, BN_CTX *ctx)
  79. {
  80. if (group->meth->point2oct == 0
  81. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  82. ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  83. return 0;
  84. }
  85. if (group->meth != point->meth) {
  86. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
  87. return 0;
  88. }
  89. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  90. if (group->meth->field_type == NID_X9_62_prime_field)
  91. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  92. else
  93. #ifdef OPENSSL_NO_EC2M
  94. {
  95. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
  96. return 0;
  97. }
  98. #else
  99. return ec_GF2m_simple_point2oct(group, point,
  100. form, buf, len, ctx);
  101. #endif
  102. }
  103. return group->meth->point2oct(group, point, form, buf, len, ctx);
  104. }
  105. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  106. const unsigned char *buf, size_t len, BN_CTX *ctx)
  107. {
  108. if (group->meth->oct2point == 0
  109. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  110. ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  111. return 0;
  112. }
  113. if (group->meth != point->meth) {
  114. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
  115. return 0;
  116. }
  117. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  118. if (group->meth->field_type == NID_X9_62_prime_field)
  119. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  120. else
  121. #ifdef OPENSSL_NO_EC2M
  122. {
  123. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
  124. return 0;
  125. }
  126. #else
  127. return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
  128. #endif
  129. }
  130. return group->meth->oct2point(group, point, buf, len, ctx);
  131. }
  132. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  133. point_conversion_form_t form,
  134. unsigned char **pbuf, BN_CTX *ctx)
  135. {
  136. size_t len;
  137. unsigned char *buf;
  138. len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
  139. if (len == 0)
  140. return 0;
  141. if ((buf = OPENSSL_malloc(len)) == NULL) {
  142. ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
  143. return 0;
  144. }
  145. len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
  146. if (len == 0) {
  147. OPENSSL_free(buf);
  148. return 0;
  149. }
  150. *pbuf = buf;
  151. return len;
  152. }