summaryrefslogtreecommitdiff
path: root/testsuite/curve25519-dh-test.c
blob: e9037523a319ead5415b7622c159af16e15f31a0 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* curve25519-dh-test.c

   Copyright (C) 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#include "testutils.h"

#include "curve25519.h"

static void
test_g (const uint8_t *s, const uint8_t *r)
{
  uint8_t p[CURVE25519_SIZE];
  curve25519_mul_g (p, s);
  if (!MEMEQ (CURVE25519_SIZE, p, r))
    {
      printf ("curve25519_mul_g failure:\ns = ");
      print_hex (CURVE25519_SIZE, s);
      printf ("\np = ");
      print_hex (CURVE25519_SIZE, p);
      printf (" (bad)\nr = ");
      print_hex (CURVE25519_SIZE, r);
      printf (" (expected)\n");
      abort ();
    }
}

static void
test_a (const uint8_t *s, const uint8_t *b, const uint8_t *r)
{
  uint8_t p[CURVE25519_SIZE];
  curve25519_mul (p, s, b);
    
  if (!MEMEQ (CURVE25519_SIZE, p, r))
    {
      printf ("curve25519_mul failure:\ns = ");
      print_hex (CURVE25519_SIZE, s);
      printf ("\nb = ");
      print_hex (CURVE25519_SIZE, b);
      printf ("\np = ");
      print_hex (CURVE25519_SIZE, p);
      printf (" (bad)\nr = ");
      print_hex (CURVE25519_SIZE, r);
      printf (" (expected)\n");
      abort ();
    }
}

void
test_main (void)
{
  /* From RFC 7748. */
  test_g (H("77076d0a7318a57d3c16c17251b26645"
	    "df4c2f87ebc0992ab177fba51db92c2a"),
	  H("8520f0098930a754748b7ddcb43ef75a"
	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"));
  test_g (H("5dab087e624a8a4b79e17f8b83800ee6"
	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
	    "3f8343c85b78674dadfc7e146f882b4f"));

  test_a (H("77076d0a7318a57d3c16c17251b26645"
	    "df4c2f87ebc0992ab177fba51db92c2a"),
	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
	    "3f8343c85b78674dadfc7e146f882b4f"),
	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
	    "e07e21c947d19e3376f09b3c1e161742"));

  test_a (H("5dab087e624a8a4b79e17f8b83800ee6"
	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
	  H("8520f0098930a754748b7ddcb43ef75a"
	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"),
	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
	    "e07e21c947d19e3376f09b3c1e161742"));

  /* Check that the least significant three bits (first octet) of the
     scalar are ignored by mul_g. */
  test_g (H("70076d0a7318a57d3c16c17251b26645"
	    "df4c2f87ebc0992ab177fba51db92c2a"),
	  H("8520f0098930a754748b7ddcb43ef75a"
	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"));
  /* Check that the most significant two bits (last octet) of the
     scalar are ignored by mul_g. */
  test_g (H("5dab087e624a8a4b79e17f8b83800ee6"
	    "6f3bb1292618b6fd1c2f8b27ff88e02b"),
	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
	    "3f8343c85b78674dadfc7e146f882b4f"));

  /* Check that the least significant three bits (first octet) of the
     scalar are ignored by mul_a. */
  test_a (H("5aab087e624a8a4b79e17f8b83800ee6"
	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
	  H("8520f0098930a754748b7ddcb43ef75a"
	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"),
	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
	    "e07e21c947d19e3376f09b3c1e161742"));

  /* Check that the most significant two bits (last octet) of the
     scalar are ignored by mul_g. */
  test_a (H("77076d0a7318a57d3c16c17251b26645"
	    "df4c2f87ebc0992ab177fba51db92cea"),
	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
	    "3f8343c85b78674dadfc7e146f882b4f"),
	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
	    "e07e21c947d19e3376f09b3c1e161742"));

  /* Check that the most significant bit (last octet) of the x
     coordinate is ignored. */
  test_a (H("77076d0a7318a57d3c16c17251b26645"
	    "df4c2f87ebc0992ab177fba51db92c2a"),
	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
	    "3f8343c85b78674dadfc7e146f882bcf"),
	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
	    "e07e21c947d19e3376f09b3c1e161742"));
}