4 //FIXME Not checked on threadsafety yet; after checking please remove this line
5 /* crypto/bn/bn_ctx.c */
6 /* Written by Ulf Moeller for the OpenSSL project. */
7 /* ====================================================================
8 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * openssl-core@openssl.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
62 # undef NDEBUG /* avoid conflicting definitions */
69 #include "openssl_mods.h"
71 BN_CTX
*BN_CTX_new(void)
75 ret
= (BN_CTX
*)OPENSSL_malloc(sizeof(BN_CTX
));
82 ret
->flags
= BN_FLG_MALLOCED
;
86 void BN_CTX_init(BN_CTX
*ctx
)
93 for(i
= 0; i
< BN_CTX_NUM
; i
++)
94 { BN_init(&(ctx
->bn
[i
])); }
97 void BN_CTX_free(BN_CTX
*ctx
)
101 if(ctx
== NULL
) { return; }
102 assert(ctx
->depth
== 0);
104 for(i
= 0; i
< BN_CTX_NUM
; i
++)
105 { BN_clear_free(&(ctx
->bn
[i
])); }
106 if(ctx
->flags
& BN_FLG_MALLOCED
)
107 { OPENSSL_free(ctx
); }
110 void BN_CTX_start(BN_CTX
*ctx
)
112 if(ctx
->depth
< BN_CTX_NUM_POS
)
113 { ctx
->pos
[ctx
->depth
] = ctx
->tos
; }
117 BIGNUM
*BN_CTX_get(BN_CTX
*ctx
)
119 if(ctx
->depth
> BN_CTX_NUM_POS
|| ctx
->tos
>= BN_CTX_NUM
)
123 /* disable error code until BN_CTX_end is called: */
128 return (&(ctx
->bn
[ctx
->tos
++]));
131 void BN_CTX_end(BN_CTX
*ctx
)
133 if(ctx
== NULL
) { return; }
134 assert(ctx
->depth
> 0);
136 /* should never happen, but we can tolerate it if not in
137 * debug mode (could be a 'goto err' in the calling function
138 * before BN_CTX_start was reached) */
139 { BN_CTX_start(ctx
); }
143 if(ctx
->depth
< BN_CTX_NUM_POS
)
144 { ctx
->tos
= ctx
->pos
[ctx
->depth
]; }