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.
 
 
 
 
 
 

272 line
7.6 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 "internal.h"
  72. int ec_GFp_mont_group_init(EC_GROUP *group) {
  73. int ok;
  74. ok = ec_GFp_simple_group_init(group);
  75. group->mont = NULL;
  76. group->one = 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. BN_free(group->one);
  83. group->one = NULL;
  84. ec_GFp_simple_group_finish(group);
  85. }
  86. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
  87. BN_MONT_CTX_free(dest->mont);
  88. dest->mont = NULL;
  89. BN_clear_free(dest->one);
  90. dest->one = NULL;
  91. if (!ec_GFp_simple_group_copy(dest, src)) {
  92. return 0;
  93. }
  94. if (src->mont != NULL) {
  95. dest->mont = BN_MONT_CTX_new();
  96. if (dest->mont == NULL) {
  97. return 0;
  98. }
  99. if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
  100. goto err;
  101. }
  102. }
  103. if (src->one != NULL) {
  104. dest->one = BN_dup(src->one);
  105. if (dest->one == NULL) {
  106. goto err;
  107. }
  108. }
  109. return 1;
  110. err:
  111. BN_MONT_CTX_free(dest->mont);
  112. dest->mont = NULL;
  113. return 0;
  114. }
  115. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  116. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  117. BN_CTX *new_ctx = NULL;
  118. BN_MONT_CTX *mont = NULL;
  119. BIGNUM *one = NULL;
  120. int ret = 0;
  121. BN_MONT_CTX_free(group->mont);
  122. group->mont = NULL;
  123. BN_free(group->one);
  124. group->one = NULL;
  125. if (ctx == NULL) {
  126. ctx = new_ctx = BN_CTX_new();
  127. if (ctx == NULL) {
  128. return 0;
  129. }
  130. }
  131. mont = BN_MONT_CTX_new();
  132. if (mont == NULL) {
  133. goto err;
  134. }
  135. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  136. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  137. goto err;
  138. }
  139. one = BN_new();
  140. if (one == NULL || !BN_to_montgomery(one, BN_value_one(), mont, ctx)) {
  141. goto err;
  142. }
  143. group->mont = mont;
  144. mont = NULL;
  145. group->one = one;
  146. one = NULL;
  147. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  148. if (!ret) {
  149. BN_MONT_CTX_free(group->mont);
  150. group->mont = NULL;
  151. BN_free(group->one);
  152. group->one = NULL;
  153. }
  154. err:
  155. BN_CTX_free(new_ctx);
  156. BN_MONT_CTX_free(mont);
  157. BN_free(one);
  158. return ret;
  159. }
  160. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  161. const BIGNUM *b, BN_CTX *ctx) {
  162. if (group->mont == NULL) {
  163. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  164. return 0;
  165. }
  166. return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
  167. }
  168. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  169. BN_CTX *ctx) {
  170. if (group->mont == NULL) {
  171. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  172. return 0;
  173. }
  174. return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
  175. }
  176. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  177. BN_CTX *ctx) {
  178. if (group->mont == NULL) {
  179. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  180. return 0;
  181. }
  182. return BN_to_montgomery(r, a, group->mont, ctx);
  183. }
  184. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  185. BN_CTX *ctx) {
  186. if (group->mont == NULL) {
  187. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  188. return 0;
  189. }
  190. return BN_from_montgomery(r, a, group->mont, ctx);
  191. }
  192. int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  193. BN_CTX *ctx) {
  194. if (group->one == NULL) {
  195. OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
  196. return 0;
  197. }
  198. if (!BN_copy(r, group->one)) {
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. static int ec_GFp_mont_check_pub_key_order(const EC_GROUP *group,
  204. const EC_POINT* pub_key,
  205. BN_CTX *ctx) {
  206. EC_POINT *point = EC_POINT_new(group);
  207. int ret = 0;
  208. if (point == NULL ||
  209. !ec_wNAF_mul(group, point, NULL, pub_key, EC_GROUP_get0_order(group),
  210. ctx) ||
  211. !EC_POINT_is_at_infinity(group, point)) {
  212. goto err;
  213. }
  214. ret = 1;
  215. err:
  216. EC_POINT_free(point);
  217. return ret;
  218. }
  219. const EC_METHOD *EC_GFp_mont_method(void) {
  220. static const EC_METHOD ret = {
  221. ec_GFp_mont_group_init,
  222. ec_GFp_mont_group_finish,
  223. ec_GFp_mont_group_copy,
  224. ec_GFp_mont_group_set_curve,
  225. ec_GFp_simple_point_get_affine_coordinates,
  226. ec_wNAF_mul /* XXX: Not constant time. */,
  227. ec_GFp_mont_check_pub_key_order,
  228. ec_GFp_mont_field_mul,
  229. ec_GFp_mont_field_sqr,
  230. ec_GFp_mont_field_encode,
  231. ec_GFp_mont_field_decode,
  232. ec_GFp_mont_field_set_to_one,
  233. };
  234. return &ret;
  235. }