2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/bn/bn_ctx.c */
20 /* Written by Ulf Moeller for the OpenSSL project. */
21 /* ====================================================================
22 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in
33 * the documentation and/or other materials provided with the
36 * 3. All advertising materials mentioning features or use of this
37 * software must display the following acknowledgment:
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
41 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
42 * endorse or promote products derived from this software without
43 * prior written permission. For written permission, please contact
44 * openssl-core@openssl.org.
46 * 5. Products derived from this software may not be called "OpenSSL"
47 * nor may "OpenSSL" appear in their names without prior written
48 * permission of the OpenSSL Project.
50 * 6. Redistributions of any form whatsoever must retain the following
52 * "This product includes software developed by the OpenSSL Project
53 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
55 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
56 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
59 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
62 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
66 * OF THE POSSIBILITY OF SUCH DAMAGE.
67 * ====================================================================
69 * This product includes cryptographic software written by Eric Young
70 * (eay@cryptsoft.com). This product includes software written by Tim
71 * Hudson (tjh@cryptsoft.com).
76 # undef NDEBUG /* avoid conflicting definitions */
81 #include <security_utilities/simulatecrash_assert.h>
83 #include <openssl/bn_legacy.h>
86 BN_CTX
*BN_CTX_new(void)
90 ret
=(BN_CTX
*)Malloc(sizeof(BN_CTX
));
93 BNerr(BN_F_BN_CTX_NEW
,ERR_R_MALLOC_FAILURE
);
98 ret
->flags
=BN_FLG_MALLOCED
;
102 void BN_CTX_init(BN_CTX
*ctx
)
109 for (i
= 0; i
< BN_CTX_NUM
; i
++)
110 BN_init(&(ctx
->bn
[i
]));
113 void BN_CTX_free(BN_CTX
*ctx
)
117 if (ctx
== NULL
) return;
118 assert(ctx
->depth
== 0);
120 for (i
=0; i
< BN_CTX_NUM
; i
++)
121 BN_clear_free(&(ctx
->bn
[i
]));
122 if (ctx
->flags
& BN_FLG_MALLOCED
)
126 void BN_CTX_start(BN_CTX
*ctx
)
128 if (ctx
->depth
< BN_CTX_NUM_POS
)
129 ctx
->pos
[ctx
->depth
] = ctx
->tos
;
133 BIGNUM
*BN_CTX_get(BN_CTX
*ctx
)
135 if (ctx
->depth
> BN_CTX_NUM_POS
|| ctx
->tos
>= BN_CTX_NUM
)
139 BNerr(BN_F_BN_CTX_GET
,BN_R_TOO_MANY_TEMPORARY_VARIABLES
);
140 /* disable error code until BN_CTX_end is called: */
145 return (&(ctx
->bn
[ctx
->tos
++]));
148 void BN_CTX_end(BN_CTX
*ctx
)
150 if (ctx
== NULL
) return;
151 assert(ctx
->depth
> 0);
153 /* should never happen, but we can tolerate it if not in
154 * debug mode (could be a 'goto err' in the calling function
155 * before BN_CTX_start was reached) */
160 if (ctx
->depth
< BN_CTX_NUM_POS
)
161 ctx
->tos
= ctx
->pos
[ctx
->depth
];