]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_print.c
Make BIGNUM code available from within the FIPS module
[thirdparty/openssl.git] / crypto / bn / bn_print.c
CommitLineData
4f22f405 1/*
b4df712a 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
367ace68 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
636b087e 11#include <openssl/bio.h>
d02b48c6
RE
12#include "bn_lcl.h"
13
0f113f3e 14static const char Hex[] = "0123456789ABCDEF";
d02b48c6 15
636b087e 16#ifndef OPENSSL_NO_STDIO
e93f9a32 17int BN_print_fp(FILE *fp, const BIGNUM *a)
0f113f3e
MC
18{
19 BIO *b;
20 int ret;
21
22 if ((b = BIO_new(BIO_s_file())) == NULL)
b4df712a 23 return 0;
0f113f3e
MC
24 BIO_set_fp(b, fp, BIO_NOCLOSE);
25 ret = BN_print(b, a);
26 BIO_free(b);
b4df712a 27 return ret;
0f113f3e 28}
636b087e 29#endif
d02b48c6 30
8d8a8041 31int BN_print(BIO *bp, const BIGNUM *a)
0f113f3e
MC
32{
33 int i, j, v, z = 0;
34 int ret = 0;
35
27c6d63d 36 if ((a->neg) && BIO_write(bp, "-", 1) != 1)
0f113f3e 37 goto end;
27c6d63d 38 if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)
0f113f3e
MC
39 goto end;
40 for (i = a->top - 1; i >= 0; i--) {
41 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
42 /* strip leading zeros */
c9b820aa 43 v = (int)((a->d[i] >> j) & 0x0f);
27c6d63d
P
44 if (z || v != 0) {
45 if (BIO_write(bp, &Hex[v], 1) != 1)
0f113f3e
MC
46 goto end;
47 z = 1;
48 }
49 }
50 }
51 ret = 1;
52 end:
b4df712a 53 return ret;
0f113f3e 54}
13a55192
DSH
55
56char *BN_options(void)
0f113f3e
MC
57{
58 static int init = 0;
59 static char data[16];
13a55192 60
0f113f3e
MC
61 if (!init) {
62 init++;
13a55192 63#ifdef BN_LLONG
86ba26c8
P
64 BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
65 sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8);
13a55192 66#else
86ba26c8
P
67 BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
68 sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8);
13a55192 69#endif
0f113f3e 70 }
b4df712a 71 return data;
0f113f3e 72}