This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpiutil.c
1 /* mpiutil.ac  -  Utility functions for MPI
2  * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include "mpi-internal.h"
22
23
24 /****************
25  * Note:  It was a bad idea to use the number of limbs to allocate
26  *        because on a alpha the limbs are large but we normally need
27  *        integers of n bits - So we should chnage this to bits (or bytes).
28  *
29  *        But mpi_alloc is used in a lot of places :-)
30  */
31 MPI mpi_alloc(unsigned nlimbs)
32 {
33         MPI a;
34
35         a = (MPI) kmalloc(sizeof *a,GFP_KERNEL);
36         if (!a)
37                 return a;
38
39         if (nlimbs) {
40                 a->d = mpi_alloc_limb_space(nlimbs);
41                 if (!a->d) {
42                         kfree(a);
43                         return NULL;
44                 }
45         }
46         else {
47                 a->d = NULL;
48         }
49
50         a->alloced = nlimbs;
51         a->nlimbs = 0;
52         a->sign = 0;
53         a->flags = 0;
54         a->nbits = 0;
55         return a;
56 }
57
58 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs )
59 {
60     size_t len = nlimbs * sizeof(mpi_limb_t);
61
62     return kmalloc(len,GFP_KERNEL);
63 }
64
65 void mpi_free_limb_space( mpi_ptr_t a )
66 {
67     if( !a )
68         return;
69
70     kfree(a);
71 }
72
73
74 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
75 {
76     mpi_free_limb_space(a->d);
77     a->d = ap;
78     a->alloced = nlimbs;
79 }
80
81 /****************
82  * Resize the array of A to NLIMBS. the additional space is cleared
83  * (set to 0) [done by m_realloc()]
84  */
85 int mpi_resize(MPI a, unsigned nlimbs)
86 {
87         void *p;
88
89         if (nlimbs <= a->alloced)
90                 return 0; /* no need to do it */
91
92         if (a->d) {
93                 p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
94                 if (!p)
95                         return -ENOMEM;
96                 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
97                 kfree(a->d);
98                 a->d = p;
99         } else {
100                 a->d = kmalloc( nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
101                 if (!a->d)
102                         return -ENOMEM;
103                 memset(a->d, 0, nlimbs * sizeof(mpi_limb_t) );
104         }
105         a->alloced = nlimbs;
106         return 0;
107 }
108
109 void mpi_clear(MPI a)
110 {
111         a->nlimbs = 0;
112         a->nbits = 0;
113         a->flags = 0;
114 }
115
116 void mpi_free(MPI a)
117 {
118         if (!a)
119                 return;
120
121         if (a->flags & 4)
122                 kfree(a->d);
123         else {
124                 mpi_free_limb_space(a->d);
125         }
126
127         if (a->flags & ~7 )
128                 printk("invalid flag value in mpi\n");
129         kfree(a);
130 }
131
132
133 /****************
134  * Note: This copy function should not interpret the MPI
135  *       but copy it transparently.
136  */
137 int mpi_copy(MPI *copied, const MPI a )
138 {
139         size_t i;
140         MPI b;
141
142         *copied = MPI_NULL;
143
144         if ( a ) {
145                 b = mpi_alloc( a->nlimbs );
146                 if (!b)
147                         return -ENOMEM;
148
149                 b->nlimbs = a->nlimbs;
150                 b->sign = a->sign;
151                 b->flags  = a->flags;
152                 b->nbits = a->nbits;
153
154                 for (i = 0; i < b->nlimbs; i++ )
155                         b->d[i] = a->d[i];
156
157                 *copied = b;
158         }
159
160         return 0;
161 }
162
163
164 int mpi_set(MPI w, const MPI u)
165 {
166         mpi_ptr_t wp, up;
167         mpi_size_t usize = u->nlimbs;
168         int usign = u->sign;
169
170         if (RESIZE_IF_NEEDED(w, (size_t) usize) < 0)
171                 return -ENOMEM;
172
173         wp = w->d;
174         up = u->d;
175         MPN_COPY(wp, up, usize);
176         w->nlimbs = usize;
177         w->nbits = u->nbits;
178         w->flags = u->flags;
179         w->sign = usign;
180         return 0;
181 }
182
183
184 int mpi_set_ui(MPI w, unsigned long u)
185 {
186         if (RESIZE_IF_NEEDED(w, 1) < 0)
187                 return -ENOMEM;
188         w->d[0] = u;
189         w->nlimbs = u? 1:0;
190         w->sign = 0;
191         w->nbits = 0;
192         w->flags = 0;
193         return 0;
194 }
195
196 MPI mpi_alloc_set_ui(unsigned long u)
197 {
198         MPI w = mpi_alloc(1);
199         if (!w)
200                 return w;
201         w->d[0] = u;
202         w->nlimbs = u? 1:0;
203         w->sign = 0;
204         return w;
205 }
206
207
208 void mpi_swap(MPI a, MPI b)
209 {
210         struct gcry_mpi tmp;
211
212         tmp = *a; *a = *b; *b = tmp;
213 }
214