PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/crypto/openssl/crypto/ec/ecp_oct.c

https://bitbucket.org/freebsd/freebsd-head/
C | 433 lines | 303 code | 53 blank | 77 comment | 132 complexity | dd5c68b59a8679c26382def400aeeecf MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /* crypto/ec/ecp_oct.c */
  2. /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
  3. * for the OpenSSL project.
  4. * Includes code written by Bodo Moeller for the OpenSSL project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@openssl.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* ====================================================================
  60. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  61. * Portions of this software developed by SUN MICROSYSTEMS, INC.,
  62. * and contributed to the OpenSSL project.
  63. */
  64. #include <openssl/err.h>
  65. #include <openssl/symhacks.h>
  66. #include "ec_lcl.h"
  67. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
  68. const BIGNUM *x_, int y_bit, BN_CTX *ctx)
  69. {
  70. BN_CTX *new_ctx = NULL;
  71. BIGNUM *tmp1, *tmp2, *x, *y;
  72. int ret = 0;
  73. /* clear error queue*/
  74. ERR_clear_error();
  75. if (ctx == NULL)
  76. {
  77. ctx = new_ctx = BN_CTX_new();
  78. if (ctx == NULL)
  79. return 0;
  80. }
  81. y_bit = (y_bit != 0);
  82. BN_CTX_start(ctx);
  83. tmp1 = BN_CTX_get(ctx);
  84. tmp2 = BN_CTX_get(ctx);
  85. x = BN_CTX_get(ctx);
  86. y = BN_CTX_get(ctx);
  87. if (y == NULL) goto err;
  88. /* Recover y. We have a Weierstrass equation
  89. * y^2 = x^3 + a*x + b,
  90. * so y is one of the square roots of x^3 + a*x + b.
  91. */
  92. /* tmp1 := x^3 */
  93. if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
  94. if (group->meth->field_decode == 0)
  95. {
  96. /* field_{sqr,mul} work on standard representation */
  97. if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
  98. if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
  99. }
  100. else
  101. {
  102. if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
  103. if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
  104. }
  105. /* tmp1 := tmp1 + a*x */
  106. if (group->a_is_minus3)
  107. {
  108. if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
  109. if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
  110. if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  111. }
  112. else
  113. {
  114. if (group->meth->field_decode)
  115. {
  116. if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
  117. if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
  118. }
  119. else
  120. {
  121. /* field_mul works on standard representation */
  122. if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
  123. }
  124. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  125. }
  126. /* tmp1 := tmp1 + b */
  127. if (group->meth->field_decode)
  128. {
  129. if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
  130. if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
  131. }
  132. else
  133. {
  134. if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
  135. }
  136. if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
  137. {
  138. unsigned long err = ERR_peek_last_error();
  139. if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
  140. {
  141. ERR_clear_error();
  142. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
  143. }
  144. else
  145. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
  146. goto err;
  147. }
  148. if (y_bit != BN_is_odd(y))
  149. {
  150. if (BN_is_zero(y))
  151. {
  152. int kron;
  153. kron = BN_kronecker(x, &group->field, ctx);
  154. if (kron == -2) goto err;
  155. if (kron == 1)
  156. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
  157. else
  158. /* BN_mod_sqrt() should have cought this error (not a square) */
  159. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
  160. goto err;
  161. }
  162. if (!BN_usub(y, &group->field, y)) goto err;
  163. }
  164. if (y_bit != BN_is_odd(y))
  165. {
  166. ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
  167. goto err;
  168. }
  169. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  170. ret = 1;
  171. err:
  172. BN_CTX_end(ctx);
  173. if (new_ctx != NULL)
  174. BN_CTX_free(new_ctx);
  175. return ret;
  176. }
  177. size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
  178. unsigned char *buf, size_t len, BN_CTX *ctx)
  179. {
  180. size_t ret;
  181. BN_CTX *new_ctx = NULL;
  182. int used_ctx = 0;
  183. BIGNUM *x, *y;
  184. size_t field_len, i, skip;
  185. if ((form != POINT_CONVERSION_COMPRESSED)
  186. && (form != POINT_CONVERSION_UNCOMPRESSED)
  187. && (form != POINT_CONVERSION_HYBRID))
  188. {
  189. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
  190. goto err;
  191. }
  192. if (EC_POINT_is_at_infinity(group, point))
  193. {
  194. /* encodes to a single 0 octet */
  195. if (buf != NULL)
  196. {
  197. if (len < 1)
  198. {
  199. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  200. return 0;
  201. }
  202. buf[0] = 0;
  203. }
  204. return 1;
  205. }
  206. /* ret := required output buffer length */
  207. field_len = BN_num_bytes(&group->field);
  208. ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  209. /* if 'buf' is NULL, just return required length */
  210. if (buf != NULL)
  211. {
  212. if (len < ret)
  213. {
  214. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  215. goto err;
  216. }
  217. if (ctx == NULL)
  218. {
  219. ctx = new_ctx = BN_CTX_new();
  220. if (ctx == NULL)
  221. return 0;
  222. }
  223. BN_CTX_start(ctx);
  224. used_ctx = 1;
  225. x = BN_CTX_get(ctx);
  226. y = BN_CTX_get(ctx);
  227. if (y == NULL) goto err;
  228. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  229. if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
  230. buf[0] = form + 1;
  231. else
  232. buf[0] = form;
  233. i = 1;
  234. skip = field_len - BN_num_bytes(x);
  235. if (skip > field_len)
  236. {
  237. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  238. goto err;
  239. }
  240. while (skip > 0)
  241. {
  242. buf[i++] = 0;
  243. skip--;
  244. }
  245. skip = BN_bn2bin(x, buf + i);
  246. i += skip;
  247. if (i != 1 + field_len)
  248. {
  249. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  250. goto err;
  251. }
  252. if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
  253. {
  254. skip = field_len - BN_num_bytes(y);
  255. if (skip > field_len)
  256. {
  257. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  258. goto err;
  259. }
  260. while (skip > 0)
  261. {
  262. buf[i++] = 0;
  263. skip--;
  264. }
  265. skip = BN_bn2bin(y, buf + i);
  266. i += skip;
  267. }
  268. if (i != ret)
  269. {
  270. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
  271. goto err;
  272. }
  273. }
  274. if (used_ctx)
  275. BN_CTX_end(ctx);
  276. if (new_ctx != NULL)
  277. BN_CTX_free(new_ctx);
  278. return ret;
  279. err:
  280. if (used_ctx)
  281. BN_CTX_end(ctx);
  282. if (new_ctx != NULL)
  283. BN_CTX_free(new_ctx);
  284. return 0;
  285. }
  286. int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
  287. const unsigned char *buf, size_t len, BN_CTX *ctx)
  288. {
  289. point_conversion_form_t form;
  290. int y_bit;
  291. BN_CTX *new_ctx = NULL;
  292. BIGNUM *x, *y;
  293. size_t field_len, enc_len;
  294. int ret = 0;
  295. if (len == 0)
  296. {
  297. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
  298. return 0;
  299. }
  300. form = buf[0];
  301. y_bit = form & 1;
  302. form = form & ~1U;
  303. if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
  304. && (form != POINT_CONVERSION_UNCOMPRESSED)
  305. && (form != POINT_CONVERSION_HYBRID))
  306. {
  307. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  308. return 0;
  309. }
  310. if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
  311. {
  312. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  313. return 0;
  314. }
  315. if (form == 0)
  316. {
  317. if (len != 1)
  318. {
  319. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  320. return 0;
  321. }
  322. return EC_POINT_set_to_infinity(group, point);
  323. }
  324. field_len = BN_num_bytes(&group->field);
  325. enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
  326. if (len != enc_len)
  327. {
  328. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  329. return 0;
  330. }
  331. if (ctx == NULL)
  332. {
  333. ctx = new_ctx = BN_CTX_new();
  334. if (ctx == NULL)
  335. return 0;
  336. }
  337. BN_CTX_start(ctx);
  338. x = BN_CTX_get(ctx);
  339. y = BN_CTX_get(ctx);
  340. if (y == NULL) goto err;
  341. if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
  342. if (BN_ucmp(x, &group->field) >= 0)
  343. {
  344. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  345. goto err;
  346. }
  347. if (form == POINT_CONVERSION_COMPRESSED)
  348. {
  349. if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
  350. }
  351. else
  352. {
  353. if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
  354. if (BN_ucmp(y, &group->field) >= 0)
  355. {
  356. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  357. goto err;
  358. }
  359. if (form == POINT_CONVERSION_HYBRID)
  360. {
  361. if (y_bit != BN_is_odd(y))
  362. {
  363. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
  364. goto err;
  365. }
  366. }
  367. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
  368. }
  369. if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
  370. {
  371. ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
  372. goto err;
  373. }
  374. ret = 1;
  375. err:
  376. BN_CTX_end(ctx);
  377. if (new_ctx != NULL)
  378. BN_CTX_free(new_ctx);
  379. return ret;
  380. }