eck_prn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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. 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. if (!BIO_indent(bp, off, 128))
  160. goto err;
  161. nid = EC_GROUP_get_curve_name(x);
  162. if (nid == 0)
  163. goto err;
  164. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  165. goto err;
  166. if (BIO_printf(bp, "\n") <= 0)
  167. goto err;
  168. } else {
  169. /* explicit parameters */
  170. int is_char_two = 0;
  171. point_conversion_form_t form;
  172. int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
  173. if (tmp_nid == NID_X9_62_characteristic_two_field)
  174. is_char_two = 1;
  175. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  176. (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
  177. (cofactor = BN_new()) == NULL) {
  178. reason = ERR_R_MALLOC_FAILURE;
  179. goto err;
  180. }
  181. #ifndef OPENSSL_NO_EC2M
  182. if (is_char_two) {
  183. if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
  184. reason = ERR_R_EC_LIB;
  185. goto err;
  186. }
  187. } else /* prime field */
  188. #endif
  189. {
  190. if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
  191. reason = ERR_R_EC_LIB;
  192. goto err;
  193. }
  194. }
  195. if ((point = EC_GROUP_get0_generator(x)) == NULL) {
  196. reason = ERR_R_EC_LIB;
  197. goto err;
  198. }
  199. if (!EC_GROUP_get_order(x, order, NULL) ||
  200. !EC_GROUP_get_cofactor(x, cofactor, NULL)) {
  201. reason = ERR_R_EC_LIB;
  202. goto err;
  203. }
  204. form = EC_GROUP_get_point_conversion_form(x);
  205. if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
  206. reason = ERR_R_EC_LIB;
  207. goto err;
  208. }
  209. buf_len = (size_t)BN_num_bytes(p);
  210. if (buf_len < (i = (size_t)BN_num_bytes(a)))
  211. buf_len = i;
  212. if (buf_len < (i = (size_t)BN_num_bytes(b)))
  213. buf_len = i;
  214. if (buf_len < (i = (size_t)BN_num_bytes(gen)))
  215. buf_len = i;
  216. if (buf_len < (i = (size_t)BN_num_bytes(order)))
  217. buf_len = i;
  218. if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
  219. buf_len = i;
  220. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  221. seed_len = EC_GROUP_get_seed_len(x);
  222. buf_len += 10;
  223. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  224. reason = ERR_R_MALLOC_FAILURE;
  225. goto err;
  226. }
  227. if (!BIO_indent(bp, off, 128))
  228. goto err;
  229. /* print the 'short name' of the field type */
  230. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  231. <= 0)
  232. goto err;
  233. if (is_char_two) {
  234. /* print the 'short name' of the base type OID */
  235. int basis_type = EC_GROUP_get_basis_type(x);
  236. if (basis_type == 0)
  237. goto err;
  238. if (!BIO_indent(bp, off, 128))
  239. goto err;
  240. if (BIO_printf(bp, "Basis Type: %s\n",
  241. OBJ_nid2sn(basis_type)) <= 0)
  242. goto err;
  243. /* print the polynomial */
  244. if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
  245. off))
  246. goto err;
  247. } else {
  248. if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off))
  249. goto err;
  250. }
  251. if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
  252. goto err;
  253. if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
  254. goto err;
  255. if (form == POINT_CONVERSION_COMPRESSED) {
  256. if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
  257. buffer, off))
  258. goto err;
  259. } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
  260. if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
  261. buffer, off))
  262. goto err;
  263. } else { /* form == POINT_CONVERSION_HYBRID */
  264. if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
  265. buffer, off))
  266. goto err;
  267. }
  268. if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
  269. buffer, off))
  270. goto err;
  271. if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
  272. buffer, off))
  273. goto err;
  274. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  275. goto err;
  276. }
  277. ret = 1;
  278. err:
  279. if (!ret)
  280. ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
  281. if (p)
  282. BN_free(p);
  283. if (a)
  284. BN_free(a);
  285. if (b)
  286. BN_free(b);
  287. if (gen)
  288. BN_free(gen);
  289. if (order)
  290. BN_free(order);
  291. if (cofactor)
  292. BN_free(cofactor);
  293. if (ctx)
  294. BN_CTX_free(ctx);
  295. if (buffer != NULL)
  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. }