Coverage Report

Created: 2024-11-25 06:29

/src/nettle/ecdsa-verify.c
Line
Count
Source (jump to first uncovered line)
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
46
ecdsa_verify (const struct ecc_point *pub,
47
        size_t length, const uint8_t *digest,
48
        const struct dsa_signature *signature)
49
4.24k
{
50
4.24k
  mp_limb_t size = ecc_size (pub->ecc);
51
4.24k
  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
4.24k
  mp_limb_t *scratch;
56
4.24k
  int res;
57
58
8.48k
#define rp scratch
59
8.48k
#define sp (scratch + size)
60
4.24k
#define scratch_out (scratch + 2*size)
61
62
4.24k
  if (mpz_sgn (signature->r) <= 0 || mpz_size (signature->r) > size
63
4.24k
      || mpz_sgn (signature->s) <= 0 || mpz_size (signature->s) > size)
64
0
    return 0;
65
66
4.24k
  scratch = gmp_alloc_limbs (itch);
67
  
68
4.24k
  mpz_limbs_copy (rp, signature->r, size);
69
4.24k
  mpz_limbs_copy (sp, signature->s, size);
70
71
4.24k
  res = ecc_ecdsa_verify (pub->ecc, pub->p, length, digest, rp, sp, scratch_out);
72
73
4.24k
  gmp_free_limbs (scratch, itch);
74
75
4.24k
  return res;
76
4.24k
#undef rp
77
4.24k
#undef sp
78
4.24k
#undef scratch_out
79
4.24k
}