1 /* ecc-ecdsa-verify.c
2
3 Copyright (C) 2013 Niels Möller
4
5 This file is part of GNU Nettle.
6
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
9
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14 or
15
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
19
20 or both in parallel, as here.
21
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
26
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
30 */
31
32 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include "ecdsa.h"
42
43 #include "gmp-glue.h"
44
45 int
ecdsa_verify(const struct ecc_point * pub,size_t length,const uint8_t * digest,const struct dsa_signature * signature)46 ecdsa_verify (const struct ecc_point *pub,
47 size_t length, const uint8_t *digest,
48 const struct dsa_signature *signature)
49 {
50 mp_limb_t size = ecc_size (pub->ecc);
51 mp_size_t itch = 2*size + ecc_ecdsa_verify_itch (pub->ecc);
52 /* For ECC_MUL_A_WBITS == 0, at most 1512 bytes. With
53 ECC_MUL_A_WBITS == 4, currently needs 67 * ecc->size, at most
54 4824 bytes. Don't use stack allocation for this. */
55 mp_limb_t *scratch;
56 int res;
57
58 #define rp scratch
59 #define sp (scratch + size)
60 #define scratch_out (scratch + 2*size)
61
62 if (mpz_sgn (signature->r) <= 0 || mpz_size (signature->r) > size
63 || mpz_sgn (signature->s) <= 0 || mpz_size (signature->s) > size)
64 return 0;
65
66 scratch = gmp_alloc_limbs (itch);
67
68 mpz_limbs_copy (rp, signature->r, size);
69 mpz_limbs_copy (sp, signature->s, size);
70
71 res = ecc_ecdsa_verify (pub->ecc, pub->p, length, digest, rp, sp, scratch_out);
72
73 gmp_free_limbs (scratch, itch);
74
75 return res;
76 #undef rp
77 #undef sp
78 #undef scratch_out
79 }
80