Thomas Gleixner | 465ae83 | 2019-05-20 19:07:51 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 2 | /* mpi.h - Multi Precision Integers |
| 3 | * Copyright (C) 1994, 1996, 1998, 1999, |
| 4 | * 2000, 2001 Free Software Foundation, Inc. |
| 5 | * |
| 6 | * This file is part of GNUPG. |
| 7 | * |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 8 | * Note: This code is heavily based on the GNU MP Library. |
| 9 | * Actually it's the same code with only minor changes in the |
| 10 | * way the data is stored; this is to support the abstraction |
| 11 | * of an optional secure memory allocation which may be used |
| 12 | * to avoid revealing of sensitive data due to paging etc. |
| 13 | * The GNU MP Library itself is published under the LGPL; |
| 14 | * however I decided to publish this code under the plain GPL. |
| 15 | */ |
| 16 | |
| 17 | #ifndef G10_MPI_H |
| 18 | #define G10_MPI_H |
| 19 | |
| 20 | #include <linux/types.h> |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 22 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 23 | #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8) |
| 24 | #define BITS_PER_MPI_LIMB BITS_PER_LONG |
| 25 | |
| 26 | typedef unsigned long int mpi_limb_t; |
| 27 | typedef signed long int mpi_limb_signed_t; |
| 28 | |
| 29 | struct gcry_mpi { |
| 30 | int alloced; /* array size (# of allocated limbs) */ |
| 31 | int nlimbs; /* number of valid limbs */ |
| 32 | int nbits; /* the real number of valid bits (info only) */ |
| 33 | int sign; /* indicates a negative number */ |
| 34 | unsigned flags; /* bit 0: array must be allocated in secure memory space */ |
| 35 | /* bit 1: not used */ |
| 36 | /* bit 2: the limb is a pointer to some m_alloced data */ |
| 37 | mpi_limb_t *d; /* array with the limbs */ |
| 38 | }; |
| 39 | |
| 40 | typedef struct gcry_mpi *MPI; |
| 41 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 42 | #define mpi_get_nlimbs(a) ((a)->nlimbs) |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 43 | |
| 44 | /*-- mpiutil.c --*/ |
| 45 | MPI mpi_alloc(unsigned nlimbs); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 46 | void mpi_free(MPI a); |
| 47 | int mpi_resize(MPI a, unsigned nlimbs); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 48 | |
| 49 | /*-- mpicoder.c --*/ |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 50 | MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 51 | MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 52 | MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 53 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign); |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 54 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
| 55 | int *sign); |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 56 | int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes, |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 57 | int *sign); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 58 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 59 | /*-- mpi-pow.c --*/ |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 60 | int mpi_powm(MPI res, MPI base, MPI exp, MPI mod); |
| 61 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 62 | /*-- mpi-cmp.c --*/ |
| 63 | int mpi_cmp_ui(MPI u, ulong v); |
| 64 | int mpi_cmp(MPI u, MPI v); |
| 65 | |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 66 | /*-- mpi-bit.c --*/ |
| 67 | void mpi_normalize(MPI a); |
| 68 | unsigned mpi_get_nbits(MPI a); |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 69 | |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 70 | /* inline functions */ |
| 71 | |
| 72 | /** |
| 73 | * mpi_get_size() - returns max size required to store the number |
| 74 | * |
| 75 | * @a: A multi precision integer for which we want to allocate a bufer |
| 76 | * |
| 77 | * Return: size required to store the number |
| 78 | */ |
| 79 | static inline unsigned int mpi_get_size(MPI a) |
| 80 | { |
| 81 | return a->nlimbs * BYTES_PER_MPI_LIMB; |
| 82 | } |
Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 83 | #endif /*G10_MPI_H */ |