ec2_oct.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 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. #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. BIGNUM *tmp, *x, *y, *z;
  35. int ret = 0, z0;
  36. #ifndef FIPS_MODE
  37. BN_CTX *new_ctx = NULL;
  38. /* clear error queue */
  39. ERR_clear_error();
  40. if (ctx == NULL) {
  41. ctx = new_ctx = BN_CTX_new();
  42. if (ctx == NULL)
  43. return 0;
  44. }
  45. #endif
  46. y_bit = (y_bit != 0) ? 1 : 0;
  47. BN_CTX_start(ctx);
  48. tmp = BN_CTX_get(ctx);
  49. x = BN_CTX_get(ctx);
  50. y = BN_CTX_get(ctx);
  51. z = BN_CTX_get(ctx);
  52. if (z == NULL)
  53. goto err;
  54. if (!BN_GF2m_mod_arr(x, x_, group->poly))
  55. goto err;
  56. if (BN_is_zero(x)) {
  57. if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
  58. goto err;
  59. } else {
  60. if (!group->meth->field_sqr(group, tmp, x, ctx))
  61. goto err;
  62. if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
  63. goto err;
  64. if (!BN_GF2m_add(tmp, group->a, tmp))
  65. goto err;
  66. if (!BN_GF2m_add(tmp, x, tmp))
  67. goto err;
  68. if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
  69. #ifndef FIPS_MODE
  70. unsigned long err = ERR_peek_last_error();
  71. if (ERR_GET_LIB(err) == ERR_LIB_BN
  72. && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
  73. ERR_clear_error();
  74. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  75. EC_R_INVALID_COMPRESSED_POINT);
  76. } else
  77. #endif
  78. {
  79. ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
  80. ERR_R_BN_LIB);
  81. }
  82. goto err;
  83. }
  84. z0 = (BN_is_odd(z)) ? 1 : 0;
  85. if (!group->meth->field_mul(group, y, x, z, ctx))
  86. goto err;
  87. if (z0 != y_bit) {
  88. if (!BN_GF2m_add(y, y, x))
  89. goto err;
  90. }
  91. }
  92. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  93. goto err;
  94. ret = 1;
  95. err:
  96. BN_CTX_end(ctx);
  97. #ifndef FIPS_MODE
  98. BN_CTX_free(new_ctx);
  99. #endif
  100. return ret;
  101. }
  102. /*
  103. * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
  104. * length will be returned. If the length len of buf is smaller than required
  105. * an error will be returned.
  106. */
  107. size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
  108. point_conversion_form_t form,
  109. unsigned char *buf, size_t len, BN_CTX *ctx)
  110. {
  111. size_t ret;
  112. int used_ctx = 0;
  113. BIGNUM *x, *y, *yxi;
  114. size_t field_len, i, skip;
  115. #ifndef FIPS_MODE
  116. BN_CTX *new_ctx = NULL;
  117. #endif
  118. if ((form != POINT_CONVERSION_COMPRESSED)
  119. && (form != POINT_CONVERSION_UNCOMPRESSED)
  120. && (form != POINT_CONVERSION_HYBRID)) {
  121. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  122. goto err;
  123. }
  124. if (EC_POINT_is_at_infinity(group, point)) {
  125. /* encodes to a single 0 octet */
  126. if (buf != NULL) {
  127. if (len < 1) {
  128. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  129. return 0;
  130. }
  131. buf[0] = 0;
  132. }
  133. return 1;
  134. }
  135. /* ret := required output buffer length */
  136. field_len = (EC_GROUP_get_degree(group) + 7) / 8;
  137. ret =
  138. (form ==
  139. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  140. /* if 'buf' is NULL, just return required length */
  141. if (buf != NULL) {
  142. if (len < ret) {
  143. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  144. goto err;
  145. }
  146. #ifndef FIPS_MODE
  147. if (ctx == NULL) {
  148. ctx = new_ctx = BN_CTX_new();
  149. if (ctx == NULL)
  150. return 0;
  151. }
  152. #endif
  153. BN_CTX_start(ctx);
  154. used_ctx = 1;
  155. x = BN_CTX_get(ctx);
  156. y = BN_CTX_get(ctx);
  157. yxi = BN_CTX_get(ctx);
  158. if (yxi == NULL)
  159. goto err;
  160. if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
  161. goto err;
  162. buf[0] = form;
  163. if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
  164. if (!group->meth->field_div(group, yxi, y, x, ctx))
  165. goto err;
  166. if (BN_is_odd(yxi))
  167. buf[0]++;
  168. }
  169. i = 1;
  170. skip = field_len - BN_num_bytes(x);
  171. if (skip > field_len) {
  172. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  173. goto err;
  174. }
  175. while (skip > 0) {
  176. buf[i++] = 0;
  177. skip--;
  178. }
  179. skip = BN_bn2bin(x, buf + i);
  180. i += skip;
  181. if (i != 1 + field_len) {
  182. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  183. goto err;
  184. }
  185. if (form == POINT_CONVERSION_UNCOMPRESSED
  186. || form == POINT_CONVERSION_HYBRID) {
  187. skip = field_len - BN_num_bytes(y);
  188. if (skip > field_len) {
  189. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  190. goto err;
  191. }
  192. while (skip > 0) {
  193. buf[i++] = 0;
  194. skip--;
  195. }
  196. skip = BN_bn2bin(y, buf + i);
  197. i += skip;
  198. }
  199. if (i != ret) {
  200. ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  201. goto err;
  202. }
  203. }
  204. if (used_ctx)
  205. BN_CTX_end(ctx);
  206. #ifndef FIPS_MODE
  207. BN_CTX_free(new_ctx);
  208. #endif
  209. return ret;
  210. err:
  211. if (used_ctx)
  212. BN_CTX_end(ctx);
  213. #ifndef FIPS_MODE
  214. BN_CTX_free(new_ctx);
  215. #endif
  216. return 0;
  217. }
  218. /*
  219. * Converts an octet string representation to an EC_POINT. Note that the
  220. * simple implementation only uses affine coordinates.
  221. */
  222. int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  223. const unsigned char *buf, size_t len,
  224. BN_CTX *ctx)
  225. {
  226. point_conversion_form_t form;
  227. int y_bit, m;
  228. BIGNUM *x, *y, *yxi;
  229. size_t field_len, enc_len;
  230. int ret = 0;
  231. #ifndef FIPS_MODE
  232. BN_CTX *new_ctx = NULL;
  233. #endif
  234. if (len == 0) {
  235. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  236. return 0;
  237. }
  238. form = buf[0];
  239. y_bit = form & 1;
  240. form = form & ~1U;
  241. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  242. && (form != POINT_CONVERSION_UNCOMPRESSED)
  243. && (form != POINT_CONVERSION_HYBRID)) {
  244. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  245. return 0;
  246. }
  247. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
  248. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  249. return 0;
  250. }
  251. if (form == 0) {
  252. if (len != 1) {
  253. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  254. return 0;
  255. }
  256. return EC_POINT_set_to_infinity(group, point);
  257. }
  258. m = EC_GROUP_get_degree(group);
  259. field_len = (m + 7) / 8;
  260. enc_len =
  261. (form ==
  262. POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
  263. if (len != enc_len) {
  264. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  265. return 0;
  266. }
  267. #ifndef FIPS_MODE
  268. if (ctx == NULL) {
  269. ctx = new_ctx = BN_CTX_new();
  270. if (ctx == NULL)
  271. return 0;
  272. }
  273. #endif
  274. BN_CTX_start(ctx);
  275. x = BN_CTX_get(ctx);
  276. y = BN_CTX_get(ctx);
  277. yxi = BN_CTX_get(ctx);
  278. if (yxi == NULL)
  279. goto err;
  280. if (!BN_bin2bn(buf + 1, field_len, x))
  281. goto err;
  282. if (BN_num_bits(x) > m) {
  283. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  284. goto err;
  285. }
  286. if (form == POINT_CONVERSION_COMPRESSED) {
  287. if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
  288. goto err;
  289. } else {
  290. if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
  291. goto err;
  292. if (BN_num_bits(y) > m) {
  293. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  294. goto err;
  295. }
  296. if (form == POINT_CONVERSION_HYBRID) {
  297. if (!group->meth->field_div(group, yxi, y, x, ctx))
  298. goto err;
  299. if (y_bit != BN_is_odd(yxi)) {
  300. ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  301. goto err;
  302. }
  303. }
  304. /*
  305. * EC_POINT_set_affine_coordinates is responsible for checking that
  306. * the point is on the curve.
  307. */
  308. if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
  309. goto err;
  310. }
  311. ret = 1;
  312. err:
  313. BN_CTX_end(ctx);
  314. #ifndef FIPS_MODE
  315. BN_CTX_free(new_ctx);
  316. #endif
  317. return ret;
  318. }
  319. #endif