ec2_oct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 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 "ec_local.h"
  12. #ifndef OPENSSL_NO_EC2M
  13. /*-
  14. * Calculates and sets the affine coordinates of an EC_POINT from the given
  15. * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
  16. * Note that the simple implementation only uses affine coordinates.
  17. *
  18. * The method is from the following publication:
  19. *
  20. * Harper, Menezes, Vanstone:
  21. * "Public-Key Cryptosystems with Very Small Key Lengths",
  22. * EUROCRYPT '92, Springer-Verlag LNCS 658,
  23. * published February 1993
  24. *
  25. * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
  26. * the same method, but claim no priority date earlier than July 29, 1994
  27. * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
  28. */
  29. int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
  30. EC_POINT *point,
  31. const BIGNUM *x_, int y_bit,
  32. BN_CTX *ctx)
  33. {
  34. BN_CTX *new_ctx = NULL;
  35. BIGNUM *tmp, *x, *y, *z;
  36. int ret = 0, z0;
  37. /* clear error queue */
  38. ERR_clear_error();
  39. if (ctx == NULL) {
  40. ctx = new_ctx = BN_CTX_new();
  41. if (ctx == NULL)
  42. return 0;
  43. }
  44. y_bit = (y_bit != 0) ? 1 : 0;
  45. BN_CTX_start(ctx);
  46. tmp = BN_CTX_get(ctx);
  47. x = BN_CTX_get(ctx);
  48. y = BN_CTX_get(ctx);
  49. z = BN_CTX_get(ctx);
  50. if (z == NULL)
  51. goto err;
  52. if (!BN_GF2m_mod_arr(x, x_, group->poly))
  53. goto err;
  54. if (BN_is_zero(x)) {
  55. if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
  56. goto err;
  57. } else {
  58. if (!group->meth->field_sqr(group, tmp, x, ctx))
  59. goto err;
  60. if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
  61. goto err;
  62. if (!BN_GF2m_add(tmp, group->a, tmp))
  63. goto err;
  64. if (!BN_GF2m_add(tmp, x, tmp))
  65. goto err;
  66. if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
  67. unsigned long err = ERR_peek_last_error();
  68. if (ERR_GET_LIB(err) == ERR_LIB_BN
  69. && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
  70. ERR_clear_error();
  71. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  72. EC_R_INVALID_COMPRESSED_POINT);
  73. } else
  74. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  75. ERR_R_BN_LIB);
  76. goto err;
  77. }
  78. z0 = (BN_is_odd(z)) ? 1 : 0;
  79. if (!group->meth->field_mul(group, y, x, z, ctx))
  80. goto err;
  81. if (z0 != y_bit) {
  82. if (!BN_GF2m_add(y, y, x))
  83. goto err;
  84. }
  85. }
  86. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  87. goto err;
  88. ret = 1;
  89. err:
  90. BN_CTX_end(ctx);
  91. BN_CTX_free(new_ctx);
  92. return ret;
  93. }
  94. /*
  95. * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
  96. * length will be returned. If the length len of buf is smaller than required
  97. * an error will be returned.
  98. */
  99. size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  100. point_conversion_form_t form,
  101. unsigned char *buf, size_t len, BN_CTX *ctx)
  102. {
  103. size_t ret;
  104. BN_CTX *new_ctx = NULL;
  105. int used_ctx = 0;
  106. BIGNUM *x, *y, *yxi;
  107. size_t field_len, i, skip;
  108. if ((form != POINT_CONVERSION_COMPRESSED)
  109. && (form != POINT_CONVERSION_UNCOMPRESSED)
  110. && (form != POINT_CONVERSION_HYBRID)) {
  111. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  112. goto err;
  113. }
  114. if (EC_POINT_is_at_infinity(group, point)) {
  115. /* encodes to a single 0 octet */
  116. if (buf != NULL) {
  117. if (len < 1) {
  118. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  119. return 0;
  120. }
  121. buf[0] = 0;
  122. }
  123. return 1;
  124. }
  125. /* ret := required output buffer length */
  126. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  127. ret =
  128. (form ==
  129. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  130. /* if 'buf' is NULL, just return required length */
  131. if (buf != NULL) {
  132. if (len < ret) {
  133. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  134. goto err;
  135. }
  136. if (ctx == NULL) {
  137. ctx = new_ctx = BN_CTX_new();
  138. if (ctx == NULL)
  139. return 0;
  140. }
  141. BN_CTX_start(ctx);
  142. used_ctx = 1;
  143. x = BN_CTX_get(ctx);
  144. y = BN_CTX_get(ctx);
  145. yxi = BN_CTX_get(ctx);
  146. if (yxi == NULL)
  147. goto err;
  148. if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
  149. goto err;
  150. buf[0] = form;
  151. if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
  152. if (!group->meth->field_div(group, yxi, y, x, ctx))
  153. goto err;
  154. if (BN_is_odd(yxi))
  155. buf[0]++;
  156. }
  157. i = 1;
  158. skip = field_len - BN_num_bytes(x);
  159. if (skip > field_len) {
  160. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  161. goto err;
  162. }
  163. while (skip > 0) {
  164. buf[i++] = 0;
  165. skip--;
  166. }
  167. skip = BN_bn2bin(x, buf + i);
  168. i += skip;
  169. if (i != 1 + field_len) {
  170. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  171. goto err;
  172. }
  173. if (form == POINT_CONVERSION_UNCOMPRESSED
  174. || form == POINT_CONVERSION_HYBRID) {
  175. skip = field_len - BN_num_bytes(y);
  176. if (skip > field_len) {
  177. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  178. goto err;
  179. }
  180. while (skip > 0) {
  181. buf[i++] = 0;
  182. skip--;
  183. }
  184. skip = BN_bn2bin(y, buf + i);
  185. i += skip;
  186. }
  187. if (i != ret) {
  188. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  189. goto err;
  190. }
  191. }
  192. if (used_ctx)
  193. BN_CTX_end(ctx);
  194. BN_CTX_free(new_ctx);
  195. return ret;
  196. err:
  197. if (used_ctx)
  198. BN_CTX_end(ctx);
  199. BN_CTX_free(new_ctx);
  200. return 0;
  201. }
  202. /*
  203. * Converts an octet string representation to an EC_POINT. Note that the
  204. * simple implementation only uses affine coordinates.
  205. */
  206. int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  207. const unsigned char *buf, size_t len,
  208. BN_CTX *ctx)
  209. {
  210. point_conversion_form_t form;
  211. int y_bit, m;
  212. BN_CTX *new_ctx = NULL;
  213. BIGNUM *x, *y, *yxi;
  214. size_t field_len, enc_len;
  215. int ret = 0;
  216. if (len == 0) {
  217. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  218. return 0;
  219. }
  220. /*
  221. * The first octet is the point converison octet PC, see X9.62, page 4
  222. * and section 4.4.2. It must be:
  223. * 0x00 for the point at infinity
  224. * 0x02 or 0x03 for compressed form
  225. * 0x04 for uncompressed form
  226. * 0x06 or 0x07 for hybrid form.
  227. * For compressed or hybrid forms, we store the last bit of buf[0] as
  228. * y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*.
  229. * We error if buf[0] contains any but the above values.
  230. */
  231. y_bit = buf[0] & 1;
  232. form = buf[0] & ~1U;
  233. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  234. && (form != POINT_CONVERSION_UNCOMPRESSED)
  235. && (form != POINT_CONVERSION_HYBRID)) {
  236. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  237. return 0;
  238. }
  239. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  240. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  241. return 0;
  242. }
  243. /* The point at infinity is represented by a single zero octet. */
  244. if (form == 0) {
  245. if (len != 1) {
  246. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  247. return 0;
  248. }
  249. return EC_POINT_set_to_infinity(group, point);
  250. }
  251. m = EC_GROUP_get_degree(group);
  252. field_len = (m + 7) / 8;
  253. enc_len =
  254. (form ==
  255. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  256. if (len != enc_len) {
  257. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  258. return 0;
  259. }
  260. if (ctx == NULL) {
  261. ctx = new_ctx = BN_CTX_new();
  262. if (ctx == NULL)
  263. return 0;
  264. }
  265. BN_CTX_start(ctx);
  266. x = BN_CTX_get(ctx);
  267. y = BN_CTX_get(ctx);
  268. yxi = BN_CTX_get(ctx);
  269. if (yxi == NULL)
  270. goto err;
  271. if (!BN_bin2bn(buf + 1, field_len, x))
  272. goto err;
  273. if (BN_num_bits(x) > m) {
  274. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  275. goto err;
  276. }
  277. if (form == POINT_CONVERSION_COMPRESSED) {
  278. if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
  279. goto err;
  280. } else {
  281. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  282. goto err;
  283. if (BN_num_bits(y) > m) {
  284. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  285. goto err;
  286. }
  287. if (form == POINT_CONVERSION_HYBRID) {
  288. /*
  289. * Check that the form in the encoding was set correctly
  290. * according to X9.62 4.4.2.a, 4(c), see also first paragraph
  291. * of X9.62, 4.4.1.b.
  292. */
  293. if (BN_is_zero(x)) {
  294. if (y_bit != 0) {
  295. ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  296. goto err;
  297. }
  298. } else {
  299. if (!group->meth->field_div(group, yxi, y, x, ctx))
  300. goto err;
  301. if (y_bit != BN_is_odd(yxi)) {
  302. ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  303. goto err;
  304. }
  305. }
  306. }
  307. /*
  308. * EC_POINT_set_affine_coordinates is responsible for checking that
  309. * the point is on the curve.
  310. */
  311. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  312. goto err;
  313. }
  314. ret = 1;
  315. err:
  316. BN_CTX_end(ctx);
  317. BN_CTX_free(new_ctx);
  318. return ret;
  319. }
  320. #endif