xref: /optee_os/core/lib/libtomcrypt/src/math/multi.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2001-2007, Tom St Denis
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
30  *
31  * LibTomCrypt is a library that provides various cryptographic
32  * algorithms in a highly modular and flexible manner.
33  *
34  * The library is free for all purposes without any express
35  * guarantee it works.
36  *
37  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
38  */
39 #include "tomcrypt.h"
40 
41 #ifdef LTC_MPI
42 #include <stdarg.h>
43 
44 int ltc_init_multi(void **a, ...)
45 {
46    void    **cur = a;
47    int       np  = 0;
48    va_list   args;
49 
50    va_start(args, a);
51    while (cur != NULL) {
52        if (mp_init(cur) != CRYPT_OK) {
53           /* failed */
54           va_list clean_list;
55 
56           va_start(clean_list, a);
57           cur = a;
58           while (np--) {
59               mp_clear(*cur);
60               cur = va_arg(clean_list, void**);
61           }
62           va_end(clean_list);
63           return CRYPT_MEM;
64        }
65        ++np;
66        cur = va_arg(args, void**);
67    }
68    va_end(args);
69    return CRYPT_OK;
70 }
71 
72 int ltc_init_multi_size(int size_bits, void **a, ...)
73 {
74    void    **cur = a;
75    int       np  = 0;
76    va_list   args;
77 
78    va_start(args, a);
79    while (cur != NULL) {
80        if (mp_init_size(size_bits, cur) != CRYPT_OK) {
81           /* failed */
82           va_list clean_list;
83 
84           va_start(clean_list, a);
85           cur = a;
86           while (np--) {
87               mp_clear(*cur);
88               cur = va_arg(clean_list, void**);
89           }
90           va_end(clean_list);
91           return CRYPT_MEM;
92        }
93        ++np;
94        cur = va_arg(args, void**);
95    }
96    va_end(args);
97    return CRYPT_OK;
98 }
99 
100 void ltc_deinit_multi(void *a, ...)
101 {
102    void     *cur = a;
103    va_list   args;
104 
105    va_start(args, a);
106    while (cur != NULL) {
107        mp_clear(cur);
108        cur = va_arg(args, void *);
109    }
110    va_end(args);
111 }
112 
113 #endif
114 
115 /* $Source: /cvs/libtom/libtomcrypt/src/math/multi.c,v $ */
116 /* $Revision: 1.6 $ */
117 /* $Date: 2006/12/28 01:27:23 $ */
118