ecp_oct.c 10 KB

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