-
+ E539EF777487B66317CD23B7D19F883A9BAB67A9CB984DFB5D1A5DF4575A66CB44B7EC199AC0829B1270EDFA68F182D646CE944C14F5C401654E1E0F68271BAFmpi/include/mpi.h(0 . 0)(1 . 169)
1533 /* mpi.h - Multi Precision Integers
1534 * Copyright (C) 1994, 1996, 1998, 1999,
1535 * 2000, 2001 Free Software Foundation, Inc.
1536 *
1537 * This file is part of GNUPG.
1538 *
1539 * GNUPG is free software; you can redistribute it and/or modify
1540 * it under the terms of the GNU General Public License as published by
1541 * the Free Software Foundation; either version 3 of the License, or
1542 * (at your option) any later version.
1543 *
1544 * GNUPG is distributed in the hope that it will be useful,
1545 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1546 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1547 * GNU General Public License for more details.
1548 *
1549 * You should have received a copy of the GNU General Public License
1550 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1551 *
1552 * Note: This code is heavily based on the GNU MP Library.
1553 * Actually it's the same code with only minor changes in the
1554 * way the data is stored; this is to support the abstraction
1555 * of an optional secure memory allocation which may be used
1556 * to avoid revealing of sensitive data due to paging etc.
1557 * The GNU MP Library itself is published under the LGPL;
1558 * however I decided to publish this code under the plain GPL.
1559 */
1560
1561 #ifndef G10_MPI_H
1562 #define G10_MPI_H
1563
1564 #include <config.h>
1565 #include <stdio.h>
1566 #include "iobuf.h"
1567 #include "types.h"
1568 #include "memory.h"
1569
1570 #ifndef EXTERN_UNLESS_MAIN_MODULE
1571 #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
1572 #define EXTERN_UNLESS_MAIN_MODULE extern
1573 #else
1574 #define EXTERN_UNLESS_MAIN_MODULE
1575 #endif
1576 #endif
1577
1578 #define DBG_MPI mpi_debug_mode
1579 EXTERN_UNLESS_MAIN_MODULE int mpi_debug_mode;
1580
1581
1582 struct gcry_mpi;
1583 typedef struct gcry_mpi *MPI;
1584
1585
1586 /*-- mpiutil.c --*/
1587
1588 #ifdef M_DEBUG
1589 #define mpi_alloc(n) mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) )
1590 #define mpi_alloc_secure(n) mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) )
1591 #define mpi_alloc_like(n) mpi_debug_alloc_like((n), M_DBGINFO( __LINE__ ) )
1592 #define mpi_free(a) mpi_debug_free((a), M_DBGINFO(__LINE__) )
1593 #define mpi_resize(a,b) mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) )
1594 #define mpi_copy(a) mpi_debug_copy((a), M_DBGINFO(__LINE__) )
1595 MPI mpi_debug_alloc( unsigned nlimbs, const char *info );
1596 MPI mpi_debug_alloc_secure( unsigned nlimbs, const char *info );
1597 MPI mpi_debug_alloc_like( MPI a, const char *info );
1598 void mpi_debug_free( MPI a, const char *info );
1599 void mpi_debug_resize( MPI a, unsigned nlimbs, const char *info );
1600 MPI mpi_debug_copy( MPI a, const char *info );
1601 #else
1602 MPI mpi_alloc( unsigned nlimbs );
1603 MPI mpi_alloc_secure( unsigned nlimbs );
1604 MPI mpi_alloc_like( MPI a );
1605 void mpi_free( MPI a );
1606 void mpi_resize( MPI a, unsigned nlimbs );
1607 MPI mpi_copy( MPI a );
1608 #endif
1609 #define mpi_is_opaque(a) ((a) && (mpi_get_flags (a)&4))
1610 MPI mpi_set_opaque( MPI a, void *p, unsigned int len );
1611 void *mpi_get_opaque( MPI a, unsigned int *len );
1612 #define mpi_is_secure(a) ((a) && (mpi_get_flags (a)&1))
1613 void mpi_set_secure( MPI a );
1614 void mpi_clear( MPI a );
1615 void mpi_set( MPI w, MPI u);
1616 void mpi_set_ui( MPI w, ulong u);
1617 MPI mpi_alloc_set_ui( unsigned long u);
1618 void mpi_m_check( MPI a );
1619 void mpi_swap( MPI a, MPI b);
1620 int mpi_get_nlimbs (MPI a);
1621 int mpi_is_neg (MPI a);
1622 unsigned int mpi_nlimb_hint_from_nbytes (unsigned int nbytes);
1623 unsigned int mpi_nlimb_hint_from_nbits (unsigned int nbits);
1624 unsigned int mpi_get_flags (MPI a);
1625
1626 /*-- mpicoder.c --*/
1627 int mpi_write( IOBUF out, MPI a );
1628 #ifdef M_DEBUG
1629 #define mpi_read(a,b,c) mpi_debug_read((a),(b),(c), M_DBGINFO( __LINE__ ) )
1630 MPI mpi_debug_read(IOBUF inp, unsigned *nread, int secure, const char *info);
1631 #else
1632 MPI mpi_read(IOBUF inp, unsigned *nread, int secure);
1633 #endif
1634 MPI mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure);
1635 int mpi_fromstr(MPI val, const char *str);
1636 int mpi_print( FILE *fp, MPI a, int mode );
1637 void g10_log_mpidump( const char *text, MPI a );
1638 u32 mpi_get_keyid( MPI a, u32 *keyid );
1639 byte *mpi_get_buffer( MPI a, unsigned *nbytes, int *sign );
1640 byte *mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign );
1641 void mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign );
1642
1643 #define log_mpidump g10_log_mpidump
1644
1645 /*-- mpi-add.c --*/
1646 void mpi_add_ui(MPI w, MPI u, ulong v );
1647 void mpi_add(MPI w, MPI u, MPI v);
1648 void mpi_addm(MPI w, MPI u, MPI v, MPI m);
1649 void mpi_sub_ui(MPI w, MPI u, ulong v );
1650 void mpi_sub( MPI w, MPI u, MPI v);
1651 void mpi_subm( MPI w, MPI u, MPI v, MPI m);
1652
1653 /*-- mpi-mul.c --*/
1654 void mpi_mul_ui(MPI w, MPI u, ulong v );
1655 void mpi_mul_2exp( MPI w, MPI u, ulong cnt);
1656 void mpi_mul( MPI w, MPI u, MPI v);
1657 void mpi_mulm( MPI w, MPI u, MPI v, MPI m);
1658
1659 /*-- mpi-div.c --*/
1660 ulong mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor );
1661 void mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor );
1662 void mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor );
1663 void mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor );
1664 void mpi_tdiv_r( MPI rem, MPI num, MPI den);
1665 void mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den);
1666 void mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count );
1667 int mpi_divisible_ui(MPI dividend, ulong divisor );
1668
1669 /*-- mpi-gcd.c --*/
1670 int mpi_gcd( MPI g, MPI a, MPI b );
1671
1672 /*-- mpi-pow.c --*/
1673 void mpi_pow( MPI w, MPI u, MPI v);
1674 void mpi_powm( MPI res, MPI base, MPI exponent, MPI mod);
1675
1676 /*-- mpi-mpow.c --*/
1677 void mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI mod);
1678
1679 /*-- mpi-cmp.c --*/
1680 int mpi_cmp_ui( MPI u, ulong v );
1681 int mpi_cmp( MPI u, MPI v );
1682
1683 /*-- mpi-scan.c --*/
1684 int mpi_getbyte( MPI a, unsigned idx );
1685 void mpi_putbyte( MPI a, unsigned idx, int value );
1686 unsigned mpi_trailing_zeros( MPI a );
1687
1688 /*-- mpi-bit.c --*/
1689 void mpi_normalize( MPI a );
1690 unsigned mpi_get_nbits( MPI a );
1691 int mpi_test_bit( MPI a, unsigned n );
1692 void mpi_set_bit( MPI a, unsigned n );
1693 void mpi_set_highbit( MPI a, unsigned n );
1694 void mpi_clear_highbit( MPI a, unsigned n );
1695 void mpi_clear_bit( MPI a, unsigned n );
1696 void mpi_rshift( MPI x, MPI a, unsigned n );
1697
1698 /*-- mpi-inv.c --*/
1699 void mpi_invm( MPI x, MPI u, MPI v );
1700
1701 #endif /*G10_MPI_H*/