ec.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /* Originally written by Bodo Moeller 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 <assert.h>
  69. #include <string.h>
  70. #include <openssl/bn.h>
  71. #include <openssl/err.h>
  72. #include <openssl/mem.h>
  73. #include <openssl/nid.h>
  74. #include "internal.h"
  75. #include "../../internal.h"
  76. #include "../bn/internal.h"
  77. #include "../delocate.h"
  78. static void ec_point_free(EC_POINT *point, int free_group);
  79. static const uint8_t kP224Params[6 * 28] = {
  80. // p
  81. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  82. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x01,
  84. // a
  85. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  86. 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  87. 0xFF, 0xFF, 0xFF, 0xFE,
  88. // b
  89. 0xB4, 0x05, 0x0A, 0x85, 0x0C, 0x04, 0xB3, 0xAB, 0xF5, 0x41, 0x32, 0x56,
  90. 0x50, 0x44, 0xB0, 0xB7, 0xD7, 0xBF, 0xD8, 0xBA, 0x27, 0x0B, 0x39, 0x43,
  91. 0x23, 0x55, 0xFF, 0xB4,
  92. // x
  93. 0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, 0x90, 0xB9,
  94. 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xD6,
  95. 0x11, 0x5C, 0x1D, 0x21,
  96. // y
  97. 0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, 0xdf, 0xe6,
  98. 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64, 0x44, 0xd5, 0x81, 0x99,
  99. 0x85, 0x00, 0x7e, 0x34,
  100. // order
  101. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  102. 0xFF, 0xFF, 0x16, 0xA2, 0xE0, 0xB8, 0xF0, 0x3E, 0x13, 0xDD, 0x29, 0x45,
  103. 0x5C, 0x5C, 0x2A, 0x3D,
  104. };
  105. static const uint8_t kP256Params[6 * 32] = {
  106. // p
  107. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  109. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  110. // a
  111. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  113. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
  114. // b
  115. 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55,
  116. 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
  117. 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B,
  118. // x
  119. 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8, 0xBC, 0xE6, 0xE5,
  120. 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
  121. 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96,
  122. // y
  123. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a,
  124. 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
  125. 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  126. // order
  127. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  128. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  129. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51,
  130. };
  131. static const uint8_t kP384Params[6 * 48] = {
  132. // p
  133. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  134. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  135. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  137. // a
  138. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  139. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  140. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFC,
  142. // b
  143. 0xB3, 0x31, 0x2F, 0xA7, 0xE2, 0x3E, 0xE7, 0xE4, 0x98, 0x8E, 0x05, 0x6B,
  144. 0xE3, 0xF8, 0x2D, 0x19, 0x18, 0x1D, 0x9C, 0x6E, 0xFE, 0x81, 0x41, 0x12,
  145. 0x03, 0x14, 0x08, 0x8F, 0x50, 0x13, 0x87, 0x5A, 0xC6, 0x56, 0x39, 0x8D,
  146. 0x8A, 0x2E, 0xD1, 0x9D, 0x2A, 0x85, 0xC8, 0xED, 0xD3, 0xEC, 0x2A, 0xEF,
  147. // x
  148. 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E,
  149. 0xF3, 0x20, 0xAD, 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98,
  150. 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38, 0x55, 0x02, 0xF2, 0x5D,
  151. 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7,
  152. // y
  153. 0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f, 0x5d, 0x9e, 0x98, 0xbf,
  154. 0x92, 0x92, 0xdc, 0x29, 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c,
  155. 0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0, 0x0a, 0x60, 0xb1, 0xce,
  156. 0x1d, 0x7e, 0x81, 0x9d, 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f,
  157. // order
  158. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  159. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  160. 0xC7, 0x63, 0x4D, 0x81, 0xF4, 0x37, 0x2D, 0xDF, 0x58, 0x1A, 0x0D, 0xB2,
  161. 0x48, 0xB0, 0xA7, 0x7A, 0xEC, 0xEC, 0x19, 0x6A, 0xCC, 0xC5, 0x29, 0x73,
  162. };
  163. static const uint8_t kP521Params[6 * 66] = {
  164. // p
  165. 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  166. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  167. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  168. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  169. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  170. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  171. // a
  172. 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  173. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  174. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  175. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  176. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  177. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
  178. // b
  179. 0x00, 0x51, 0x95, 0x3E, 0xB9, 0x61, 0x8E, 0x1C, 0x9A, 0x1F, 0x92, 0x9A,
  180. 0x21, 0xA0, 0xB6, 0x85, 0x40, 0xEE, 0xA2, 0xDA, 0x72, 0x5B, 0x99, 0xB3,
  181. 0x15, 0xF3, 0xB8, 0xB4, 0x89, 0x91, 0x8E, 0xF1, 0x09, 0xE1, 0x56, 0x19,
  182. 0x39, 0x51, 0xEC, 0x7E, 0x93, 0x7B, 0x16, 0x52, 0xC0, 0xBD, 0x3B, 0xB1,
  183. 0xBF, 0x07, 0x35, 0x73, 0xDF, 0x88, 0x3D, 0x2C, 0x34, 0xF1, 0xEF, 0x45,
  184. 0x1F, 0xD4, 0x6B, 0x50, 0x3F, 0x00,
  185. // x
  186. 0x00, 0xC6, 0x85, 0x8E, 0x06, 0xB7, 0x04, 0x04, 0xE9, 0xCD, 0x9E, 0x3E,
  187. 0xCB, 0x66, 0x23, 0x95, 0xB4, 0x42, 0x9C, 0x64, 0x81, 0x39, 0x05, 0x3F,
  188. 0xB5, 0x21, 0xF8, 0x28, 0xAF, 0x60, 0x6B, 0x4D, 0x3D, 0xBA, 0xA1, 0x4B,
  189. 0x5E, 0x77, 0xEF, 0xE7, 0x59, 0x28, 0xFE, 0x1D, 0xC1, 0x27, 0xA2, 0xFF,
  190. 0xA8, 0xDE, 0x33, 0x48, 0xB3, 0xC1, 0x85, 0x6A, 0x42, 0x9B, 0xF9, 0x7E,
  191. 0x7E, 0x31, 0xC2, 0xE5, 0xBD, 0x66,
  192. // y
  193. 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a,
  194. 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
  195. 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee,
  196. 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
  197. 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe,
  198. 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
  199. // order
  200. 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  201. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  202. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFA, 0x51, 0x86,
  203. 0x87, 0x83, 0xBF, 0x2F, 0x96, 0x6B, 0x7F, 0xCC, 0x01, 0x48, 0xF7, 0x09,
  204. 0xA5, 0xD0, 0x3B, 0xB5, 0xC9, 0xB8, 0x89, 0x9C, 0x47, 0xAE, 0xBB, 0x6F,
  205. 0xB7, 0x1E, 0x91, 0x38, 0x64, 0x09,
  206. };
  207. DEFINE_METHOD_FUNCTION(struct built_in_curves, OPENSSL_built_in_curves) {
  208. // 1.3.132.0.35
  209. static const uint8_t kOIDP521[] = {0x2b, 0x81, 0x04, 0x00, 0x23};
  210. out->curves[0].nid = NID_secp521r1;
  211. out->curves[0].oid = kOIDP521;
  212. out->curves[0].oid_len = sizeof(kOIDP521);
  213. out->curves[0].comment = "NIST P-521";
  214. out->curves[0].param_len = 66;
  215. out->curves[0].params = kP521Params;
  216. out->curves[0].method = EC_GFp_mont_method();
  217. // 1.3.132.0.34
  218. static const uint8_t kOIDP384[] = {0x2b, 0x81, 0x04, 0x00, 0x22};
  219. out->curves[1].nid = NID_secp384r1;
  220. out->curves[1].oid = kOIDP384;
  221. out->curves[1].oid_len = sizeof(kOIDP384);
  222. out->curves[1].comment = "NIST P-384";
  223. out->curves[1].param_len = 48;
  224. out->curves[1].params = kP384Params;
  225. out->curves[1].method = EC_GFp_mont_method();
  226. // 1.2.840.10045.3.1.7
  227. static const uint8_t kOIDP256[] = {0x2a, 0x86, 0x48, 0xce,
  228. 0x3d, 0x03, 0x01, 0x07};
  229. out->curves[2].nid = NID_X9_62_prime256v1;
  230. out->curves[2].oid = kOIDP256;
  231. out->curves[2].oid_len = sizeof(kOIDP256);
  232. out->curves[2].comment = "NIST P-256";
  233. out->curves[2].param_len = 32;
  234. out->curves[2].params = kP256Params;
  235. out->curves[2].method =
  236. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  237. !defined(OPENSSL_SMALL)
  238. EC_GFp_nistz256_method();
  239. #else
  240. EC_GFp_nistp256_method();
  241. #endif
  242. // 1.3.132.0.33
  243. static const uint8_t kOIDP224[] = {0x2b, 0x81, 0x04, 0x00, 0x21};
  244. out->curves[3].nid = NID_secp224r1;
  245. out->curves[3].oid = kOIDP224;
  246. out->curves[3].oid_len = sizeof(kOIDP224);
  247. out->curves[3].comment = "NIST P-224";
  248. out->curves[3].param_len = 28;
  249. out->curves[3].params = kP224Params;
  250. out->curves[3].method =
  251. #if defined(BORINGSSL_HAS_UINT128) && !defined(OPENSSL_SMALL)
  252. EC_GFp_nistp224_method();
  253. #else
  254. EC_GFp_mont_method();
  255. #endif
  256. }
  257. EC_GROUP *ec_group_new(const EC_METHOD *meth) {
  258. EC_GROUP *ret;
  259. if (meth == NULL) {
  260. OPENSSL_PUT_ERROR(EC, EC_R_SLOT_FULL);
  261. return NULL;
  262. }
  263. if (meth->group_init == 0) {
  264. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  265. return NULL;
  266. }
  267. ret = OPENSSL_malloc(sizeof(EC_GROUP));
  268. if (ret == NULL) {
  269. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  270. return NULL;
  271. }
  272. OPENSSL_memset(ret, 0, sizeof(EC_GROUP));
  273. ret->references = 1;
  274. ret->meth = meth;
  275. BN_init(&ret->order);
  276. if (!meth->group_init(ret)) {
  277. OPENSSL_free(ret);
  278. return NULL;
  279. }
  280. return ret;
  281. }
  282. static void ec_group_set0_generator(EC_GROUP *group, EC_POINT *generator) {
  283. assert(group->generator == NULL);
  284. assert(group == generator->group);
  285. // Avoid a reference cycle. |group->generator| does not maintain an owning
  286. // pointer to |group|.
  287. group->generator = generator;
  288. int is_zero = CRYPTO_refcount_dec_and_test_zero(&group->references);
  289. assert(!is_zero);
  290. (void)is_zero;
  291. }
  292. EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
  293. const BIGNUM *b, BN_CTX *ctx) {
  294. if (BN_num_bytes(p) > EC_MAX_SCALAR_BYTES) {
  295. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
  296. return NULL;
  297. }
  298. EC_GROUP *ret = ec_group_new(EC_GFp_mont_method());
  299. if (ret == NULL) {
  300. return NULL;
  301. }
  302. if (ret->meth->group_set_curve == NULL) {
  303. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  304. EC_GROUP_free(ret);
  305. return NULL;
  306. }
  307. if (!ret->meth->group_set_curve(ret, p, a, b, ctx)) {
  308. EC_GROUP_free(ret);
  309. return NULL;
  310. }
  311. return ret;
  312. }
  313. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  314. const BIGNUM *order, const BIGNUM *cofactor) {
  315. if (group->curve_name != NID_undef || group->generator != NULL ||
  316. generator->group != group) {
  317. // |EC_GROUP_set_generator| may only be used with |EC_GROUP|s returned by
  318. // |EC_GROUP_new_curve_GFp| and may only used once on each group.
  319. // Additionally, |generator| must been created from
  320. // |EC_GROUP_new_curve_GFp|, not a copy, so that
  321. // |generator->group->generator| is set correctly.
  322. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  323. return 0;
  324. }
  325. if (BN_num_bytes(order) > EC_MAX_SCALAR_BYTES) {
  326. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
  327. return 0;
  328. }
  329. // Require a cofactor of one for custom curves, which implies prime order.
  330. if (!BN_is_one(cofactor)) {
  331. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COFACTOR);
  332. return 0;
  333. }
  334. // Require that p < 2×order. This simplifies some ECDSA operations.
  335. //
  336. // Note any curve which did not satisfy this must have been invalid or use a
  337. // tiny prime (less than 17). See the proof in |field_element_to_scalar| in
  338. // the ECDSA implementation.
  339. BIGNUM *tmp = BN_new();
  340. if (tmp == NULL ||
  341. !BN_lshift1(tmp, order)) {
  342. BN_free(tmp);
  343. return 0;
  344. }
  345. int ok = BN_cmp(tmp, &group->field) > 0;
  346. BN_free(tmp);
  347. if (!ok) {
  348. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER);
  349. return 0;
  350. }
  351. EC_POINT *copy = EC_POINT_new(group);
  352. if (copy == NULL ||
  353. !EC_POINT_copy(copy, generator) ||
  354. !BN_copy(&group->order, order)) {
  355. EC_POINT_free(copy);
  356. return 0;
  357. }
  358. // Store the order in minimal form, so it can be used with |BN_ULONG| arrays.
  359. bn_set_minimal_width(&group->order);
  360. BN_MONT_CTX_free(group->order_mont);
  361. group->order_mont = BN_MONT_CTX_new_for_modulus(&group->order, NULL);
  362. if (group->order_mont == NULL) {
  363. return 0;
  364. }
  365. ec_group_set0_generator(group, copy);
  366. return 1;
  367. }
  368. static EC_GROUP *ec_group_new_from_data(const struct built_in_curve *curve) {
  369. EC_GROUP *group = NULL;
  370. EC_POINT *P = NULL;
  371. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  372. int ok = 0;
  373. BN_CTX *ctx = BN_CTX_new();
  374. if (ctx == NULL) {
  375. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  376. goto err;
  377. }
  378. const unsigned param_len = curve->param_len;
  379. const uint8_t *params = curve->params;
  380. if (!(p = BN_bin2bn(params + 0 * param_len, param_len, NULL)) ||
  381. !(a = BN_bin2bn(params + 1 * param_len, param_len, NULL)) ||
  382. !(b = BN_bin2bn(params + 2 * param_len, param_len, NULL))) {
  383. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  384. goto err;
  385. }
  386. group = ec_group_new(curve->method);
  387. if (group == NULL ||
  388. !group->meth->group_set_curve(group, p, a, b, ctx)) {
  389. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  390. goto err;
  391. }
  392. if ((P = EC_POINT_new(group)) == NULL) {
  393. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  394. goto err;
  395. }
  396. if (!(x = BN_bin2bn(params + 3 * param_len, param_len, NULL)) ||
  397. !(y = BN_bin2bn(params + 4 * param_len, param_len, NULL))) {
  398. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  399. goto err;
  400. }
  401. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) {
  402. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  403. goto err;
  404. }
  405. if (!BN_bin2bn(params + 5 * param_len, param_len, &group->order)) {
  406. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  407. goto err;
  408. }
  409. group->order_mont = BN_MONT_CTX_new_for_modulus(&group->order, ctx);
  410. if (group->order_mont == NULL) {
  411. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  412. goto err;
  413. }
  414. ec_group_set0_generator(group, P);
  415. P = NULL;
  416. ok = 1;
  417. err:
  418. if (!ok) {
  419. EC_GROUP_free(group);
  420. group = NULL;
  421. }
  422. EC_POINT_free(P);
  423. BN_CTX_free(ctx);
  424. BN_free(p);
  425. BN_free(a);
  426. BN_free(b);
  427. BN_free(x);
  428. BN_free(y);
  429. return group;
  430. }
  431. // Built-in groups are allocated lazily and static once allocated.
  432. // TODO(davidben): Make these actually static. https://crbug.com/boringssl/20.
  433. struct built_in_groups_st {
  434. EC_GROUP *groups[OPENSSL_NUM_BUILT_IN_CURVES];
  435. };
  436. DEFINE_BSS_GET(struct built_in_groups_st, built_in_groups);
  437. DEFINE_STATIC_MUTEX(built_in_groups_lock);
  438. EC_GROUP *EC_GROUP_new_by_curve_name(int nid) {
  439. struct built_in_groups_st *groups = built_in_groups_bss_get();
  440. EC_GROUP **group_ptr = NULL;
  441. const struct built_in_curves *const curves = OPENSSL_built_in_curves();
  442. const struct built_in_curve *curve = NULL;
  443. for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
  444. if (curves->curves[i].nid == nid) {
  445. curve = &curves->curves[i];
  446. group_ptr = &groups->groups[i];
  447. break;
  448. }
  449. }
  450. if (curve == NULL) {
  451. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  452. return NULL;
  453. }
  454. CRYPTO_STATIC_MUTEX_lock_read(built_in_groups_lock_bss_get());
  455. EC_GROUP *ret = *group_ptr;
  456. CRYPTO_STATIC_MUTEX_unlock_read(built_in_groups_lock_bss_get());
  457. if (ret != NULL) {
  458. return ret;
  459. }
  460. ret = ec_group_new_from_data(curve);
  461. if (ret == NULL) {
  462. return NULL;
  463. }
  464. EC_GROUP *to_free = NULL;
  465. CRYPTO_STATIC_MUTEX_lock_write(built_in_groups_lock_bss_get());
  466. if (*group_ptr == NULL) {
  467. *group_ptr = ret;
  468. // Filling in |ret->curve_name| makes |EC_GROUP_free| and |EC_GROUP_dup|
  469. // into no-ops. At this point, |ret| is considered static.
  470. ret->curve_name = nid;
  471. } else {
  472. to_free = ret;
  473. ret = *group_ptr;
  474. }
  475. CRYPTO_STATIC_MUTEX_unlock_write(built_in_groups_lock_bss_get());
  476. EC_GROUP_free(to_free);
  477. return ret;
  478. }
  479. void EC_GROUP_free(EC_GROUP *group) {
  480. if (group == NULL ||
  481. // Built-in curves are static.
  482. group->curve_name != NID_undef ||
  483. !CRYPTO_refcount_dec_and_test_zero(&group->references)) {
  484. return;
  485. }
  486. if (group->meth->group_finish != NULL) {
  487. group->meth->group_finish(group);
  488. }
  489. ec_point_free(group->generator, 0 /* don't free group */);
  490. BN_free(&group->order);
  491. BN_MONT_CTX_free(group->order_mont);
  492. OPENSSL_free(group);
  493. }
  494. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) {
  495. if (a == NULL ||
  496. // Built-in curves are static.
  497. a->curve_name != NID_undef) {
  498. return (EC_GROUP *)a;
  499. }
  500. // Groups are logically immutable (but for |EC_GROUP_set_generator| which must
  501. // be called early on), so we simply take a reference.
  502. EC_GROUP *group = (EC_GROUP *)a;
  503. CRYPTO_refcount_inc(&group->references);
  504. return group;
  505. }
  506. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ignored) {
  507. // Note this function returns 0 if equal and non-zero otherwise.
  508. if (a == b) {
  509. return 0;
  510. }
  511. if (a->curve_name != b->curve_name) {
  512. return 1;
  513. }
  514. if (a->curve_name != NID_undef) {
  515. // Built-in curves may be compared by curve name alone.
  516. return 0;
  517. }
  518. // |a| and |b| are both custom curves. We compare the entire curve
  519. // structure. If |a| or |b| is incomplete (due to legacy OpenSSL mistakes,
  520. // custom curve construction is sadly done in two parts) but otherwise not the
  521. // same object, we consider them always unequal.
  522. return a->generator == NULL ||
  523. b->generator == NULL ||
  524. BN_cmp(&a->order, &b->order) != 0 ||
  525. BN_cmp(&a->field, &b->field) != 0 ||
  526. BN_cmp(&a->a, &b->a) != 0 ||
  527. BN_cmp(&a->b, &b->b) != 0 ||
  528. ec_GFp_simple_cmp(a, a->generator, b->generator, NULL) != 0;
  529. }
  530. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) {
  531. return group->generator;
  532. }
  533. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) {
  534. assert(!BN_is_zero(&group->order));
  535. return &group->order;
  536. }
  537. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) {
  538. if (BN_copy(order, EC_GROUP_get0_order(group)) == NULL) {
  539. return 0;
  540. }
  541. return 1;
  542. }
  543. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  544. BN_CTX *ctx) {
  545. // All |EC_GROUP|s have cofactor 1.
  546. return BN_set_word(cofactor, 1);
  547. }
  548. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p, BIGNUM *out_a,
  549. BIGNUM *out_b, BN_CTX *ctx) {
  550. return ec_GFp_simple_group_get_curve(group, out_p, out_a, out_b, ctx);
  551. }
  552. int EC_GROUP_get_curve_name(const EC_GROUP *group) { return group->curve_name; }
  553. unsigned EC_GROUP_get_degree(const EC_GROUP *group) {
  554. return ec_GFp_simple_group_get_degree(group);
  555. }
  556. EC_POINT *EC_POINT_new(const EC_GROUP *group) {
  557. EC_POINT *ret;
  558. if (group == NULL) {
  559. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  560. return NULL;
  561. }
  562. ret = OPENSSL_malloc(sizeof *ret);
  563. if (ret == NULL) {
  564. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  565. return NULL;
  566. }
  567. ret->group = EC_GROUP_dup(group);
  568. if (ret->group == NULL ||
  569. !ec_GFp_simple_point_init(ret)) {
  570. OPENSSL_free(ret);
  571. return NULL;
  572. }
  573. return ret;
  574. }
  575. static void ec_point_free(EC_POINT *point, int free_group) {
  576. if (!point) {
  577. return;
  578. }
  579. ec_GFp_simple_point_finish(point);
  580. if (free_group) {
  581. EC_GROUP_free(point->group);
  582. }
  583. OPENSSL_free(point);
  584. }
  585. void EC_POINT_free(EC_POINT *point) {
  586. ec_point_free(point, 1 /* free group */);
  587. }
  588. void EC_POINT_clear_free(EC_POINT *point) { EC_POINT_free(point); }
  589. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) {
  590. if (EC_GROUP_cmp(dest->group, src->group, NULL) != 0) {
  591. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  592. return 0;
  593. }
  594. if (dest == src) {
  595. return 1;
  596. }
  597. return ec_GFp_simple_point_copy(dest, src);
  598. }
  599. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) {
  600. if (a == NULL) {
  601. return NULL;
  602. }
  603. EC_POINT *ret = EC_POINT_new(group);
  604. if (ret == NULL ||
  605. !EC_POINT_copy(ret, a)) {
  606. EC_POINT_free(ret);
  607. return NULL;
  608. }
  609. return ret;
  610. }
  611. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) {
  612. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  613. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  614. return 0;
  615. }
  616. return ec_GFp_simple_point_set_to_infinity(group, point);
  617. }
  618. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
  619. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  620. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  621. return 0;
  622. }
  623. return ec_GFp_simple_is_at_infinity(group, point);
  624. }
  625. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  626. BN_CTX *ctx) {
  627. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  628. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  629. return 0;
  630. }
  631. return ec_GFp_simple_is_on_curve(group, point, ctx);
  632. }
  633. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  634. BN_CTX *ctx) {
  635. if (EC_GROUP_cmp(group, a->group, NULL) != 0 ||
  636. EC_GROUP_cmp(group, b->group, NULL) != 0) {
  637. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  638. return -1;
  639. }
  640. return ec_GFp_simple_cmp(group, a, b, ctx);
  641. }
  642. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
  643. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  644. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  645. return 0;
  646. }
  647. return ec_GFp_simple_make_affine(group, point, ctx);
  648. }
  649. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
  650. BN_CTX *ctx) {
  651. for (size_t i = 0; i < num; i++) {
  652. if (EC_GROUP_cmp(group, points[i]->group, NULL) != 0) {
  653. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  654. return 0;
  655. }
  656. }
  657. return ec_GFp_simple_points_make_affine(group, num, points, ctx);
  658. }
  659. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  660. const EC_POINT *point, BIGNUM *x,
  661. BIGNUM *y, BN_CTX *ctx) {
  662. if (group->meth->point_get_affine_coordinates == 0) {
  663. OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  664. return 0;
  665. }
  666. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  667. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  668. return 0;
  669. }
  670. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  671. }
  672. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
  673. const BIGNUM *x, const BIGNUM *y,
  674. BN_CTX *ctx) {
  675. if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
  676. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  677. return 0;
  678. }
  679. if (!ec_GFp_simple_point_set_affine_coordinates(group, point, x, y, ctx)) {
  680. return 0;
  681. }
  682. if (!EC_POINT_is_on_curve(group, point, ctx)) {
  683. // In the event of an error, defend against the caller not checking the
  684. // return value by setting a known safe value: the base point.
  685. const EC_POINT *generator = EC_GROUP_get0_generator(group);
  686. // The generator can be missing if the caller is in the process of
  687. // constructing an arbitrary group. In this, we give up and hope they're
  688. // checking the return value.
  689. if (generator) {
  690. EC_POINT_copy(point, generator);
  691. }
  692. OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
  693. return 0;
  694. }
  695. return 1;
  696. }
  697. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  698. const EC_POINT *b, BN_CTX *ctx) {
  699. if (EC_GROUP_cmp(group, r->group, NULL) != 0 ||
  700. EC_GROUP_cmp(group, a->group, NULL) != 0 ||
  701. EC_GROUP_cmp(group, b->group, NULL) != 0) {
  702. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  703. return 0;
  704. }
  705. return ec_GFp_simple_add(group, r, a, b, ctx);
  706. }
  707. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  708. BN_CTX *ctx) {
  709. if (EC_GROUP_cmp(group, r->group, NULL) != 0 ||
  710. EC_GROUP_cmp(group, a->group, NULL) != 0) {
  711. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  712. return 0;
  713. }
  714. return ec_GFp_simple_dbl(group, r, a, ctx);
  715. }
  716. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) {
  717. if (EC_GROUP_cmp(group, a->group, NULL) != 0) {
  718. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  719. return 0;
  720. }
  721. return ec_GFp_simple_invert(group, a, ctx);
  722. }
  723. static int arbitrary_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
  724. const BIGNUM *in, BN_CTX *ctx) {
  725. if (ec_bignum_to_scalar(group, out, in)) {
  726. return 1;
  727. }
  728. ERR_clear_error();
  729. // This is an unusual input, so we do not guarantee constant-time processing.
  730. const BIGNUM *order = &group->order;
  731. BN_CTX_start(ctx);
  732. BIGNUM *tmp = BN_CTX_get(ctx);
  733. int ok = tmp != NULL &&
  734. BN_nnmod(tmp, in, order, ctx) &&
  735. ec_bignum_to_scalar_unchecked(group, out, tmp);
  736. BN_CTX_end(ctx);
  737. return ok;
  738. }
  739. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  740. const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx) {
  741. // Previously, this function set |r| to the point at infinity if there was
  742. // nothing to multiply. But, nobody should be calling this function with
  743. // nothing to multiply in the first place.
  744. if ((g_scalar == NULL && p_scalar == NULL) ||
  745. (p == NULL) != (p_scalar == NULL)) {
  746. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  747. return 0;
  748. }
  749. int ret = 0;
  750. EC_SCALAR g_scalar_storage, p_scalar_storage;
  751. EC_SCALAR *g_scalar_arg = NULL, *p_scalar_arg = NULL;
  752. BN_CTX *new_ctx = NULL;
  753. if (ctx == NULL) {
  754. new_ctx = BN_CTX_new();
  755. if (new_ctx == NULL) {
  756. goto err;
  757. }
  758. ctx = new_ctx;
  759. }
  760. if (g_scalar != NULL) {
  761. if (!arbitrary_bignum_to_scalar(group, &g_scalar_storage, g_scalar, ctx)) {
  762. goto err;
  763. }
  764. g_scalar_arg = &g_scalar_storage;
  765. }
  766. if (p_scalar != NULL) {
  767. if (!arbitrary_bignum_to_scalar(group, &p_scalar_storage, p_scalar, ctx)) {
  768. goto err;
  769. }
  770. p_scalar_arg = &p_scalar_storage;
  771. }
  772. ret = ec_point_mul_scalar(group, r, g_scalar_arg, p, p_scalar_arg, ctx);
  773. err:
  774. BN_CTX_free(new_ctx);
  775. OPENSSL_cleanse(&g_scalar_storage, sizeof(g_scalar_storage));
  776. OPENSSL_cleanse(&p_scalar_storage, sizeof(p_scalar_storage));
  777. return ret;
  778. }
  779. int ec_point_mul_scalar_public(const EC_GROUP *group, EC_POINT *r,
  780. const EC_SCALAR *g_scalar, const EC_POINT *p,
  781. const EC_SCALAR *p_scalar, BN_CTX *ctx) {
  782. if ((g_scalar == NULL && p_scalar == NULL) ||
  783. (p == NULL) != (p_scalar == NULL)) {
  784. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  785. return 0;
  786. }
  787. if (EC_GROUP_cmp(group, r->group, NULL) != 0 ||
  788. (p != NULL && EC_GROUP_cmp(group, p->group, NULL) != 0)) {
  789. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  790. return 0;
  791. }
  792. return group->meth->mul_public(group, r, g_scalar, p, p_scalar, ctx);
  793. }
  794. int ec_point_mul_scalar(const EC_GROUP *group, EC_POINT *r,
  795. const EC_SCALAR *g_scalar, const EC_POINT *p,
  796. const EC_SCALAR *p_scalar, BN_CTX *ctx) {
  797. if ((g_scalar == NULL && p_scalar == NULL) ||
  798. (p == NULL) != (p_scalar == NULL)) {
  799. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  800. return 0;
  801. }
  802. if (EC_GROUP_cmp(group, r->group, NULL) != 0 ||
  803. (p != NULL && EC_GROUP_cmp(group, p->group, NULL) != 0)) {
  804. OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
  805. return 0;
  806. }
  807. return group->meth->mul(group, r, g_scalar, p, p_scalar, ctx);
  808. }
  809. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) {}
  810. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) {
  811. return NULL;
  812. }
  813. int EC_METHOD_get_field_type(const EC_METHOD *meth) {
  814. return NID_X9_62_prime_field;
  815. }
  816. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  817. point_conversion_form_t form) {
  818. if (form != POINT_CONVERSION_UNCOMPRESSED) {
  819. abort();
  820. }
  821. }
  822. size_t EC_get_builtin_curves(EC_builtin_curve *out_curves,
  823. size_t max_num_curves) {
  824. const struct built_in_curves *const curves = OPENSSL_built_in_curves();
  825. for (size_t i = 0; i < max_num_curves && i < OPENSSL_NUM_BUILT_IN_CURVES;
  826. i++) {
  827. out_curves[i].comment = curves->curves[i].comment;
  828. out_curves[i].nid = curves->curves[i].nid;
  829. }
  830. return OPENSSL_NUM_BUILT_IN_CURVES;
  831. }
  832. int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
  833. const BIGNUM *in) {
  834. if (!ec_bignum_to_scalar_unchecked(group, out, in)) {
  835. return 0;
  836. }
  837. if (!bn_less_than_words(out->words, group->order.d, group->order.width)) {
  838. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
  839. return 0;
  840. }
  841. return 1;
  842. }
  843. int ec_bignum_to_scalar_unchecked(const EC_GROUP *group, EC_SCALAR *out,
  844. const BIGNUM *in) {
  845. if (!bn_copy_words(out->words, group->order.width, in)) {
  846. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
  847. return 0;
  848. }
  849. return 1;
  850. }
  851. int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
  852. const uint8_t additional_data[32]) {
  853. return bn_rand_range_words(out->words, 1, group->order.d, group->order.width,
  854. additional_data);
  855. }