1*465ae836SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 25ce3e312SDmitry Kasatkin /* mpi.h - Multi Precision Integers 35ce3e312SDmitry Kasatkin * Copyright (C) 1994, 1996, 1998, 1999, 45ce3e312SDmitry Kasatkin * 2000, 2001 Free Software Foundation, Inc. 55ce3e312SDmitry Kasatkin * 65ce3e312SDmitry Kasatkin * This file is part of GNUPG. 75ce3e312SDmitry Kasatkin * 85ce3e312SDmitry Kasatkin * Note: This code is heavily based on the GNU MP Library. 95ce3e312SDmitry Kasatkin * Actually it's the same code with only minor changes in the 105ce3e312SDmitry Kasatkin * way the data is stored; this is to support the abstraction 115ce3e312SDmitry Kasatkin * of an optional secure memory allocation which may be used 125ce3e312SDmitry Kasatkin * to avoid revealing of sensitive data due to paging etc. 135ce3e312SDmitry Kasatkin * The GNU MP Library itself is published under the LGPL; 145ce3e312SDmitry Kasatkin * however I decided to publish this code under the plain GPL. 155ce3e312SDmitry Kasatkin */ 165ce3e312SDmitry Kasatkin 175ce3e312SDmitry Kasatkin #ifndef G10_MPI_H 185ce3e312SDmitry Kasatkin #define G10_MPI_H 195ce3e312SDmitry Kasatkin 205ce3e312SDmitry Kasatkin #include <linux/types.h> 212d4d1eeaSTadeusz Struk #include <linux/scatterlist.h> 225ce3e312SDmitry Kasatkin 235ce3e312SDmitry Kasatkin #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8) 245ce3e312SDmitry Kasatkin #define BITS_PER_MPI_LIMB BITS_PER_LONG 255ce3e312SDmitry Kasatkin 265ce3e312SDmitry Kasatkin typedef unsigned long int mpi_limb_t; 275ce3e312SDmitry Kasatkin typedef signed long int mpi_limb_signed_t; 285ce3e312SDmitry Kasatkin 295ce3e312SDmitry Kasatkin struct gcry_mpi { 305ce3e312SDmitry Kasatkin int alloced; /* array size (# of allocated limbs) */ 315ce3e312SDmitry Kasatkin int nlimbs; /* number of valid limbs */ 325ce3e312SDmitry Kasatkin int nbits; /* the real number of valid bits (info only) */ 335ce3e312SDmitry Kasatkin int sign; /* indicates a negative number */ 345ce3e312SDmitry Kasatkin unsigned flags; /* bit 0: array must be allocated in secure memory space */ 355ce3e312SDmitry Kasatkin /* bit 1: not used */ 365ce3e312SDmitry Kasatkin /* bit 2: the limb is a pointer to some m_alloced data */ 375ce3e312SDmitry Kasatkin mpi_limb_t *d; /* array with the limbs */ 385ce3e312SDmitry Kasatkin }; 395ce3e312SDmitry Kasatkin 405ce3e312SDmitry Kasatkin typedef struct gcry_mpi *MPI; 415ce3e312SDmitry Kasatkin 425ce3e312SDmitry Kasatkin #define mpi_get_nlimbs(a) ((a)->nlimbs) 435ce3e312SDmitry Kasatkin 445ce3e312SDmitry Kasatkin /*-- mpiutil.c --*/ 455ce3e312SDmitry Kasatkin MPI mpi_alloc(unsigned nlimbs); 465ce3e312SDmitry Kasatkin void mpi_free(MPI a); 475ce3e312SDmitry Kasatkin int mpi_resize(MPI a, unsigned nlimbs); 485ce3e312SDmitry Kasatkin 495ce3e312SDmitry Kasatkin /*-- mpicoder.c --*/ 50e1045992SDavid Howells MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes); 515ce3e312SDmitry Kasatkin MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread); 522d4d1eeaSTadeusz Struk MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len); 535ce3e312SDmitry Kasatkin void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign); 54d37e2969STadeusz Struk int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, 55d37e2969STadeusz Struk int *sign); 569b45b7bbSHerbert Xu int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes, 572d4d1eeaSTadeusz Struk int *sign); 585ce3e312SDmitry Kasatkin 595ce3e312SDmitry Kasatkin /*-- mpi-pow.c --*/ 605ce3e312SDmitry Kasatkin int mpi_powm(MPI res, MPI base, MPI exp, MPI mod); 615ce3e312SDmitry Kasatkin 625ce3e312SDmitry Kasatkin /*-- mpi-cmp.c --*/ 635ce3e312SDmitry Kasatkin int mpi_cmp_ui(MPI u, ulong v); 645ce3e312SDmitry Kasatkin int mpi_cmp(MPI u, MPI v); 655ce3e312SDmitry Kasatkin 665ce3e312SDmitry Kasatkin /*-- mpi-bit.c --*/ 675ce3e312SDmitry Kasatkin void mpi_normalize(MPI a); 685ce3e312SDmitry Kasatkin unsigned mpi_get_nbits(MPI a); 695ce3e312SDmitry Kasatkin 70d37e2969STadeusz Struk /* inline functions */ 71d37e2969STadeusz Struk 72d37e2969STadeusz Struk /** 73d37e2969STadeusz Struk * mpi_get_size() - returns max size required to store the number 74d37e2969STadeusz Struk * 75d37e2969STadeusz Struk * @a: A multi precision integer for which we want to allocate a bufer 76d37e2969STadeusz Struk * 77d37e2969STadeusz Struk * Return: size required to store the number 78d37e2969STadeusz Struk */ 79d37e2969STadeusz Struk static inline unsigned int mpi_get_size(MPI a) 80d37e2969STadeusz Struk { 81d37e2969STadeusz Struk return a->nlimbs * BYTES_PER_MPI_LIMB; 82d37e2969STadeusz Struk } 835ce3e312SDmitry Kasatkin #endif /*G10_MPI_H */ 84