Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 1 | /* mpi.h - Multi Precision Integers |
| 2 | * Copyright (C) 1994, 1996, 1998, 1999, |
| 3 | * 2000, 2001 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This file is part of GNUPG. |
| 6 | * |
| 7 | * GNUPG is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * GNUPG is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
| 20 | * |
| 21 | * Note: This code is heavily based on the GNU MP Library. |
| 22 | * Actually it's the same code with only minor changes in the |
| 23 | * way the data is stored; this is to support the abstraction |
| 24 | * of an optional secure memory allocation which may be used |
| 25 | * to avoid revealing of sensitive data due to paging etc. |
| 26 | * The GNU MP Library itself is published under the LGPL; |
| 27 | * however I decided to publish this code under the plain GPL. |
| 28 | */ |
| 29 | |
| 30 | #ifndef G10_MPI_H |
| 31 | #define G10_MPI_H |
| 32 | |
| 33 | #include <linux/types.h> |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 34 | #include <linux/scatterlist.h> |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 35 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 36 | #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8) |
| 37 | #define BITS_PER_MPI_LIMB BITS_PER_LONG |
| 38 | |
| 39 | typedef unsigned long int mpi_limb_t; |
| 40 | typedef signed long int mpi_limb_signed_t; |
| 41 | |
| 42 | struct gcry_mpi { |
| 43 | int alloced; /* array size (# of allocated limbs) */ |
| 44 | int nlimbs; /* number of valid limbs */ |
| 45 | int nbits; /* the real number of valid bits (info only) */ |
| 46 | int sign; /* indicates a negative number */ |
| 47 | unsigned flags; /* bit 0: array must be allocated in secure memory space */ |
| 48 | /* bit 1: not used */ |
| 49 | /* bit 2: the limb is a pointer to some m_alloced data */ |
| 50 | mpi_limb_t *d; /* array with the limbs */ |
| 51 | }; |
| 52 | |
| 53 | typedef struct gcry_mpi *MPI; |
| 54 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 55 | #define mpi_get_nlimbs(a) ((a)->nlimbs) |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 56 | |
| 57 | /*-- mpiutil.c --*/ |
| 58 | MPI mpi_alloc(unsigned nlimbs); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 59 | void mpi_free(MPI a); |
| 60 | int mpi_resize(MPI a, unsigned nlimbs); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 61 | |
| 62 | /*-- mpicoder.c --*/ |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 63 | MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 64 | MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 65 | MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 66 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign); |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 67 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
| 68 | int *sign); |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 69 | int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes, |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 70 | int *sign); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 71 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 72 | /*-- mpi-pow.c --*/ |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 73 | int mpi_powm(MPI res, MPI base, MPI exp, MPI mod); |
| 74 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 75 | /*-- mpi-cmp.c --*/ |
| 76 | int mpi_cmp_ui(MPI u, ulong v); |
| 77 | int mpi_cmp(MPI u, MPI v); |
| 78 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 79 | /*-- mpi-bit.c --*/ |
| 80 | void mpi_normalize(MPI a); |
| 81 | unsigned mpi_get_nbits(MPI a); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 82 | |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 83 | /* inline functions */ |
| 84 | |
| 85 | /** |
| 86 | * mpi_get_size() - returns max size required to store the number |
| 87 | * |
| 88 | * @a: A multi precision integer for which we want to allocate a bufer |
| 89 | * |
| 90 | * Return: size required to store the number |
| 91 | */ |
| 92 | static inline unsigned int mpi_get_size(MPI a) |
| 93 | { |
| 94 | return a->nlimbs * BYTES_PER_MPI_LIMB; |
| 95 | } |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 96 | #endif /*G10_MPI_H */ |