static void initialize(void) { initialize_basics (); /* The data buffer is allocated somewhat larger, so that we can use this extra space (which is allocated in secure memory) as a temporary hash buffer */ rndpool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN) : gcry_xcalloc(1,POOLSIZE+BLOCKLEN); keypool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN) : gcry_xcalloc(1,POOLSIZE+BLOCKLEN); is_initialized = 1; }
/**************** * Resize the array of A to NLIMBS. The additional space is cleared * (set to 0). */ void _gcry_mpi_resize (gcry_mpi_t a, unsigned nlimbs) { size_t i; if (nlimbs <= a->alloced) { /* We only need to clear the new space (this is a nop if the limb space is already of the correct size. */ for (i=a->nlimbs; i < a->alloced; i++) a->d[i] = 0; return; } /* Actually resize the limb space. */ if (a->d) { a->d = gcry_xrealloc (a->d, nlimbs * sizeof (mpi_limb_t)); for (i=a->alloced; i < nlimbs; i++) a->d[i] = 0; } else { if (a->flags & 1) /* Secure memory is wanted. */ a->d = gcry_xcalloc_secure (nlimbs , sizeof (mpi_limb_t)); else /* Standard memory. */ a->d = gcry_xcalloc (nlimbs , sizeof (mpi_limb_t)); } a->alloced = nlimbs; }
void * _gsti_xcalloc (size_t n, size_t m) { void *p = gcry_xcalloc (n, m); if (!p) out_of_core (); return p; }
/* This function returns a new context for elliptic curve based on the field GF(p). P is the prime specifying this field, A is the first coefficient. This function is only used within Libgcrypt and not part of the public API. This context needs to be released using _gcry_mpi_ec_free. */ mpi_ec_t _gcry_mpi_ec_p_internal_new (gcry_mpi_t p, gcry_mpi_t a) { mpi_ec_t ctx; ctx = gcry_xcalloc (1, sizeof *ctx); ec_p_init (ctx, p, a); return ctx; }
KBNODE make_comment_node_from_buffer (const char *s, size_t n) { PACKET *pkt; pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_COMMENT; pkt->pkt.comment = gcry_xmalloc( sizeof *pkt->pkt.comment + n - 1 ); pkt->pkt.comment->len = n; strcpy(pkt->pkt.comment->data, s); return new_kbnode( pkt ); }
/* Full initialization of this module. */ static void initialize(void) { /* Although the basic initialization should have happened already, we call it here to make sure that all prerequisites are met. */ initialize_basics (); /* Now we can look the pool and complete the initialization if necessary. */ lock_pool (); if (!rndpool) { /* The data buffer is allocated somewhat larger, so that we can use this extra space (which is allocated in secure memory) as a temporary hash buffer */ rndpool = (secure_alloc ? gcry_xcalloc_secure (1, POOLSIZE + BLOCKLEN) : gcry_xcalloc (1, POOLSIZE + BLOCKLEN)); keypool = (secure_alloc ? gcry_xcalloc_secure (1, POOLSIZE + BLOCKLEN) : gcry_xcalloc (1, POOLSIZE + BLOCKLEN)); /* Setup the slow entropy gathering function. The code requires that this function exists. */ slow_gather_fnc = getfnc_gather_random (); if (!slow_gather_fnc) { faked_rng = 1; slow_gather_fnc = gather_faked; } /* Setup the fast entropy gathering function. */ fast_gather_fnc = getfnc_fast_random_poll (); } unlock_pool (); }
/**************** * Resize the array of A to NLIMBS. the additional space is cleared * (set to 0) [done by gcry_realloc()] */ void _gcry_mpi_resize (gcry_mpi_t a, unsigned nlimbs) { if (nlimbs <= a->alloced) return; /* no need to do it */ if (a->d) a->d = gcry_xrealloc (a->d, nlimbs * sizeof (mpi_limb_t)); else { if (a->flags & 1) /* Secure memory is wanted. */ a->d = gcry_xcalloc_secure (nlimbs , sizeof (mpi_limb_t)); else /* Standard memory. */ a->d = gcry_xcalloc (nlimbs , sizeof (mpi_limb_t)); } a->alloced = nlimbs; }
/**************** * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M */ void _gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t m) { int k; /* number of elements */ int t; /* bit size of largest exponent */ int i, j, idx; gcry_mpi_t *G; /* table with precomputed values of size 2^k */ gcry_mpi_t tmp; #ifdef USE_BARRETT gcry_mpi_t barrett_y, barrett_r1, barrett_r2; int barrett_k; #endif for(k=0; basearray[k]; k++ ) ; gcry_assert(k); for(t=0, i=0; (tmp=exparray[i]); i++ ) { /*log_mpidump("exp: ", tmp );*/ j = mpi_get_nbits(tmp); if( j > t ) t = j; } /*log_mpidump("mod: ", m );*/ gcry_assert (i==k); gcry_assert (t); gcry_assert (k < 10); G = gcry_xcalloc( (1<<k) , sizeof *G ); #ifdef USE_BARRETT barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 ); #endif /* and calculate */ tmp = mpi_alloc( mpi_get_nlimbs(m)+1 ); mpi_set_ui( res, 1 ); for(i = 1; i <= t; i++ ) { barrett_mulm(tmp, res, res, m, barrett_y, barrett_k, barrett_r1, barrett_r2 ); idx = build_index( exparray, k, i, t ); gcry_assert (idx >= 0 && idx < (1<<k)); if( !G[idx] ) { if( !idx ) G[0] = mpi_alloc_set_ui( 1 ); else { for(j=0; j < k; j++ ) { if( (idx & (1<<j) ) ) { if( !G[idx] ) G[idx] = mpi_copy( basearray[j] ); else barrett_mulm( G[idx], G[idx], basearray[j], m, barrett_y, barrett_k, barrett_r1, barrett_r2 ); } } if( !G[idx] ) G[idx] = mpi_alloc(0); } } barrett_mulm(res, tmp, G[idx], m, barrett_y, barrett_k, barrett_r1, barrett_r2 ); } /* cleanup */ mpi_free(tmp); #ifdef USE_BARRETT mpi_free(barrett_y); mpi_free(barrett_r1); mpi_free(barrett_r2); #endif for(i=0; i < (1<<k); i++ ) mpi_free(G[i]); gcry_free(G); }