summaryrefslogtreecommitdiff
path: root/tests/fipsrngdrv.c
blob: 0fc19d564abd8fd121c0f65ddf7798177d762f99 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* fipsrngdrv.c  -  A driver to test the FIPS RNG.
   Copyright (C) 2008 Free Software Foundation, Inc.

   This file is part of Libgcrypt.

   Libgcrypt is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   Libgcrypt 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#ifndef HAVE_W32_SYSTEM
# include <signal.h>
#endif

#include <gcrypt.h>

#define PGM "fipsrngdrv"

#define my_isascii(c) (!((c) & 0x80))
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
#define hexdigitp(a) (digitp (a)                     \
                      || (*(a) >= 'A' && *(a) <= 'F')  \
                      || (*(a) >= 'a' && *(a) <= 'f'))
#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))


static void
die (const char *format, ...)
{
  va_list arg_ptr;

  va_start (arg_ptr, format);
  fputs (PGM ": ", stderr);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  exit (1);
}


/* Convert STRING consisting of hex characters into its binary
   representation and store that at BUFFER.  BUFFER needs to be of
   LENGTH bytes.  The function checks that the STRING will convert
   exactly to LENGTH bytes. The string is delimited by either end of
   string or a white space character.  The function returns -1 on
   error or the length of the parsed string.  */
static int
hex2bin (const char *string, void *buffer, size_t length)
{
  int i;
  const char *s = string;

  for (i=0; i < length; )
    {
      if (!hexdigitp (s) || !hexdigitp (s+1))
        return -1;           /* Invalid hex digits. */
      ((unsigned char*)buffer)[i++] = xtoi_2 (s);
      s += 2;
    }
  if (*s && (!my_isascii (*s) || !isspace (*s)) )
    return -1;             /* Not followed by Nul or white space.  */
  if (i != length)
    return -1;             /* Not of expected length.  */
  if (*s)
    s++; /* Skip the delimiter. */
  return s - string;
}




static gcry_error_t
init_external_test (void **r_context,
                    unsigned int flags,
                    const void *key, size_t keylen,
                    const void *seed, size_t seedlen,
                    const void *dt, size_t dtlen)
{
  return gcry_control (58,
                       r_context, flags,
                       key, keylen,
                       seed, seedlen,
                       dt, dtlen);
}

static gcry_error_t
run_external_test (void *context, void *buffer, size_t buflen)
{
  return gcry_control (59, context, buffer, buflen);
}

static void
deinit_external_test (void *context)
{
  gcry_control (60, context);
}


static void
print_buffer (const unsigned char *buffer, size_t length)
{
  while (length--)
    printf ("%02X", *buffer++);
}


int
main (int argc, char **argv)
{
  int last_argc = -1;
  int verbose = 0;
  int binary = 0;
  int loop = 0;
  int progress = 0;
  int no_fips = 0;
  unsigned char key[16];
  unsigned char seed[16];
  unsigned char dt[16];
  void *context;
  gpg_error_t err;
  unsigned char buffer[16];

  if (argc)
    { argc--; argv++; }

  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--help"))
        {
          fputs ("usage: " PGM
                 " [--verbose] [--binary] [--loop] [--progress] KEY V DT\n",
                 stdout);
          exit (0);
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--no-fips"))
        {
          no_fips++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--binary"))
        {
          binary = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--loop"))
        {
          loop = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--progress"))
        {
          progress = 1;
          argc--; argv++;
        }
    }

  if (!argc)
    {
      memcpy (key,  "1234567890123456", 16);
      memcpy (seed, "abcdefghijklmnop", 16);
      memcpy (dt,   "XXXXXXXXXXXXXXXX", 16);
    }
  else if (argc == 3)
    {
      if (    hex2bin (argv[0], key, 16) < 0
           || hex2bin (argv[1], seed, 16) < 0
           || hex2bin (argv[2], dt, 16) < 0 )
        die ("args are not 32 hex digits each\n");
    }
  else
    die ("invalid usage (try --help)\n");

#ifndef HAVE_W32_SYSTEM
  if (loop)
    signal (SIGPIPE, SIG_IGN);
#endif

  if (verbose)
    fputs (PGM ": started\n", stderr);

  gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
  if (!no_fips)
    gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
  if (!gcry_check_version ("1.4.3"))
    die ("version mismatch\n");
  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);

  /* The flag value 1 disables the dup check, so that the RNG returns
     all generated data.  */
  err = init_external_test (&context, 1, key, 16, seed, 16, dt, 16);
  if (err)
    die ("init external test failed: %s\n", gpg_strerror (err));

  do
    {
      int writerr = 0;

      err = run_external_test (context, buffer, sizeof buffer);
      if (err)
        die ("run external test failed: %s\n", gpg_strerror (err));
      if (binary)
        {
          if (fwrite (buffer, 16, 1, stdout) != 1)
            writerr = 1;
          else
            fflush (stdout);
        }
      else
        {
          print_buffer (buffer, sizeof buffer);
          if (putchar ('\n') == EOF)
            writerr = 1;
        }
      if (writerr)
        {
#ifndef HAVE_W32_SYSTEM
          if (loop && errno == EPIPE)
            break;
#endif
          die ("writing output failed: %s\n", strerror (errno));
        }

      if (progress)
        {
          putc ('.', stderr);
          fflush (stderr);
        }
    }
  while (loop);

  if (progress)
    putc ('\n', stderr);

  deinit_external_test (context);

  if (verbose)
    fputs (PGM ": ready\n", stderr);

  return 0;
}