You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

305 lines
9.3 KiB

  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 "../delocate.h"
  73. #include "internal.h"
  74. int ec_GFp_mont_group_init(EC_GROUP *group) {
  75. int ok;
  76. ok = ec_GFp_simple_group_init(group);
  77. group->mont = NULL;
  78. return ok;
  79. }
  80. void ec_GFp_mont_group_finish(EC_GROUP *group) {
  81. BN_MONT_CTX_free(group->mont);
  82. group->mont = NULL;
  83. ec_GFp_simple_group_finish(group);
  84. }
  85. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
  86. BN_MONT_CTX_free(dest->mont);
  87. dest->mont = NULL;
  88. if (!ec_GFp_simple_group_copy(dest, src)) {
  89. return 0;
  90. }
  91. if (src->mont != NULL) {
  92. dest->mont = BN_MONT_CTX_new();
  93. if (dest->mont == NULL) {
  94. return 0;
  95. }
  96. if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
  97. goto err;
  98. }
  99. }
  100. return 1;
  101. err:
  102. BN_MONT_CTX_free(dest->mont);
  103. dest->mont = NULL;
  104. return 0;
  105. }
  106. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  107. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  108. BN_CTX *new_ctx = NULL;
  109. BN_MONT_CTX *mont = NULL;
  110. int ret = 0;
  111. BN_MONT_CTX_free(group->mont);
  112. group->mont = NULL;
  113. if (ctx == NULL) {
  114. ctx = new_ctx = BN_CTX_new();
  115. if (ctx == NULL) {
  116. return 0;
  117. }
  118. }
  119. mont = BN_MONT_CTX_new();
  120. if (mont == NULL) {
  121. goto err;
  122. }
  123. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  124. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  125. goto err;
  126. }
  127. group->mont = mont;
  128. mont = NULL;
  129. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  130. if (!ret) {
  131. BN_MONT_CTX_free(group->mont);
  132. group->mont = NULL;
  133. }
  134. err:
  135. BN_CTX_free(new_ctx);
  136. BN_MONT_CTX_free(mont);
  137. return ret;
  138. }
  139. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  140. const BIGNUM *b, BN_CTX *ctx) {
  141. if (group->mont == NULL) {
  142. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  143. return 0;
  144. }
  145. return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
  146. }
  147. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  148. BN_CTX *ctx) {
  149. if (group->mont == NULL) {
  150. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  151. return 0;
  152. }
  153. return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
  154. }
  155. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  156. BN_CTX *ctx) {
  157. if (group->mont == NULL) {
  158. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  159. return 0;
  160. }
  161. return BN_to_montgomery(r, a, group->mont, ctx);
  162. }
  163. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  164. BN_CTX *ctx) {
  165. if (group->mont == NULL) {
  166. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  167. return 0;
  168. }
  169. return BN_from_montgomery(r, a, group->mont, ctx);
  170. }
  171. static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
  172. const EC_POINT *point,
  173. BIGNUM *x, BIGNUM *y,
  174. BN_CTX *ctx) {
  175. if (EC_POINT_is_at_infinity(group, point)) {
  176. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  177. return 0;
  178. }
  179. BN_CTX *new_ctx = NULL;
  180. if (ctx == NULL) {
  181. ctx = new_ctx = BN_CTX_new();
  182. if (ctx == NULL) {
  183. return 0;
  184. }
  185. }
  186. int ret = 0;
  187. BN_CTX_start(ctx);
  188. if (BN_cmp(&point->Z, &group->one) == 0) {
  189. /* |point| is already affine. */
  190. if (x != NULL && !BN_from_montgomery(x, &point->X, group->mont, ctx)) {
  191. goto err;
  192. }
  193. if (y != NULL && !BN_from_montgomery(y, &point->Y, group->mont, ctx)) {
  194. goto err;
  195. }
  196. } else {
  197. /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
  198. BIGNUM *Z_1 = BN_CTX_get(ctx);
  199. BIGNUM *Z_2 = BN_CTX_get(ctx);
  200. BIGNUM *Z_3 = BN_CTX_get(ctx);
  201. if (Z_1 == NULL ||
  202. Z_2 == NULL ||
  203. Z_3 == NULL) {
  204. goto err;
  205. }
  206. /* The straightforward way to calculate the inverse of a Montgomery-encoded
  207. * value where the result is Montgomery-encoded is:
  208. *
  209. * |BN_from_montgomery| + invert + |BN_to_montgomery|.
  210. *
  211. * This is equivalent, but more efficient, because |BN_from_montgomery|
  212. * is more efficient (at least in theory) than |BN_to_montgomery|, since it
  213. * doesn't have to do the multiplication before the reduction.
  214. *
  215. * Use Fermat's Little Theorem instead of |BN_mod_inverse_odd| since this
  216. * inversion may be done as the final step of private key operations.
  217. * Unfortunately, this is suboptimal for ECDSA verification. */
  218. if (!BN_from_montgomery(Z_1, &point->Z, group->mont, ctx) ||
  219. !BN_from_montgomery(Z_1, Z_1, group->mont, ctx) ||
  220. !bn_mod_inverse_prime(Z_1, Z_1, &group->field, ctx, group->mont)) {
  221. goto err;
  222. }
  223. if (!BN_mod_mul_montgomery(Z_2, Z_1, Z_1, group->mont, ctx)) {
  224. goto err;
  225. }
  226. /* Instead of using |BN_from_montgomery| to convert the |x| coordinate
  227. * and then calling |BN_from_montgomery| again to convert the |y|
  228. * coordinate below, convert the common factor |Z_2| once now, saving one
  229. * reduction. */
  230. if (!BN_from_montgomery(Z_2, Z_2, group->mont, ctx)) {
  231. goto err;
  232. }
  233. if (x != NULL) {
  234. if (!BN_mod_mul_montgomery(x, &point->X, Z_2, group->mont, ctx)) {
  235. goto err;
  236. }
  237. }
  238. if (y != NULL) {
  239. if (!BN_mod_mul_montgomery(Z_3, Z_2, Z_1, group->mont, ctx) ||
  240. !BN_mod_mul_montgomery(y, &point->Y, Z_3, group->mont, ctx)) {
  241. goto err;
  242. }
  243. }
  244. }
  245. ret = 1;
  246. err:
  247. BN_CTX_end(ctx);
  248. BN_CTX_free(new_ctx);
  249. return ret;
  250. }
  251. DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_mont_method) {
  252. out->group_init = ec_GFp_mont_group_init;
  253. out->group_finish = ec_GFp_mont_group_finish;
  254. out->group_copy = ec_GFp_mont_group_copy;
  255. out->group_set_curve = ec_GFp_mont_group_set_curve;
  256. out->point_get_affine_coordinates = ec_GFp_mont_point_get_affine_coordinates;
  257. out->mul = ec_wNAF_mul /* XXX: Not constant time. */;
  258. out->field_mul = ec_GFp_mont_field_mul;
  259. out->field_sqr = ec_GFp_mont_field_sqr;
  260. out->field_encode = ec_GFp_mont_field_encode;
  261. out->field_decode = ec_GFp_mont_field_decode;
  262. }