-
+ D98F0266D7C1F2466393E4951BABE3A7289CF8133821FBAF3BD139383AA5E14FC1CDAB87C6329AB6790B250CE9F141B2B7F935804EC43BC5A4933A2932A8178B
smg_comms/mpi/mpiutil.c
(0 . 0)(1 . 517)
7337 /* mpiutil.ac - Utility functions for MPI
7338 * Modified by No Such Labs. (C) 2015. See README.
7339 * Modified by S.MG, 2018. Added mpi_get_alloced(MPI a)
7340 *
7341 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
7342 * SHA256(gnupg-1.4.10.tar.gz):
7343 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
7344 * (C) 1994-2005 Free Software Foundation, Inc.
7345 *
7346 * This program is free software: you can redistribute it and/or modify
7347 * it under the terms of the GNU General Public License as published by
7348 * the Free Software Foundation, either version 3 of the License, or
7349 * (at your option) any later version.
7350 *
7351 * This program is distributed in the hope that it will be useful,
7352 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7353 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7354 * GNU General Public License for more details.
7355 *
7356 * You should have received a copy of the GNU General Public License
7357 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7358 */
7359
7360 #include <stdio.h>
7361 #include <stdlib.h>
7362 #include <string.h>
7363 #include <assert.h>
7364
7365 #include "knobs.h"
7366 #include "mpi.h"
7367 #include "mpi-internal.h"
7368 #include "memory.h"
7369 #include "util.h"
7370
7371
7372 #ifdef M_DEBUG
7373 #undef mpi_alloc
7374 #undef mpi_alloc_secure
7375 #undef mpi_free
7376 #endif
7377
7378 /****************
7379 * Note: It was a bad idea to use the number of limbs to allocate
7380 * because on a alpha the limbs are large but we normally need
7381 * integers of n bits - So we should chnage this to bits (or bytes).
7382 *
7383 * But mpi_alloc is used in a lot of places :-)
7384 */
7385 MPI
7386 #ifdef M_DEBUG
7387 mpi_debug_alloc( unsigned nlimbs, const char *info )
7388 #else
7389 mpi_alloc( unsigned nlimbs )
7390 #endif
7391 {
7392 MPI a;
7393
7394 if( DBG_MEMORY )
7395 log_debug("mpi_alloc(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
7396 #ifdef M_DEBUG
7397 a = m_debug_alloc( sizeof *a, info );
7398 a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 0, info ) : NULL;
7399 #else
7400 a = xmalloc( sizeof *a );
7401 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL;
7402 #endif
7403 a->alloced = nlimbs;
7404 a->nlimbs = 0;
7405 a->sign = 0;
7406 a->flags = 0;
7407 a->nbits = 0;
7408 return a;
7409 }
7410
7411 void
7412 mpi_m_check( MPI a )
7413 {
7414 m_check(a);
7415 m_check(a->d);
7416 }
7417
7418 MPI
7419 #ifdef M_DEBUG
7420 mpi_debug_alloc_secure( unsigned nlimbs, const char *info )
7421 #else
7422 mpi_alloc_secure( unsigned nlimbs )
7423 #endif
7424 {
7425 MPI a;
7426
7427 if( DBG_MEMORY )
7428 log_debug("mpi_alloc_secure(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
7429 #ifdef M_DEBUG
7430 a = m_debug_alloc( sizeof *a, info );
7431 a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 1, info ) : NULL;
7432 #else
7433 a = xmalloc( sizeof *a );
7434 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL;
7435 #endif
7436 a->alloced = nlimbs;
7437 a->flags = 1;
7438 a->nlimbs = 0;
7439 a->sign = 0;
7440 a->nbits = 0;
7441 return a;
7442 }
7443
7444
7445 #if 0
7446 static void *unused_limbs_5;
7447 static void *unused_limbs_32;
7448 static void *unused_limbs_64;
7449 #endif
7450
7451 mpi_ptr_t
7452 #ifdef M_DEBUG
7453 mpi_debug_alloc_limb_space( unsigned nlimbs, int secure, const char *info )
7454 #else
7455 mpi_alloc_limb_space( unsigned nlimbs, int secure )
7456 #endif
7457 {
7458 size_t len = nlimbs * sizeof(mpi_limb_t);
7459 mpi_ptr_t p;
7460
7461 if( DBG_MEMORY )
7462 log_debug("mpi_alloc_limb_space(%u)\n", (unsigned)len*8 );
7463 #if 0
7464 if( !secure ) {
7465 if( nlimbs == 5 && unused_limbs_5 ) { /* DSA 160 bits */
7466 p = unused_limbs_5;
7467 unused_limbs_5 = *p;
7468 return p;
7469 }
7470 else if( nlimbs == 32 && unused_limbs_32 ) { /* DSA 1024 bits */
7471 p = unused_limbs_32;
7472 unused_limbs_32 = *p;
7473 return p;
7474 }
7475 else if( nlimbs == 64 && unused_limbs_64 ) { /* DSA 2*1024 bits */
7476 p = unused_limbs_64;
7477 unused_limbs_64 = *p;
7478 return p;
7479 }
7480 }
7481 #endif
7482
7483 #ifdef M_DEBUG
7484 p = secure? m_debug_alloc_secure(len, info):m_debug_alloc( len, info );
7485 #else
7486 p = secure? xmalloc_secure( len ):xmalloc( len );
7487 #endif
7488
7489 return p;
7490 }
7491
7492 void
7493 #ifdef M_DEBUG
7494 mpi_debug_free_limb_space( mpi_ptr_t a, const char *info )
7495 #else
7496 mpi_free_limb_space( mpi_ptr_t a )
7497 #endif
7498 {
7499 if( !a )
7500 return;
7501 if( DBG_MEMORY )
7502 log_debug("mpi_free_limb_space of size %lu\n", (ulong)m_size(a)*8 );
7503
7504 #if 0
7505 if( !m_is_secure(a) ) {
7506 size_t nlimbs = m_size(a) / 4 ;
7507 void *p = a;
7508
7509 if( nlimbs == 5 ) { /* DSA 160 bits */
7510 *a = unused_limbs_5;
7511 unused_limbs_5 = a;
7512 return;
7513 }
7514 else if( nlimbs == 32 ) { /* DSA 1024 bits */
7515 *a = unused_limbs_32;
7516 unused_limbs_32 = a;
7517 return;
7518 }
7519 else if( nlimbs == 64 ) { /* DSA 2*1024 bits */
7520 *a = unused_limbs_64;
7521 unused_limbs_64 = a;
7522 return;
7523 }
7524 }
7525 #endif
7526
7527 xfree(a);
7528 }
7529
7530
7531 void
7532 mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs )
7533 {
7534 mpi_free_limb_space(a->d);
7535 a->d = ap;
7536 a->alloced = nlimbs;
7537 }
7538
7539
7540
7541 /****************
7542 * Resize the array of A to NLIMBS. the additional space is cleared
7543 * (set to 0) [done by xrealloc()]
7544 */
7545 void
7546 #ifdef M_DEBUG
7547 mpi_debug_resize( MPI a, unsigned nlimbs, const char *info )
7548 #else
7549 mpi_resize( MPI a, unsigned nlimbs )
7550 #endif
7551 {
7552 if( nlimbs <= a->alloced )
7553 return; /* no need to do it */
7554 /* Note: a->secure is not used - instead the realloc functions
7555 * take care of it. Maybe we should drop a->secure completely
7556 * and rely on a mpi_is_secure function, which would be
7557 * a wrapper around m_is_secure
7558 */
7559 #ifdef M_DEBUG
7560 if( a->d )
7561 a->d = m_debug_realloc(a->d, nlimbs * sizeof(mpi_limb_t), info );
7562 else
7563 a->d = m_debug_alloc_clear( nlimbs * sizeof(mpi_limb_t), info );
7564 #else
7565 if( a->d )
7566 a->d = xrealloc(a->d, nlimbs * sizeof(mpi_limb_t) );
7567 else
7568 a->d = xmalloc_clear( nlimbs * sizeof(mpi_limb_t) );
7569 #endif
7570 a->alloced = nlimbs;
7571 }
7572
7573 void
7574 mpi_clear( MPI a )
7575 {
7576 a->nlimbs = 0;
7577 a->nbits = 0;
7578 a->flags = 0;
7579 }
7580
7581
7582 void
7583 #ifdef M_DEBUG
7584 mpi_debug_free( MPI a, const char *info )
7585 #else
7586 mpi_free( MPI a )
7587 #endif
7588 {
7589 if( !a )
7590 return;
7591 if( DBG_MEMORY )
7592 log_debug("mpi_free\n" );
7593 if( a->flags & 4 )
7594 xfree( a->d );
7595 else {
7596 #ifdef M_DEBUG
7597 mpi_debug_free_limb_space(a->d, info);
7598 #else
7599 mpi_free_limb_space(a->d);
7600 #endif
7601 }
7602 if( a->flags & ~7 )
7603 log_bug("invalid flag value in mpi\n");
7604 xfree(a);
7605 }
7606
7607
7608 void
7609 mpi_set_secure( MPI a )
7610 {
7611 mpi_ptr_t ap, bp;
7612
7613 if( (a->flags & 1) )
7614 return;
7615 a->flags |= 1;
7616 ap = a->d;
7617 if( !a->nlimbs ) {
7618 assert(!ap);
7619 return;
7620 }
7621 #ifdef M_DEBUG
7622 bp = mpi_debug_alloc_limb_space( a->nlimbs, 1, "set_secure" );
7623 #else
7624 bp = mpi_alloc_limb_space( a->nlimbs, 1 );
7625 #endif
7626 MPN_COPY( bp, ap, a->nlimbs );
7627 a->d = bp;
7628 #ifdef M_DEBUG
7629 mpi_debug_free_limb_space(ap, "set_secure");
7630 #else
7631 mpi_free_limb_space(ap);
7632 #endif
7633 }
7634
7635
7636 MPI
7637 mpi_set_opaque( MPI a, void *p, unsigned int len )
7638 {
7639 if( !a ) {
7640 #ifdef M_DEBUG
7641 a = mpi_debug_alloc(0,"alloc_opaque");
7642 #else
7643 a = mpi_alloc(0);
7644 #endif
7645 }
7646
7647 if( a->flags & 4 )
7648 xfree( a->d );
7649 else {
7650 #ifdef M_DEBUG
7651 mpi_debug_free_limb_space(a->d, "alloc_opaque");
7652 #else
7653 mpi_free_limb_space(a->d);
7654 #endif
7655 }
7656
7657 a->d = p;
7658 a->alloced = 0;
7659 a->nlimbs = 0;
7660 a->nbits = len;
7661 a->flags = 4;
7662 return a;
7663 }
7664
7665
7666 void *
7667 mpi_get_opaque( MPI a, unsigned int *len )
7668 {
7669 if( !(a->flags & 4) )
7670 log_bug("mpi_get_opaque on normal mpi\n");
7671 if( len )
7672 *len = a->nbits;
7673 return a->d;
7674 }
7675
7676
7677 /****************
7678 * Note: This copy function should not interpret the MPI
7679 * but copy it transparently.
7680 */
7681 MPI
7682 #ifdef M_DEBUG
7683 mpi_debug_copy( MPI a, const char *info )
7684 #else
7685 mpi_copy( MPI a )
7686 #endif
7687 {
7688 int i;
7689 MPI b;
7690
7691 if( a && (a->flags & 4) ) {
7692 void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits )
7693 : xmalloc( a->nbits );
7694 memcpy( p, a->d, a->nbits );
7695 b = mpi_set_opaque( NULL, p, a->nbits );
7696 }
7697 else if( a ) {
7698 #ifdef M_DEBUG
7699 b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info )
7700 : mpi_debug_alloc( a->nlimbs, info );
7701 #else
7702 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
7703 : mpi_alloc( a->nlimbs );
7704 #endif
7705 b->nlimbs = a->nlimbs;
7706 b->sign = a->sign;
7707 b->flags = a->flags;
7708 b->nbits = a->nbits;
7709 for(i=0; i < b->nlimbs; i++ )
7710 b->d[i] = a->d[i];
7711 }
7712 else
7713 b = NULL;
7714 return b;
7715 }
7716
7717
7718 /****************
7719 * This function allocates an MPI which is optimized to hold
7720 * a value as large as the one given in the argument and allocates it
7721 * with the same flags as A.
7722 */
7723 MPI
7724 #ifdef M_DEBUG
7725 mpi_debug_alloc_like( MPI a, const char *info )
7726 #else
7727 mpi_alloc_like( MPI a )
7728 #endif
7729 {
7730 MPI b;
7731
7732 if( a && (a->flags & 4) ) {
7733 void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits )
7734 : xmalloc( a->nbits );
7735 memcpy( p, a->d, a->nbits );
7736 b = mpi_set_opaque( NULL, p, a->nbits );
7737 }
7738 else if( a ) {
7739 #ifdef M_DEBUG
7740 b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info )
7741 : mpi_debug_alloc( a->nlimbs, info );
7742 #else
7743 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
7744 : mpi_alloc( a->nlimbs );
7745 #endif
7746 b->nlimbs = 0;
7747 b->sign = 0;
7748 b->flags = a->flags;
7749 b->nbits = 0;
7750 }
7751 else
7752 b = NULL;
7753 return b;
7754 }
7755
7756
7757 void
7758 mpi_set( MPI w, MPI u)
7759 {
7760 mpi_ptr_t wp, up;
7761 mpi_size_t usize = u->nlimbs;
7762 int usign = u->sign;
7763
7764 RESIZE_IF_NEEDED(w, usize);
7765 wp = w->d;
7766 up = u->d;
7767 MPN_COPY( wp, up, usize );
7768 w->nlimbs = usize;
7769 w->nbits = u->nbits;
7770 w->flags = u->flags;
7771 w->sign = usign;
7772 }
7773
7774
7775 void
7776 mpi_set_ui( MPI w, unsigned long u)
7777 {
7778 RESIZE_IF_NEEDED(w, 1);
7779 w->d[0] = u;
7780 w->nlimbs = u? 1:0;
7781 w->sign = 0;
7782 w->nbits = 0;
7783 w->flags = 0;
7784 }
7785
7786
7787 MPI
7788 mpi_alloc_set_ui( unsigned long u)
7789 {
7790 #ifdef M_DEBUG
7791 MPI w = mpi_debug_alloc(1,"alloc_set_ui");
7792 #else
7793 MPI w = mpi_alloc(1);
7794 #endif
7795 w->d[0] = u;
7796 w->nlimbs = u? 1:0;
7797 w->sign = 0;
7798 return w;
7799 }
7800
7801
7802 void
7803 mpi_swap( MPI a, MPI b)
7804 {
7805 struct gcry_mpi tmp;
7806
7807 tmp = *a; *a = *b; *b = tmp;
7808 }
7809
7810
7811 int
7812 mpi_get_nlimbs (MPI a)
7813 {
7814 return a->nlimbs;
7815 }
7816
7817 /*
7818 * Returns the allocated space for the given MPI, as number of limbs.
7819 */
7820 int
7821 mpi_get_alloced (MPI a)
7822 {
7823 return a->alloced;
7824 }
7825
7826 int
7827 mpi_is_neg (MPI a)
7828 {
7829 return a->sign;
7830 }
7831
7832
7833 /* Return the number of limbs to store an MPI which is specified by
7834 the number of bytes to represent it. */
7835 unsigned int
7836 mpi_nlimb_hint_from_nbytes (unsigned int nbytes)
7837 {
7838 return (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
7839 }
7840
7841 /* Return the number of limbs to store an MPI which is specified by
7842 the number of bytes to represent it. */
7843 unsigned int
7844 mpi_nlimb_hint_from_nbits (unsigned int nbits)
7845 {
7846 return (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB;
7847 }
7848
7849 unsigned int
7850 mpi_get_flags (MPI a)
7851 {
7852 return a->flags;
7853 }