ec_montgomery.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <openssl/bn.h>
  69. #include <openssl/err.h>
  70. #include <openssl/mem.h>
  71. #include "../bn/internal.h"
  72. #include "internal.h"
  73. int ec_GFp_mont_group_init(EC_GROUP *group) {
  74. int ok;
  75. ok = ec_GFp_simple_group_init(group);
  76. group->mont = NULL;
  77. return ok;
  78. }
  79. void ec_GFp_mont_group_finish(EC_GROUP *group) {
  80. BN_MONT_CTX_free(group->mont);
  81. group->mont = NULL;
  82. ec_GFp_simple_group_finish(group);
  83. }
  84. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
  85. BN_MONT_CTX_free(dest->mont);
  86. dest->mont = NULL;
  87. if (!ec_GFp_simple_group_copy(dest, src)) {
  88. return 0;
  89. }
  90. if (src->mont != NULL) {
  91. dest->mont = BN_MONT_CTX_new();
  92. if (dest->mont == NULL) {
  93. return 0;
  94. }
  95. if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
  96. goto err;
  97. }
  98. }
  99. return 1;
  100. err:
  101. BN_MONT_CTX_free(dest->mont);
  102. dest->mont = NULL;
  103. return 0;
  104. }
  105. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  106. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  107. BN_CTX *new_ctx = NULL;
  108. BN_MONT_CTX *mont = NULL;
  109. int ret = 0;
  110. BN_MONT_CTX_free(group->mont);
  111. group->mont = NULL;
  112. if (ctx == NULL) {
  113. ctx = new_ctx = BN_CTX_new();
  114. if (ctx == NULL) {
  115. return 0;
  116. }
  117. }
  118. mont = BN_MONT_CTX_new();
  119. if (mont == NULL) {
  120. goto err;
  121. }
  122. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  123. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  124. goto err;
  125. }
  126. group->mont = mont;
  127. mont = NULL;
  128. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  129. if (!ret) {
  130. BN_MONT_CTX_free(group->mont);
  131. group->mont = NULL;
  132. }
  133. err:
  134. BN_CTX_free(new_ctx);
  135. BN_MONT_CTX_free(mont);
  136. return ret;
  137. }
  138. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  139. const BIGNUM *b, BN_CTX *ctx) {
  140. if (group->mont == NULL) {
  141. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  142. return 0;
  143. }
  144. return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
  145. }
  146. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  147. BN_CTX *ctx) {
  148. if (group->mont == NULL) {
  149. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  150. return 0;
  151. }
  152. return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
  153. }
  154. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  155. BN_CTX *ctx) {
  156. if (group->mont == NULL) {
  157. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  158. return 0;
  159. }
  160. return BN_to_montgomery(r, a, group->mont, ctx);
  161. }
  162. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  163. BN_CTX *ctx) {
  164. if (group->mont == NULL) {
  165. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  166. return 0;
  167. }
  168. return BN_from_montgomery(r, a, group->mont, ctx);
  169. }
  170. static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
  171. const EC_POINT *point,
  172. BIGNUM *x, BIGNUM *y,
  173. BN_CTX *ctx) {
  174. if (EC_POINT_is_at_infinity(group, point)) {
  175. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  176. return 0;
  177. }
  178. BN_CTX *new_ctx = NULL;
  179. if (ctx == NULL) {
  180. ctx = new_ctx = BN_CTX_new();
  181. if (ctx == NULL) {
  182. return 0;
  183. }
  184. }
  185. int ret = 0;
  186. BN_CTX_start(ctx);
  187. if (BN_cmp(&point->Z, &group->one) == 0) {
  188. /* |point| is already affine. */
  189. if (x != NULL && !BN_from_montgomery(x, &point->X, group->mont, ctx)) {
  190. goto err;
  191. }
  192. if (y != NULL && !BN_from_montgomery(y, &point->Y, group->mont, ctx)) {
  193. goto err;
  194. }
  195. } else {
  196. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  197. BIGNUM *Z_1 = BN_CTX_get(ctx);
  198. BIGNUM *Z_2 = BN_CTX_get(ctx);
  199. BIGNUM *Z_3 = BN_CTX_get(ctx);
  200. if (Z_1 == NULL ||
  201. Z_2 == NULL ||
  202. Z_3 == NULL) {
  203. goto err;
  204. }
  205. /* The straightforward way to calculate the inverse of a Montgomery-encoded
  206. * value where the result is Montgomery-encoded is:
  207. *
  208. * |BN_from_montgomery| + invert + |BN_to_montgomery|.
  209. *
  210. * This is equivalent, but more efficient, because |BN_from_montgomery|
  211. * is more efficient (at least in theory) than |BN_to_montgomery|, since it
  212. * doesn't have to do the multiplication before the reduction.
  213. *
  214. * Use Fermat's Little Theorem instead of |BN_mod_inverse_odd| since this
  215. * inversion may be done as the final step of private key operations.
  216. * Unfortunately, this is suboptimal for ECDSA verification. */
  217. if (!BN_from_montgomery(Z_1, &point->Z, group->mont, ctx) ||
  218. !BN_from_montgomery(Z_1, Z_1, group->mont, ctx) ||
  219. !bn_mod_inverse_prime(Z_1, Z_1, &group->field, ctx, group->mont)) {
  220. goto err;
  221. }
  222. if (!BN_mod_mul_montgomery(Z_2, Z_1, Z_1, group->mont, ctx)) {
  223. goto err;
  224. }
  225. /* Instead of using |BN_from_montgomery| to convert the |x| coordinate
  226. * and then calling |BN_from_montgomery| again to convert the |y|
  227. * coordinate below, convert the common factor |Z_2| once now, saving one
  228. * reduction. */
  229. if (!BN_from_montgomery(Z_2, Z_2, group->mont, ctx)) {
  230. goto err;
  231. }
  232. if (x != NULL) {
  233. if (!BN_mod_mul_montgomery(x, &point->X, Z_2, group->mont, ctx)) {
  234. goto err;
  235. }
  236. }
  237. if (y != NULL) {
  238. if (!BN_mod_mul_montgomery(Z_3, Z_2, Z_1, group->mont, ctx) ||
  239. !BN_mod_mul_montgomery(y, &point->Y, Z_3, group->mont, ctx)) {
  240. goto err;
  241. }
  242. }
  243. }
  244. ret = 1;
  245. err:
  246. BN_CTX_end(ctx);
  247. BN_CTX_free(new_ctx);
  248. return ret;
  249. }
  250. const EC_METHOD EC_GFp_mont_method = {
  251. ec_GFp_mont_group_init,
  252. ec_GFp_mont_group_finish,
  253. ec_GFp_mont_group_copy,
  254. ec_GFp_mont_group_set_curve,
  255. ec_GFp_mont_point_get_affine_coordinates,
  256. ec_wNAF_mul /* XXX: Not constant time. */,
  257. ec_GFp_mont_field_mul,
  258. ec_GFp_mont_field_sqr,
  259. ec_GFp_mont_field_encode,
  260. ec_GFp_mont_field_decode,
  261. };