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
35
36
37#define SHA1_DIGEST_LENGTH 20
38
39
40
41#define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
42#define BITS_PER_MPI_LIMB BITS_PER_LONG
43
44typedef unsigned long int mpi_limb_t;
45typedef signed long int mpi_limb_signed_t;
46
47struct gcry_mpi {
48 int alloced;
49 int nlimbs;
50 int nbits;
51 int sign;
52 unsigned flags;
53
54
55 mpi_limb_t *d;
56};
57
58typedef struct gcry_mpi *MPI;
59
60#define mpi_get_nlimbs(a) ((a)->nlimbs)
61#define mpi_is_neg(a) ((a)->sign)
62
63
64MPI mpi_alloc(unsigned nlimbs);
65MPI mpi_alloc_secure(unsigned nlimbs);
66MPI mpi_alloc_like(MPI a);
67void mpi_free(MPI a);
68int mpi_resize(MPI a, unsigned nlimbs);
69int mpi_copy(MPI *copy, const MPI a);
70void mpi_clear(MPI a);
71int mpi_set(MPI w, MPI u);
72int mpi_set_ui(MPI w, ulong u);
73MPI mpi_alloc_set_ui(unsigned long u);
74void mpi_m_check(MPI a);
75void mpi_swap(MPI a, MPI b);
76
77
78MPI do_encode_md(const void *sha_buffer, unsigned nbits);
79MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
80MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
81int mpi_fromstr(MPI val, const char *str);
82u32 mpi_get_keyid(MPI a, u32 *keyid);
83void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
84void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
85int mpi_set_buffer(MPI a, const void *buffer, unsigned nbytes, int sign);
86
87#define log_mpidump g10_log_mpidump
88
89
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
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
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
114int mpi_gcd(MPI g, const MPI a, const MPI b);
115
116
117int mpi_pow(MPI w, MPI u, MPI v);
118int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
119
120
121int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI mod);
122
123
124int mpi_cmp_ui(MPI u, ulong v);
125int mpi_cmp(MPI u, MPI v);
126
127
128int mpi_getbyte(MPI a, unsigned idx);
129void mpi_putbyte(MPI a, unsigned idx, int value);
130unsigned mpi_trailing_zeros(MPI a);
131
132
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
143int mpi_invm(MPI x, MPI u, MPI v);
144
145#endif
146