eck_prn.c 11 KB

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