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