eck_prn.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #ifndef OPENSSL_NO_FP_API
  68. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
  69. {
  70. BIO *b;
  71. int ret;
  72. if ((b=BIO_new(BIO_s_file())) == NULL)
  73. {
  74. ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
  75. return(0);
  76. }
  77. BIO_set_fp(b, fp, BIO_NOCLOSE);
  78. ret = ECPKParameters_print(b, x, off);
  79. BIO_free(b);
  80. return(ret);
  81. }
  82. int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
  83. {
  84. BIO *b;
  85. int ret;
  86. if ((b=BIO_new(BIO_s_file())) == NULL)
  87. {
  88. ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
  89. return(0);
  90. }
  91. BIO_set_fp(b, fp, BIO_NOCLOSE);
  92. ret = EC_KEY_print(b, x, off);
  93. BIO_free(b);
  94. return(ret);
  95. }
  96. int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
  97. {
  98. BIO *b;
  99. int ret;
  100. if ((b=BIO_new(BIO_s_file())) == NULL)
  101. {
  102. ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
  103. return(0);
  104. }
  105. BIO_set_fp(b, fp, BIO_NOCLOSE);
  106. ret = ECParameters_print(b, x);
  107. BIO_free(b);
  108. return(ret);
  109. }
  110. #endif
  111. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  112. {
  113. EVP_PKEY *pk;
  114. int ret;
  115. pk = EVP_PKEY_new();
  116. if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
  117. return 0;
  118. ret = EVP_PKEY_print_private(bp, pk, off, NULL);
  119. EVP_PKEY_free(pk);
  120. return ret;
  121. }
  122. int ECParameters_print(BIO *bp, const EC_KEY *x)
  123. {
  124. EVP_PKEY *pk;
  125. int ret;
  126. pk = EVP_PKEY_new();
  127. if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
  128. return 0;
  129. ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
  130. EVP_PKEY_free(pk);
  131. return ret;
  132. }
  133. static int print_bin(BIO *fp, const char *str, const unsigned char *num,
  134. size_t len, int off);
  135. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
  136. {
  137. unsigned char *buffer=NULL;
  138. size_t buf_len=0, i;
  139. int ret=0, reason=ERR_R_BIO_LIB;
  140. BN_CTX *ctx=NULL;
  141. const EC_POINT *point=NULL;
  142. BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
  143. *order=NULL, *cofactor=NULL;
  144. const unsigned char *seed;
  145. size_t seed_len=0;
  146. static const char *gen_compressed = "Generator (compressed):";
  147. static const char *gen_uncompressed = "Generator (uncompressed):";
  148. static const char *gen_hybrid = "Generator (hybrid):";
  149. if (!x)
  150. {
  151. reason = ERR_R_PASSED_NULL_PARAMETER;
  152. goto err;
  153. }
  154. ctx = BN_CTX_new();
  155. if (ctx == NULL)
  156. {
  157. reason = ERR_R_MALLOC_FAILURE;
  158. goto err;
  159. }
  160. if (EC_GROUP_get_asn1_flag(x))
  161. {
  162. /* the curve parameter are given by an asn1 OID */
  163. int nid;
  164. if (!BIO_indent(bp, off, 128))
  165. goto err;
  166. nid = EC_GROUP_get_curve_name(x);
  167. if (nid == 0)
  168. goto err;
  169. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  170. goto err;
  171. if (BIO_printf(bp, "\n") <= 0)
  172. goto err;
  173. }
  174. else
  175. {
  176. /* explicit parameters */
  177. int is_char_two = 0;
  178. point_conversion_form_t form;
  179. int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
  180. if (tmp_nid == NID_X9_62_characteristic_two_field)
  181. is_char_two = 1;
  182. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  183. (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
  184. (cofactor = BN_new()) == NULL)
  185. {
  186. reason = ERR_R_MALLOC_FAILURE;
  187. goto err;
  188. }
  189. if (is_char_two)
  190. {
  191. if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
  192. {
  193. reason = ERR_R_EC_LIB;
  194. goto err;
  195. }
  196. }
  197. else /* prime field */
  198. {
  199. if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
  200. {
  201. reason = ERR_R_EC_LIB;
  202. goto err;
  203. }
  204. }
  205. if ((point = EC_GROUP_get0_generator(x)) == NULL)
  206. {
  207. reason = ERR_R_EC_LIB;
  208. goto err;
  209. }
  210. if (!EC_GROUP_get_order(x, order, NULL) ||
  211. !EC_GROUP_get_cofactor(x, cofactor, NULL))
  212. {
  213. reason = ERR_R_EC_LIB;
  214. goto err;
  215. }
  216. form = EC_GROUP_get_point_conversion_form(x);
  217. if ((gen = EC_POINT_point2bn(x, point,
  218. form, NULL, ctx)) == NULL)
  219. {
  220. reason = ERR_R_EC_LIB;
  221. goto err;
  222. }
  223. buf_len = (size_t)BN_num_bytes(p);
  224. if (buf_len < (i = (size_t)BN_num_bytes(a)))
  225. buf_len = i;
  226. if (buf_len < (i = (size_t)BN_num_bytes(b)))
  227. buf_len = i;
  228. if (buf_len < (i = (size_t)BN_num_bytes(gen)))
  229. buf_len = i;
  230. if (buf_len < (i = (size_t)BN_num_bytes(order)))
  231. buf_len = i;
  232. if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
  233. buf_len = i;
  234. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  235. seed_len = EC_GROUP_get_seed_len(x);
  236. buf_len += 10;
  237. if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
  238. {
  239. reason = ERR_R_MALLOC_FAILURE;
  240. goto err;
  241. }
  242. if (!BIO_indent(bp, off, 128))
  243. goto err;
  244. /* print the 'short name' of the field type */
  245. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  246. <= 0)
  247. goto err;
  248. if (is_char_two)
  249. {
  250. /* print the 'short name' of the base type OID */
  251. int basis_type = EC_GROUP_get_basis_type(x);
  252. if (basis_type == 0)
  253. goto err;
  254. if (!BIO_indent(bp, off, 128))
  255. goto err;
  256. if (BIO_printf(bp, "Basis Type: %s\n",
  257. OBJ_nid2sn(basis_type)) <= 0)
  258. goto err;
  259. /* print the polynomial */
  260. if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
  261. off))
  262. goto err;
  263. }
  264. else
  265. {
  266. if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off))
  267. goto err;
  268. }
  269. if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
  270. goto err;
  271. if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
  272. goto err;
  273. if (form == POINT_CONVERSION_COMPRESSED)
  274. {
  275. if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
  276. buffer, off))
  277. goto err;
  278. }
  279. else if (form == POINT_CONVERSION_UNCOMPRESSED)
  280. {
  281. if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
  282. buffer, off))
  283. goto err;
  284. }
  285. else /* form == POINT_CONVERSION_HYBRID */
  286. {
  287. if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
  288. buffer, off))
  289. goto err;
  290. }
  291. if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
  292. buffer, off)) goto err;
  293. if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
  294. buffer, off)) goto err;
  295. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  296. goto err;
  297. }
  298. ret=1;
  299. err:
  300. if (!ret)
  301. ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
  302. if (p)
  303. BN_free(p);
  304. if (a)
  305. BN_free(a);
  306. if (b)
  307. BN_free(b);
  308. if (gen)
  309. BN_free(gen);
  310. if (order)
  311. BN_free(order);
  312. if (cofactor)
  313. BN_free(cofactor);
  314. if (ctx)
  315. BN_CTX_free(ctx);
  316. if (buffer != NULL)
  317. OPENSSL_free(buffer);
  318. return(ret);
  319. }
  320. static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
  321. size_t len, int off)
  322. {
  323. size_t i;
  324. char str[128];
  325. if (buf == NULL)
  326. return 1;
  327. if (off)
  328. {
  329. if (off > 128)
  330. off=128;
  331. memset(str,' ',off);
  332. if (BIO_write(fp, str, off) <= 0)
  333. return 0;
  334. }
  335. if (BIO_printf(fp,"%s", name) <= 0)
  336. return 0;
  337. for (i=0; i<len; i++)
  338. {
  339. if ((i%15) == 0)
  340. {
  341. str[0]='\n';
  342. memset(&(str[1]),' ',off+4);
  343. if (BIO_write(fp, str, off+1+4) <= 0)
  344. return 0;
  345. }
  346. if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
  347. return 0;
  348. }
  349. if (BIO_write(fp,"\n",1) <= 0)
  350. return 0;
  351. return 1;
  352. }