1d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * All rights reserved.
3d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
4d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This package is an SSL implementation written
5d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * by Eric Young (eay@cryptsoft.com).
6d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The implementation was written so as to conform with Netscapes SSL.
7d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
8d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This library is free for commercial and non-commercial use as long as
9d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the following conditions are aheared to.  The following conditions
10d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * apply to all code found in this distribution, be it the RC4, RSA,
11d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * included with this distribution is covered by the same copyright terms
13d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
15d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Copyright remains Eric Young's, and as such any Copyright notices in
16d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the code are not to be removed.
17d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If this package is used in a product, Eric Young should be given attribution
18d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * as the author of the parts of the library used.
19d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This can be in the form of a textual message at program startup or
20d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * in documentation (online or textual) provided with the package.
21d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
22d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Redistribution and use in source and binary forms, with or without
23d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * modification, are permitted provided that the following conditions
24d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * are met:
25d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 1. Redistributions of source code must retain the copyright
26d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer.
27d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 2. Redistributions in binary form must reproduce the above copyright
28d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer in the
29d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    documentation and/or other materials provided with the distribution.
30d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 3. All advertising materials mentioning features or use of this software
31d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    must display the following acknowledgement:
32d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes cryptographic software written by
33d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *     Eric Young (eay@cryptsoft.com)"
34d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    The word 'cryptographic' can be left out if the rouines from the library
35d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    being used are not cryptographic related :-).
36d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 4. If you include any Windows specific code (or a derivative thereof) from
37d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    the apps directory (application code) you must include an acknowledgement:
38d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
40d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SUCH DAMAGE.
51d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
52d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The licence and distribution terms for any publically available version or
53d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * derivative of this code cannot be changed.  i.e. this code cannot simply be
54d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * copied and put under another distribution licence
55d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * [including the GNU Public Licence.]
56d9e397b599b13d642138480a28c14db7a136bf0Adam Langley */
57d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* ====================================================================
58d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
59d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
60d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Redistribution and use in source and binary forms, with or without
61d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * modification, are permitted provided that the following conditions
62d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * are met:
63d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
64d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 1. Redistributions of source code must retain the above copyright
65d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer.
66d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
67d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 2. Redistributions in binary form must reproduce the above copyright
68d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer in
69d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    the documentation and/or other materials provided with the
70d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    distribution.
71d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
72d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 3. All advertising materials mentioning features or use of this
73d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    software must display the following acknowledgment:
74d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes software developed by the OpenSSL Project
75d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
77d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    endorse or promote products derived from this software without
79d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    prior written permission. For written permission, please contact
80d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    openssl-core@openssl.org.
81d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
82d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 5. Products derived from this software may not be called "OpenSSL"
83d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    nor may "OpenSSL" appear in their names without prior written
84d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    permission of the OpenSSL Project.
85d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
86d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 6. Redistributions of any form whatsoever must retain the following
87d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    acknowledgment:
88d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes software developed by the OpenSSL Project
89d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
91d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OF THE POSSIBILITY OF SUCH DAMAGE.
103d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ====================================================================
104d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
105d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This product includes cryptographic software written by Eric Young
106d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * (eay@cryptsoft.com).  This product includes software written by Tim
107d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Hudson (tjh@cryptsoft.com).
108d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
109d9e397b599b13d642138480a28c14db7a136bf0Adam Langley */
110d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* ====================================================================
111d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
113d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Portions of the attached software ("Contribution") are developed by
114d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
116d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The Contribution is licensed pursuant to the Eric Young open source
117d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * license provided above.
118d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
119d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The binary polynomial arithmetic software is originally written by
120d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
121d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Laboratories. */
122d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
123d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#ifndef OPENSSL_HEADER_BN_H
124d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define OPENSSL_HEADER_BN_H
125d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
126d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/base.h>
127e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <openssl/thread.h>
128d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
129e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <inttypes.h>  /* for PRIu64 and friends */
130d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <stdio.h>  /* for FILE* */
131d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
132d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#if defined(__cplusplus)
133d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyextern "C" {
134d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif
135d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
136d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
137d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN provides support for working with arbitary sized integers. For example,
138d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * although the largest integer supported by the compiler might be 64 bits, BN
139d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * will allow you to work with numbers until you run out of memory. */
140d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
141d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
142e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley/* BN_ULONG is the native word size when working with big integers.
143e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley *
144e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * Note: on some platforms, inttypes.h does not define print format macros in
145e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * C++ unless |__STDC_FORMAT_MACROS| defined. As this is a public header, bn.h
146e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * does not define |__STDC_FORMAT_MACROS| itself. C++ source files which use the
147e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * FMT macros must define it externally. */
148d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#if defined(OPENSSL_64_BIT)
149d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_ULONG uint64_t
150d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_BITS2 64
151e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_DEC_FMT1	"%" PRIu64
152e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_DEC_FMT2	"%019" PRIu64
153e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_HEX_FMT1	"%" PRIx64
154d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#elif defined(OPENSSL_32_BIT)
155d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_ULONG uint32_t
156d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_BITS2 32
157e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_DEC_FMT1	"%" PRIu32
158e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_DEC_FMT2	"%09" PRIu32
159e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_HEX_FMT1	"%" PRIx32
160d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#else
161d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
162d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif
163d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
164d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
165d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Allocation and freeing. */
166d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
167d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_new creates a new, allocated BIGNUM and initialises it. */
168d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_new(void);
169d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
170d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_init initialises a stack allocated |BIGNUM|. */
171d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_init(BIGNUM *bn);
172d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
173d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_free frees the data referenced by |bn| and, if |bn| was originally
174d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocated on the heap, frees |bn| also. */
175d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_free(BIGNUM *bn);
176d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
177d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_clear_free erases and frees the data referenced by |bn| and, if |bn| was
178d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * originally allocated on the heap, frees |bn| also. */
179d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_clear_free(BIGNUM *bn);
180d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
181d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_dup allocates a new BIGNUM and sets it equal to |src|. It returns the
182d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocated BIGNUM on success or NULL otherwise. */
183d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_dup(const BIGNUM *src);
184d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
185b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_copy sets |dest| equal to |src| and returns |dest| or NULL on allocation
186b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * failure. */
187d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src);
188d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
189d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_clear sets |bn| to zero and erases the old data. */
190d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_clear(BIGNUM *bn);
191d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
192d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_value_one returns a static BIGNUM with value 1. */
193d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT const BIGNUM *BN_value_one(void);
194d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
195d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_with_flags initialises a stack allocated |BIGNUM| with pointers to the
196d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * contents of |in| but with |flags| ORed into the flags field.
197d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
198d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Note: the two BIGNUMs share state and so |out| should /not/ be passed to
199d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_free|. */
200d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_with_flags(BIGNUM *out, const BIGNUM *in, int flags);
201d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
202d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
203d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Basic functions. */
204d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
205d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_num_bits returns the minimum number of bits needed to represent the
206d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * absolute value of |bn|. */
207d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT unsigned BN_num_bits(const BIGNUM *bn);
208d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
209d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_num_bytes returns the minimum number of bytes needed to represent the
210d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * absolute value of |bn|. */
211d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT unsigned BN_num_bytes(const BIGNUM *bn);
212d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
213d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_zero sets |bn| to zero. */
214d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_zero(BIGNUM *bn);
215d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
216d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_one sets |bn| to one. It returns one on success or zero on allocation
217d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * failure. */
218d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_one(BIGNUM *bn);
219d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
220d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_set_word sets |bn| to |value|. It returns one on success or zero on
221d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocation failure. */
222d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_set_word(BIGNUM *bn, BN_ULONG value);
223d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
224d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_set_negative sets the sign of |bn|. */
225d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_set_negative(BIGNUM *bn, int sign);
226d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
227d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_negative returns one if |bn| is negative and zero otherwise. */
228d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_negative(const BIGNUM *bn);
229d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
230d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_get_flags returns |bn->flags| & |flags|. */
231d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_get_flags(const BIGNUM *bn, int flags);
232d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
233d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_set_flags sets |flags| on |bn|. */
234d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_set_flags(BIGNUM *bn, int flags);
235d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
236d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
237d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Conversion functions. */
238d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
239d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_bin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as
240d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * a big-endian number, and returns |ret|. If |ret| is NULL then a fresh
241d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BIGNUM| is allocated and returned. It returns NULL on allocation
242d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * failure. */
243d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret);
244d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
245d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_bn2bin serialises the absolute value of |in| to |out| as a big-endian
246d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * integer, which must have |BN_num_bytes| of space available. It returns the
247d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * number of bytes written. */
248d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT size_t BN_bn2bin(const BIGNUM *in, uint8_t *out);
249d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
250d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_bn2bin_padded serialises the absolute value of |in| to |out| as a
251d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * big-endian integer. The integer is padded with leading zeros up to size
252d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |len|. If |len| is smaller than |BN_num_bytes|, the function fails and
253d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * returns 0. Otherwise, it returns 1. */
254d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in);
255d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
2564139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley/* BN_bn2cbb_padded behaves like |BN_bn2bin_padded| but writes to a |CBB|. */
2574139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam LangleyOPENSSL_EXPORT int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in);
2584139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley
259d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_bn2hex returns an allocated string that contains a NUL-terminated, hex
260d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * representation of |bn|. If |bn| is negative, the first char in the resulting
261d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * string will be '-'. Returns NULL on allocation failure. */
262d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT char *BN_bn2hex(const BIGNUM *bn);
263d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
264d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_hex2bn parses the leading hex number from |in|, which may be proceeded by
265d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * a '-' to indicate a negative number and may contain trailing, non-hex data.
266d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |outp| is not NULL, it constructs a BIGNUM equal to the hex number and
267d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * stores it in |*outp|. If |*outp| is NULL then it allocates a new BIGNUM and
268d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * updates |*outp|. It returns the number of bytes of |in| processed or zero on
269d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * error. */
270d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_hex2bn(BIGNUM **outp, const char *in);
271d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
272d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_bn2dec returns an allocated string that contains a NUL-terminated,
273d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * decimal representation of |bn|. If |bn| is negative, the first char in the
274d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * resulting string will be '-'. Returns NULL on allocation failure. */
275d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT char *BN_bn2dec(const BIGNUM *a);
276d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
277d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_dec2bn parses the leading decimal number from |in|, which may be
278d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * proceeded by a '-' to indicate a negative number and may contain trailing,
279d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * non-decimal data. If |outp| is not NULL, it constructs a BIGNUM equal to the
280d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * decimal number and stores it in |*outp|. If |*outp| is NULL then it
281d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocates a new BIGNUM and updates |*outp|. It returns the number of bytes
282d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * of |in| processed or zero on error. */
283d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_dec2bn(BIGNUM **outp, const char *in);
284d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
285d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_asc2bn acts like |BN_dec2bn| or |BN_hex2bn| depending on whether |in|
286d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * begins with "0X" or "0x" (indicating hex) or not (indicating decimal). A
287d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * leading '-' is still permitted and comes before the optional 0X/0x. It
288d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * returns one on success or zero on error. */
289d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_asc2bn(BIGNUM **outp, const char *in);
290d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
291d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_print writes a hex encoding of |a| to |bio|. It returns one on success
292d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * and zero on error. */
293d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_print(BIO *bio, const BIGNUM *a);
294d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
295d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_print_fp acts like |BIO_print|, but wraps |fp| in a |BIO| first. */
296d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_print_fp(FILE *fp, const BIGNUM *a);
297d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
298d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_get_word returns the absolute value of |bn| as a single word. If |bn| is
299d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * too large to be represented as a single word, the maximum possible value
300d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * will be returned. */
301d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_ULONG BN_get_word(const BIGNUM *bn);
302d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
303d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
304b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* ASN.1 functions. */
305b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
306b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_cbs2unsigned parses a non-negative DER INTEGER from |cbs| writes the
307b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * result to |ret|. It returns one on success and zero on failure. */
308b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT int BN_cbs2unsigned(CBS *cbs, BIGNUM *ret);
309b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
310b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_cbs2unsigned_buggy acts like |BN_cbs2unsigned| but tolerates some invalid
311b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * encodings. Do not use this function. */
312b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT int BN_cbs2unsigned_buggy(CBS *cbs, BIGNUM *ret);
313b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
314b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_bn2cbb marshals |bn| as a non-negative DER INTEGER and appends the result
315b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * to |cbb|. It returns one on success and zero on failure. */
316b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT int BN_bn2cbb(CBB *cbb, const BIGNUM *bn);
317b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
318b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
319d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Internal functions.
320d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
321d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * These functions are useful for code that is doing low-level manipulations of
322d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * BIGNUM values. However, be sure that no other function in this file does
323d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * what you want before turning to these. */
324d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
325d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* bn_correct_top decrements |bn->top| until |bn->d[top-1]| is non-zero or
326d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * until |top| is zero. */
327d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void bn_correct_top(BIGNUM *bn);
328d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
329d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* bn_wexpand ensures that |bn| has at least |words| works of space without
330d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * altering its value. It returns one on success or zero on allocation
331d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * failure. */
332b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT BIGNUM *bn_wexpand(BIGNUM *bn, size_t words);
333d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
334d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
335d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BIGNUM pools.
336d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
337d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Certain BIGNUM operations need to use many temporary variables and
338d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocating and freeing them can be quite slow. Thus such opertions typically
339d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * take a |BN_CTX| parameter, which contains a pool of |BIGNUMs|. The |ctx|
340d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * argument to a public function may be NULL, in which case a local |BN_CTX|
341d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * will be created just for the lifetime of that call.
342d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
343d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * A function must call |BN_CTX_start| first. Then, |BN_CTX_get| may be called
344d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * repeatedly to obtain temporary |BIGNUM|s. All |BN_CTX_get| calls must be made
345d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * before calling any other functions that use the |ctx| as an argument.
346d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
347d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Finally, |BN_CTX_end| must be called before returning from the function.
348d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * When |BN_CTX_end| is called, the |BIGNUM| pointers obtained from
349d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_CTX_get| become invalid. */
350d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
351d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_CTX_new returns a new, empty BN_CTX or NULL on allocation failure. */
352d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_CTX *BN_CTX_new(void);
353d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
354d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_CTX_free frees all BIGNUMs contained in |ctx| and then frees |ctx|
355d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * itself. */
356d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_CTX_free(BN_CTX *ctx);
357d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
358d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_CTX_start "pushes" a new entry onto the |ctx| stack and allows future
359d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * calls to |BN_CTX_get|. */
360d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_CTX_start(BN_CTX *ctx);
361d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
362d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_CTX_get returns a new |BIGNUM|, or NULL on allocation failure. Once
363d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_CTX_get| has returned NULL, all future calls will also return NULL until
364d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_CTX_end| is called. */
365d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_CTX_get(BN_CTX *ctx);
366d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
367d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_CTX_end invalidates all |BIGNUM|s returned from |BN_CTX_get| since the
368d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * matching |BN_CTX_start| call. */
369d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_CTX_end(BN_CTX *ctx);
370d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
371d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
372d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Simple arithmetic */
373d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
374d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_add sets |r| = |a| + |b|, where |r| may be the same pointer as either |a|
375d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * or |b|. It returns one on success and zero on allocation failure. */
376d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
377d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
378d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_uadd sets |r| = |a| + |b|, where |a| and |b| are non-negative and |r| may
379d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * be the same pointer as either |a| or |b|. It returns one on success and zero
380d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on allocation failure. */
381d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
382d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
383d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_add_word adds |w| to |a|. It returns one on success and zero otherwise. */
384d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_add_word(BIGNUM *a, BN_ULONG w);
385d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
386d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_sub sets |r| = |a| - |b|, where |r| must be a distinct pointer from |a|
387d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * and |b|. It returns one on success and zero on allocation failure. */
388d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
389d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
390d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_usub sets |r| = |a| - |b|, where |a| and |b| are non-negative integers,
391d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |b| < |a| and |r| must be a distinct pointer from |a| and |b|. It returns
392d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * one on success and zero on allocation failure. */
393d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
394d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
395d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_sub_word subtracts |w| from |a|. It returns one on success and zero on
396d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocation failure. */
397d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_sub_word(BIGNUM *a, BN_ULONG w);
398d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
399d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mul sets |r| = |a| * |b|, where |r| may be the same pointer as |a| or
400d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |b|. Returns one on success and zero otherwise. */
401d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
402d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                          BN_CTX *ctx);
403d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
404d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mul_word sets |bn| = |bn| * |w|. It returns one on success or zero on
405d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allocation failure. */
406d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mul_word(BIGNUM *bn, BN_ULONG w);
407d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
408d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_sqr sets |r| = |a|^2 (i.e. squares), where |r| may be the same pointer as
409d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |a|. Returns one on success and zero otherwise. This is more efficient than
410d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * BN_mul(r, a, a, ctx). */
411d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
412d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
413d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_div divides |numerator| by |divisor| and places the result in |quotient|
414d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * and the remainder in |rem|. Either of |quotient| or |rem| may be NULL, in
415d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * which case the respective value is not returned. The result is rounded
416d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * towards zero; thus if |numerator| is negative, the remainder will be zero or
417d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * negative. It returns one on success or zero on error. */
418d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_div(BIGNUM *quotient, BIGNUM *rem,
419d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                          const BIGNUM *numerator, const BIGNUM *divisor,
420d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                          BN_CTX *ctx);
421d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
422d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_div_word sets |numerator| = |numerator|/|divisor| and returns the
423d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * remainder or (BN_ULONG)-1 on error. */
424d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_ULONG BN_div_word(BIGNUM *numerator, BN_ULONG divisor);
425d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
426d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_sqrt sets |*out_sqrt| (which may be the same |BIGNUM| as |in|) to the
427d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * square root of |in|, using |ctx|. It returns one on success or zero on
428d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * error. Negative numbers and non-square numbers will result in an error with
429d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * appropriate errors on the error queue. */
430d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx);
431d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
432d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
433d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Comparison functions */
434d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
435d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_cmp returns a value less than, equal to or greater than zero if |a| is
436d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * less than, equal to or greater than |b|, respectively. */
437d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_cmp(const BIGNUM *a, const BIGNUM *b);
438d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
439d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_ucmp returns a value less than, equal to or greater than zero if the
440d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * absolute value of |a| is less than, equal to or greater than the absolute
441d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * value of |b|, respectively. */
442d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
443d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
444d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_abs_is_word returns one if the absolute value of |bn| equals |w| and zero
445d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * otherwise. */
446d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w);
447d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
448d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_zero returns one if |bn| is zero and zero otherwise. */
449d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_zero(const BIGNUM *bn);
450d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
451d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_one returns one if |bn| equals one and zero otherwise. */
452d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_one(const BIGNUM *bn);
453d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
454d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_word returns one if |bn| is exactly |w| and zero otherwise. */
455d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_word(const BIGNUM *bn, BN_ULONG w);
456d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
457d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_odd returns one if |bn| is odd and zero otherwise. */
458d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_odd(const BIGNUM *bn);
459d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
460d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
461d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Bitwise operations. */
462d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
463d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_lshift sets |r| equal to |a| << n. The |a| and |r| arguments may be the
464d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * same |BIGNUM|. It returns one on success and zero on allocation failure. */
465d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
466d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
467d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_lshift1 sets |r| equal to |a| << 1, where |r| and |a| may be the same
468d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * pointer. It returns one on success and zero on allocation failure. */
469d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_lshift1(BIGNUM *r, const BIGNUM *a);
470d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
471d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_rshift sets |r| equal to |a| >> n, where |r| and |a| may be the same
472d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * pointer. It returns one on success and zero on allocation failure. */
473d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
474d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
475d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_rshift1 sets |r| equal to |a| >> 1, where |r| and |a| may be the same
476d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * pointer. It returns one on success and zero on allocation failure. */
477d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_rshift1(BIGNUM *r, const BIGNUM *a);
478d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
479d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_set_bit sets the |n|th, least-significant bit in |a|. For example, if |a|
480d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * is 2 then setting bit zero will make it 3. It returns one on success or zero
481d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on allocation failure. */
482d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_set_bit(BIGNUM *a, int n);
483d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
484d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_clear_bit clears the |n|th, least-significant bit in |a|. For example, if
485d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |a| is 3, clearing bit zero will make it two. It returns one on success or
486d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * zero on allocation failure. */
487d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_clear_bit(BIGNUM *a, int n);
488d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
489d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_bit_set returns the value of the |n|th, least-significant bit in |a|,
490d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * or zero if the bit doesn't exist. */
491d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_bit_set(const BIGNUM *a, int n);
492d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
493d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mask_bits truncates |a| so that it is only |n| bits long. It returns one
494d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on success or zero if |n| is greater than the length of |a| already. */
495d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mask_bits(BIGNUM *a, int n);
496d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
497d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
498d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Modulo arithmetic. */
499d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
500d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_word returns |a| mod |w|. */
501d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
502d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
503d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod is a helper macro that calls |BN_div| and discards the quotient. */
504d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_mod(rem, numerator, divisor, ctx) \
505d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  BN_div(NULL, (rem), (numerator), (divisor), (ctx))
506d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
507d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_nnmod is a non-negative modulo function. It acts like |BN_mod|, but 0 <=
508e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * |rem| < |divisor| is always true. It returns one on success and zero on
509e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * error. */
510d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_nnmod(BIGNUM *rem, const BIGNUM *numerator,
511d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                            const BIGNUM *divisor, BN_CTX *ctx);
512d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
513d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_add sets |r| = |a| + |b| mod |m|. It returns one on success and zero
514d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on error. */
515d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
516d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                              const BIGNUM *m, BN_CTX *ctx);
517d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
518d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_add_quick acts like |BN_mod_add| but requires that |a| and |b| be
519d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * non-negative and less than |m|. */
520d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
521d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                    const BIGNUM *m);
522d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
523d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_sub sets |r| = |a| - |b| mod |m|. It returns one on success and zero
524d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on error. */
525d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
526d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                              const BIGNUM *m, BN_CTX *ctx);
527d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
528d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_sub_quick acts like |BN_mod_sub| but requires that |a| and |b| be
529d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * non-negative and less than |m|. */
530d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
531d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                    const BIGNUM *m);
532d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
533d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_mul sets |r| = |a|*|b| mod |m|. It returns one on success and zero
534d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on error. */
535d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
536d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                              const BIGNUM *m, BN_CTX *ctx);
537d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
538fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley/* BN_mod_sqr sets |r| = |a|^2 mod |m|. It returns one on success and zero
539d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * on error. */
540d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
541d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                              BN_CTX *ctx);
542d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
543d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_lshift sets |r| = (|a| << n) mod |m|, where |r| and |a| may be the
544d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * same pointer. It returns one on success and zero on error. */
545d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n,
546d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                 const BIGNUM *m, BN_CTX *ctx);
547d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
548d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_lshift_quick acts like |BN_mod_lshift| but requires that |a| be
549d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * non-negative and less than |m|. */
550d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n,
551d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                       const BIGNUM *m);
552d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
553d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_lshift1 sets |r| = (|a| << 1) mod |m|, where |r| and |a| may be the
554d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * same pointer. It returns one on success and zero on error. */
555d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
556d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                  BN_CTX *ctx);
557d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
558d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_lshift1_quick acts like |BN_mod_lshift1| but requires that |a| be
559d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * non-negative and less than |m|. */
560d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a,
561d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                        const BIGNUM *m);
562d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
563d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_sqrt returns a |BIGNUM|, r, such that r^2 == a (mod p). */
564d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p,
565d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                   BN_CTX *ctx);
566d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
567d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
568d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Random and prime number generation. */
569d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
570f4e427204234da139fd0585def4b4e22502e33f0Adam Langley/* BN_rand sets |rnd| to a random number of length |bits|. If |top| is zero, the
571f4e427204234da139fd0585def4b4e22502e33f0Adam Langley * most-significant bit, if any, will be set. If |top| is one, the two most
572f4e427204234da139fd0585def4b4e22502e33f0Adam Langley * significant bits, if any, will be set.
573d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
574d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |top| is -1 then no extra action will be taken and |BN_num_bits(rnd)| may
575d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * not equal |bits| if the most significant bits randomly ended up as zeros.
576d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
577f4e427204234da139fd0585def4b4e22502e33f0Adam Langley * If |bottom| is non-zero, the least-significant bit, if any, will be set. The
578f4e427204234da139fd0585def4b4e22502e33f0Adam Langley * function returns one on success or zero otherwise. */
579d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
580d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
581d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_pseudo_rand is an alias for |BN_rand|. */
582d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
583d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
584d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_rand_range sets |rnd| to a random value [0..range). It returns one on
585d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * success and zero otherwise. */
586d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
587d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
588d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_pseudo_rand_range is an alias for BN_rand_range. */
589d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
590d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
591d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
592d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * BN_rand_range, it also includes the contents of |priv| and |message| in the
593d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * generation so that an RNG failure isn't fatal as long as |priv| remains
594d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * secret. This is intended for use in DSA and ECDSA where an RNG weakness
595d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * leads directly to private key exposure unless this function is used.
596d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * It returns one on success and zero on error. */
597d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
598d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                         const BIGNUM *priv,
599d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                         const uint8_t *message,
600d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                         size_t message_len, BN_CTX *ctx);
601d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
602d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_GENCB holds a callback function that is used by generation functions that
603d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * can take a very long time to complete. Use |BN_GENCB_set| to initialise a
604d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_GENCB| structure.
605d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
606d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The callback receives the address of that |BN_GENCB| structure as its last
607d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * argument and the user is free to put an arbitary pointer in |arg|. The other
608d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * arguments are set as follows:
609d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *   event=BN_GENCB_GENERATED, n=i:   after generating the i'th possible prime
610d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *                                    number.
611d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *   event=BN_GENCB_PRIME_TEST, n=-1: when finished trial division primality
612d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *                                    checks.
613d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *   event=BN_GENCB_PRIME_TEST, n=i:  when the i'th primality test has finished.
614d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
615d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The callback can return zero to abort the generation progress or one to
616d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * allow it to continue.
617d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
618d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * When other code needs to call a BN generation function it will often take a
619d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * BN_GENCB argument and may call the function with other argument values. */
620d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_GENCB_GENERATED 0
621d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_GENCB_PRIME_TEST 1
622d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
623d9e397b599b13d642138480a28c14db7a136bf0Adam Langleystruct bn_gencb_st {
624d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  void *arg;        /* callback-specific data */
625d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int (*callback)(int event, int n, struct bn_gencb_st *);
626d9e397b599b13d642138480a28c14db7a136bf0Adam Langley};
627d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
628d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_GENCB_set configures |callback| to call |f| and sets |callout->arg| to
629d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |arg|. */
630d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_GENCB_set(BN_GENCB *callback,
631d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                 int (*f)(int event, int n,
632d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                          struct bn_gencb_st *),
633d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                 void *arg);
634d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
635d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_GENCB_call calls |callback|, if not NULL, and returns the return value of
636d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the callback, or 1 if |callback| is NULL. */
637d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_GENCB_call(BN_GENCB *callback, int event, int n);
638d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
639d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_generate_prime_ex sets |ret| to a prime number of |bits| length. If safe
640d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * is non-zero then the prime will be such that (ret-1)/2 is also a prime.
641d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * (This is needed for Diffie-Hellman groups to ensure that the only subgroups
642d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * are of size 2 and (p-1)/2.).
643d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
644d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |add| is not NULL, the prime will fulfill the condition |ret| % |add| ==
645d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |rem| in order to suit a given generator. (If |rem| is NULL then |ret| %
646d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |add| == 1.)
647d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
648d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |cb| is not NULL, it will be called during processing to give an
649d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * indication of progress. See the comments for |BN_GENCB|. It returns one on
650d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * success and zero otherwise. */
651d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
652d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                        const BIGNUM *add, const BIGNUM *rem,
653d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                        BN_GENCB *cb);
654d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
655d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_prime_checks is magic value that can be used as the |checks| argument to
656d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the primality testing functions in order to automatically select a number of
657d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Miller-Rabin checks that gives a false positive rate of ~2^{-80}. */
658d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_prime_checks 0
659d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
660d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_primality_test sets |*is_probably_prime| to one if |candidate| is
661d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * probably a prime number by the Miller-Rabin test or zero if it's certainly
662d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * not.
663d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
664d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |do_trial_division| is non-zero then |candidate| will be tested against a
665d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * list of small primes before Miller-Rabin tests. The probability of this
666d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * function returning a false positive is 2^{2*checks}. If |checks| is
667d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_prime_checks| then a value that results in approximately 2^{-80} false
668d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * positive probability is used. If |cb| is not NULL then it is called during
669d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the checking process. See the comment above |BN_GENCB|.
670d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
671d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The function returns one on success and zero on error.
672d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
673d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * (If you are unsure whether you want |do_trial_division|, don't set it.) */
674d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_primality_test(int *is_probably_prime,
675d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                     const BIGNUM *candidate, int checks,
676d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                     BN_CTX *ctx, int do_trial_division,
677d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                     BN_GENCB *cb);
678d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
679d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_prime_fasttest_ex returns one if |candidate| is probably a prime
680d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * number by the Miller-Rabin test, zero if it's certainly not and -1 on error.
681d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
682d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |do_trial_division| is non-zero then |candidate| will be tested against a
683d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * list of small primes before Miller-Rabin tests. The probability of this
684d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * function returning one when |candidate| is composite is 2^{2*checks}. If
685d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |checks| is |BN_prime_checks| then a value that results in approximately
686d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 2^{-80} false positive probability is used. If |cb| is not NULL then it is
687d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * called during the checking process. See the comment above |BN_GENCB|.
688d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
689d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WARNING: deprecated. Use |BN_primality_test|. */
690d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_prime_fasttest_ex(const BIGNUM *candidate, int checks,
691d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                           BN_CTX *ctx, int do_trial_division,
692d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                           BN_GENCB *cb);
693d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
694d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_is_prime_ex acts the same as |BN_is_prime_fasttest_ex| with
695d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |do_trial_division| set to zero.
696d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
697d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WARNING: deprecated: Use |BN_primality_test|. */
698d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_is_prime_ex(const BIGNUM *candidate, int checks,
699d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                  BN_CTX *ctx, BN_GENCB *cb);
700d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
701d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
702d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Number theory functions */
703d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
704d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_gcd sets |r| = gcd(|a|, |b|). It returns one on success and zero
705d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * otherwise. */
706d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
707d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                          BN_CTX *ctx);
708d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
709d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_inverse sets |out| equal to |a|^-1, mod |n|. If either of |a| or |n|
710d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * have |BN_FLG_CONSTTIME| set then the operation is performed in constant
711d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * time. If |out| is NULL, a fresh BIGNUM is allocated. It returns the result
712d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * or NULL on error. */
713d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a,
714d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                      const BIGNUM *n, BN_CTX *ctx);
715d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
716b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_mod_inverse_ex acts like |BN_mod_inverse| except that, when it returns
717b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * zero, it will set |*out_no_inverse| to one if the failure was caused because
718b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * |a| has no inverse mod |n|. Otherwise it will set |*out_no_inverse| to
719b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * zero. */
720b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT BIGNUM *BN_mod_inverse_ex(BIGNUM *out, int *out_no_inverse,
721b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root                                         const BIGNUM *a, const BIGNUM *n,
722b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root                                         BN_CTX *ctx);
723b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
724d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_kronecker returns the Kronecker symbol of |a| and |b| (which is -1, 0 or
725d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 1), or -2 on error. */
726d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
727d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
728d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
729d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Montgomery arithmetic. */
730d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
731d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_MONT_CTX contains the precomputed values needed to work in a specific
732d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Montgomery domain. */
733d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
734d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_MONT_CTX_new returns a fresh BN_MONT_CTX or NULL on allocation failure. */
735d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new(void);
736d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
737e99801b603dea8893dcc61c70b327ef2d00b652cKenny Root/* BN_MONT_CTX_free frees memory associated with |mont|. */
738d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT void BN_MONT_CTX_free(BN_MONT_CTX *mont);
739d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
740d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_MONT_CTX_copy sets |to| equal to |from|. It returns |to| on success or
741d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * NULL on error. */
742d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,
743fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                             const BN_MONT_CTX *from);
744d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
745d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_MONT_CTX_set sets up a Montgomery context given the modulus, |mod|. It
746d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * returns one on success and zero on error. */
747d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod,
748d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                   BN_CTX *ctx);
749d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
750e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley/* BN_MONT_CTX_set_locked takes |lock| and checks whether |*pmont| is NULL. If
751e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * so, it creates a new |BN_MONT_CTX| and sets the modulus for it to |mod|. It
752e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley * then stores it as |*pmont| and returns it, or NULL on error.
753d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
754d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If |*pmont| is already non-NULL then the existing value is returned. */
755e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam LangleyBN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
756e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                    const BIGNUM *mod, BN_CTX *bn_ctx);
757d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
758d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_to_montgomery sets |ret| equal to |a| in the Montgomery domain. It
759d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * returns one on success and zero on error. */
760d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a,
761d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                    const BN_MONT_CTX *mont, BN_CTX *ctx);
762d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
763d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_from_montgomery sets |ret| equal to |a| * R^-1, i.e. translates values
764d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * out of the Montgomery domain. It returns one on success or zero on error. */
765d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a,
766d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                      const BN_MONT_CTX *mont, BN_CTX *ctx);
767d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
768d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_mul_montgomery set |r| equal to |a| * |b|, in the Montgomery domain.
769d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Both |a| and |b| must already be in the Montgomery domain (by
770d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_to_montgomery|). It returns one on success or zero on error. */
771d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a,
772d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                         const BIGNUM *b,
773d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                         const BN_MONT_CTX *mont, BN_CTX *ctx);
774d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
775d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
776d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Exponentiation. */
777d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
778d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_exp sets |r| equal to |a|^{|p|}. It does so with a square-and-multiply
779d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * algorithm that leaks side-channel information. It returns one on success or
780d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * zero otherwise. */
781d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
782d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                          BN_CTX *ctx);
783d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
784d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* BN_mod_exp sets |r| equal to |a|^{|p|} mod |m|. It does so with the best
785d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * algorithm for the values provided and can run in constant time if
786d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * |BN_FLG_CONSTTIME| is set for |p|. It returns one on success or zero
787d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * otherwise. */
788d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
789d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                              const BIGNUM *m, BN_CTX *ctx);
790d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
791d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
792d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                   const BIGNUM *m, BN_CTX *ctx,
793fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                   const BN_MONT_CTX *mont);
794d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
795d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a,
796d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                             const BIGNUM *p, const BIGNUM *m,
797fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                             BN_CTX *ctx,
798fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                             const BN_MONT_CTX *mont);
799d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
800d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
801d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                        const BIGNUM *m, BN_CTX *ctx,
802fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                        const BN_MONT_CTX *mont);
803d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1,
804d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                    const BIGNUM *p1, const BIGNUM *a2,
805d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                                    const BIGNUM *p2, const BIGNUM *m,
806fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley                                    BN_CTX *ctx, const BN_MONT_CTX *mont);
807d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
808d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
809b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* Deprecated functions */
810b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
811b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root/* BN_bn2mpi serialises the value of |in| to |out|, using a format that consists
812b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * of the number's length in bytes represented as a 4-byte big-endian number,
813b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * and the number itself in big-endian format, where the most significant bit
814b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * signals a negative number. (The representation of numbers with the MSB set is
815b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * prefixed with null byte). |out| must have sufficient space available; to
816b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * find the needed amount of space, call the function with |out| set to NULL. */
817b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out);
818b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
819fad6327e4112082b1e77e89a995723f26bd5a9aaAdam Langley/* BN_mpi2bn parses |len| bytes from |in| and returns the resulting value. The
820b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * bytes at |in| are expected to be in the format emitted by |BN_bn2mpi|.
821b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root *
822b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * If |out| is NULL then a fresh |BIGNUM| is allocated and returned, otherwise
823b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * |out| is reused and returned. On error, NULL is returned and the error queue
824b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root * is updated. */
825b8494591d1b1a143f3b192d845c238bbf3bc629dKenny RootOPENSSL_EXPORT BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out);
826b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
827b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root
828d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Private functions */
829d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
830d9e397b599b13d642138480a28c14db7a136bf0Adam Langleystruct bignum_st {
831d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks in little-endian
832d9e397b599b13d642138480a28c14db7a136bf0Adam Langley                  order. */
833d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int top;   /* Index of last used element in |d|, plus one. */
834d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int dmax;  /* Size of |d|, in words. */
835d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int neg;   /* one if the number is negative */
836d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int flags; /* bitmask of BN_FLG_* values */
837d9e397b599b13d642138480a28c14db7a136bf0Adam Langley};
838d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
839d9e397b599b13d642138480a28c14db7a136bf0Adam Langleystruct bn_mont_ctx_st {
840d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  BIGNUM RR; /* used to convert to montgomery form */
841d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  BIGNUM N;  /* The modulus */
8424139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley  BN_ULONG n0[2]; /* least significant words of (R*Ri-1)/N */
843d9e397b599b13d642138480a28c14db7a136bf0Adam Langley};
844d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
845d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT unsigned BN_num_bits_word(BN_ULONG l);
846d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
847d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_FLG_MALLOCED 0x01
848d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_FLG_STATIC_DATA 0x02
849d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* avoid leaking exponent information through timing, BN_mod_exp_mont() will
850d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * call BN_mod_exp_mont_consttime, BN_div() will call BN_div_no_branch,
851d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * BN_mod_inverse() will call BN_mod_inverse_no_branch. */
852d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#define BN_FLG_CONSTTIME 0x04
853d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
854d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
855d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Android compatibility section.
856d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
857d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * These functions are declared, temporarily, for Android because
858d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * wpa_supplicant will take a little time to sync with upstream. Outside of
859d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Android they'll have no definition. */
860d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
861d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyOPENSSL_EXPORT BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn);
862d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
863d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
864d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#if defined(__cplusplus)
865d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}  /* extern C */
866d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif
867d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
868e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_ARG2_LT_ARG3 100
869e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_BAD_RECIPROCAL 101
870e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_BIGNUM_TOO_LONG 102
871e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_BITS_TOO_SMALL 103
872e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_CALLED_WITH_EVEN_MODULUS 104
873e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_DIV_BY_ZERO 105
874e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 106
875e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_INPUT_NOT_REDUCED 107
876e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_INVALID_RANGE 108
877e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_NEGATIVE_NUMBER 109
878e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_NOT_A_SQUARE 110
879e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_NOT_INITIALIZED 111
880e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_NO_INVERSE 112
881e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_PRIVATE_KEY_TOO_LARGE 113
882e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_P_IS_NOT_PRIME 114
883e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_TOO_MANY_ITERATIONS 115
884e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#define BN_R_TOO_MANY_TEMPORARY_VARIABLES 116
885b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root#define BN_R_BAD_ENCODING 117
886b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root#define BN_R_ENCODE_ERROR 118
887d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
888d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif  /* OPENSSL_HEADER_BN_H */
889