ec_oct.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Originally written by Bodo Moeller for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * openssl-core@openssl.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. * Binary polynomial ECC support in OpenSSL originally developed by
  60. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  61. */
  62. #include <string.h>
  63. #include <openssl/err.h>
  64. #include <openssl/opensslv.h>
  65. #include "ec_lcl.h"
  66. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  67. EC_POINT *point, const BIGNUM *x,
  68. int y_bit, BN_CTX *ctx)
  69. {
  70. if (group->meth->point_set_compressed_coordinates == 0
  71. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  72. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  73. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  74. return 0;
  75. }
  76. if (group->meth != point->meth) {
  77. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  78. 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_set_compressed_coordinates(group, point, x,
  84. y_bit, ctx);
  85. else
  86. #ifdef OPENSSL_NO_EC2M
  87. {
  88. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
  89. EC_R_GF2M_NOT_SUPPORTED);
  90. return 0;
  91. }
  92. #else
  93. return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
  94. y_bit, ctx);
  95. #endif
  96. }
  97. return group->meth->point_set_compressed_coordinates(group, point, x,
  98. y_bit, ctx);
  99. }
  100. #ifndef OPENSSL_NO_EC2M
  101. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  102. EC_POINT *point, const BIGNUM *x,
  103. int y_bit, BN_CTX *ctx)
  104. {
  105. if (group->meth->point_set_compressed_coordinates == 0
  106. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  107. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
  108. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  109. return 0;
  110. }
  111. if (group->meth != point->meth) {
  112. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
  113. EC_R_INCOMPATIBLE_OBJECTS);
  114. return 0;
  115. }
  116. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  117. if (group->meth->field_type == NID_X9_62_prime_field)
  118. return ec_GFp_simple_set_compressed_coordinates(group, point, x,
  119. y_bit, ctx);
  120. else
  121. return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
  122. y_bit, ctx);
  123. }
  124. return group->meth->point_set_compressed_coordinates(group, point, x,
  125. y_bit, ctx);
  126. }
  127. #endif
  128. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  129. point_conversion_form_t form, unsigned char *buf,
  130. size_t len, BN_CTX *ctx)
  131. {
  132. if (group->meth->point2oct == 0
  133. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  134. ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  135. return 0;
  136. }
  137. if (group->meth != point->meth) {
  138. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
  139. return 0;
  140. }
  141. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  142. if (group->meth->field_type == NID_X9_62_prime_field)
  143. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  144. else
  145. #ifdef OPENSSL_NO_EC2M
  146. {
  147. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
  148. return 0;
  149. }
  150. #else
  151. return ec_GF2m_simple_point2oct(group, point,
  152. form, buf, len, ctx);
  153. #endif
  154. }
  155. return group->meth->point2oct(group, point, form, buf, len, ctx);
  156. }
  157. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  158. const unsigned char *buf, size_t len, BN_CTX *ctx)
  159. {
  160. if (group->meth->oct2point == 0
  161. && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
  162. ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  163. return 0;
  164. }
  165. if (group->meth != point->meth) {
  166. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
  167. return 0;
  168. }
  169. if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
  170. if (group->meth->field_type == NID_X9_62_prime_field)
  171. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  172. else
  173. #ifdef OPENSSL_NO_EC2M
  174. {
  175. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
  176. return 0;
  177. }
  178. #else
  179. return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
  180. #endif
  181. }
  182. return group->meth->oct2point(group, point, buf, len, ctx);
  183. }
  184. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  185. point_conversion_form_t form,
  186. unsigned char **pbuf, BN_CTX *ctx)
  187. {
  188. size_t len;
  189. unsigned char *buf;
  190. len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
  191. if (len == 0)
  192. return 0;
  193. buf = OPENSSL_malloc(len);
  194. if (buf == NULL)
  195. return 0;
  196. len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
  197. if (len == 0) {
  198. OPENSSL_free(buf);
  199. return 0;
  200. }
  201. *pbuf = buf;
  202. return len;
  203. }