blob: 3a5abe95affd381564b1b08b1996dd4e0ee52490 [file] [log] [blame]
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +03001/* 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 Struk2d4d1ee2015-10-08 09:26:50 -070034#include <linux/scatterlist.h>
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030035
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030036#define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
37#define BITS_PER_MPI_LIMB BITS_PER_LONG
38
39typedef unsigned long int mpi_limb_t;
40typedef signed long int mpi_limb_signed_t;
41
42struct 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
53typedef struct gcry_mpi *MPI;
54
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030055#define mpi_get_nlimbs(a) ((a)->nlimbs)
56#define mpi_is_neg(a) ((a)->sign)
57
58/*-- mpiutil.c --*/
59MPI mpi_alloc(unsigned nlimbs);
60MPI mpi_alloc_secure(unsigned nlimbs);
61MPI mpi_alloc_like(MPI a);
62void mpi_free(MPI a);
63int mpi_resize(MPI a, unsigned nlimbs);
64int mpi_copy(MPI *copy, const MPI a);
65void mpi_clear(MPI a);
66int mpi_set(MPI w, MPI u);
67int mpi_set_ui(MPI w, ulong u);
68MPI mpi_alloc_set_ui(unsigned long u);
69void mpi_m_check(MPI a);
70void mpi_swap(MPI a, MPI b);
71
72/*-- mpicoder.c --*/
73MPI do_encode_md(const void *sha_buffer, unsigned nbits);
David Howellse1045992012-09-24 17:11:27 +010074MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030075MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -070076MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030077int mpi_fromstr(MPI val, const char *str);
78u32 mpi_get_keyid(MPI a, u32 *keyid);
79void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
Tadeusz Strukd37e2962015-06-15 13:18:36 -070080int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
81 int *sign);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030082void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
83int mpi_set_buffer(MPI a, const void *buffer, unsigned nbytes, int sign);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -070084int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned *nbytes,
85 int *sign);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030086
87#define log_mpidump g10_log_mpidump
88
89/*-- mpi-add.c --*/
90int mpi_add_ui(MPI w, MPI u, ulong v);
91int mpi_add(MPI w, MPI u, MPI v);
92int mpi_addm(MPI w, MPI u, MPI v, MPI m);
93int mpi_sub_ui(MPI w, MPI u, ulong v);
94int mpi_sub(MPI w, MPI u, MPI v);
95int mpi_subm(MPI w, MPI u, MPI v, MPI m);
96
97/*-- mpi-mul.c --*/
98int mpi_mul_ui(MPI w, MPI u, ulong v);
99int mpi_mul_2exp(MPI w, MPI u, ulong cnt);
100int mpi_mul(MPI w, MPI u, MPI v);
101int mpi_mulm(MPI w, MPI u, MPI v, MPI m);
102
103/*-- mpi-div.c --*/
104ulong mpi_fdiv_r_ui(MPI rem, MPI dividend, ulong divisor);
105int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
106int mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);
107int mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor);
108int mpi_tdiv_r(MPI rem, MPI num, MPI den);
109int mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den);
110int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count);
111int mpi_divisible_ui(const MPI dividend, ulong divisor);
112
113/*-- mpi-gcd.c --*/
114int mpi_gcd(MPI g, const MPI a, const MPI b);
115
116/*-- mpi-pow.c --*/
117int mpi_pow(MPI w, MPI u, MPI v);
118int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
119
120/*-- mpi-mpow.c --*/
121int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI mod);
122
123/*-- mpi-cmp.c --*/
124int mpi_cmp_ui(MPI u, ulong v);
125int mpi_cmp(MPI u, MPI v);
126
127/*-- mpi-scan.c --*/
128int mpi_getbyte(MPI a, unsigned idx);
129void mpi_putbyte(MPI a, unsigned idx, int value);
130unsigned mpi_trailing_zeros(MPI a);
131
132/*-- mpi-bit.c --*/
133void mpi_normalize(MPI a);
134unsigned mpi_get_nbits(MPI a);
135int mpi_test_bit(MPI a, unsigned n);
136int mpi_set_bit(MPI a, unsigned n);
137int mpi_set_highbit(MPI a, unsigned n);
138void mpi_clear_highbit(MPI a, unsigned n);
139void mpi_clear_bit(MPI a, unsigned n);
140int mpi_rshift(MPI x, MPI a, unsigned n);
141
142/*-- mpi-inv.c --*/
143int mpi_invm(MPI x, MPI u, MPI v);
144
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700145/* inline functions */
146
147/**
148 * mpi_get_size() - returns max size required to store the number
149 *
150 * @a: A multi precision integer for which we want to allocate a bufer
151 *
152 * Return: size required to store the number
153 */
154static inline unsigned int mpi_get_size(MPI a)
155{
156 return a->nlimbs * BYTES_PER_MPI_LIMB;
157}
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300158#endif /*G10_MPI_H */