Line | Count | Source (jump to first uncovered line) |
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 NETTLE_GMP_GLUE_H_INCLUDED |
34 | | #define NETTLE_GMP_GLUE_H_INCLUDED |
35 | | |
36 | | #include "bignum.h" |
37 | | |
38 | 0 | #define mpz_limbs_copy _nettle_mpz_limbs_copy |
39 | 0 | #define mpz_set_n _nettle_mpz_set_n |
40 | 0 | #define sec_zero_p _nettle_sec_zero_p |
41 | 0 | #define mpn_set_base256 _nettle_mpn_set_base256 |
42 | 0 | #define mpn_set_base256_le _nettle_mpn_set_base256_le |
43 | 0 | #define mpn_get_base256 _nettle_mpn_get_base256 |
44 | 0 | #define mpn_get_base256_le _nettle_mpn_get_base256_le |
45 | 0 | #define gmp_alloc_limbs _nettle_gmp_alloc_limbs |
46 | 0 | #define gmp_free_limbs _nettle_gmp_free_limbs |
47 | 0 | #define gmp_free _nettle_gmp_free |
48 | 0 | #define gmp_alloc _nettle_gmp_alloc |
49 | | |
50 | 0 | #define TMP_GMP_DECL(name, type) type *name; \ |
51 | 0 | size_t tmp_##name##_size |
52 | 0 | #define TMP_GMP_ALLOC(name, size) do { \ |
53 | 0 | tmp_##name##_size = (size); \ |
54 | 0 | (name) = gmp_alloc(sizeof (*name) * (size)); \ |
55 | 0 | } while (0) |
56 | 0 | #define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size)) |
57 | | |
58 | | #if NETTLE_USE_MINI_GMP |
59 | | #define GMP_LIMB_BITS GMP_NUMB_BITS |
60 | | |
61 | | mp_limb_t |
62 | | mpn_cnd_add_n (mp_limb_t cnd, mp_limb_t *rp, |
63 | | const mp_limb_t *ap, const mp_limb_t *bp, mp_size_t n); |
64 | | |
65 | | mp_limb_t |
66 | | mpn_cnd_sub_n (mp_limb_t cnd, mp_limb_t *rp, |
67 | | const mp_limb_t *ap, const mp_limb_t *bp, mp_size_t n); |
68 | | |
69 | | void |
70 | | mpn_cnd_swap (mp_limb_t cnd, volatile mp_limb_t *ap, volatile mp_limb_t *bp, mp_size_t n); |
71 | | |
72 | | void |
73 | | mpn_sec_tabselect (volatile mp_limb_t *rp, volatile const mp_limb_t *table, |
74 | | mp_size_t rn, unsigned tn, unsigned k); |
75 | | #endif |
76 | | |
77 | | static inline int |
78 | | is_zero_limb (mp_limb_t x) |
79 | 0 | { |
80 | 0 | x |= (x << 1); |
81 | 0 | return ((x >> 1) - 1) >> (GMP_LIMB_BITS - 1); |
82 | 0 | } Unexecuted instantiation: bignum-random.c:is_zero_limb Unexecuted instantiation: rsa-sign.c:is_zero_limb Unexecuted instantiation: rsa-sec-compute-root.c:is_zero_limb Unexecuted instantiation: rsa-oaep-decrypt.c:is_zero_limb Unexecuted instantiation: rsa-sec-decrypt.c:is_zero_limb Unexecuted instantiation: rsa-decrypt-tr.c:is_zero_limb Unexecuted instantiation: dsa-sign.c:is_zero_limb Unexecuted instantiation: dsa-verify.c:is_zero_limb Unexecuted instantiation: dsa-hash.c:is_zero_limb Unexecuted instantiation: gmp-glue.c:is_zero_limb Unexecuted instantiation: ecc-gost-gc256b.c:is_zero_limb Unexecuted instantiation: ecc-gost-gc512a.c:is_zero_limb Unexecuted instantiation: ecc-secp192r1.c:is_zero_limb Unexecuted instantiation: ecc-secp224r1.c:is_zero_limb Unexecuted instantiation: ecc-secp256r1.c:is_zero_limb Unexecuted instantiation: ecc-secp384r1.c:is_zero_limb Unexecuted instantiation: ecc-secp521r1.c:is_zero_limb Unexecuted instantiation: ecc-size.c:is_zero_limb Unexecuted instantiation: ecc-j-to-a.c:is_zero_limb Unexecuted instantiation: ecc-dup-jj.c:is_zero_limb Unexecuted instantiation: ecc-add-jja.c:is_zero_limb Unexecuted instantiation: ecc-add-jjj.c:is_zero_limb Unexecuted instantiation: ecc-mul-g.c:is_zero_limb Unexecuted instantiation: ecc-mul-a.c:is_zero_limb Unexecuted instantiation: ecc-point.c:is_zero_limb Unexecuted instantiation: ecc-scalar.c:is_zero_limb Unexecuted instantiation: ecc-point-mul.c:is_zero_limb Unexecuted instantiation: ecc-point-mul-g.c:is_zero_limb Unexecuted instantiation: ecdsa-sign.c:is_zero_limb Unexecuted instantiation: ecdsa-verify.c:is_zero_limb Unexecuted instantiation: ecdsa-keygen.c:is_zero_limb Unexecuted instantiation: gostdsa-sign.c:is_zero_limb Unexecuted instantiation: gostdsa-verify.c:is_zero_limb Unexecuted instantiation: gostdsa-vko.c:is_zero_limb Unexecuted instantiation: curve25519-mul-g.c:is_zero_limb Unexecuted instantiation: curve25519-mul.c:is_zero_limb Unexecuted instantiation: curve25519-eh-to-x.c:is_zero_limb Unexecuted instantiation: curve448-mul-g.c:is_zero_limb Unexecuted instantiation: curve448-mul.c:is_zero_limb Unexecuted instantiation: curve448-eh-to-x.c:is_zero_limb Unexecuted instantiation: ed25519-sha512-pubkey.c:is_zero_limb Unexecuted instantiation: ed25519-sha512-sign.c:is_zero_limb Unexecuted instantiation: ed25519-sha512-verify.c:is_zero_limb Unexecuted instantiation: ed448-shake256-pubkey.c:is_zero_limb Unexecuted instantiation: ed448-shake256-sign.c:is_zero_limb Unexecuted instantiation: ed448-shake256-verify.c:is_zero_limb Unexecuted instantiation: pkcs1-encrypt.c:is_zero_limb Unexecuted instantiation: oaep.c:is_zero_limb Unexecuted instantiation: pkcs1-sec-decrypt.c:is_zero_limb Unexecuted instantiation: pkcs1-rsa-digest.c:is_zero_limb Unexecuted instantiation: pss.c:is_zero_limb Unexecuted instantiation: rsa-sign-tr.c:is_zero_limb Unexecuted instantiation: sec-add-1.c:is_zero_limb Unexecuted instantiation: cnd-copy.c:is_zero_limb Unexecuted instantiation: ecc-mod.c:is_zero_limb Unexecuted instantiation: ecc-mod-inv.c:is_zero_limb Unexecuted instantiation: ecc-mod-arith.c:is_zero_limb Unexecuted instantiation: ecc-pp1-redc.c:is_zero_limb Unexecuted instantiation: ecc-pm1-redc.c:is_zero_limb Unexecuted instantiation: ecc-curve25519.c:is_zero_limb Unexecuted instantiation: ecc-curve448.c:is_zero_limb Unexecuted instantiation: ecc-a-to-j.c:is_zero_limb Unexecuted instantiation: ecc-eh-to-a.c:is_zero_limb Unexecuted instantiation: ecc-dup-eh.c:is_zero_limb Unexecuted instantiation: ecc-add-eh.c:is_zero_limb Unexecuted instantiation: ecc-add-ehh.c:is_zero_limb Unexecuted instantiation: ecc-dup-th.c:is_zero_limb Unexecuted instantiation: ecc-add-th.c:is_zero_limb Unexecuted instantiation: ecc-add-thh.c:is_zero_limb Unexecuted instantiation: ecc-mul-g-eh.c:is_zero_limb Unexecuted instantiation: ecc-mul-a-eh.c:is_zero_limb Unexecuted instantiation: ecc-mul-m.c:is_zero_limb Unexecuted instantiation: ecc-random.c:is_zero_limb Unexecuted instantiation: ecc-ecdsa-sign.c:is_zero_limb Unexecuted instantiation: ecc-ecdsa-verify.c:is_zero_limb Unexecuted instantiation: ecc-gostdsa-sign.c:is_zero_limb Unexecuted instantiation: ecc-gostdsa-verify.c:is_zero_limb Unexecuted instantiation: eddsa-decompress.c:is_zero_limb Unexecuted instantiation: eddsa-expand.c:is_zero_limb Unexecuted instantiation: eddsa-pubkey.c:is_zero_limb Unexecuted instantiation: eddsa-sign.c:is_zero_limb Unexecuted instantiation: eddsa-verify.c:is_zero_limb Unexecuted instantiation: ecc-nonsec-add-jjj.c:is_zero_limb Unexecuted instantiation: eddsa-compress.c:is_zero_limb Unexecuted instantiation: eddsa-hash.c:is_zero_limb |
83 | | |
84 | | /* Side-channel silent variant of mpn_zero_p. */ |
85 | | int |
86 | | sec_zero_p (const mp_limb_t *ap, mp_size_t n); |
87 | | |
88 | | #define NETTLE_BIT_SIZE_TO_LIMB_SIZE(n) \ |
89 | 0 | (((n) + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS) |
90 | | |
91 | | #define NETTLE_OCTET_SIZE_TO_LIMB_SIZE(n) \ |
92 | 0 | (NETTLE_BIT_SIZE_TO_LIMB_SIZE((n) * 8)) |
93 | | |
94 | | /* Convenience functions */ |
95 | | |
96 | | /* Copy limbs, with zero-padding. */ |
97 | | /* FIXME: Reorder arguments, on the theory that the first argument of |
98 | | an _mpz_* function should be an mpz_t? Or rename to _mpz_get_limbs, |
99 | | with argument order consistent with mpz_get_*. */ |
100 | | void |
101 | | mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n); |
102 | | |
103 | | void |
104 | | mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn); |
105 | | |
106 | | /* Like mpn_set_str, but always writes rn limbs. If input is larger, |
107 | | higher bits are ignored. */ |
108 | | void |
109 | | mpn_set_base256 (mp_limb_t *rp, mp_size_t rn, |
110 | | const uint8_t *xp, size_t xn); |
111 | | |
112 | | void |
113 | | mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn, |
114 | | const uint8_t *xp, size_t xn); |
115 | | |
116 | | void |
117 | | mpn_get_base256 (uint8_t *rp, size_t rn, |
118 | | const mp_limb_t *xp, mp_size_t xn); |
119 | | |
120 | | void |
121 | | mpn_get_base256_le (uint8_t *rp, size_t rn, |
122 | | const mp_limb_t *xp, mp_size_t xn); |
123 | | |
124 | | |
125 | | mp_limb_t * |
126 | | gmp_alloc_limbs (mp_size_t n); |
127 | | |
128 | | void |
129 | | gmp_free_limbs (mp_limb_t *p, mp_size_t n); |
130 | | |
131 | | void *gmp_alloc(size_t n); |
132 | | void gmp_free(void *p, size_t n); |
133 | | |
134 | | #endif /* NETTLE_GMP_GLUE_H_INCLUDED */ |