eck_prn.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* crypto/ec/eck_prn.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Portions originally developed by SUN MICROSYSTEMS, INC., and
  61. * contributed to the OpenSSL project.
  62. */
  63. #include <stdio.h>
  64. #include "cryptlib.h"
  65. #include <openssl/evp.h>
  66. #include <openssl/ec.h>
  67. #include <openssl/bn.h>
  68. #ifndef OPENSSL_NO_FP_API
  69. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
  70. {
  71. BIO *b;
  72. int ret;
  73. if ((b=BIO_new(BIO_s_file())) == NULL)
  74. {
  75. ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
  76. return(0);
  77. }
  78. BIO_set_fp(b, fp, BIO_NOCLOSE);
  79. ret = ECPKParameters_print(b, x, off);
  80. BIO_free(b);
  81. return(ret);
  82. }
  83. int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
  84. {
  85. BIO *b;
  86. int ret;
  87. if ((b=BIO_new(BIO_s_file())) == NULL)
  88. {
  89. ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
  90. return(0);
  91. }
  92. BIO_set_fp(b, fp, BIO_NOCLOSE);
  93. ret = EC_KEY_print(b, x, off);
  94. BIO_free(b);
  95. return(ret);
  96. }
  97. int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
  98. {
  99. BIO *b;
  100. int ret;
  101. if ((b=BIO_new(BIO_s_file())) == NULL)
  102. {
  103. ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
  104. return(0);
  105. }
  106. BIO_set_fp(b, fp, BIO_NOCLOSE);
  107. ret = ECParameters_print(b, x);
  108. BIO_free(b);
  109. return(ret);
  110. }
  111. #endif
  112. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  113. {
  114. EVP_PKEY *pk;
  115. int ret;
  116. pk = EVP_PKEY_new();
  117. if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
  118. return 0;
  119. ret = EVP_PKEY_print_private(bp, pk, off, NULL);
  120. EVP_PKEY_free(pk);
  121. return ret;
  122. }
  123. int ECParameters_print(BIO *bp, const EC_KEY *x)
  124. {
  125. EVP_PKEY *pk;
  126. int ret;
  127. pk = EVP_PKEY_new();
  128. if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
  129. return 0;
  130. ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
  131. EVP_PKEY_free(pk);
  132. return ret;
  133. }
  134. static int print_bin(BIO *fp, const char *str, const unsigned char *num,
  135. size_t len, int off);
  136. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
  137. {
  138. unsigned char *buffer=NULL;
  139. size_t buf_len=0, i;
  140. int ret=0, reason=ERR_R_BIO_LIB;
  141. BN_CTX *ctx=NULL;
  142. const EC_POINT *point=NULL;
  143. BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
  144. *order=NULL, *cofactor=NULL;
  145. const unsigned char *seed;
  146. size_t seed_len=0;
  147. static const char *gen_compressed = "Generator (compressed):";
  148. static const char *gen_uncompressed = "Generator (uncompressed):";
  149. static const char *gen_hybrid = "Generator (hybrid):";
  150. if (!x)
  151. {
  152. reason = ERR_R_PASSED_NULL_PARAMETER;
  153. goto err;
  154. }
  155. ctx = BN_CTX_new();
  156. if (ctx == NULL)
  157. {
  158. reason = ERR_R_MALLOC_FAILURE;
  159. goto err;
  160. }
  161. if (EC_GROUP_get_asn1_flag(x))
  162. {
  163. /* the curve parameter are given by an asn1 OID */
  164. int nid;
  165. if (!BIO_indent(bp, off, 128))
  166. goto err;
  167. nid = EC_GROUP_get_curve_name(x);
  168. if (nid == 0)
  169. goto err;
  170. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  171. goto err;
  172. if (BIO_printf(bp, "\n") <= 0)
  173. goto err;
  174. }
  175. else
  176. {
  177. /* explicit parameters */
  178. int is_char_two = 0;
  179. point_conversion_form_t form;
  180. int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
  181. if (tmp_nid == NID_X9_62_characteristic_two_field)
  182. is_char_two = 1;
  183. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  184. (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
  185. (cofactor = BN_new()) == NULL)
  186. {
  187. reason = ERR_R_MALLOC_FAILURE;
  188. goto err;
  189. }
  190. #ifndef OPENSSL_NO_EC2M
  191. if (is_char_two)
  192. {
  193. if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
  194. {
  195. reason = ERR_R_EC_LIB;
  196. goto err;
  197. }
  198. }
  199. else /* prime field */
  200. #endif
  201. {
  202. if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
  203. {
  204. reason = ERR_R_EC_LIB;
  205. goto err;
  206. }
  207. }
  208. if ((point = EC_GROUP_get0_generator(x)) == NULL)
  209. {
  210. reason = ERR_R_EC_LIB;
  211. goto err;
  212. }
  213. if (!EC_GROUP_get_order(x, order, NULL) ||
  214. !EC_GROUP_get_cofactor(x, cofactor, NULL))
  215. {
  216. reason = ERR_R_EC_LIB;
  217. goto err;
  218. }
  219. form = EC_GROUP_get_point_conversion_form(x);
  220. if ((gen = EC_POINT_point2bn(x, point,
  221. form, NULL, ctx)) == NULL)
  222. {
  223. reason = ERR_R_EC_LIB;
  224. goto err;
  225. }
  226. buf_len = (size_t)BN_num_bytes(p);
  227. if (buf_len < (i = (size_t)BN_num_bytes(a)))
  228. buf_len = i;
  229. if (buf_len < (i = (size_t)BN_num_bytes(b)))
  230. buf_len = i;
  231. if (buf_len < (i = (size_t)BN_num_bytes(gen)))
  232. buf_len = i;
  233. if (buf_len < (i = (size_t)BN_num_bytes(order)))
  234. buf_len = i;
  235. if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
  236. buf_len = i;
  237. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  238. seed_len = EC_GROUP_get_seed_len(x);
  239. buf_len += 10;
  240. if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
  241. {
  242. reason = ERR_R_MALLOC_FAILURE;
  243. goto err;
  244. }
  245. if (!BIO_indent(bp, off, 128))
  246. goto err;
  247. /* print the 'short name' of the field type */
  248. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  249. <= 0)
  250. goto err;
  251. if (is_char_two)
  252. {
  253. /* print the 'short name' of the base type OID */
  254. int basis_type = EC_GROUP_get_basis_type(x);
  255. if (basis_type == 0)
  256. goto err;
  257. if (!BIO_indent(bp, off, 128))
  258. goto err;
  259. if (BIO_printf(bp, "Basis Type: %s\n",
  260. OBJ_nid2sn(basis_type)) <= 0)
  261. goto err;
  262. /* print the polynomial */
  263. if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
  264. off))
  265. goto err;
  266. }
  267. else
  268. {
  269. if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off))
  270. goto err;
  271. }
  272. if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
  273. goto err;
  274. if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
  275. goto err;
  276. if (form == POINT_CONVERSION_COMPRESSED)
  277. {
  278. if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
  279. buffer, off))
  280. goto err;
  281. }
  282. else if (form == POINT_CONVERSION_UNCOMPRESSED)
  283. {
  284. if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
  285. buffer, off))
  286. goto err;
  287. }
  288. else /* form == POINT_CONVERSION_HYBRID */
  289. {
  290. if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
  291. buffer, off))
  292. goto err;
  293. }
  294. if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
  295. buffer, off)) goto err;
  296. if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
  297. buffer, off)) goto err;
  298. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  299. goto err;
  300. }
  301. ret=1;
  302. err:
  303. if (!ret)
  304. ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
  305. if (p)
  306. BN_free(p);
  307. if (a)
  308. BN_free(a);
  309. if (b)
  310. BN_free(b);
  311. if (gen)
  312. BN_free(gen);
  313. if (order)
  314. BN_free(order);
  315. if (cofactor)
  316. BN_free(cofactor);
  317. if (ctx)
  318. BN_CTX_free(ctx);
  319. if (buffer != NULL)
  320. OPENSSL_free(buffer);
  321. return(ret);
  322. }
  323. static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
  324. size_t len, int off)
  325. {
  326. size_t i;
  327. char str[128];
  328. if (buf == NULL)
  329. return 1;
  330. if (off)
  331. {
  332. if (off > 128)
  333. off=128;
  334. memset(str,' ',off);
  335. if (BIO_write(fp, str, off) <= 0)
  336. return 0;
  337. }
  338. if (BIO_printf(fp,"%s", name) <= 0)
  339. return 0;
  340. for (i=0; i<len; i++)
  341. {
  342. if ((i%15) == 0)
  343. {
  344. str[0]='\n';
  345. memset(&(str[1]),' ',off+4);
  346. if (BIO_write(fp, str, off+1+4) <= 0)
  347. return 0;
  348. }
  349. if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
  350. return 0;
  351. }
  352. if (BIO_write(fp,"\n",1) <= 0)
  353. return 0;
  354. return 1;
  355. }