1 /* gmp-glue.h
2 
3    Copyright (C) 2013 Niels Möller
4    Copyright (C) 2013 Red Hat
5 
6    This file is part of GNU Nettle.
7 
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10 
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14 
15    or
16 
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20 
21    or both in parallel, as here.
22 
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27 
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 */
32 
33 #ifndef GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED
34 #define GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED
35 
36 #include <nettle/bignum.h>
37 
38 #define cnd_swap _gnutls_nettle_ecc_cnd_swap
39 #define mpz_limbs_cmp _gnutls_nettle_ecc_mpz_limbs_cmp
40 #define mpz_limbs_read_n _gnutls_nettle_ecc_mpz_limbs_read_n
41 #define mpz_limbs_copy _gnutls_nettle_ecc_mpz_limbs_copy
42 #define mpz_set_n _gnutls_nettle_ecc_mpz_set_n
43 #define mpn_set_base256 _gnutls_nettle_ecc_mpn_set_base256
44 #define mpn_set_base256_le _gnutls_nettle_ecc_mpn_set_base256_le
45 #define mpn_get_base256 _gnutls_nettle_ecc_mpn_get_base256
46 #define mpn_get_base256_le _gnutls_nettle_ecc_mpn_get_base256_le
47 #define gmp_alloc_limbs _gnutls_nettle_ecc_gmp_alloc_limbs
48 #define gmp_free_limbs _gnutls_nettle_ecc_gmp_free_limbs
49 #define gmp_free _gnutls_nettle_ecc_gmp_free
50 #define gmp_alloc _gnutls_nettle_ecc_gmp_alloc
51 
52 #define TMP_GMP_DECL(name, type) type *name;	\
53   size_t tmp_##name##_size
54 #define TMP_GMP_ALLOC(name, size) do {					\
55     tmp_##name##_size = (size);						\
56     (name) = gmp_alloc(sizeof (*name) * (size));	\
57   } while (0)
58 #define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
59 
60 
61 /* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
62 #ifdef mpn_cnd_add_n
63 # define cnd_add_n(cnd, rp, ap, n) mpn_cnd_add_n ((cnd), (rp), (rp), (ap), (n))
64 # define cnd_sub_n(cnd, rp, ap, n) mpn_cnd_sub_n ((cnd), (rp), (rp), (ap), (n))
65 #else
66 # define cnd_add_n(cnd, rp, ap, n) mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
67 # define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
68 #endif
69 
70 #define NETTLE_OCTET_SIZE_TO_LIMB_SIZE(n) \
71   (((n) * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
72 
73 void
74 cnd_swap (mp_limb_t cnd, mp_limb_t *ap, mp_limb_t *bp, mp_size_t n);
75 
76 /* Convenience functions */
77 int
78 mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
79 
80 /* Get a pointer to an n limb area, for read-only operation. n must be
81    greater or equal to the current size, and the mpz is zero-padded if
82    needed. */
83 const mp_limb_t *
84 mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
85 
86 /* Copy limbs, with zero-padding. */
87 /* FIXME: Reorder arguments, on the theory that the first argument of
88    an _mpz_* function should be an mpz_t? Or rename to _mpz_get_limbs,
89    with argument order consistent with mpz_get_*. */
90 void
91 mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
92 
93 void
94 mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
95 
96 /* Like mpn_set_str, but always writes rn limbs. If input is larger,
97    higher bits are ignored. */
98 void
99 mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
100 		 const uint8_t *xp, size_t xn);
101 
102 void
103 mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
104 		    const uint8_t *xp, size_t xn);
105 
106 void
107 mpn_get_base256 (uint8_t *rp, size_t rn,
108 	         const mp_limb_t *xp, mp_size_t xn);
109 
110 void
111 mpn_get_base256_le (uint8_t *rp, size_t rn,
112 		    const mp_limb_t *xp, mp_size_t xn);
113 
114 
115 mp_limb_t *
116 gmp_alloc_limbs (mp_size_t n);
117 
118 void
119 gmp_free_limbs (mp_limb_t *p, mp_size_t n);
120 
121 void *gmp_alloc(size_t n);
122 void gmp_free(void *p, size_t n);
123 
124 #endif /* GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED */
125