Debian Codegraph @

mpi/mpiutil.c

raw
1 /* mpiutil.ac - Utility functions for MPI
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003,
3 * 2007 Free Software Foundation, Inc.
4 * Copyright (C) 2013 g10 Code GmbH
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "g10lib.h"
28 #include "mpi-internal.h"
29 #include "mod-source-info.h"
30 #include "const-time.h"
31
32
33 #if SIZEOF_UNSIGNED_INT == 2
34 # define MY_UINT_MAX 0xffff
35 /* (visual check: 0123 ) */
36 #elif SIZEOF_UNSIGNED_INT == 4
37 # define MY_UINT_MAX 0xffffffff
38 /* (visual check: 01234567 ) */
39 #elif SIZEOF_UNSIGNED_INT == 8
40 # define MY_UINT_MAX 0xffffffffffffffff
41 /* (visual check: 0123456789abcdef ) */
42 #else
43 # error Need MY_UINT_MAX for this limb size
44 #endif
45
46
47 /* Constants allocated right away at startup. */
48 static gcry_mpi_t constants[MPI_NUMBER_OF_CONSTANTS];
49
50
51 const char *
52 _gcry_mpi_get_hw_config (void)
53 {
54 return mod_source_info + 1;
55 }
56
57
58 /* Initialize the MPI subsystem. This is called early and allows
59 doing some initialization without taking care of threading issues. */
60 gcry_err_code_t
61 _gcry_mpi_init (void)
62 {
63 int idx;
64 unsigned long value;
65
66 for (idx=0; idx < MPI_NUMBER_OF_CONSTANTS; idx++)
67 {
68 switch (idx)
69 {
70 case MPI_C_ZERO: value = 0; break;
71 case MPI_C_ONE: value = 1; break;
72 case MPI_C_TWO: value = 2; break;
73 case MPI_C_THREE: value = 3; break;
74 case MPI_C_FOUR: value = 4; break;
75 case MPI_C_EIGHT: value = 8; break;
76 default: log_bug ("invalid mpi_const selector %d\n", idx);
77 }
78 constants[idx] = mpi_alloc_set_ui (value);
79 constants[idx]->flags = (16|32);
80 }
81
82 return 0;
83 }
84
85
86 /****************
87 * Note: It was a bad idea to use the number of limbs to allocate
88 * because on a alpha the limbs are large but we normally need
89 * integers of n bits - So we should change this to bits (or bytes).
90 *
91 * But mpi_alloc is used in a lot of places :-(. New code
92 * should use mpi_new.
93 */
94 gcry_mpi_t
95 _gcry_mpi_alloc( unsigned nlimbs )
96 {
97 gcry_mpi_t a;
98
99 a = xmalloc( sizeof *a );
100 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL;
101 a->alloced = nlimbs;
102 a->nlimbs = 0;
103 a->sign = 0;
104 a->flags = 0;
105 return a;
106 }
107
108 gcry_mpi_t
109 _gcry_mpi_alloc_secure( unsigned nlimbs )
110 {
111 gcry_mpi_t a;
112
113 a = xmalloc( sizeof *a );
114 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL;
115 a->alloced = nlimbs;
116 a->flags = 1;
117 a->nlimbs = 0;
118 a->sign = 0;
119 return a;
120 }
121
122
123
124 mpi_ptr_t
125 _gcry_mpi_alloc_limb_space( unsigned int nlimbs, int secure )
126 {
127 mpi_ptr_t p;
128 size_t len;
129
130 len = (nlimbs ? nlimbs : 1) * sizeof (mpi_limb_t);
131 p = secure ? xmalloc_secure (len) : xmalloc (len);
132 if (! nlimbs)
133 *p = 0;
134
135 return p;
136 }
137
138 void
139 _gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs)
140 {
141 if (a)
142 {
143 size_t len = nlimbs * sizeof(mpi_limb_t);
144
145 /* If we have information on the number of allocated limbs, we
146 better wipe that space out. This is a failsafe feature if
147 secure memory has been disabled or was not properly
148 implemented in user provided allocation functions. */
149 if (len)
150 wipememory (a, len);
151 xfree(a);
152 }
153 }
154
155
156 void
157 _gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned int nlimbs )
158 {
159 int secure = _gcry_is_secure (a->d);
160
161 if (secure && !_gcry_is_secure (ap))
162 {
163 mpi_ptr_t sp = mpi_alloc_limb_space (nlimbs, 1);
164 MPN_COPY (sp, ap, nlimbs);
165 _gcry_mpi_free_limb_space (ap, nlimbs);
166 ap = sp;
167 }
168
169 _gcry_mpi_free_limb_space (a->d, a->alloced);
170 a->d = ap;
171 a->alloced = nlimbs;
172 }
173
174
175
176 /****************
177 * Resize the array of A to NLIMBS. The additional space is cleared
178 * (set to 0).
179 */
180 void
181 _gcry_mpi_resize (gcry_mpi_t a, unsigned nlimbs)
182 {
183 size_t i;
184
185 if (nlimbs <= a->alloced)
186 {
187 /* We only need to clear the new space (this is a nop if the
188 limb space is already of the correct size. */
189 for (i=a->nlimbs; i < a->alloced; i++)
190 a->d[i] = 0;
191 return;
192 }
193
194 /* Actually resize the limb space. */
195 if (a->d)
196 {
197 a->d = xrealloc (a->d, nlimbs * sizeof (mpi_limb_t));
198 for (i=a->nlimbs; i < nlimbs; i++)
199 a->d[i] = 0;
200 }
201 else
202 {
203 if (a->flags & 1)
204 /* Secure memory is wanted. */
205 a->d = xcalloc_secure (nlimbs , sizeof (mpi_limb_t));
206 else
207 /* Standard memory. */
208 a->d = xcalloc (nlimbs , sizeof (mpi_limb_t));
209 }
210 a->alloced = nlimbs;
211 }
212
213 void
214 _gcry_mpi_clear( gcry_mpi_t a )
215 {
216 if (mpi_is_immutable (a))
217 {
218 mpi_immutable_failed ();
219 return;
220 }
221 a->nlimbs = 0;
222 a->flags = 0;
223 }
224
225
226 void
227 _gcry_mpi_free( gcry_mpi_t a )
228 {
229 if (!a )
230 return;
231 if ((a->flags & 32))
232 {
233 gpgrt_annotate_leaked_object(a);
234 return; /* Never release a constant. */
235 }
236 if ((a->flags & 4))
237 xfree( a->d );
238 else
239 {
240 _gcry_mpi_free_limb_space(a->d, a->alloced);
241 }
242 /* Check that the flags makes sense. We better allow for bit 1
243 (value 2) for backward ABI compatibility. */
244 if ((a->flags & ~(1|2|4|16
245 |GCRYMPI_FLAG_USER1
246 |GCRYMPI_FLAG_USER2
247 |GCRYMPI_FLAG_USER3
248 |GCRYMPI_FLAG_USER4)))
249 log_bug("invalid flag value in mpi_free\n");
250 xfree (a);
251 }
252
253
254 void
255 _gcry_mpi_immutable_failed (void)
256 {
257 log_info ("Warning: trying to change an immutable MPI\n");
258 }
259
260
261 static void
262 mpi_set_secure( gcry_mpi_t a )
263 {
264 mpi_ptr_t ap, bp;
265
266 if ( (a->flags & 1) )
267 return;
268 a->flags |= 1;
269 ap = a->d;
270 if (!a->nlimbs)
271 {
272 gcry_assert (!ap);
273 return;
274 }
275 bp = mpi_alloc_limb_space (a->alloced, 1);
276 MPN_COPY( bp, ap, a->nlimbs );
277 a->d = bp;
278 _gcry_mpi_free_limb_space (ap, a->alloced);
279 }
280
281
282 gcry_mpi_t
283 _gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits)
284 {
285 if (!a)
286 a = mpi_alloc(0);
287
288 if (mpi_is_immutable (a))
289 {
290 mpi_immutable_failed ();
291 return a;
292 }
293
294 if( a->flags & 4 )
295 xfree (a->d);
296 else
297 _gcry_mpi_free_limb_space (a->d, a->alloced);
298
299 a->d = p;
300 a->alloced = 0;
301 a->nlimbs = 0;
302 a->sign = nbits;
303 a->flags = 4 | (a->flags & (GCRYMPI_FLAG_USER1|GCRYMPI_FLAG_USER2
304 |GCRYMPI_FLAG_USER3|GCRYMPI_FLAG_USER4));
305 if (_gcry_is_secure (a->d))
306 a->flags |= 1;
307 return a;
308 }
309
310
311 gcry_mpi_t
312 _gcry_mpi_set_opaque_copy (gcry_mpi_t a, const void *p, unsigned int nbits)
313 {
314 void *d;
315 unsigned int n;
316
317 n = (nbits+7)/8;
318 d = _gcry_is_secure (p)? xtrymalloc_secure (n) : xtrymalloc (n);
319 if (!d)
320 return NULL;
321 memcpy (d, p, n);
322 return mpi_set_opaque (a, d, nbits);
323 }
324
325
326 void *
327 _gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits)
328 {
329 if( !(a->flags & 4) )
330 log_bug("mpi_get_opaque on normal mpi\n");
331 if( nbits )
332 *nbits = a->sign;
333 return a->d;
334 }
335
336
337 void *
338 _gcry_mpi_get_opaque_copy (gcry_mpi_t a, unsigned int *nbits)
339 {
340 const void *s;
341 void *d;
342 unsigned int n;
343
344 s = mpi_get_opaque (a, nbits);
345 if (!s && nbits)
346 return NULL;
347 n = (*nbits+7)/8;
348 d = _gcry_is_secure (s)? xtrymalloc_secure (n) : xtrymalloc (n);
349 if (d)
350 memcpy (d, s, n);
351 return d;
352 }
353
354 /****************
355 * Note: This copy function should not interpret the MPI
356 * but copy it transparently.
357 */
358 gcry_mpi_t
359 _gcry_mpi_copy (gcry_mpi_t a)
360 {
361 int i;
362 gcry_mpi_t b;
363
364 if( a && (a->flags & 4) ) {
365 void *p = NULL;
366 if (a->sign) {
367 p = _gcry_is_secure(a->d)? xmalloc_secure ((a->sign+7)/8)
368 : xmalloc ((a->sign+7)/8);
369 if (a->d)
370 memcpy( p, a->d, (a->sign+7)/8 );
371 }
372 b = mpi_set_opaque( NULL, p, a->sign );
373 b->flags = a->flags;
374 b->flags &= ~(16|32); /* Reset the immutable and constant flags. */
375 }
376 else if( a ) {
377 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
378 : mpi_alloc( a->nlimbs );
379 b->nlimbs = a->nlimbs;
380 b->sign = a->sign;
381 b->flags = a->flags;
382 b->flags &= ~(16|32); /* Reset the immutable and constant flags. */
383 for(i=0; i < b->nlimbs; i++ )
384 b->d[i] = a->d[i];
385 }
386 else
387 b = NULL;
388 return b;
389 }
390
391
392 /* Return true if A is negative. */
393 int
394 _gcry_mpi_is_neg (gcry_mpi_t a)
395 {
396 if (a->sign && _gcry_mpi_cmp_ui (a, 0))
397 return 1;
398 else
399 return 0;
400 }
401
402
403 /* W = - U */
404 void
405 _gcry_mpi_neg (gcry_mpi_t w, gcry_mpi_t u)
406 {
407 if (w != u)
408 mpi_set (w, u);
409 else if (mpi_is_immutable (w))
410 {
411 mpi_immutable_failed ();
412 return;
413 }
414
415 w->sign = !u->sign;
416 }
417
418
419 /* W = [W] */
420 void
421 _gcry_mpi_abs (gcry_mpi_t w)
422 {
423 if (mpi_is_immutable (w))
424 {
425 mpi_immutable_failed ();
426 return;
427 }
428
429 w->sign = 0;
430 }
431
432
433 /****************
434 * This function allocates an MPI which is optimized to hold
435 * a value as large as the one given in the argument and allocates it
436 * with the same flags as A.
437 */
438 gcry_mpi_t
439 _gcry_mpi_alloc_like( gcry_mpi_t a )
440 {
441 gcry_mpi_t b;
442
443 if( a && (a->flags & 4) ) {
444 int n = (a->sign+7)/8;
445 void *p = _gcry_is_secure(a->d)? xtrymalloc_secure (n)
446 : xtrymalloc (n);
447 memcpy( p, a->d, n );
448 b = mpi_set_opaque( NULL, p, a->sign );
449 }
450 else if( a ) {
451 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
452 : mpi_alloc( a->nlimbs );
453 b->nlimbs = 0;
454 b->sign = 0;
455 b->flags = a->flags;
456 }
457 else
458 b = NULL;
459 return b;
460 }
461
462
463 /* Set U into W and release U. If W is NULL only U will be released. */
464 void
465 _gcry_mpi_snatch (gcry_mpi_t w, gcry_mpi_t u)
466 {
467 if (w)
468 {
469 if (mpi_is_immutable (w))
470 {
471 mpi_immutable_failed ();
472 return;
473 }
474 _gcry_mpi_assign_limb_space (w, u->d, u->alloced);
475 w->nlimbs = u->nlimbs;
476 w->sign = u->sign;
477 w->flags = u->flags;
478 u->alloced = 0;
479 u->nlimbs = 0;
480 u->d = NULL;
481 }
482 _gcry_mpi_free (u);
483 }
484
485
486 gcry_mpi_t
487 _gcry_mpi_set (gcry_mpi_t w, gcry_mpi_t u)
488 {
489 mpi_ptr_t wp, up;
490 mpi_size_t usize = u->nlimbs;
491 int usign = u->sign;
492
493 if (!w)
494 w = _gcry_mpi_alloc( mpi_get_nlimbs(u) );
495 if (mpi_is_immutable (w))
496 {
497 mpi_immutable_failed ();
498 return w;
499 }
500 RESIZE_IF_NEEDED(w, usize);
501 wp = w->d;
502 up = u->d;
503 MPN_COPY( wp, up, usize );
504 w->nlimbs = usize;
505 w->flags = u->flags;
506 w->flags &= ~(16|32); /* Reset the immutable and constant flags. */
507 w->sign = usign;
508 return w;
509 }
510
511 /****************
512 * Set the value of W by the one of U, when SET is 1.
513 * Leave the value when SET is 0.
514 * This implementation should be constant-time regardless of SET.
515 */
516 gcry_mpi_t
517 _gcry_mpi_set_cond (gcry_mpi_t w, const gcry_mpi_t u, unsigned long set)
518 {
519 /* Note: dual mask with AND/OR used for EM leakage mitigation */
520 mpi_limb_t mask1 = ct_limb_gen_mask(set);
521 mpi_limb_t mask2 = ct_limb_gen_inv_mask(set);
522 mpi_size_t i;
523 mpi_size_t nlimbs = u->alloced;
524 mpi_limb_t xu;
525 mpi_limb_t xw;
526 mpi_limb_t *uu = u->d;
527 mpi_limb_t *uw = w->d;
528
529 if (w->alloced != u->alloced)
530 log_bug ("mpi_set_cond: different sizes\n");
531
532 for (i = 0; i < nlimbs; i++)
533 {
534 xu = uu[i];
535 xw = uw[i];
536 uw[i] = (xw & mask2) | (xu & mask1);
537 }
538
539 xu = u->nlimbs;
540 xw = w->nlimbs;
541 w->nlimbs = (xw & mask2) | (xu & mask1);
542
543 xu = u->sign;
544 xw = w->sign;
545 w->sign = (xw & mask2) | (xu & mask1);
546 return w;
547 }
548
549
550 gcry_mpi_t
551 _gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u)
552 {
553 if (!w)
554 w = _gcry_mpi_alloc (1);
555 /* FIXME: If U is 0 we have no need to resize and thus possible
556 allocating the the limbs. */
557 if (mpi_is_immutable (w))
558 {
559 mpi_immutable_failed ();
560 return w;
561 }
562 RESIZE_IF_NEEDED(w, 1);
563 w->d[0] = u;
564 w->nlimbs = u? 1:0;
565 w->sign = 0;
566 w->flags = 0;
567 return w;
568 }
569
570 /* If U is non-negative and small enough store it as an unsigned int
571 * at W. If the value does not fit into an unsigned int or is
572 * negative return GPG_ERR_ERANGE. Note that we return an unsigned
573 * int so that the value can be used with the bit test functions; in
574 * contrast the other _ui functions take an unsigned long so that on
575 * some platforms they may accept a larger value. On error the value
576 * at W is not changed. */
577 gcry_err_code_t
578 _gcry_mpi_get_ui (unsigned int *w, gcry_mpi_t u)
579 {
580 mpi_limb_t x;
581
582 if (u->nlimbs > 1 || u->sign)
583 return GPG_ERR_ERANGE;
584
585 x = (u->nlimbs == 1) ? u->d[0] : 0;
586 if (sizeof (x) > sizeof (unsigned int) && x > MY_UINT_MAX)
587 return GPG_ERR_ERANGE;
588
589 *w = x;
590 return 0;
591 }
592
593
594 gcry_mpi_t
595 _gcry_mpi_alloc_set_ui( unsigned long u)
596 {
597 gcry_mpi_t w = mpi_alloc(1);
598 w->d[0] = u;
599 w->nlimbs = u? 1:0;
600 w->sign = 0;
601 return w;
602 }
603
604 void
605 _gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b)
606 {
607 struct gcry_mpi tmp;
608
609 tmp = *a; *a = *b; *b = tmp;
610 }
611
612
613 /****************
614 * Swap the value of A and B, when SWAP is 1.
615 * Leave the value when SWAP is 0.
616 * This implementation should be constant-time regardless of SWAP.
617 */
618 void
619 _gcry_mpi_swap_cond (gcry_mpi_t a, gcry_mpi_t b, unsigned long swap)
620 {
621 /* Note: dual mask with AND/OR used for EM leakage mitigation */
622 mpi_limb_t mask1 = ct_limb_gen_mask(swap);
623 mpi_limb_t mask2 = ct_limb_gen_inv_mask(swap);
624 mpi_size_t i;
625 mpi_size_t nlimbs;
626 mpi_limb_t *ua = a->d;
627 mpi_limb_t *ub = b->d;
628 mpi_limb_t xa;
629 mpi_limb_t xb;
630
631 if (a->alloced > b->alloced)
632 nlimbs = b->alloced;
633 else
634 nlimbs = a->alloced;
635 if (a->nlimbs > nlimbs || b->nlimbs > nlimbs)
636 log_bug ("mpi_swap_cond: different sizes\n");
637
638 for (i = 0; i < nlimbs; i++)
639 {
640 xa = ua[i];
641 xb = ub[i];
642 ua[i] = (xa & mask2) | (xb & mask1);
643 ub[i] = (xa & mask1) | (xb & mask2);
644 }
645
646 xa = a->nlimbs;
647 xb = b->nlimbs;
648 a->nlimbs = (xa & mask2) | (xb & mask1);
649 b->nlimbs = (xa & mask1) | (xb & mask2);
650
651 xa = a->sign;
652 xb = b->sign;
653 a->sign = (xa & mask2) | (xb & mask1);
654 b->sign = (xa & mask1) | (xb & mask2);
655 }
656
657
658 /****************
659 * Move the MPI value of A from B, when DIR is 1.
660 * Move the MPI value of A to B, when DIR is 0.
661 * This implementation should be constant-time regardless of DIR.
662 *
663 * The word "tfr" comes from the mnemonic of Motorola 6809
664 * instruction, which does "transfer" a register value to another
665 * register. "TFR A,B" means "Transfer A to B".
666 */
667 void
668 _gcry_mpi_tfr (gcry_mpi_t a, gcry_mpi_t b, unsigned long dir)
669 {
670 /* Note: dual mask with AND/OR used for EM leakage mitigation */
671 mpi_limb_t mask1 = ct_limb_gen_mask(dir);
672 mpi_limb_t mask2 = ct_limb_gen_inv_mask(dir);
673 mpi_size_t i;
674 mpi_size_t nlimbs;
675 mpi_limb_t *ua = a->d;
676 mpi_limb_t *ub = b->d;
677 mpi_limb_t xa;
678 mpi_limb_t xb;
679 mpi_limb_t v;
680
681 if (a->alloced > b->alloced)
682 nlimbs = b->alloced;
683 else
684 nlimbs = a->alloced;
685 if (a->nlimbs > nlimbs || b->nlimbs > nlimbs)
686 log_bug ("mpi_c0py: different sizes\n");
687
688 for (i = 0; i < nlimbs; i++)
689 {
690 xa = ua[i];
691 xb = ub[i];
692 v = (xa & mask2) | (xb & mask1);
693 ua[i] = v;
694 ub[i] = v;
695 }
696
697 xa = a->nlimbs;
698 xb = b->nlimbs;
699 v = (xa & mask2) | (xb & mask1);
700 a->nlimbs = v;
701 b->nlimbs = v;
702
703 xa = a->sign;
704 xb = b->sign;
705 v = (xa & mask2) | (xb & mask1);
706 a->sign = v;
707 b->sign = v;
708 }
709
710
711 /****************
712 * Set bit N of A, when SET is 1.
713 * This implementation should be constant-time regardless of SET.
714 */
715 void
716 _gcry_mpi_set_bit_cond (gcry_mpi_t a, unsigned int n, unsigned long set)
717 {
718 unsigned int limbno, bitno;
719 mpi_limb_t set_the_bit = !!set;
720
721 limbno = n / BITS_PER_MPI_LIMB;
722 bitno = n % BITS_PER_MPI_LIMB;
723
724 a->d[limbno] |= (set_the_bit<<bitno);
725 }
726
727
728 gcry_mpi_t
729 _gcry_mpi_new (unsigned int nbits)
730 {
731 return _gcry_mpi_alloc ( (nbits+BITS_PER_MPI_LIMB-1)
732 / BITS_PER_MPI_LIMB );
733 }
734
735
736 gcry_mpi_t
737 _gcry_mpi_snew (unsigned int nbits)
738 {
739 return _gcry_mpi_alloc_secure ( (nbits+BITS_PER_MPI_LIMB-1)
740 / BITS_PER_MPI_LIMB );
741 }
742
743 void
744 _gcry_mpi_release( gcry_mpi_t a )
745 {
746 _gcry_mpi_free( a );
747 }
748
749 void
750 _gcry_mpi_randomize (gcry_mpi_t w,
751 unsigned int nbits, enum gcry_random_level level)
752 {
753 unsigned char *p;
754 size_t nbytes = (nbits+7)/8;
755
756 if (mpi_is_immutable (w))
757 {
758 mpi_immutable_failed ();
759 return;
760 }
761 if (level == GCRY_WEAK_RANDOM)
762 {
763 p = mpi_is_secure(w) ? xmalloc_secure (nbytes)
764 : xmalloc (nbytes);
765 _gcry_create_nonce (p, nbytes);
766 }
767 else
768 {
769 p = mpi_is_secure(w) ? _gcry_random_bytes_secure (nbytes, level)
770 : _gcry_random_bytes (nbytes, level);
771 }
772 _gcry_mpi_set_buffer( w, p, nbytes, 0 );
773 xfree (p);
774 }
775
776
777 void
778 _gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag)
779 {
780 switch (flag)
781 {
782 case GCRYMPI_FLAG_SECURE: mpi_set_secure(a); break;
783 case GCRYMPI_FLAG_CONST: a->flags |= (16|32); break;
784 case GCRYMPI_FLAG_IMMUTABLE: a->flags |= 16; break;
785
786 case GCRYMPI_FLAG_USER1:
787 case GCRYMPI_FLAG_USER2:
788 case GCRYMPI_FLAG_USER3:
789 case GCRYMPI_FLAG_USER4: a->flags |= flag; break;
790
791 case GCRYMPI_FLAG_OPAQUE:
792 default: log_bug("invalid flag value\n");
793 }
794 }
795
796 void
797 _gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag)
798 {
799 (void)a; /* Not yet used. */
800
801 switch (flag)
802 {
803 case GCRYMPI_FLAG_IMMUTABLE:
804 if (!(a->flags & 32))
805 a->flags &= ~16;
806 break;
807
808 case GCRYMPI_FLAG_USER1:
809 case GCRYMPI_FLAG_USER2:
810 case GCRYMPI_FLAG_USER3:
811 case GCRYMPI_FLAG_USER4:
812 a->flags &= ~flag;
813 break;
814
815 case GCRYMPI_FLAG_CONST:
816 case GCRYMPI_FLAG_SECURE:
817 case GCRYMPI_FLAG_OPAQUE:
818 default: log_bug("invalid flag value\n");
819 }
820 }
821
822 int
823 _gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag)
824 {
825 switch (flag)
826 {
827 case GCRYMPI_FLAG_SECURE: return !!(a->flags & 1);
828 case GCRYMPI_FLAG_OPAQUE: return !!(a->flags & 4);
829 case GCRYMPI_FLAG_IMMUTABLE: return !!(a->flags & 16);
830 case GCRYMPI_FLAG_CONST: return !!(a->flags & 32);
831 case GCRYMPI_FLAG_USER1:
832 case GCRYMPI_FLAG_USER2:
833 case GCRYMPI_FLAG_USER3:
834 case GCRYMPI_FLAG_USER4: return !!(a->flags & flag);
835 default: log_bug("invalid flag value\n");
836 }
837 /*NOTREACHED*/
838 return 0;
839 }
840
841
842 /* Return a constant MPI descripbed by NO which is one of the
843 MPI_C_xxx macros. There is no need to copy this returned value; it
844 may be used directly. */
845 gcry_mpi_t
846 _gcry_mpi_const (enum gcry_mpi_constants no)
847 {
848 if ((int)no < 0 || no > MPI_NUMBER_OF_CONSTANTS)
849 log_bug("invalid mpi_const selector %d\n", no);
850 if (!constants[no])
851 log_bug("MPI subsystem not initialized\n");
852 return constants[no];
853 }