4272N/A/*
4272N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4272N/A * Use is subject to license terms.
1674N/A *
4272N/A * This library is free software; you can redistribute it and/or
4272N/A * modify it under the terms of the GNU Lesser General Public
4272N/A * License as published by the Free Software Foundation; either
4272N/A * version 2.1 of the License, or (at your option) any later version.
1674N/A *
4272N/A * This library is distributed in the hope that it will be useful,
4272N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
4272N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4272N/A * Lesser General Public License for more details.
1674N/A *
4272N/A * You should have received a copy of the GNU Lesser General Public License
4272N/A * along with this library; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1674N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
4272N/A
4272N/A/* *********************************************************************
1674N/A *
1674N/A * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
1674N/A *
1674N/A * The Initial Developer of the Original Code is
1674N/A * Michael J. Fromberger.
1674N/A * Portions created by the Initial Developer are Copyright (C) 1998
1674N/A * the Initial Developer. All Rights Reserved.
1674N/A *
1674N/A * Contributor(s):
1674N/A * Netscape Communications Corporation
1674N/A *
4272N/A *********************************************************************** */
4272N/A
4272N/A/* Arbitrary precision integer arithmetic library
1674N/A *
4272N/A * NOTE WELL: the content of this header file is NOT part of the "public"
4272N/A * API for the MPI library, and may change at any time.
4272N/A * Application programs that use libmpi should NOT include this header file.
1674N/A */
1674N/A
1674N/A#ifndef _MPI_PRIV_H
1674N/A#define _MPI_PRIV_H
1674N/A
1674N/A/* $Id: mpi-priv.h,v 1.20 2005/11/22 07:16:43 relyea%netscape.com Exp $ */
1674N/A
1674N/A#include "mpi.h"
1674N/A#ifndef _KERNEL
1674N/A#include <stdlib.h>
1674N/A#include <string.h>
1674N/A#include <ctype.h>
1674N/A#endif /* _KERNEL */
1674N/A
1674N/A#if MP_DEBUG
1674N/A#include <stdio.h>
1674N/A
1674N/A#define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);}
1674N/A#else
1674N/A#define DIAG(T,V)
1674N/A#endif
1674N/A
1674N/A/* If we aren't using a wired-in logarithm table, we need to include
1674N/A the math library to get the log() function
1674N/A */
1674N/A
1674N/A/* {{{ s_logv_2[] - log table for 2 in various bases */
1674N/A
1674N/A#if MP_LOGTAB
1674N/A/*
1674N/A A table of the logs of 2 for various bases (the 0 and 1 entries of
1674N/A this table are meaningless and should not be referenced).
1674N/A
1674N/A This table is used to compute output lengths for the mp_toradix()
1674N/A function. Since a number n in radix r takes up about log_r(n)
1674N/A digits, we estimate the output size by taking the least integer
1674N/A greater than log_r(n), where:
1674N/A
1674N/A log_r(n) = log_2(n) * log_r(2)
1674N/A
1674N/A This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
1674N/A which are the output bases supported.
1674N/A */
1674N/A
1674N/Aextern const float s_logv_2[];
1674N/A#define LOG_V_2(R) s_logv_2[(R)]
1674N/A
1674N/A#else
1674N/A
1674N/A/*
1674N/A If MP_LOGTAB is not defined, use the math library to compute the
1674N/A logarithms on the fly. Otherwise, use the table.
1674N/A Pick which works best for your system.
1674N/A */
1674N/A
1674N/A#include <math.h>
1674N/A#define LOG_V_2(R) (log(2.0)/log(R))
1674N/A
1674N/A#endif /* if MP_LOGTAB */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Digit arithmetic macros */
1674N/A
1674N/A/*
1674N/A When adding and multiplying digits, the results can be larger than
1674N/A can be contained in an mp_digit. Thus, an mp_word is used. These
1674N/A macros mask off the upper and lower digits of the mp_word (the
1674N/A mp_word may be more than 2 mp_digits wide, but we only concern
1674N/A ourselves with the low-order 2 mp_digits)
1674N/A */
1674N/A
1674N/A#define CARRYOUT(W) (mp_digit)((W)>>DIGIT_BIT)
1674N/A#define ACCUM(W) (mp_digit)(W)
1674N/A
1674N/A#define MP_MIN(a,b) (((a) < (b)) ? (a) : (b))
1674N/A#define MP_MAX(a,b) (((a) > (b)) ? (a) : (b))
1674N/A#define MP_HOWMANY(a,b) (((a) + (b) - 1)/(b))
1674N/A#define MP_ROUNDUP(a,b) (MP_HOWMANY(a,b) * (b))
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Comparison constants */
1674N/A
1674N/A#define MP_LT -1
1674N/A#define MP_EQ 0
1674N/A#define MP_GT 1
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ private function declarations */
1674N/A
1674N/A/*
1674N/A If MP_MACRO is false, these will be defined as actual functions;
1674N/A otherwise, suitable macro definitions will be used. This works
1674N/A around the fact that ANSI C89 doesn't support an 'inline' keyword
1674N/A (although I hear C9x will ... about bloody time). At present, the
1674N/A macro definitions are identical to the function bodies, but they'll
1674N/A expand in place, instead of generating a function call.
1674N/A
1674N/A I chose these particular functions to be made into macros because
1674N/A some profiling showed they are called a lot on a typical workload,
1674N/A and yet they are primarily housekeeping.
1674N/A */
1674N/A#if MP_MACRO == 0
1674N/A void s_mp_setz(mp_digit *dp, mp_size count); /* zero digits */
1674N/A void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
1674N/A void *s_mp_alloc(size_t nb, size_t ni, int flag); /* general allocator */
1674N/A void s_mp_free(void *ptr, mp_size); /* general free function */
1674N/Aextern unsigned long mp_allocs;
1674N/Aextern unsigned long mp_frees;
1674N/Aextern unsigned long mp_copies;
1674N/A#else
1674N/A
1674N/A /* Even if these are defined as macros, we need to respect the settings
1674N/A of the MP_MEMSET and MP_MEMCPY configuration options...
1674N/A */
1674N/A #if MP_MEMSET == 0
1674N/A #define s_mp_setz(dp, count) \
1674N/A {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}
1674N/A #else
1674N/A #define s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))
1674N/A #endif /* MP_MEMSET */
1674N/A
1674N/A #if MP_MEMCPY == 0
1674N/A #define s_mp_copy(sp, dp, count) \
1674N/A {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}
1674N/A #else
1674N/A #define s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))
1674N/A #endif /* MP_MEMCPY */
1674N/A
1674N/A #define s_mp_alloc(nb, ni) calloc(nb, ni)
1674N/A #define s_mp_free(ptr) {if(ptr) free(ptr);}
1674N/A#endif /* MP_MACRO */
1674N/A
1674N/Amp_err s_mp_grow(mp_int *mp, mp_size min); /* increase allocated size */
1674N/Amp_err s_mp_pad(mp_int *mp, mp_size min); /* left pad with zeroes */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A void s_mp_clamp(mp_int *mp); /* clip leading zeroes */
1674N/A#else
1674N/A #define s_mp_clamp(mp)\
1674N/A { mp_size used = MP_USED(mp); \
1674N/A while (used > 1 && DIGIT(mp, used - 1) == 0) --used; \
1674N/A MP_USED(mp) = used; \
1674N/A }
1674N/A#endif /* MP_MACRO */
1674N/A
1674N/Avoid s_mp_exch(mp_int *a, mp_int *b); /* swap a and b in place */
1674N/A
1674N/Amp_err s_mp_lshd(mp_int *mp, mp_size p); /* left-shift by p digits */
1674N/Avoid s_mp_rshd(mp_int *mp, mp_size p); /* right-shift by p digits */
1674N/Amp_err s_mp_mul_2d(mp_int *mp, mp_digit d); /* multiply by 2^d in place */
1674N/Avoid s_mp_div_2d(mp_int *mp, mp_digit d); /* divide by 2^d in place */
1674N/Avoid s_mp_mod_2d(mp_int *mp, mp_digit d); /* modulo 2^d in place */
1674N/Avoid s_mp_div_2(mp_int *mp); /* divide by 2 in place */
1674N/Amp_err s_mp_mul_2(mp_int *mp); /* multiply by 2 in place */
1674N/Amp_err s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd);
1674N/A /* normalize for division */
1674N/Amp_err s_mp_add_d(mp_int *mp, mp_digit d); /* unsigned digit addition */
1674N/Amp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */
1674N/Amp_err s_mp_mul_d(mp_int *mp, mp_digit d); /* unsigned digit multiply */
1674N/Amp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r);
1674N/A /* unsigned digit divide */
1674N/Amp_err s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
1674N/A /* Barrett reduction */
1674N/Amp_err s_mp_add(mp_int *a, const mp_int *b); /* magnitude addition */
1674N/Amp_err s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c);
1674N/Amp_err s_mp_sub(mp_int *a, const mp_int *b); /* magnitude subtract */
1674N/Amp_err s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c);
1674N/Amp_err s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset);
1674N/A /* a += b * RADIX^offset */
1674N/Amp_err s_mp_mul(mp_int *a, const mp_int *b); /* magnitude multiply */
1674N/A#if MP_SQUARE
1674N/Amp_err s_mp_sqr(mp_int *a); /* magnitude square */
1674N/A#else
1674N/A#define s_mp_sqr(a) s_mp_mul(a, a)
1674N/A#endif
1674N/Amp_err s_mp_div(mp_int *rem, mp_int *div, mp_int *quot); /* magnitude div */
1674N/Amp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
1674N/Amp_err s_mp_2expt(mp_int *a, mp_digit k); /* a = 2^k */
1674N/Aint s_mp_cmp(const mp_int *a, const mp_int *b); /* magnitude comparison */
1674N/Aint s_mp_cmp_d(const mp_int *a, mp_digit d); /* magnitude digit compare */
1674N/Aint s_mp_ispow2(const mp_int *v); /* is v a power of 2? */
1674N/Aint s_mp_ispow2d(mp_digit d); /* is d a power of 2? */
1674N/A
1674N/Aint s_mp_tovalue(char ch, int r); /* convert ch to value */
1674N/Achar s_mp_todigit(mp_digit val, int r, int low); /* convert val to digit */
1674N/Aint s_mp_outlen(int bits, int r); /* output length in bytes */
1674N/Amp_digit s_mp_invmod_radix(mp_digit P); /* returns (P ** -1) mod RADIX */
1674N/Amp_err s_mp_invmod_odd_m( const mp_int *a, const mp_int *m, mp_int *c);
1674N/Amp_err s_mp_invmod_2d( const mp_int *a, mp_size k, mp_int *c);
1674N/Amp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c);
1674N/A
1674N/A#ifdef NSS_USE_COMBA
1674N/A
1674N/A#define IS_POWER_OF_2(a) ((a) && !((a) & ((a)-1)))
1674N/A
1674N/Avoid s_mp_mul_comba_4(const mp_int *A, const mp_int *B, mp_int *C);
1674N/Avoid s_mp_mul_comba_8(const mp_int *A, const mp_int *B, mp_int *C);
1674N/Avoid s_mp_mul_comba_16(const mp_int *A, const mp_int *B, mp_int *C);
1674N/Avoid s_mp_mul_comba_32(const mp_int *A, const mp_int *B, mp_int *C);
1674N/A
1674N/Avoid s_mp_sqr_comba_4(const mp_int *A, mp_int *B);
1674N/Avoid s_mp_sqr_comba_8(const mp_int *A, mp_int *B);
1674N/Avoid s_mp_sqr_comba_16(const mp_int *A, mp_int *B);
1674N/Avoid s_mp_sqr_comba_32(const mp_int *A, mp_int *B);
1674N/A
1674N/A#endif /* end NSS_USE_COMBA */
1674N/A
1674N/A/* ------ mpv functions, operate on arrays of digits, not on mp_int's ------ */
1674N/A#if defined (__OS2__) && defined (__IBMC__)
1674N/A#define MPI_ASM_DECL __cdecl
1674N/A#else
1674N/A#define MPI_ASM_DECL
1674N/A#endif
1674N/A
1674N/A#ifdef MPI_AMD64
1674N/A
1674N/Amp_digit MPI_ASM_DECL s_mpv_mul_set_vec64(mp_digit*, mp_digit *, mp_size, mp_digit);
1674N/Amp_digit MPI_ASM_DECL s_mpv_mul_add_vec64(mp_digit*, const mp_digit*, mp_size, mp_digit);
1674N/A
1674N/A/* c = a * b */
1674N/A#define s_mpv_mul_d(a, a_len, b, c) \
1674N/A ((unsigned long*)c)[a_len] = s_mpv_mul_set_vec64(c, a, a_len, b)
1674N/A
1674N/A/* c += a * b */
1674N/A#define s_mpv_mul_d_add(a, a_len, b, c) \
1674N/A ((unsigned long*)c)[a_len] = s_mpv_mul_add_vec64(c, a, a_len, b)
1674N/A
1674N/A#else
1674N/A
1674N/Avoid MPI_ASM_DECL s_mpv_mul_d(const mp_digit *a, mp_size a_len,
1674N/A mp_digit b, mp_digit *c);
1674N/Avoid MPI_ASM_DECL s_mpv_mul_d_add(const mp_digit *a, mp_size a_len,
1674N/A mp_digit b, mp_digit *c);
1674N/A
1674N/A#endif
1674N/A
1674N/Avoid MPI_ASM_DECL s_mpv_mul_d_add_prop(const mp_digit *a,
1674N/A mp_size a_len, mp_digit b,
1674N/A mp_digit *c);
1674N/Avoid MPI_ASM_DECL s_mpv_sqr_add_prop(const mp_digit *a,
1674N/A mp_size a_len,
1674N/A mp_digit *sqrs);
1674N/A
1674N/Amp_err MPI_ASM_DECL s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo,
1674N/A mp_digit divisor, mp_digit *quot, mp_digit *rem);
1674N/A
1674N/A/* c += a * b * (MP_RADIX ** offset); */
1674N/A#define s_mp_mul_d_add_offset(a, b, c, off) \
1674N/A(s_mpv_mul_d_add_prop(MP_DIGITS(a), MP_USED(a), b, MP_DIGITS(c) + off), MP_OKAY)
1674N/A
1674N/Atypedef struct {
1674N/A mp_int N; /* modulus N */
1674N/A mp_digit n0prime; /* n0' = - (n0 ** -1) mod MP_RADIX */
1674N/A mp_size b; /* R == 2 ** b, also b = # significant bits in N */
1674N/A} mp_mont_modulus;
1674N/A
1674N/Amp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,
1674N/A mp_mont_modulus *mmm);
1674N/Amp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm);
1674N/A
1674N/A/*
1674N/A * s_mpi_getProcessorLineSize() returns the size in bytes of the cache line
1674N/A * if a cache exists, or zero if there is no cache. If more than one
1674N/A * cache line exists, it should return the smallest line size (which is
1674N/A * usually the L1 cache).
1674N/A *
1674N/A * mp_modexp uses this information to make sure that private key information
1674N/A * isn't being leaked through the cache.
1674N/A *
1674N/A * see mpcpucache.c for the implementation.
1674N/A */
1674N/Aunsigned long s_mpi_getProcessorLineSize();
1674N/A
1674N/A/* }}} */
1674N/A#endif /* _MPI_PRIV_H */