bn_print.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <openssl/bio.h>
  11. #include "bn_local.h"
  12. static const char Hex[] = "0123456789ABCDEF";
  13. #ifndef OPENSSL_NO_STDIO
  14. int BN_print_fp(FILE *fp, const BIGNUM *a)
  15. {
  16. BIO *b;
  17. int ret;
  18. if ((b = BIO_new(BIO_s_file())) == NULL)
  19. return 0;
  20. BIO_set_fp(b, fp, BIO_NOCLOSE);
  21. ret = BN_print(b, a);
  22. BIO_free(b);
  23. return ret;
  24. }
  25. #endif
  26. int BN_print(BIO *bp, const BIGNUM *a)
  27. {
  28. int i, j, v, z = 0;
  29. int ret = 0;
  30. if ((a->neg) && BIO_write(bp, "-", 1) != 1)
  31. goto end;
  32. if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)
  33. goto end;
  34. for (i = a->top - 1; i >= 0; i--) {
  35. for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
  36. /* strip leading zeros */
  37. v = (int)((a->d[i] >> j) & 0x0f);
  38. if (z || v != 0) {
  39. if (BIO_write(bp, &Hex[v], 1) != 1)
  40. goto end;
  41. z = 1;
  42. }
  43. }
  44. }
  45. ret = 1;
  46. end:
  47. return ret;
  48. }
  49. char *BN_options(void)
  50. {
  51. static int init = 0;
  52. static char data[16];
  53. if (!init) {
  54. init++;
  55. #ifdef BN_LLONG
  56. BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
  57. sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8);
  58. #else
  59. BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
  60. sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8);
  61. #endif
  62. }
  63. return data;
  64. }