2019-05-21 01:07:51 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-08-31 19:05:16 +08:00
|
|
|
/* mpi.h - Multi Precision Integers
|
|
|
|
* Copyright (C) 1994, 1996, 1998, 1999,
|
|
|
|
* 2000, 2001 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GNUPG.
|
|
|
|
*
|
|
|
|
* Note: This code is heavily based on the GNU MP Library.
|
|
|
|
* Actually it's the same code with only minor changes in the
|
|
|
|
* way the data is stored; this is to support the abstraction
|
|
|
|
* of an optional secure memory allocation which may be used
|
|
|
|
* to avoid revealing of sensitive data due to paging etc.
|
|
|
|
* The GNU MP Library itself is published under the LGPL;
|
|
|
|
* however I decided to publish this code under the plain GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef G10_MPI_H
|
|
|
|
#define G10_MPI_H
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
2015-10-09 00:26:50 +08:00
|
|
|
#include <linux/scatterlist.h>
|
2011-08-31 19:05:16 +08:00
|
|
|
|
|
|
|
#define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
|
|
|
|
#define BITS_PER_MPI_LIMB BITS_PER_LONG
|
|
|
|
|
|
|
|
typedef unsigned long int mpi_limb_t;
|
|
|
|
typedef signed long int mpi_limb_signed_t;
|
|
|
|
|
|
|
|
struct gcry_mpi {
|
|
|
|
int alloced; /* array size (# of allocated limbs) */
|
|
|
|
int nlimbs; /* number of valid limbs */
|
|
|
|
int nbits; /* the real number of valid bits (info only) */
|
|
|
|
int sign; /* indicates a negative number */
|
|
|
|
unsigned flags; /* bit 0: array must be allocated in secure memory space */
|
|
|
|
/* bit 1: not used */
|
|
|
|
/* bit 2: the limb is a pointer to some m_alloced data */
|
|
|
|
mpi_limb_t *d; /* array with the limbs */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct gcry_mpi *MPI;
|
|
|
|
|
|
|
|
#define mpi_get_nlimbs(a) ((a)->nlimbs)
|
|
|
|
|
|
|
|
/*-- mpiutil.c --*/
|
|
|
|
MPI mpi_alloc(unsigned nlimbs);
|
|
|
|
void mpi_free(MPI a);
|
|
|
|
int mpi_resize(MPI a, unsigned nlimbs);
|
|
|
|
|
|
|
|
/*-- mpicoder.c --*/
|
2012-09-25 00:11:27 +08:00
|
|
|
MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
|
2011-08-31 19:05:16 +08:00
|
|
|
MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
|
2015-10-09 00:26:50 +08:00
|
|
|
MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
|
2011-08-31 19:05:16 +08:00
|
|
|
void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
|
2015-06-16 04:18:36 +08:00
|
|
|
int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
|
|
|
|
int *sign);
|
2016-06-29 19:32:21 +08:00
|
|
|
int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
|
2015-10-09 00:26:50 +08:00
|
|
|
int *sign);
|
2011-08-31 19:05:16 +08:00
|
|
|
|
|
|
|
/*-- mpi-pow.c --*/
|
|
|
|
int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
|
|
|
|
|
|
|
|
/*-- mpi-cmp.c --*/
|
|
|
|
int mpi_cmp_ui(MPI u, ulong v);
|
|
|
|
int mpi_cmp(MPI u, MPI v);
|
|
|
|
|
|
|
|
/*-- mpi-bit.c --*/
|
|
|
|
void mpi_normalize(MPI a);
|
|
|
|
unsigned mpi_get_nbits(MPI a);
|
|
|
|
|
2015-06-16 04:18:36 +08:00
|
|
|
/* inline functions */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mpi_get_size() - returns max size required to store the number
|
|
|
|
*
|
|
|
|
* @a: A multi precision integer for which we want to allocate a bufer
|
|
|
|
*
|
|
|
|
* Return: size required to store the number
|
|
|
|
*/
|
|
|
|
static inline unsigned int mpi_get_size(MPI a)
|
|
|
|
{
|
|
|
|
return a->nlimbs * BYTES_PER_MPI_LIMB;
|
|
|
|
}
|
2011-08-31 19:05:16 +08:00
|
|
|
#endif /*G10_MPI_H */
|