1/* curves.c -  ECC curves regression tests
2 *	Copyright (C) 2011 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdarg.h>
28
29#include "../src/gcrypt.h"
30
31/* Number of curves defined in ../cipger/ecc.c */
32#define N_CURVES 12
33
34/* A real world sample public key.  */
35static char const sample_key_1[] =
36"(public-key\n"
37" (ecdsa\n"
38"  (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)\n"
39"  (a #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC#)\n"
40"  (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)\n"
41"  (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
42        "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)\n"
43"  (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)\n"
44"  (q #0442B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146EE"
45      "86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E#)\n"
46"  ))";
47static char const sample_key_1_curve[] = "NIST P-256";
48static unsigned int sample_key_1_nbits = 256;
49
50/* A made up sample public key.  */
51static char const sample_key_2[] =
52"(public-key\n"
53" (ecdh\n"
54"  (p #e95e4a5f737059dc60dfc7ad95b3d8139515620f#)\n"
55"  (a #340e7be2a280eb74e2be61bada745d97e8f7c300#)\n"
56"  (b #1e589a8595423412134faa2dbdec95c8d8675e58#)\n"
57"  (g #04bed5af16ea3f6a4f62938c4631eb5af7bdbcdbc3"
58        "1667cb477a1a8ec338f94741669c976316da6321#)\n"
59"  (n #e95e4a5f737059dc60df5991d45029409e60fc09#)\n"
60"  (q #041111111111111111111111111111111111111111"
61        "2222222222222222222222222222222222222222#)\n"
62"  ))";
63static char const sample_key_2_curve[] = "brainpoolP160r1";
64static unsigned int sample_key_2_nbits = 160;
65
66
67/* Program option flags.  */
68static int verbose;
69static int error_count;
70
71static void
72fail (const char *format, ...)
73{
74  va_list arg_ptr;
75
76  va_start (arg_ptr, format);
77  vfprintf (stderr, format, arg_ptr);
78  va_end (arg_ptr);
79  error_count++;
80}
81
82static void
83die (const char *format, ...)
84{
85  va_list arg_ptr;
86
87  va_start (arg_ptr, format);
88  vfprintf (stderr, format, arg_ptr);
89  va_end (arg_ptr);
90  exit (1);
91}
92
93
94static void
95list_curves (void)
96{
97  int idx;
98  const char *name;
99  unsigned int nbits;
100
101  for (idx=0; (name = gcry_pk_get_curve (NULL, idx, &nbits)); idx++)
102    {
103      if (verbose)
104        printf ("%s - %u bits\n", name, nbits);
105    }
106  if (idx != N_CURVES)
107    fail ("expected %d curves but got %d\n", N_CURVES, idx);
108  if (gcry_pk_get_curve (NULL, -1, NULL))
109    fail ("curve iteration failed\n");
110}
111
112
113static void
114check_matching (void)
115{
116  gpg_error_t err;
117  gcry_sexp_t key;
118  const char *name;
119  unsigned int nbits;
120
121  err = gcry_sexp_new (&key, sample_key_1, 0, 1);
122  if (err)
123    die ("parsing s-expression string failed: %s\n", gpg_strerror (err));
124  name = gcry_pk_get_curve (key, 0, &nbits);
125  if (!name)
126    fail ("curve name not found for sample_key_1\n");
127  else if (strcmp (name, sample_key_1_curve))
128    fail ("expected curve name %s but got %s for sample_key_1\n",
129          sample_key_1_curve, name);
130  else if (nbits != sample_key_1_nbits)
131    fail ("expected curve size %u but got %u for sample_key_1\n",
132          sample_key_1_nbits, nbits);
133
134  gcry_sexp_release (key);
135
136  err = gcry_sexp_new (&key, sample_key_2, 0, 1);
137  if (err)
138    die ("parsing s-expression string failed: %s\n", gpg_strerror (err));
139  name = gcry_pk_get_curve (key, 0, &nbits);
140  if (!name)
141    fail ("curve name not found for sample_key_2\n");
142  else if (strcmp (name, sample_key_2_curve))
143    fail ("expected curve name %s but got %s for sample_key_2\n",
144          sample_key_2_curve, name);
145  else if (nbits != sample_key_2_nbits)
146    fail ("expected curve size %u but got %u for sample_key_2\n",
147          sample_key_2_nbits, nbits);
148
149  gcry_sexp_release (key);
150}
151
152
153static void
154check_get_params (void)
155{
156  gcry_sexp_t param;
157  const char *name;
158
159  param = gcry_pk_get_param (GCRY_PK_ECDSA, sample_key_1_curve);
160  if (!param)
161    fail ("error gerring parameters for `%s'\n", sample_key_1_curve);
162
163  name = gcry_pk_get_curve (param, 0, NULL);
164  if (!name)
165    fail ("get_param: curve name not found for sample_key_1\n");
166  else if (strcmp (name, sample_key_1_curve))
167    fail ("get_param: expected curve name %s but got %s for sample_key_1\n",
168          sample_key_1_curve, name);
169
170  gcry_sexp_release (param);
171
172
173  param = gcry_pk_get_param (GCRY_PK_ECDSA, sample_key_2_curve);
174  if (!param)
175    fail ("error gerring parameters for `%s'\n", sample_key_2_curve);
176
177  name = gcry_pk_get_curve (param, 0, NULL);
178  if (!name)
179    fail ("get_param: curve name not found for sample_key_2\n");
180  else if (strcmp (name, sample_key_2_curve))
181    fail ("get_param: expected curve name %s but got %s for sample_key_2\n",
182          sample_key_2_curve, name);
183
184  gcry_sexp_release (param);
185}
186
187
188int
189main (int argc, char **argv)
190{
191  int debug = 0;
192
193  if (argc > 1 && !strcmp (argv[1], "--verbose"))
194    verbose = 1;
195  else if (argc > 1 && !strcmp (argv[1], "--debug"))
196    verbose = debug = 1;
197
198  if (!gcry_check_version (GCRYPT_VERSION))
199    die ("version mismatch\n");
200
201  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
202  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
203  if (debug)
204    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
205  list_curves ();
206  check_matching ();
207  check_get_params ();
208
209  return error_count ? 1 : 0;
210}
211