ec_oct.c 5.1 KB

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