2
0

ecp_oct.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <openssl/err.h>
  11. #include <openssl/symhacks.h>
  12. #include "ec_local.h"
  13. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
  14. EC_POINT *point,
  15. const BIGNUM *x_, int y_bit,
  16. BN_CTX *ctx)
  17. {
  18. BN_CTX *new_ctx = NULL;
  19. BIGNUM *tmp1, *tmp2, *x, *y;
  20. int ret = 0;
  21. /* clear error queue */
  22. ERR_clear_error();
  23. if (ctx == NULL) {
  24. ctx = new_ctx = BN_CTX_new();
  25. if (ctx == NULL)
  26. return 0;
  27. }
  28. y_bit = (y_bit != 0);
  29. BN_CTX_start(ctx);
  30. tmp1 = BN_CTX_get(ctx);
  31. tmp2 = BN_CTX_get(ctx);
  32. x = BN_CTX_get(ctx);
  33. y = BN_CTX_get(ctx);
  34. if (y == NULL)
  35. goto err;
  36. /*-
  37. * Recover y. We have a Weierstrass equation
  38. * y^2 = x^3 + a*x + b,
  39. * so y is one of the square roots of x^3 + a*x + b.
  40. */
  41. /* tmp1 := x^3 */
  42. if (!BN_nnmod(x, x_, group->field, ctx))
  43. goto err;
  44. if (group->meth->field_decode == 0) {
  45. /* field_{sqr,mul} work on standard representation */
  46. if (!group->meth->field_sqr(group, tmp2, x_, ctx))
  47. goto err;
  48. if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
  49. goto err;
  50. } else {
  51. if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
  52. goto err;
  53. if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
  54. goto err;
  55. }
  56. /* tmp1 := tmp1 + a*x */
  57. if (group->a_is_minus3) {
  58. if (!BN_mod_lshift1_quick(tmp2, x, group->field))
  59. goto err;
  60. if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
  61. goto err;
  62. if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
  63. goto err;
  64. } else {
  65. if (group->meth->field_decode) {
  66. if (!group->meth->field_decode(group, tmp2, group->a, ctx))
  67. goto err;
  68. if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
  69. goto err;
  70. } else {
  71. /* field_mul works on standard representation */
  72. if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
  73. goto err;
  74. }
  75. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
  76. goto err;
  77. }
  78. /* tmp1 := tmp1 + b */
  79. if (group->meth->field_decode) {
  80. if (!group->meth->field_decode(group, tmp2, group->b, ctx))
  81. goto err;
  82. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
  83. goto err;
  84. } else {
  85. if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
  86. goto err;
  87. }
  88. if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
  89. unsigned long err = ERR_peek_last_error();
  90. if (ERR_GET_LIB(err) == ERR_LIB_BN
  91. && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
  92. ERR_clear_error();
  93. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  94. EC_R_INVALID_COMPRESSED_POINT);
  95. } else
  96. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  97. ERR_R_BN_LIB);
  98. goto err;
  99. }
  100. if (y_bit != BN_is_odd(y)) {
  101. if (BN_is_zero(y)) {
  102. int kron;
  103. kron = BN_kronecker(x, group->field, ctx);
  104. if (kron == -2)
  105. goto err;
  106. if (kron == 1)
  107. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  108. EC_R_INVALID_COMPRESSION_BIT);
  109. else
  110. /*
  111. * BN_mod_sqrt() should have caught this error (not a square)
  112. */
  113. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  114. EC_R_INVALID_COMPRESSED_POINT);
  115. goto err;
  116. }
  117. if (!BN_usub(y, group->field, y))
  118. goto err;
  119. }
  120. if (y_bit != BN_is_odd(y)) {
  121. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
  122. ERR_R_INTERNAL_ERROR);
  123. goto err;
  124. }
  125. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  126. goto err;
  127. ret = 1;
  128. err:
  129. BN_CTX_end(ctx);
  130. BN_CTX_free(new_ctx);
  131. return ret;
  132. }
  133. size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  134. point_conversion_form_t form,
  135. unsigned char *buf, size_t len, BN_CTX *ctx)
  136. {
  137. size_t ret;
  138. BN_CTX *new_ctx = NULL;
  139. int used_ctx = 0;
  140. BIGNUM *x, *y;
  141. size_t field_len, i, skip;
  142. if ((form != POINT_CONVERSION_COMPRESSED)
  143. && (form != POINT_CONVERSION_UNCOMPRESSED)
  144. && (form != POINT_CONVERSION_HYBRID)) {
  145. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  146. goto err;
  147. }
  148. if (EC_POINT_is_at_infinity(group, point)) {
  149. /* encodes to a single 0 octet */
  150. if (buf != NULL) {
  151. if (len < 1) {
  152. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  153. return 0;
  154. }
  155. buf[0] = 0;
  156. }
  157. return 1;
  158. }
  159. /* ret := required output buffer length */
  160. field_len = BN_num_bytes(group->field);
  161. ret =
  162. (form ==
  163. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  164. /* if 'buf' is NULL, just return required length */
  165. if (buf != NULL) {
  166. if (len < ret) {
  167. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  168. goto err;
  169. }
  170. if (ctx == NULL) {
  171. ctx = new_ctx = BN_CTX_new();
  172. if (ctx == NULL)
  173. return 0;
  174. }
  175. BN_CTX_start(ctx);
  176. used_ctx = 1;
  177. x = BN_CTX_get(ctx);
  178. y = BN_CTX_get(ctx);
  179. if (y == NULL)
  180. goto err;
  181. if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
  182. goto err;
  183. if ((form == POINT_CONVERSION_COMPRESSED
  184. || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
  185. buf[0] = form + 1;
  186. else
  187. buf[0] = form;
  188. i = 1;
  189. skip = field_len - BN_num_bytes(x);
  190. if (skip > field_len) {
  191. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  192. goto err;
  193. }
  194. while (skip > 0) {
  195. buf[i++] = 0;
  196. skip--;
  197. }
  198. skip = BN_bn2bin(x, buf + i);
  199. i += skip;
  200. if (i != 1 + field_len) {
  201. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  202. goto err;
  203. }
  204. if (form == POINT_CONVERSION_UNCOMPRESSED
  205. || form == POINT_CONVERSION_HYBRID) {
  206. skip = field_len - BN_num_bytes(y);
  207. if (skip > field_len) {
  208. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  209. goto err;
  210. }
  211. while (skip > 0) {
  212. buf[i++] = 0;
  213. skip--;
  214. }
  215. skip = BN_bn2bin(y, buf + i);
  216. i += skip;
  217. }
  218. if (i != ret) {
  219. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  220. goto err;
  221. }
  222. }
  223. if (used_ctx)
  224. BN_CTX_end(ctx);
  225. BN_CTX_free(new_ctx);
  226. return ret;
  227. err:
  228. if (used_ctx)
  229. BN_CTX_end(ctx);
  230. BN_CTX_free(new_ctx);
  231. return 0;
  232. }
  233. int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  234. const unsigned char *buf, size_t len, BN_CTX *ctx)
  235. {
  236. point_conversion_form_t form;
  237. int y_bit;
  238. BN_CTX *new_ctx = NULL;
  239. BIGNUM *x, *y;
  240. size_t field_len, enc_len;
  241. int ret = 0;
  242. if (len == 0) {
  243. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  244. return 0;
  245. }
  246. form = buf[0];
  247. y_bit = form & 1;
  248. form = form & ~1U;
  249. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  250. && (form != POINT_CONVERSION_UNCOMPRESSED)
  251. && (form != POINT_CONVERSION_HYBRID)) {
  252. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  253. return 0;
  254. }
  255. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  256. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  257. return 0;
  258. }
  259. if (form == 0) {
  260. if (len != 1) {
  261. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  262. return 0;
  263. }
  264. return EC_POINT_set_to_infinity(group, point);
  265. }
  266. field_len = BN_num_bytes(group->field);
  267. enc_len =
  268. (form ==
  269. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  270. if (len != enc_len) {
  271. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  272. return 0;
  273. }
  274. if (ctx == NULL) {
  275. ctx = new_ctx = BN_CTX_new();
  276. if (ctx == NULL)
  277. return 0;
  278. }
  279. BN_CTX_start(ctx);
  280. x = BN_CTX_get(ctx);
  281. y = BN_CTX_get(ctx);
  282. if (y == NULL)
  283. goto err;
  284. if (!BN_bin2bn(buf + 1, field_len, x))
  285. goto err;
  286. if (BN_ucmp(x, group->field) >= 0) {
  287. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  288. goto err;
  289. }
  290. if (form == POINT_CONVERSION_COMPRESSED) {
  291. if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
  292. goto err;
  293. } else {
  294. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  295. goto err;
  296. if (BN_ucmp(y, group->field) >= 0) {
  297. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  298. goto err;
  299. }
  300. if (form == POINT_CONVERSION_HYBRID) {
  301. if (y_bit != BN_is_odd(y)) {
  302. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  303. goto err;
  304. }
  305. }
  306. /*
  307. * EC_POINT_set_affine_coordinates is responsible for checking that
  308. * the point is on the curve.
  309. */
  310. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  311. goto err;
  312. }
  313. ret = 1;
  314. err:
  315. BN_CTX_end(ctx);
  316. BN_CTX_free(new_ctx);
  317. return ret;
  318. }