eck_prn.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/ec.h>
  14. #include <openssl/bn.h>
  15. #ifndef OPENSSL_NO_STDIO
  16. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
  17. {
  18. BIO *b;
  19. int ret;
  20. if ((b = BIO_new(BIO_s_file())) == NULL) {
  21. ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
  22. return 0;
  23. }
  24. BIO_set_fp(b, fp, BIO_NOCLOSE);
  25. ret = ECPKParameters_print(b, x, off);
  26. BIO_free(b);
  27. return ret;
  28. }
  29. int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
  30. {
  31. BIO *b;
  32. int ret;
  33. if ((b = BIO_new(BIO_s_file())) == NULL) {
  34. ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
  35. return 0;
  36. }
  37. BIO_set_fp(b, fp, BIO_NOCLOSE);
  38. ret = EC_KEY_print(b, x, off);
  39. BIO_free(b);
  40. return ret;
  41. }
  42. int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
  43. {
  44. BIO *b;
  45. int ret;
  46. if ((b = BIO_new(BIO_s_file())) == NULL) {
  47. ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
  48. return 0;
  49. }
  50. BIO_set_fp(b, fp, BIO_NOCLOSE);
  51. ret = ECParameters_print(b, x);
  52. BIO_free(b);
  53. return ret;
  54. }
  55. #endif
  56. static int print_bin(BIO *fp, const char *str, const unsigned char *num,
  57. size_t len, int off);
  58. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
  59. {
  60. int ret = 0, reason = ERR_R_BIO_LIB;
  61. BN_CTX *ctx = NULL;
  62. const EC_POINT *point = NULL;
  63. BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
  64. const BIGNUM *order = NULL, *cofactor = NULL;
  65. const unsigned char *seed;
  66. size_t seed_len = 0;
  67. static const char *gen_compressed = "Generator (compressed):";
  68. static const char *gen_uncompressed = "Generator (uncompressed):";
  69. static const char *gen_hybrid = "Generator (hybrid):";
  70. if (!x) {
  71. reason = ERR_R_PASSED_NULL_PARAMETER;
  72. goto err;
  73. }
  74. ctx = BN_CTX_new();
  75. if (ctx == NULL) {
  76. reason = ERR_R_MALLOC_FAILURE;
  77. goto err;
  78. }
  79. if (EC_GROUP_get_asn1_flag(x)) {
  80. /* the curve parameter are given by an asn1 OID */
  81. int nid;
  82. const char *nname;
  83. if (!BIO_indent(bp, off, 128))
  84. goto err;
  85. nid = EC_GROUP_get_curve_name(x);
  86. if (nid == 0)
  87. goto err;
  88. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  89. goto err;
  90. if (BIO_printf(bp, "\n") <= 0)
  91. goto err;
  92. nname = EC_curve_nid2nist(nid);
  93. if (nname) {
  94. if (!BIO_indent(bp, off, 128))
  95. goto err;
  96. if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
  97. goto err;
  98. }
  99. } else {
  100. /* explicit parameters */
  101. int is_char_two = 0;
  102. point_conversion_form_t form;
  103. int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
  104. if (tmp_nid == NID_X9_62_characteristic_two_field)
  105. is_char_two = 1;
  106. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  107. (b = BN_new()) == NULL) {
  108. reason = ERR_R_MALLOC_FAILURE;
  109. goto err;
  110. }
  111. if (!EC_GROUP_get_curve(x, p, a, b, ctx)) {
  112. reason = ERR_R_EC_LIB;
  113. goto err;
  114. }
  115. if ((point = EC_GROUP_get0_generator(x)) == NULL) {
  116. reason = ERR_R_EC_LIB;
  117. goto err;
  118. }
  119. order = EC_GROUP_get0_order(x);
  120. cofactor = EC_GROUP_get0_cofactor(x);
  121. if (order == NULL) {
  122. reason = ERR_R_EC_LIB;
  123. goto err;
  124. }
  125. form = EC_GROUP_get_point_conversion_form(x);
  126. if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
  127. reason = ERR_R_EC_LIB;
  128. goto err;
  129. }
  130. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  131. seed_len = EC_GROUP_get_seed_len(x);
  132. if (!BIO_indent(bp, off, 128))
  133. goto err;
  134. /* print the 'short name' of the field type */
  135. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  136. <= 0)
  137. goto err;
  138. if (is_char_two) {
  139. /* print the 'short name' of the base type OID */
  140. int basis_type = EC_GROUP_get_basis_type(x);
  141. if (basis_type == 0)
  142. goto err;
  143. if (!BIO_indent(bp, off, 128))
  144. goto err;
  145. if (BIO_printf(bp, "Basis Type: %s\n",
  146. OBJ_nid2sn(basis_type)) <= 0)
  147. goto err;
  148. /* print the polynomial */
  149. if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
  150. off))
  151. goto err;
  152. } else {
  153. if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
  154. goto err;
  155. }
  156. if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
  157. goto err;
  158. if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
  159. goto err;
  160. if (form == POINT_CONVERSION_COMPRESSED) {
  161. if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
  162. NULL, off))
  163. goto err;
  164. } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
  165. if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
  166. NULL, off))
  167. goto err;
  168. } else { /* form == POINT_CONVERSION_HYBRID */
  169. if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
  170. NULL, off))
  171. goto err;
  172. }
  173. if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
  174. NULL, off))
  175. goto err;
  176. if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
  177. NULL, off))
  178. goto err;
  179. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  180. goto err;
  181. }
  182. ret = 1;
  183. err:
  184. if (!ret)
  185. ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
  186. BN_free(p);
  187. BN_free(a);
  188. BN_free(b);
  189. BN_free(gen);
  190. BN_CTX_free(ctx);
  191. return ret;
  192. }
  193. static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
  194. size_t len, int off)
  195. {
  196. size_t i;
  197. char str[128 + 1 + 4];
  198. if (buf == NULL)
  199. return 1;
  200. if (off > 0) {
  201. if (off > 128)
  202. off = 128;
  203. memset(str, ' ', off);
  204. if (BIO_write(fp, str, off) <= 0)
  205. return 0;
  206. } else {
  207. off = 0;
  208. }
  209. if (BIO_printf(fp, "%s", name) <= 0)
  210. return 0;
  211. for (i = 0; i < len; i++) {
  212. if ((i % 15) == 0) {
  213. str[0] = '\n';
  214. memset(&(str[1]), ' ', off + 4);
  215. if (BIO_write(fp, str, off + 1 + 4) <= 0)
  216. return 0;
  217. }
  218. if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
  219. 0)
  220. return 0;
  221. }
  222. if (BIO_write(fp, "\n", 1) <= 0)
  223. return 0;
  224. return 1;
  225. }