1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#ifndef G10_MPI_H
31#define G10_MPI_H
32
33#include <linux/types.h>
34#include <linux/scatterlist.h>
35
36#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;
44 int nlimbs;
45 int nbits;
46 int sign;
47 unsigned flags;
48
49
50 mpi_limb_t *d;
51};
52
53typedef struct gcry_mpi *MPI;
54
55#define mpi_get_nlimbs(a) ((a)->nlimbs)
56#define mpi_is_neg(a) ((a)->sign)
57
58
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
73MPI do_encode_md(const void *sha_buffer, unsigned nbits);
74MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
75MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
76MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
77int 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);
80int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
81 int *sign);
82void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
83int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
84 int *sign);
85
86#define log_mpidump g10_log_mpidump
87
88
89int mpi_add_ui(MPI w, MPI u, ulong v);
90int mpi_add(MPI w, MPI u, MPI v);
91int mpi_addm(MPI w, MPI u, MPI v, MPI m);
92int mpi_sub_ui(MPI w, MPI u, ulong v);
93int mpi_sub(MPI w, MPI u, MPI v);
94int mpi_subm(MPI w, MPI u, MPI v, MPI m);
95
96
97int mpi_mul_ui(MPI w, MPI u, ulong v);
98int mpi_mul_2exp(MPI w, MPI u, ulong cnt);
99int mpi_mul(MPI w, MPI u, MPI v);
100int mpi_mulm(MPI w, MPI u, MPI v, MPI m);
101
102
103ulong mpi_fdiv_r_ui(MPI rem, MPI dividend, ulong divisor);
104int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
105int mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);
106int mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor);
107int mpi_tdiv_r(MPI rem, MPI num, MPI den);
108int mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den);
109int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count);
110int mpi_divisible_ui(const MPI dividend, ulong divisor);
111
112
113int mpi_gcd(MPI g, const MPI a, const MPI b);
114
115
116int mpi_pow(MPI w, MPI u, MPI v);
117int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
118
119
120int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI mod);
121
122
123int mpi_cmp_ui(MPI u, ulong v);
124int mpi_cmp(MPI u, MPI v);
125
126
127int mpi_getbyte(MPI a, unsigned idx);
128void mpi_putbyte(MPI a, unsigned idx, int value);
129unsigned mpi_trailing_zeros(MPI a);
130
131
132void mpi_normalize(MPI a);
133unsigned mpi_get_nbits(MPI a);
134int mpi_test_bit(MPI a, unsigned n);
135int mpi_set_bit(MPI a, unsigned n);
136int mpi_set_highbit(MPI a, unsigned n);
137void mpi_clear_highbit(MPI a, unsigned n);
138void mpi_clear_bit(MPI a, unsigned n);
139int mpi_rshift(MPI x, MPI a, unsigned n);
140
141
142int mpi_invm(MPI x, MPI u, MPI v);
143
144
145
146
147
148
149
150
151
152
153static inline unsigned int mpi_get_size(MPI a)
154{
155 return a->nlimbs * BYTES_PER_MPI_LIMB;
156}
157#endif
158