oct.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <openssl/bn.h>
  69. #include <openssl/err.h>
  70. #include "internal.h"
  71. static size_t ec_GFp_simple_point2oct(const EC_GROUP *group,
  72. const EC_POINT *point,
  73. point_conversion_form_t form,
  74. uint8_t *buf, size_t len, BN_CTX *ctx) {
  75. size_t ret = 0;
  76. BN_CTX *new_ctx = NULL;
  77. int used_ctx = 0;
  78. if ((form != POINT_CONVERSION_COMPRESSED) &&
  79. (form != POINT_CONVERSION_UNCOMPRESSED)) {
  80. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
  81. goto err;
  82. }
  83. if (EC_POINT_is_at_infinity(group, point)) {
  84. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  85. goto err;
  86. }
  87. const size_t field_len = BN_num_bytes(&group->field);
  88. size_t output_len = 1 /* type byte */ + field_len;
  89. if (form == POINT_CONVERSION_UNCOMPRESSED) {
  90. // Uncompressed points have a second coordinate.
  91. output_len += field_len;
  92. }
  93. // if 'buf' is NULL, just return required length
  94. if (buf != NULL) {
  95. if (len < output_len) {
  96. OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
  97. goto err;
  98. }
  99. if (ctx == NULL) {
  100. ctx = new_ctx = BN_CTX_new();
  101. if (ctx == NULL) {
  102. goto err;
  103. }
  104. }
  105. BN_CTX_start(ctx);
  106. used_ctx = 1;
  107. BIGNUM *x = BN_CTX_get(ctx);
  108. BIGNUM *y = BN_CTX_get(ctx);
  109. if (y == NULL) {
  110. goto err;
  111. }
  112. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) {
  113. goto err;
  114. }
  115. if ((form == POINT_CONVERSION_COMPRESSED) &&
  116. BN_is_odd(y)) {
  117. buf[0] = form + 1;
  118. } else {
  119. buf[0] = form;
  120. }
  121. size_t i = 1;
  122. if (!BN_bn2bin_padded(buf + i, field_len, x)) {
  123. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  124. goto err;
  125. }
  126. i += field_len;
  127. if (form == POINT_CONVERSION_UNCOMPRESSED) {
  128. if (!BN_bn2bin_padded(buf + i, field_len, y)) {
  129. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  130. goto err;
  131. }
  132. i += field_len;
  133. }
  134. if (i != output_len) {
  135. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  136. goto err;
  137. }
  138. }
  139. ret = output_len;
  140. err:
  141. if (used_ctx) {
  142. BN_CTX_end(ctx);
  143. }
  144. BN_CTX_free(new_ctx);
  145. return ret;
  146. }
  147. static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  148. const uint8_t *buf, size_t len,
  149. BN_CTX *ctx) {
  150. BN_CTX *new_ctx = NULL;
  151. int ret = 0, used_ctx = 0;
  152. if (len == 0) {
  153. OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
  154. goto err;
  155. }
  156. point_conversion_form_t form = buf[0];
  157. const int y_bit = form & 1;
  158. form = form & ~1U;
  159. if ((form != POINT_CONVERSION_COMPRESSED &&
  160. form != POINT_CONVERSION_UNCOMPRESSED) ||
  161. (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
  162. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  163. goto err;
  164. }
  165. const size_t field_len = BN_num_bytes(&group->field);
  166. size_t enc_len = 1 /* type byte */ + field_len;
  167. if (form == POINT_CONVERSION_UNCOMPRESSED) {
  168. // Uncompressed points have a second coordinate.
  169. enc_len += field_len;
  170. }
  171. if (len != enc_len) {
  172. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  173. goto err;
  174. }
  175. if (ctx == NULL) {
  176. ctx = new_ctx = BN_CTX_new();
  177. if (ctx == NULL) {
  178. goto err;
  179. }
  180. }
  181. BN_CTX_start(ctx);
  182. used_ctx = 1;
  183. BIGNUM *x = BN_CTX_get(ctx);
  184. BIGNUM *y = BN_CTX_get(ctx);
  185. if (x == NULL || y == NULL) {
  186. goto err;
  187. }
  188. if (!BN_bin2bn(buf + 1, field_len, x)) {
  189. goto err;
  190. }
  191. if (BN_ucmp(x, &group->field) >= 0) {
  192. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  193. goto err;
  194. }
  195. if (form == POINT_CONVERSION_COMPRESSED) {
  196. if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
  197. goto err;
  198. }
  199. } else {
  200. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
  201. goto err;
  202. }
  203. if (BN_ucmp(y, &group->field) >= 0) {
  204. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
  205. goto err;
  206. }
  207. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  208. goto err;
  209. }
  210. }
  211. ret = 1;
  212. err:
  213. if (used_ctx) {
  214. BN_CTX_end(ctx);
  215. }
  216. BN_CTX_free(new_ctx);
  217. return ret;
  218. }
  219. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  220. const uint8_t *buf, size_t len, BN_CTX *ctx) {
  221. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  222. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  223. return 0;
  224. }
  225. return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
  226. }
  227. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
  228. point_conversion_form_t form, uint8_t *buf,
  229. size_t len, BN_CTX *ctx) {
  230. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  231. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  232. return 0;
  233. }
  234. return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
  235. }
  236. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  237. EC_POINT *point, const BIGNUM *x,
  238. int y_bit, BN_CTX *ctx) {
  239. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  240. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  241. return 0;
  242. }
  243. if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
  244. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  245. return 0;
  246. }
  247. BN_CTX *new_ctx = NULL;
  248. int ret = 0;
  249. ERR_clear_error();
  250. if (ctx == NULL) {
  251. ctx = new_ctx = BN_CTX_new();
  252. if (ctx == NULL) {
  253. return 0;
  254. }
  255. }
  256. y_bit = (y_bit != 0);
  257. BN_CTX_start(ctx);
  258. BIGNUM *tmp1 = BN_CTX_get(ctx);
  259. BIGNUM *tmp2 = BN_CTX_get(ctx);
  260. BIGNUM *a = BN_CTX_get(ctx);
  261. BIGNUM *b = BN_CTX_get(ctx);
  262. BIGNUM *y = BN_CTX_get(ctx);
  263. if (y == NULL ||
  264. !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) {
  265. goto err;
  266. }
  267. // Recover y. We have a Weierstrass equation
  268. // y^2 = x^3 + a*x + b,
  269. // so y is one of the square roots of x^3 + a*x + b.
  270. // tmp1 := x^3
  271. if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
  272. !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
  273. goto err;
  274. }
  275. // tmp1 := tmp1 + a*x
  276. if (group->a_is_minus3) {
  277. if (!bn_mod_lshift1_consttime(tmp2, x, &group->field, ctx) ||
  278. !bn_mod_add_consttime(tmp2, tmp2, x, &group->field, ctx) ||
  279. !bn_mod_sub_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
  280. goto err;
  281. }
  282. } else {
  283. if (!BN_mod_mul(tmp2, a, x, &group->field, ctx) ||
  284. !bn_mod_add_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
  285. goto err;
  286. }
  287. }
  288. // tmp1 := tmp1 + b
  289. if (!bn_mod_add_consttime(tmp1, tmp1, b, &group->field, ctx)) {
  290. goto err;
  291. }
  292. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
  293. unsigned long err = ERR_peek_last_error();
  294. if (ERR_GET_LIB(err) == ERR_LIB_BN &&
  295. ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  296. ERR_clear_error();
  297. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
  298. } else {
  299. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  300. }
  301. goto err;
  302. }
  303. if (y_bit != BN_is_odd(y)) {
  304. if (BN_is_zero(y)) {
  305. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
  306. goto err;
  307. }
  308. if (!BN_usub(y, &group->field, y)) {
  309. goto err;
  310. }
  311. }
  312. if (y_bit != BN_is_odd(y)) {
  313. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  314. goto err;
  315. }
  316. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  317. goto err;
  318. }
  319. ret = 1;
  320. err:
  321. BN_CTX_end(ctx);
  322. BN_CTX_free(new_ctx);
  323. return ret;
  324. }