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