mpi_alloc_like( MPI a ) #endif { MPI b; if( a && (a->flags & 4) ) { void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits ) : xmalloc( a->nbits ); memcpy( p, a->d, a->nbits ); b = mpi_set_opaque( NULL, p, a->nbits ); } else if( a ) { #ifdef M_DEBUG b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info ) : mpi_debug_alloc( a->nlimbs, info ); #else b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs ) : mpi_alloc( a->nlimbs ); #endif b->nlimbs = 0; b->sign = 0; b->flags = a->flags; b->nbits = 0; } else b = NULL; return b; }
mpi_copy_gpg( MPI a ) #endif { int i; MPI b; if( a && (a->flags & 4) ) { void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits ) : xmalloc( a->nbits ); memcpy( p, a->d, a->nbits ); b = mpi_set_opaque( NULL, p, a->nbits ); } else if( a ) { #ifdef M_DEBUG b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info ) : mpi_debug_alloc( a->nlimbs, info ); #else b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs ) : mpi_alloc( a->nlimbs ); #endif b->nlimbs = a->nlimbs; b->sign = a->sign; b->flags = a->flags; b->nbits = a->nbits; for(i=0; i < b->nlimbs; i++ ) b->d[i] = a->d[i]; } else b = NULL; return b; }
MPI mpi_set_opaque( MPI a, void *p, unsigned int len ) { if( !a ) { #ifdef M_DEBUG a = mpi_debug_alloc(0,"alloc_opaque"); #else a = mpi_alloc(0); #endif } if( a->flags & 4 ) xfree( a->d ); else { #ifdef M_DEBUG mpi_debug_free_limb_space(a->d, "alloc_opaque"); #else mpi_free_gpg_limb_space(a->d); #endif } a->d = p; a->alloced = 0; a->nlimbs = 0; a->nbits = len; a->flags = 4; return a; }
MPI mpi_alloc_set_ui( unsigned long u) { #ifdef M_DEBUG MPI w = mpi_debug_alloc(1,"alloc_set_ui"); #else MPI w = mpi_alloc(1); #endif w->d[0] = u; w->nlimbs = u? 1:0; w->sign = 0; return w; }
mpi_read(IOBUF inp, unsigned *ret_nread, int secure) #endif { int c, i, j; unsigned nbits, nbytes, nlimbs, nread=0; mpi_limb_t a; MPI val = MPI_NULL; if( (c = iobuf_get(inp)) == -1 ) goto leave; nbits = c << 8; if( (c = iobuf_get(inp)) == -1 ) goto leave; nbits |= c; if( nbits > MAX_EXTERN_MPI_BITS ) { log_error("mpi too large (%u bits)\n", nbits); goto leave; } nread = 2; nbytes = (nbits+7) / 8; nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB; #ifdef M_DEBUG val = secure? mpi_debug_alloc_secure( nlimbs, info ) : mpi_debug_alloc( nlimbs, info ); #else val = secure? mpi_alloc_secure( nlimbs ) : mpi_alloc( nlimbs ); #endif i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; i %= BYTES_PER_MPI_LIMB; val->nbits = nbits; j= val->nlimbs = nlimbs; val->sign = 0; for( ; j > 0; j-- ) { a = 0; for(; i < BYTES_PER_MPI_LIMB; i++ ) { a <<= 8; a |= iobuf_get(inp) & 0xff; nread++; } i = 0; val->d[j-1] = a; } leave: if( nread > *ret_nread ) log_bug("mpi crosses packet border\n"); else *ret_nread = nread; return val; }
mpi_read(IOBUF inp, unsigned *ret_nread, int secure) #endif { int c, i, j; unsigned int nmax = *ret_nread; unsigned nbits, nbytes, nlimbs, nread=0; mpi_limb_t a; MPI val = NULL; if (nread == nmax) goto overflow; if( (c = iobuf_get(inp)) == -1 ) goto leave; nread++; nbits = c << 8; if (nread == nmax) goto overflow; if( (c = iobuf_get(inp)) == -1 ) goto leave; nread++; nbits |= c; if( nbits > MAX_EXTERN_MPI_BITS ) { log_error("mpi too large for this implementation (%u bits)\n", nbits); goto leave; } nbytes = (nbits+7) / 8; nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB; #ifdef M_DEBUG val = secure? mpi_debug_alloc_secure( nlimbs, info ) : mpi_debug_alloc( nlimbs, info ); #else val = secure? mpi_alloc_secure( nlimbs ) : mpi_alloc( nlimbs ); #endif i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; i %= BYTES_PER_MPI_LIMB; val->nbits = nbits; j= val->nlimbs = nlimbs; val->sign = 0; for( ; j > 0; j-- ) { a = 0; for(; i < BYTES_PER_MPI_LIMB; i++ ) { if (nread == nmax) { #ifdef M_DEBUG mpi_debug_free (val); #else mpi_free (val); #endif val = NULL; goto overflow; } a <<= 8; a |= iobuf_get(inp) & 0xff; nread++; } i = 0; val->d[j-1] = a; } leave: *ret_nread = nread; return val; overflow: log_error ("mpi larger than indicated length (%u bytes)\n", nmax); *ret_nread = nread; return val; }