2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2017-07-07 05:29:55 +08:00
|
|
|
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:22:12 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-05-30 00:31:22 +08:00
|
|
|
#include <openssl/bio.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "bn_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static const char Hex[] = "0123456789ABCDEF";
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-05-30 00:31:22 +08:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2000-01-27 09:50:42 +08:00
|
|
|
int BN_print_fp(FILE *fp, const BIGNUM *a)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL)
|
2017-07-07 05:29:55 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = BN_print(b, a);
|
|
|
|
BIO_free(b);
|
2017-07-07 05:29:55 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2019-05-30 00:31:22 +08:00
|
|
|
#endif
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-06-05 20:16:33 +08:00
|
|
|
int BN_print(BIO *bp, const BIGNUM *a)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i, j, v, z = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-08-21 05:23:36 +08:00
|
|
|
if ((a->neg) && BIO_write(bp, "-", 1) != 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto end;
|
2017-08-21 05:23:36 +08:00
|
|
|
if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto end;
|
|
|
|
for (i = a->top - 1; i >= 0; i--) {
|
|
|
|
for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
|
|
|
|
/* strip leading zeros */
|
2017-08-24 00:28:05 +08:00
|
|
|
v = (int)((a->d[i] >> j) & 0x0f);
|
2017-08-21 05:23:36 +08:00
|
|
|
if (z || v != 0) {
|
|
|
|
if (BIO_write(bp, &Hex[v], 1) != 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto end;
|
|
|
|
z = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
end:
|
2017-07-07 05:29:55 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2011-01-26 01:10:30 +08:00
|
|
|
|
|
|
|
char *BN_options(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
static char data[16];
|
2011-01-26 01:10:30 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!init) {
|
|
|
|
init++;
|
2011-01-26 01:10:30 +08:00
|
|
|
#ifdef BN_LLONG
|
2017-07-07 08:17:59 +08:00
|
|
|
BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
|
|
|
|
sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8);
|
2011-01-26 01:10:30 +08:00
|
|
|
#else
|
2017-07-07 08:17:59 +08:00
|
|
|
BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
|
|
|
|
sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8);
|
2011-01-26 01:10:30 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-07-07 05:29:55 +08:00
|
|
|
return data;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|