ec2_oct.c 10.0 KB

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