Coverage Report

Created: 2022-12-08 06:09

/src/libgcrypt/random/random-drbg.c
Line
Count
Source (jump to first uncovered line)
1
/* random-drbg.c - Deterministic Random Bits Generator
2
 * Copyright 2014 Stephan Mueller <smueller@chronox.de>
3
 *
4
 * DRBG: Deterministic Random Bits Generator
5
 *       Based on NIST Recommended DRBG from NIST SP800-90A with the following
6
 *       properties:
7
 *    * CTR DRBG with DF with AES-128, AES-192, AES-256 cores
8
 *    * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
9
 *    * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
10
 *    * with and without prediction resistance
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, and the entire permission notice in its entirety,
17
 *    including the disclaimer of warranties.
18
 * 2. Redistributions in binary form must reproduce the above copyright
19
 *    notice, this list of conditions and the following disclaimer in the
20
 *    documentation and/or other materials provided with the distribution.
21
 * 3. The name of the author may not be used to endorse or promote
22
 *    products derived from this software without specific prior
23
 *    written permission.
24
 *
25
 * ALTERNATIVELY, this product may be distributed under the terms of
26
 * LGPLv2+, in which case the provisions of the LGPL are
27
 * required INSTEAD OF the above restrictions.  (This clause is
28
 * necessary due to a potential bad interaction between the LGPL and
29
 * the restrictions contained in a BSD-style copyright.)
30
 *
31
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
34
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
35
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
41
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
42
 * DAMAGE.
43
 *
44
 *
45
 * gcry_control GCRYCTL_DRBG_REINIT
46
 * ================================
47
 * This control request re-initializes the DRBG completely, i.e. the entire
48
 * state of the DRBG is zeroized (with two exceptions listed in
49
 * GCRYCTL_DRBG_SET_ENTROPY).
50
 *
51
 * The control request takes the following values which influences how
52
 * the DRBG is re-initialized:
53
 *
54
 *  - const char *flagstr
55
 *
56
 *      This variable specifies the DRBG type to be used for the next
57
 *  initialization.  If set to NULL, the previous DRBG type is
58
 *  used for the initialization.  If not NULL a space separated
59
 *  list of tokens with associated flag values is expected which
60
 *  are ORed to form the mandatory flags of the requested DRBG
61
 *  strength and cipher type.  Optionally, the prediction
62
 *  resistance flag can be ORed into the flags variable.
63
 *
64
 *      | String token | Flag value             |
65
 *      |--------------+------------------------|
66
 *      | aes          | DRBG_CTRAES            |
67
 *      | serpent      | DRBG_CTRSERPENT        |
68
 *      | twofish      | DRBG_CTRTWOFISH        |
69
 *      | sha1         | DRBG_HASHSHA1          |
70
 *      | sha256       | DRBG_HASHSHA256        |
71
 *      | sha512       | DRBG_HASHSHA512        |
72
 *      | hmac         | DRBG_HMAC              |
73
 *      | sym128       | DRBG_SYM128            |
74
 *      | sym192       | DRBG_SYM192            |
75
 *      | sym256       | DRBG_SYM256            |
76
 *      | pr           | DRBG_PREDICTION_RESIST |
77
 *
78
 *    For example:
79
 *
80
 *  - CTR-DRBG with AES-128 without prediction resistance:
81
 *      "aes sym128"
82
 *  - HMAC-DRBG with SHA-512 with prediction resistance:
83
 *      "hmac sha512 pr"
84
 *
85
 *  - gcry_buffer_t *pers
86
 *
87
 *      NULL terminated array with personalization strings to be used
88
 *  for initialization.
89
 *
90
 *  - int npers
91
 *
92
 *     Size of PERS.
93
 *
94
 *  - void *guard
95
 *
96
 *      A value of NULL must be passed for this.
97
 *
98
 * The variable of flags is independent from the pers/perslen variables. If
99
 * flags is set to 0 and perslen is set to 0, the current DRBG type is
100
 * completely reset without using a personalization string.
101
 *
102
 * DRBG Usage
103
 * ==========
104
 * The SP 800-90A DRBG allows the user to specify a personalization string
105
 * for initialization as well as an additional information string for each
106
 * random number request.  The following code fragments show how a caller
107
 * uses the API to use the full functionality of the DRBG.
108
 *
109
 * Usage without any additional data
110
 * ---------------------------------
111
 * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
112
 *
113
 *
114
 * Usage with personalization string during initialization
115
 * -------------------------------------------------------
116
 * drbg_string_t pers;
117
 * char personalization[11] = "some-string";
118
 *
119
 * drbg_string_fill(&pers, personalization, strlen(personalization));
120
 * // The reset completely re-initializes the DRBG with the provided
121
 * // personalization string without changing the DRBG type
122
 * ret = gcry_control(GCRYCTL_DRBG_REINIT, 0, &pers);
123
 * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
124
 *
125
 *
126
 * Usage with additional information string during random number request
127
 * ---------------------------------------------------------------------
128
 * drbg_string_t addtl;
129
 * char addtl_string[11] = "some-string";
130
 *
131
 * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
132
 * // The following call is a wrapper to gcry_randomize() and returns
133
 * // the same error codes.
134
 * gcry_randomize_drbg(outbuf, OUTLEN, GCRY_STRONG_RANDOM, &addtl);
135
 *
136
 *
137
 * Usage with personalization and additional information strings
138
 * -------------------------------------------------------------
139
 * Just mix both scenarios above.
140
 *
141
 *
142
 * Switch the DRBG type to some other type
143
 * ---------------------------------------
144
 * // Switch to CTR DRBG AES-128 without prediction resistance
145
 * ret = gcry_control(GCRYCTL_DRBG_REINIT, DRBG_NOPR_CTRAES128, NULL);
146
 * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
147
 */
148
149
#include <config.h>
150
151
#include <string.h>
152
#include <unistd.h>
153
#include <stdint.h>
154
155
#include "g10lib.h"
156
#include "random.h"
157
#include "rand-internal.h"
158
#include "../cipher/bufhelp.h"
159
160
161

162
/******************************************************************
163
 * Constants
164
 ******************************************************************/
165
166
/*
167
 * DRBG flags bitmasks
168
 *
169
 * 31 (B) 28      19         (A)         0
170
 *  +-+-+-+--------+---+-----------+-----+
171
 *  |~|~|u|~~~~~~~~| 3 |     2     |  1  |
172
 *  +-+-+-+--------+- -+-----------+-----+
173
 * ctl flg|        |drbg use selection flags
174
 *
175
 */
176
177
/* Internal state control flags (B) */
178
0
#define DRBG_PREDICTION_RESIST  ((u32)1<<28)
179
180
/* CTR type modifiers (A.1)*/
181
0
#define DRBG_CTRAES   ((u32)1<<0)
182
0
#define DRBG_CTRSERPENT   ((u32)1<<1)
183
0
#define DRBG_CTRTWOFISH   ((u32)1<<2)
184
0
#define DRBG_CTR_MASK         (DRBG_CTRAES | DRBG_CTRSERPENT \
185
0
                                 | DRBG_CTRTWOFISH)
186
187
/* HASH type modifiers (A.2)*/
188
0
#define DRBG_HASHSHA1   ((u32)1<<4)
189
0
#define DRBG_HASHSHA224   ((u32)1<<5)
190
0
#define DRBG_HASHSHA256   ((u32)1<<6)
191
0
#define DRBG_HASHSHA384   ((u32)1<<7)
192
0
#define DRBG_HASHSHA512   ((u32)1<<8)
193
0
#define DRBG_HASH_MASK    (DRBG_HASHSHA1 | DRBG_HASHSHA224 \
194
0
         | DRBG_HASHSHA256 | DRBG_HASHSHA384 \
195
0
         | DRBG_HASHSHA512)
196
/* type modifiers (A.3)*/
197
0
#define DRBG_HMAC   ((u32)1<<12)
198
0
#define DRBG_SYM128   ((u32)1<<13)
199
0
#define DRBG_SYM192   ((u32)1<<14)
200
0
#define DRBG_SYM256   ((u32)1<<15)
201
0
#define DRBG_TYPE_MASK    (DRBG_HMAC | DRBG_SYM128 | DRBG_SYM192 \
202
0
         | DRBG_SYM256)
203
0
#define DRBG_CIPHER_MASK        (DRBG_CTR_MASK | DRBG_HASH_MASK \
204
0
                                 | DRBG_TYPE_MASK)
205
206
#define DRBG_PR_CTRAES128   (DRBG_PREDICTION_RESIST | DRBG_CTRAES | DRBG_SYM128)
207
#define DRBG_PR_CTRAES192   (DRBG_PREDICTION_RESIST | DRBG_CTRAES | DRBG_SYM192)
208
#define DRBG_PR_CTRAES256   (DRBG_PREDICTION_RESIST | DRBG_CTRAES | DRBG_SYM256)
209
#define DRBG_NOPR_CTRAES128 (DRBG_CTRAES | DRBG_SYM128)
210
#define DRBG_NOPR_CTRAES192 (DRBG_CTRAES | DRBG_SYM192)
211
#define DRBG_NOPR_CTRAES256 (DRBG_CTRAES | DRBG_SYM256)
212
#define DRBG_PR_HASHSHA1     (DRBG_PREDICTION_RESIST | DRBG_HASHSHA1)
213
#define DRBG_PR_HASHSHA256   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA256)
214
#define DRBG_PR_HASHSHA384   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA384)
215
#define DRBG_PR_HASHSHA512   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA512)
216
#define DRBG_NOPR_HASHSHA1   (DRBG_HASHSHA1)
217
#define DRBG_NOPR_HASHSHA256 (DRBG_HASHSHA256)
218
#define DRBG_NOPR_HASHSHA384 (DRBG_HASHSHA384)
219
#define DRBG_NOPR_HASHSHA512 (DRBG_HASHSHA512)
220
#define DRBG_PR_HMACSHA1     (DRBG_PREDICTION_RESIST | DRBG_HASHSHA1 \
221
                              | DRBG_HMAC)
222
#define DRBG_PR_HMACSHA256   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA256 \
223
                              | DRBG_HMAC)
224
#define DRBG_PR_HMACSHA384   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA384 \
225
                              | DRBG_HMAC)
226
#define DRBG_PR_HMACSHA512   (DRBG_PREDICTION_RESIST | DRBG_HASHSHA512 \
227
                              | DRBG_HMAC)
228
#define DRBG_NOPR_HMACSHA1   (DRBG_HASHSHA1 | DRBG_HMAC)
229
0
#define DRBG_NOPR_HMACSHA256 (DRBG_HASHSHA256 | DRBG_HMAC)
230
#define DRBG_NOPR_HMACSHA384 (DRBG_HASHSHA384 | DRBG_HMAC)
231
#define DRBG_NOPR_HMACSHA512 (DRBG_HASHSHA512 | DRBG_HMAC)
232
233
234
/* The default DRGB type.  */
235
0
#define DRBG_DEFAULT_TYPE    DRBG_NOPR_HMACSHA256
236
237
238
#define DRBG_CTR_NULL_LEN 128
239
240

241
/******************************************************************
242
 * Common data structures
243
 ******************************************************************/
244
245
/*
246
 * SP800-90A requires the concatenation of different data. To avoid copying
247
 * buffers around or allocate additional memory, the following data structure
248
 * is used to point to the original memory with its size. In addition, it
249
 * is used to build a linked list. The linked list defines the concatenation
250
 * of individual buffers. The order of memory block referenced in that
251
 * linked list determines the order of concatenation.
252
 */
253
struct drbg_string_s
254
{
255
  const unsigned char *buf;
256
  size_t len;
257
  struct drbg_string_s *next;
258
};
259
typedef struct drbg_string_s drbg_string_t;
260
261
262
/* DRBG input data structure for DRBG generate with additional
263
 * information string.  */
264
struct drbg_gen_s
265
{
266
  unsigned char *outbuf;  /* output buffer for random numbers */
267
  unsigned int outlen;          /* size of output buffer */
268
  drbg_string_t *addtl;         /* input buffer for
269
         * additional information string */
270
};
271
typedef struct drbg_gen_s drbg_gen_t;
272
273
274
/* Forward declaration of the state object pointer.  */
275
struct drbg_state_s;
276
typedef struct drbg_state_s *drbg_state_t;
277
278
279
struct drbg_core_s
280
{
281
  u32 flags;      /* flags for the cipher */
282
  ushort statelen;    /* maximum state length */
283
  ushort blocklen_bytes;  /* block size of output in bytes */
284
  int backend_cipher;   /* libgcrypt backend cipher */
285
};
286
287
struct drbg_state_ops_s
288
{
289
  gpg_err_code_t (*update) (drbg_state_t drbg,
290
          drbg_string_t *seed, int reseed);
291
  gpg_err_code_t (*generate) (drbg_state_t drbg,
292
            unsigned char *buf, unsigned int buflen,
293
            drbg_string_t *addtl);
294
  gpg_err_code_t (*crypto_init) (drbg_state_t drbg);
295
  void     (*crypto_fini) (drbg_state_t drbg);
296
};
297
298
struct drbg_test_data_s
299
{
300
  drbg_string_t *testentropy; /* TEST PARAMETER: test entropy */
301
  int fail_seed_source:1; /* If set, the seed function will
302
                                 * return an error. */
303
};
304
305
306
/* This state object keeps the state of an DRBG instance.  */
307
struct drbg_state_s
308
{
309
  unsigned char *V;   /* internal state 10.1.1.1 1a) */
310
  unsigned char *C;   /* hash: static value 10.1.1.1 1b)
311
         * hmac / ctr: key */
312
  size_t reseed_ctr;    /* Number of RNG requests since last reseed --
313
         * 10.1.1.1 1c) */
314
  unsigned char *scratchpad;  /* some memory the DRBG can use for its
315
         * operation -- allocated during init */
316
  void *priv_data;    /* Cipher handle */
317
  gcry_cipher_hd_t ctr_handle;  /* CTR mode cipher handle */
318
  int seeded:1;     /* DRBG fully seeded? */
319
  int pr:1;     /* Prediction resistance enabled? */
320
  /* Taken from libgcrypt ANSI X9.31 DRNG: We need to keep track of the
321
   * process which did the initialization so that we can detect a fork.
322
   * The volatile modifier is required so that the compiler does not
323
   * optimize it away in case the getpid function is badly attributed. */
324
  pid_t seed_init_pid;
325
  const struct drbg_state_ops_s *d_ops;
326
  const struct drbg_core_s *core;
327
  struct drbg_test_data_s *test_data;
328
};
329
330
enum drbg_prefixes
331
{
332
  DRBG_PREFIX0 = 0x00,
333
  DRBG_PREFIX1,
334
  DRBG_PREFIX2,
335
  DRBG_PREFIX3
336
};
337
338
0
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
339
340
/***************************************************************
341
 * Global variables
342
 ***************************************************************/
343
344
/* The instance of the DRBG, to be refereed by drbg_state.  */
345
static struct drbg_state_s drbg_instance;
346
347
/* Global state variable holding the current instance of the DRBG.  */
348
static drbg_state_t drbg_state;
349
350
/* This is the lock variable we use to serialize access to this RNG. */
351
GPGRT_LOCK_DEFINE(drbg_lock_var);
352
353
354
/***************************************************************
355
 * Backend cipher definitions available to DRBG
356
 ***************************************************************/
357
358
static const struct drbg_core_s drbg_cores[] = {
359
  /* Hash DRBGs */
360
  {DRBG_HASHSHA1, 55, 20, GCRY_MD_SHA1},
361
  {DRBG_HASHSHA256, 55, 32, GCRY_MD_SHA256},
362
  {DRBG_HASHSHA384, 111, 48, GCRY_MD_SHA384},
363
  {DRBG_HASHSHA512, 111, 64, GCRY_MD_SHA512},
364
  /* HMAC DRBGs */
365
  {DRBG_HASHSHA1   | DRBG_HMAC, 20, 20, GCRY_MD_SHA1},
366
  {DRBG_HASHSHA256 | DRBG_HMAC, 32, 32, GCRY_MD_SHA256},
367
  {DRBG_HASHSHA384 | DRBG_HMAC, 48, 48, GCRY_MD_SHA384},
368
  {DRBG_HASHSHA512 | DRBG_HMAC, 64, 64, GCRY_MD_SHA512},
369
  /* block ciphers */
370
  {DRBG_CTRAES | DRBG_SYM128, 32, 16, GCRY_CIPHER_AES128},
371
  {DRBG_CTRAES | DRBG_SYM192, 40, 16, GCRY_CIPHER_AES192},
372
  {DRBG_CTRAES | DRBG_SYM256, 48, 16, GCRY_CIPHER_AES256}
373
};
374
375
static gpg_err_code_t drbg_hash_init (drbg_state_t drbg);
376
static gpg_err_code_t drbg_hmac_init (drbg_state_t drbg);
377
static gpg_err_code_t drbg_hmac_setkey (drbg_state_t drbg,
378
          const unsigned char *key);
379
static void drbg_hash_fini (drbg_state_t drbg);
380
static byte *drbg_hash (drbg_state_t drbg, const drbg_string_t *buf);
381
static gpg_err_code_t drbg_sym_init (drbg_state_t drbg);
382
static void drbg_sym_fini (drbg_state_t drbg);
383
static gpg_err_code_t drbg_sym_setkey (drbg_state_t drbg,
384
               const unsigned char *key);
385
static gpg_err_code_t drbg_sym (drbg_state_t drbg, unsigned char *outval,
386
        const drbg_string_t *buf);
387
static gpg_err_code_t drbg_sym_ctr (drbg_state_t drbg,
388
      const unsigned char *inbuf, unsigned int inbuflen,
389
      unsigned char *outbuf, unsigned int outbuflen);
390
391
/******************************************************************
392
 ******************************************************************
393
 ******************************************************************
394
 * Generic DRBG code
395
 ******************************************************************
396
 ******************************************************************
397
 ******************************************************************/
398
399
/******************************************************************
400
 * Generic helper functions
401
 ******************************************************************/
402
403
#if 0
404
#define dbg(x) do { log_debug x; } while(0)
405
#else
406
#define dbg(x)
407
#endif
408
409
/*
410
 * Parse a string of flags and store the flag values at R_FLAGS.
411
 * Return 0 on success.
412
 */
413
static gpg_err_code_t
414
parse_flag_string (const char *string, u32 *r_flags)
415
0
{
416
0
  struct {
417
0
    const char *name;
418
0
    u32 flag;
419
0
  } table[] = {
420
0
    { "aes",     DRBG_CTRAES            },
421
0
    { "serpent", DRBG_CTRSERPENT        },
422
0
    { "twofish", DRBG_CTRTWOFISH        },
423
0
    { "sha1",    DRBG_HASHSHA1          },
424
0
    { "sha256",  DRBG_HASHSHA256        },
425
0
    { "sha512",  DRBG_HASHSHA512        },
426
0
    { "hmac",    DRBG_HMAC              },
427
0
    { "sym128",  DRBG_SYM128            },
428
0
    { "sym192",  DRBG_SYM192            },
429
0
    { "sym256",  DRBG_SYM256            },
430
0
    { "pr",      DRBG_PREDICTION_RESIST }
431
0
  };
432
433
0
  *r_flags = 0;
434
0
  if (string)
435
0
    {
436
0
      char **tl;
437
0
      const char *s;
438
0
      int i, j;
439
440
0
      tl = _gcry_strtokenize (string, NULL);
441
0
      if (!tl)
442
0
        return gpg_err_code_from_syserror ();
443
0
      for (i=0; (s=tl[i]); i++)
444
0
        {
445
0
          for (j=0; j < DIM (table); j++)
446
0
            if (!strcmp (s, table[j].name))
447
0
              {
448
0
                *r_flags |= table[j].flag;
449
0
                break;
450
0
              }
451
0
          if (!(j < DIM (table)))
452
0
            {
453
0
              xfree (tl);
454
0
              return GPG_ERR_INV_FLAG;
455
0
            }
456
0
        }
457
0
      xfree (tl);
458
0
    }
459
460
0
  return 0;
461
0
}
462
463
static inline void
464
drbg_string_fill (drbg_string_t *string,
465
                       const unsigned char *buf, size_t len)
466
0
{
467
0
  string->buf = buf;
468
0
  string->len = len;
469
0
  string->next = NULL;
470
0
}
471
472
static inline ushort
473
drbg_statelen (drbg_state_t drbg)
474
0
{
475
0
  if (drbg && drbg->core)
476
0
    return drbg->core->statelen;
477
0
  return 0;
478
0
}
479
480
static inline ushort
481
drbg_blocklen (drbg_state_t drbg)
482
0
{
483
0
  if (drbg && drbg->core)
484
0
    return drbg->core->blocklen_bytes;
485
0
  return 0;
486
0
}
487
488
static inline ushort
489
drbg_keylen (drbg_state_t drbg)
490
0
{
491
0
  if (drbg && drbg->core)
492
0
    return (drbg->core->statelen - drbg->core->blocklen_bytes);
493
0
  return 0;
494
0
}
495
496
static inline size_t
497
drbg_max_request_bytes (void)
498
0
{
499
  /* SP800-90A requires the limit 2**19 bits, but we return bytes */
500
0
  return (1 << 16);
501
0
}
502
503
static inline size_t
504
drbg_max_addtl (void)
505
0
{
506
  /* SP800-90A requires 2**35 bytes additional info str / pers str */
507
0
#ifdef __LP64__
508
0
  return (1UL << 35);
509
#else
510
  /*
511
   * SP800-90A allows smaller maximum numbers to be returned -- we
512
   * return SIZE_MAX - 1 to allow the verification of the enforcement
513
   * of this value in drbg_healthcheck_sanity.
514
   */
515
  return (SIZE_MAX - 1);
516
#endif
517
0
}
518
519
static inline size_t
520
drbg_max_requests (void)
521
0
{
522
  /* SP800-90A requires 2**48 maximum requests before reseeding */
523
0
#ifdef __LP64__
524
0
  return (1UL << 48);
525
#else
526
  return SIZE_MAX;
527
#endif
528
0
}
529
530
/*
531
 * Return strength of DRBG according to SP800-90A section 8.4
532
 *
533
 * flags: DRBG flags reference
534
 *
535
 * Return: normalized strength value or 32 as a default to counter
536
 *     programming errors
537
 */
538
static inline unsigned short
539
drbg_sec_strength (u32 flags)
540
0
{
541
0
  if ((flags & DRBG_HASHSHA1) || (flags & DRBG_SYM128))
542
0
    return 16;
543
0
  else if (flags & DRBG_SYM192)
544
0
    return 24;
545
0
  else if ((flags & DRBG_SYM256) || (flags & DRBG_HASHSHA256) ||
546
0
     (flags & DRBG_HASHSHA384) || (flags & DRBG_HASHSHA512))
547
0
    return 32;
548
0
  else
549
0
    return 32;
550
0
}
551
552
static void
553
drbg_add_buf (unsigned char *dst, size_t dstlen,
554
              unsigned char *add, size_t addlen)
555
0
{
556
  /* implied: dstlen > addlen */
557
0
  unsigned char *dstptr, *addptr;
558
0
  unsigned int remainder = 0;
559
0
  size_t len = addlen;
560
561
0
  dstptr = dst + (dstlen - 1);
562
0
  addptr = add + (addlen - 1);
563
0
  while (len)
564
0
    {
565
0
      remainder += *dstptr + *addptr;
566
0
      *dstptr = remainder & 0xff;
567
0
      remainder >>= 8;
568
0
      len--;
569
0
      dstptr--;
570
0
      addptr--;
571
0
    }
572
0
  len = dstlen - addlen;
573
0
  while (len && remainder > 0)
574
0
    {
575
0
      remainder = *dstptr + 1;
576
0
      *dstptr = remainder & 0xff;
577
0
      remainder >>= 8;
578
0
      len--;
579
0
      dstptr--;
580
0
    }
581
0
}
582
583
/* Helper variables for read_cb().
584
 *
585
 *   The _gcry_rnd*_gather_random interface does not allow to provide a
586
 *   data pointer.  Thus we need to use a global variable for
587
 *   communication.  However, the then required locking is anyway a good
588
 *   idea because it does not make sense to have several readers of (say
589
 *   /dev/random).  It is easier to serve them one after the other.
590
 */
591
static unsigned char *read_cb_buffer; /* The buffer.  */
592
static size_t read_cb_size;         /* Size of the buffer.  */
593
static size_t read_cb_len;          /* Used length.  */
594
595
/* Callback for generating seed from kernel device. */
596
static void
597
drbg_read_cb (const void *buffer, size_t length,
598
              enum random_origins origin)
599
0
{
600
0
  const unsigned char *p = buffer;
601
602
0
  (void) origin;
603
0
  gcry_assert (read_cb_buffer);
604
605
  /* Note that we need to protect against gatherers returning more
606
   * than the requested bytes (e.g. rndw32).  */
607
0
  while (length-- && read_cb_len < read_cb_size)
608
0
    read_cb_buffer[read_cb_len++] = *p++;
609
0
}
610
611
static inline int
612
drbg_get_entropy (drbg_state_t drbg, unsigned char *buffer,
613
           size_t len)
614
0
{
615
0
  int rc = 0;
616
617
  /* Perform testing as defined in 11.3.2 */
618
0
  if (drbg->test_data && drbg->test_data->fail_seed_source)
619
0
    return -1;
620
621
0
  read_cb_buffer = buffer;
622
0
  read_cb_size = len;
623
0
  read_cb_len = 0;
624
0
#if USE_RNDGETENTROPY
625
0
  rc = _gcry_rndgetentropy_gather_random (drbg_read_cb, 0, len,
626
0
                                          GCRY_VERY_STRONG_RANDOM);
627
#elif USE_RNDOLDLINUX
628
  rc = _gcry_rndoldlinux_gather_random (drbg_read_cb, 0, len,
629
                                        GCRY_VERY_STRONG_RANDOM);
630
#elif USE_RNDUNIX
631
  rc = _gcry_rndunix_gather_random (drbg_read_cb, 0, len,
632
            GCRY_VERY_STRONG_RANDOM);
633
#elif USE_RNDW32
634
  do
635
    {
636
      rc = _gcry_rndw32_gather_random (drbg_read_cb, 0, len,
637
               GCRY_VERY_STRONG_RANDOM);
638
    }
639
  while (rc >= 0 && read_cb_len < read_cb_size);
640
#else
641
  rc = -1;
642
#endif
643
0
  return rc;
644
0
}
645
646
/******************************************************************
647
 * CTR DRBG callback functions
648
 ******************************************************************/
649
650
/* BCC function for CTR DRBG as defined in 10.4.3 */
651
static gpg_err_code_t
652
drbg_ctr_bcc (drbg_state_t drbg,
653
              unsigned char *out, const unsigned char *key,
654
              drbg_string_t *in)
655
0
{
656
0
  gpg_err_code_t ret = GPG_ERR_GENERAL;
657
0
  drbg_string_t *curr = in;
658
0
  size_t inpos = curr->len;
659
0
  const unsigned char *pos = curr->buf;
660
0
  drbg_string_t data;
661
662
0
  drbg_string_fill (&data, out, drbg_blocklen (drbg));
663
664
  /* 10.4.3 step 1 */
665
0
  memset (out, 0, drbg_blocklen (drbg));
666
667
0
  ret = drbg_sym_setkey(drbg, key);
668
0
  if (ret)
669
0
    return ret;
670
671
  /* 10.4.3 step 2 / 4 */
672
0
  while (inpos)
673
0
    {
674
0
      short cnt = 0;
675
      /* 10.4.3 step 4.1 */
676
0
      for (cnt = 0; cnt < drbg_blocklen (drbg); cnt++)
677
0
  {
678
0
    out[cnt] ^= *pos;
679
0
    pos++;
680
0
    inpos--;
681
    /* the following branch implements the linked list
682
     * iteration. If we are at the end of the current data
683
     * set, we have to start using the next data set if
684
     * available -- the inpos value always points to the
685
     * current byte and will be zero if we have processed
686
     * the last byte of the last linked list member */
687
0
    if (0 == inpos)
688
0
      {
689
0
        curr = curr->next;
690
0
        if (NULL != curr)
691
0
    {
692
0
      pos = curr->buf;
693
0
      inpos = curr->len;
694
0
    }
695
0
        else
696
0
    {
697
0
      inpos = 0;
698
0
      break;
699
0
    }
700
0
      }
701
0
  }
702
      /* 10.4.3 step 4.2 */
703
0
      ret = drbg_sym (drbg, out, &data);
704
0
      if (ret)
705
0
  return ret;
706
      /* 10.4.3 step 2 */
707
0
    }
708
0
  return 0;
709
0
}
710
711
712
/*
713
 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
714
 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
715
 * the scratchpad is used as follows:
716
 * drbg_ctr_update:
717
 *  temp
718
 *    start: drbg->scratchpad
719
 *    length: drbg_statelen(drbg) + drbg_blocklen(drbg)
720
 *      note: the cipher writing into this variable works
721
 *      blocklen-wise. Now, when the statelen is not a multiple
722
 *      of blocklen, the generateion loop below "spills over"
723
 *      by at most blocklen. Thus, we need to give sufficient
724
 *      memory.
725
 *  df_data
726
 *    start: drbg->scratchpad +
727
 *        drbg_statelen(drbg) +
728
 *        drbg_blocklen(drbg)
729
 *    length: drbg_statelen(drbg)
730
 *
731
 * drbg_ctr_df:
732
 *  pad
733
 *    start: df_data + drbg_statelen(drbg)
734
 *    length: drbg_blocklen(drbg)
735
 *  iv
736
 *    start: pad + drbg_blocklen(drbg)
737
 *    length: drbg_blocklen(drbg)
738
 *  temp
739
 *    start: iv + drbg_blocklen(drbg)
740
 *    length: drbg_satelen(drbg) + drbg_blocklen(drbg)
741
 *      note: temp is the buffer that the BCC function operates
742
 *      on. BCC operates blockwise. drbg_statelen(drbg)
743
 *      is sufficient when the DRBG state length is a multiple
744
 *      of the block size. For AES192 (and maybe other ciphers)
745
 *      this is not correct and the length for temp is
746
 *      insufficient (yes, that also means for such ciphers,
747
 *      the final output of all BCC rounds are truncated).
748
 *      Therefore, add drbg_blocklen(drbg) to cover all
749
 *      possibilities.
750
 */
751
752
/* Derivation Function for CTR DRBG as defined in 10.4.2 */
753
static gpg_err_code_t
754
drbg_ctr_df (drbg_state_t drbg, unsigned char *df_data,
755
             size_t bytes_to_return, drbg_string_t *addtl)
756
0
{
757
0
  gpg_err_code_t ret = GPG_ERR_GENERAL;
758
0
  unsigned char L_N[8];
759
  /* S3 is input */
760
0
  drbg_string_t S1, S2, S4, cipherin;
761
0
  drbg_string_t *tempstr = addtl;
762
0
  unsigned char *pad = df_data + drbg_statelen (drbg);
763
0
  unsigned char *iv = pad + drbg_blocklen (drbg);
764
0
  unsigned char *temp = iv + drbg_blocklen (drbg);
765
0
  size_t padlen = 0;
766
0
  unsigned int templen = 0;
767
  /* 10.4.2 step 7 */
768
0
  unsigned int i = 0;
769
  /* 10.4.2 step 8 */
770
0
  const unsigned char *K = (unsigned char *)
771
0
    "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
772
0
    "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
773
0
  unsigned char *X;
774
0
  size_t generated_len = 0;
775
0
  size_t inputlen = 0;
776
777
0
  memset (pad, 0, drbg_blocklen (drbg));
778
0
  memset (iv, 0, drbg_blocklen (drbg));
779
0
  memset (temp, 0, drbg_statelen (drbg));
780
781
  /* 10.4.2 step 1 is implicit as we work byte-wise */
782
783
  /* 10.4.2 step 2 */
784
0
  if ((512 / 8) < bytes_to_return)
785
0
    return GPG_ERR_INV_ARG;
786
787
  /* 10.4.2 step 2 -- calculate the entire length of all input data */
788
0
  for (; NULL != tempstr; tempstr = tempstr->next)
789
0
    inputlen += tempstr->len;
790
0
  buf_put_be32 (&L_N[0], inputlen);
791
792
  /* 10.4.2 step 3 */
793
0
  buf_put_be32 (&L_N[4], bytes_to_return);
794
795
  /* 10.4.2 step 5: length is size of L_N, input_string, one byte, padding */
796
0
  padlen = (inputlen + sizeof (L_N) + 1) % (drbg_blocklen (drbg));
797
  /* wrap the padlen appropriately */
798
0
  if (padlen)
799
0
    padlen = drbg_blocklen (drbg) - padlen;
800
  /* pad / padlen contains the 0x80 byte and the following zero bytes, so
801
   * add one for byte for 0x80 */
802
0
  padlen++;
803
0
  pad[0] = 0x80;
804
805
  /* 10.4.2 step 4 -- first fill the linked list and then order it */
806
0
  drbg_string_fill (&S1, iv, drbg_blocklen (drbg));
807
0
  drbg_string_fill (&S2, L_N, sizeof (L_N));
808
0
  drbg_string_fill (&S4, pad, padlen);
809
0
  S1.next = &S2;
810
0
  S2.next = addtl;
811
812
  /* Splice in addtl between S2 and S4 -- we place S4 at the end of the
813
   * input data chain. As this code is only triggered when addtl is not
814
   * NULL, no NULL checks are necessary.*/
815
0
  tempstr = addtl;
816
0
  while (tempstr->next)
817
0
    tempstr = tempstr->next;
818
0
  tempstr->next = &S4;
819
820
  /* 10.4.2 step 9 */
821
0
  while (templen < (drbg_keylen (drbg) + (drbg_blocklen (drbg))))
822
0
    {
823
      /* 10.4.2 step 9.1 - the padding is implicit as the buffer
824
       * holds zeros after allocation -- even the increment of i
825
       * is irrelevant as the increment remains within length of i */
826
0
      buf_put_be32 (iv, i);
827
      /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
828
0
      ret = drbg_ctr_bcc (drbg, temp + templen, K, &S1);
829
0
      if (ret)
830
0
  goto out;
831
      /* 10.4.2 step 9.3 */
832
0
      i++;
833
0
      templen += drbg_blocklen (drbg);
834
0
    }
835
836
  /* 10.4.2 step 11 */
837
  /* implicit key len with seedlen - blocklen according to table 3 */
838
0
  X = temp + (drbg_keylen (drbg));
839
0
  drbg_string_fill (&cipherin, X, drbg_blocklen (drbg));
840
841
  /* 10.4.2 step 12: overwriting of outval */
842
843
  /* 10.4.2 step 13 */
844
0
  ret = drbg_sym_setkey(drbg, temp);
845
0
  if (ret)
846
0
    goto out;
847
0
  while (generated_len < bytes_to_return)
848
0
    {
849
0
      short blocklen = 0;
850
      /* 10.4.2 step 13.1 */
851
      /* the truncation of the key length is implicit as the key
852
       * is only drbg_blocklen in size -- check for the implementation
853
       * of the cipher function callback */
854
0
      ret = drbg_sym (drbg, X, &cipherin);
855
0
      if (ret)
856
0
  goto out;
857
0
      blocklen = (drbg_blocklen (drbg) < (bytes_to_return - generated_len)) ?
858
0
  drbg_blocklen (drbg) : (bytes_to_return - generated_len);
859
      /* 10.4.2 step 13.2 and 14 */
860
0
      memcpy (df_data + generated_len, X, blocklen);
861
0
      generated_len += blocklen;
862
0
    }
863
864
0
  ret = 0;
865
866
0
 out:
867
0
  memset (iv, 0, drbg_blocklen (drbg));
868
0
  memset (temp, 0, drbg_statelen (drbg));
869
0
  memset (pad, 0, drbg_blocklen (drbg));
870
0
  return ret;
871
0
}
872
873
/*
874
 * Update function of CTR DRBG as defined in 10.2.1.2
875
 *
876
 * The reseed variable has an enhanced meaning compared to the update
877
 * functions of the other DRBGs as follows:
878
 * 0 => initial seed from initialization
879
 * 1 => reseed via drbg_seed
880
 * 2 => first invocation from drbg_ctr_update when addtl is present. In
881
 *      this case, the df_data scratchpad is not deleted so that it is
882
 *      available for another calls to prevent calling the DF function
883
 *      again.
884
 * 3 => second invocation from drbg_ctr_update. When the update function
885
 *      was called with addtl, the df_data memory already contains the
886
 *      DFed addtl information and we do not need to call DF again.
887
 */
888
static gpg_err_code_t
889
drbg_ctr_update (drbg_state_t drbg, drbg_string_t *addtl, int reseed)
890
0
{
891
0
  gpg_err_code_t ret = GPG_ERR_GENERAL;
892
  /* 10.2.1.2 step 1 */
893
0
  unsigned char *temp = drbg->scratchpad;
894
0
  unsigned char *df_data = drbg->scratchpad +
895
0
    drbg_statelen (drbg) + drbg_blocklen (drbg);
896
0
  unsigned char prefix = DRBG_PREFIX1;
897
898
0
  memset (temp, 0, drbg_statelen (drbg) + drbg_blocklen (drbg));
899
0
  if (3 > reseed)
900
0
    memset (df_data, 0, drbg_statelen (drbg));
901
902
0
  if (!reseed)
903
0
    {
904
      /*
905
       * The DRBG uses the CTR mode of the underlying AES cipher. The
906
       * CTR mode increments the counter value after the AES operation
907
       * but SP800-90A requires that the counter is incremented before
908
       * the AES operation. Hence, we increment it at the time we set
909
       * it by one.
910
       */
911
0
      drbg_add_buf (drbg->V, drbg_blocklen (drbg), &prefix, 1);
912
913
0
      ret = _gcry_cipher_setkey (drbg->ctr_handle, drbg->C, drbg_keylen (drbg));
914
0
      if (ret)
915
0
        goto out;
916
0
    }
917
918
  /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
919
0
  if (addtl && 0 < addtl->len)
920
0
    {
921
0
      ret =
922
0
  drbg_ctr_df (drbg, df_data, drbg_statelen (drbg), addtl);
923
0
      if (ret)
924
0
  goto out;
925
0
    }
926
927
0
  ret = drbg_sym_ctr (drbg, df_data, drbg_statelen(drbg),
928
0
          temp, drbg_statelen(drbg));
929
0
  if (ret)
930
0
    goto out;
931
932
  /* 10.2.1.2 step 5 */
933
0
  ret = _gcry_cipher_setkey (drbg->ctr_handle, temp, drbg_keylen (drbg));
934
0
  if (ret)
935
0
    goto out;
936
937
  /* 10.2.1.2 step 6 */
938
0
  memcpy (drbg->V, temp + drbg_keylen (drbg), drbg_blocklen (drbg));
939
  /* See above: increment counter by one to compensate timing of CTR op */
940
0
  drbg_add_buf (drbg->V, drbg_blocklen (drbg), &prefix, 1);
941
0
  ret = 0;
942
943
0
 out:
944
0
  memset (temp, 0, drbg_statelen (drbg) + drbg_blocklen (drbg));
945
0
  if (2 != reseed)
946
0
    memset (df_data, 0, drbg_statelen (drbg));
947
0
  return ret;
948
0
}
949
950
/*
951
 * scratchpad use: drbg_ctr_update is called independently from
952
 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
953
 */
954
/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
955
static gpg_err_code_t
956
drbg_ctr_generate (drbg_state_t drbg,
957
                   unsigned char *buf, unsigned int buflen,
958
                   drbg_string_t *addtl)
959
0
{
960
0
  static const unsigned char drbg_ctr_null[DRBG_CTR_NULL_LEN] = { 0, };
961
0
  gpg_err_code_t ret = 0;
962
963
0
  memset (drbg->scratchpad, 0, drbg_blocklen (drbg));
964
965
  /* 10.2.1.5.2 step 2 */
966
0
  if (addtl && 0 < addtl->len)
967
0
    {
968
0
      addtl->next = NULL;
969
0
      ret = drbg_ctr_update (drbg, addtl, 2);
970
0
      if (ret)
971
0
  return ret;
972
0
    }
973
974
  /* 10.2.1.5.2 step 4.1 */
975
0
  ret = drbg_sym_ctr (drbg, drbg_ctr_null, sizeof(drbg_ctr_null), buf, buflen);
976
0
  if (ret)
977
0
    goto out;
978
979
  /* 10.2.1.5.2 step 6 */
980
0
  if (addtl)
981
0
    addtl->next = NULL;
982
0
  ret = drbg_ctr_update (drbg, addtl, 3);
983
984
0
 out:
985
0
  return ret;
986
0
}
987
988
static struct drbg_state_ops_s drbg_ctr_ops = {
989
  drbg_ctr_update,
990
  drbg_ctr_generate,
991
  drbg_sym_init,
992
  drbg_sym_fini,
993
};
994
995
/******************************************************************
996
 * HMAC DRBG callback functions
997
 ******************************************************************/
998
999
static gpg_err_code_t
1000
drbg_hmac_update (drbg_state_t drbg, drbg_string_t *seed, int reseed)
1001
0
{
1002
0
  gpg_err_code_t ret = GPG_ERR_GENERAL;
1003
0
  int i = 0;
1004
0
  drbg_string_t seed1, seed2, cipherin;
1005
1006
0
  if (!reseed)
1007
0
    {
1008
      /* 10.1.2.3 step 2 already implicitly covered with
1009
       * the initial memset(0) of drbg->C */
1010
0
      memset (drbg->V, 1, drbg_statelen (drbg));
1011
0
      ret = drbg_hmac_setkey (drbg, drbg->C);
1012
0
      if (ret)
1013
0
  return ret;
1014
0
    }
1015
1016
  /* build linked list which implements the concatenation and fill
1017
   * first part*/
1018
0
  drbg_string_fill (&seed1, drbg->V, drbg_statelen (drbg));
1019
  /* buffer will be filled in for loop below with one byte */
1020
0
  drbg_string_fill (&seed2, NULL, 1);
1021
0
  seed1.next = &seed2;
1022
  /* seed may be NULL */
1023
0
  seed2.next = seed;
1024
1025
0
  drbg_string_fill (&cipherin, drbg->V, drbg_statelen (drbg));
1026
  /* we execute two rounds of V/K massaging */
1027
0
  for (i = 2; 0 < i; i--)
1028
0
    {
1029
0
      byte *retval;
1030
      /* first round uses 0x0, second 0x1 */
1031
0
      unsigned char prefix = DRBG_PREFIX0;
1032
0
      if (1 == i)
1033
0
  prefix = DRBG_PREFIX1;
1034
      /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
1035
0
      seed2.buf = &prefix;
1036
0
      retval = drbg_hash (drbg, &seed1);
1037
0
      ret = drbg_hmac_setkey (drbg, retval);
1038
0
      if (ret)
1039
0
  return ret;
1040
1041
      /* 10.1.2.2 step 2 and 5 -- HMAC for V */
1042
0
      retval = drbg_hash (drbg, &cipherin);
1043
0
      memcpy(drbg->V, retval, drbg_blocklen (drbg));
1044
1045
      /* 10.1.2.2 step 3 */
1046
0
      if (!seed || 0 == seed->len)
1047
0
  return ret;
1048
0
    }
1049
0
  return 0;
1050
0
}
1051
1052
/* generate function of HMAC DRBG as defined in 10.1.2.5 */
1053
static gpg_err_code_t
1054
drbg_hmac_generate (drbg_state_t drbg, unsigned char *buf, unsigned int buflen,
1055
                    drbg_string_t *addtl)
1056
0
{
1057
0
  gpg_err_code_t ret = 0;
1058
0
  unsigned int len = 0;
1059
0
  drbg_string_t data;
1060
1061
  /* 10.1.2.5 step 2 */
1062
0
  if (addtl && 0 < addtl->len)
1063
0
    {
1064
0
      addtl->next = NULL;
1065
0
      ret = drbg_hmac_update (drbg, addtl, 1);
1066
0
      if (ret)
1067
0
  return ret;
1068
0
    }
1069
1070
0
  drbg_string_fill (&data, drbg->V, drbg_statelen (drbg));
1071
0
  while (len < buflen)
1072
0
    {
1073
0
      unsigned int outlen = 0;
1074
      /* 10.1.2.5 step 4.1 */
1075
0
      byte *retval = drbg_hash (drbg, &data);
1076
0
      memcpy(drbg->V, retval, drbg_blocklen (drbg));
1077
0
      outlen = (drbg_blocklen (drbg) < (buflen - len)) ?
1078
0
  drbg_blocklen (drbg) : (buflen - len);
1079
1080
      /* 10.1.2.5 step 4.2 */
1081
0
      memcpy (buf + len, drbg->V, outlen);
1082
0
      len += outlen;
1083
0
    }
1084
1085
  /* 10.1.2.5 step 6 */
1086
0
  if (addtl)
1087
0
    addtl->next = NULL;
1088
0
  ret = drbg_hmac_update (drbg, addtl, 1);
1089
1090
0
  return ret;
1091
0
}
1092
1093
static struct drbg_state_ops_s drbg_hmac_ops = {
1094
  drbg_hmac_update,
1095
  drbg_hmac_generate,
1096
  drbg_hmac_init,
1097
  drbg_hash_fini,
1098
};
1099
1100
/******************************************************************
1101
 * Hash DRBG callback functions
1102
 ******************************************************************/
1103
1104
/*
1105
 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
1106
 * interlinked, the scratchpad is used as follows:
1107
 * drbg_hash_update
1108
 *  start: drbg->scratchpad
1109
 *  length: drbg_statelen(drbg)
1110
 * drbg_hash_df:
1111
 *  start: drbg->scratchpad + drbg_statelen(drbg)
1112
 *  length: drbg_blocklen(drbg)
1113
 */
1114
/* Derivation Function for Hash DRBG as defined in 10.4.1 */
1115
static gpg_err_code_t
1116
drbg_hash_df (drbg_state_t drbg,
1117
              unsigned char *outval, size_t outlen,
1118
              drbg_string_t *entropy)
1119
0
{
1120
0
  size_t len = 0;
1121
0
  unsigned char input[5];
1122
0
  drbg_string_t data1;
1123
1124
  /* 10.4.1 step 3 */
1125
0
  input[0] = 1;
1126
0
  buf_put_be32 (&input[1], (outlen * 8));
1127
1128
  /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
1129
0
  drbg_string_fill (&data1, input, 5);
1130
0
  data1.next = entropy;
1131
1132
  /* 10.4.1 step 4 */
1133
0
  while (len < outlen)
1134
0
    {
1135
0
      short blocklen = 0;
1136
      /* 10.4.1 step 4.1 */
1137
0
      byte *retval = drbg_hash (drbg, &data1);
1138
      /* 10.4.1 step 4.2 */
1139
0
      input[0]++;
1140
0
      blocklen = (drbg_blocklen (drbg) < (outlen - len)) ?
1141
0
  drbg_blocklen (drbg) : (outlen - len);
1142
0
      memcpy (outval + len, retval, blocklen);
1143
0
      len += blocklen;
1144
0
    }
1145
1146
0
  return 0;
1147
0
}
1148
1149
/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
1150
static gpg_err_code_t
1151
drbg_hash_update (drbg_state_t drbg, drbg_string_t *seed, int reseed)
1152
0
{
1153
0
  gpg_err_code_t ret = 0;
1154
0
  drbg_string_t data1, data2;
1155
0
  unsigned char *V = drbg->scratchpad;
1156
0
  unsigned char prefix = DRBG_PREFIX1;
1157
1158
0
  memset (drbg->scratchpad, 0, drbg_statelen (drbg));
1159
0
  if (!seed)
1160
0
    return GPG_ERR_INV_ARG;
1161
1162
0
  if (reseed)
1163
0
    {
1164
      /* 10.1.1.3 step 1: string length is concatenation of
1165
       * 1 byte, V and seed (which is concatenated entropy/addtl
1166
       * input)
1167
       */
1168
0
      memcpy (V, drbg->V, drbg_statelen (drbg));
1169
0
      drbg_string_fill (&data1, &prefix, 1);
1170
0
      drbg_string_fill (&data2, V, drbg_statelen (drbg));
1171
0
      data1.next = &data2;
1172
0
      data2.next = seed;
1173
0
    }
1174
0
  else
1175
0
    {
1176
0
      drbg_string_fill (&data1, seed->buf, seed->len);
1177
0
      data1.next = seed->next;
1178
0
    }
1179
1180
  /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
1181
0
  ret = drbg_hash_df (drbg, drbg->V, drbg_statelen (drbg), &data1);
1182
0
  if (ret)
1183
0
    goto out;
1184
1185
  /* 10.1.1.2 / 10.1.1.3 step 4 -- concatenation  */
1186
0
  prefix = DRBG_PREFIX0;
1187
0
  drbg_string_fill (&data1, &prefix, 1);
1188
0
  drbg_string_fill (&data2, drbg->V, drbg_statelen (drbg));
1189
0
  data1.next = &data2;
1190
  /* 10.1.1.2 / 10.1.1.3 step 4 -- df operation */
1191
0
  ret = drbg_hash_df (drbg, drbg->C, drbg_statelen (drbg), &data1);
1192
1193
0
 out:
1194
0
  memset (drbg->scratchpad, 0, drbg_statelen (drbg));
1195
0
  return ret;
1196
0
}
1197
1198
/* Processing of additional information string for Hash DRBG.  */
1199
static gpg_err_code_t
1200
drbg_hash_process_addtl (drbg_state_t drbg, drbg_string_t *addtl)
1201
0
{
1202
0
  drbg_string_t data1, data2;
1203
0
  drbg_string_t *data3;
1204
0
  unsigned char prefix = DRBG_PREFIX2;
1205
0
  byte *retval;
1206
1207
  /* 10.1.1.4 step 2 */
1208
0
  if (!addtl || 0 == addtl->len)
1209
0
    return 0;
1210
1211
  /* 10.1.1.4 step 2a -- concatenation */
1212
0
  drbg_string_fill (&data1, &prefix, 1);
1213
0
  drbg_string_fill (&data2, drbg->V, drbg_statelen (drbg));
1214
0
  data3 = addtl;
1215
0
  data1.next = &data2;
1216
0
  data2.next = data3;
1217
0
  data3->next = NULL;
1218
  /* 10.1.1.4 step 2a -- cipher invocation */
1219
0
  retval = drbg_hash (drbg, &data1);
1220
1221
  /* 10.1.1.4 step 2b */
1222
0
  drbg_add_buf (drbg->V, drbg_statelen (drbg), retval, drbg_blocklen (drbg));
1223
1224
0
  return 0;
1225
0
}
1226
1227
/*
1228
 * Hashgen defined in 10.1.1.4
1229
 */
1230
static gpg_err_code_t
1231
drbg_hash_hashgen (drbg_state_t drbg, unsigned char *buf, unsigned int buflen)
1232
0
{
1233
0
  unsigned int len = 0;
1234
0
  unsigned char *src = drbg->scratchpad;
1235
0
  drbg_string_t data;
1236
0
  unsigned char prefix = DRBG_PREFIX1;
1237
1238
  /* 10.1.1.4 step hashgen 2 */
1239
0
  memcpy (src, drbg->V, drbg_statelen (drbg));
1240
1241
0
  drbg_string_fill (&data, src, drbg_statelen (drbg));
1242
0
  while (len < buflen)
1243
0
    {
1244
0
      unsigned int outlen = 0;
1245
      /* 10.1.1.4 step hashgen 4.1 */
1246
0
      byte *retval = drbg_hash (drbg, &data);
1247
0
      outlen = (drbg_blocklen (drbg) < (buflen - len)) ?
1248
0
  drbg_blocklen (drbg) : (buflen - len);
1249
      /* 10.1.1.4 step hashgen 4.2 */
1250
0
      memcpy (buf + len, retval, outlen);
1251
0
      len += outlen;
1252
      /* 10.1.1.4 hashgen step 4.3 */
1253
0
      if (len < buflen)
1254
0
  drbg_add_buf (src, drbg_statelen (drbg), &prefix, 1);
1255
0
    }
1256
1257
0
  memset (drbg->scratchpad, 0, drbg_statelen (drbg));
1258
0
  return 0;
1259
0
}
1260
1261
/* Generate function for Hash DRBG as defined in 10.1.1.4  */
1262
static gpg_err_code_t
1263
drbg_hash_generate (drbg_state_t drbg, unsigned char *buf, unsigned int buflen,
1264
        drbg_string_t *addtl)
1265
0
{
1266
0
  gpg_err_code_t ret;
1267
0
  unsigned char prefix = DRBG_PREFIX3;
1268
0
  drbg_string_t data1, data2;
1269
0
  byte *retval;
1270
0
  union
1271
0
  {
1272
0
    unsigned char req[8];
1273
0
    u64 req_int;
1274
0
  } u;
1275
1276
  /* 10.1.1.4 step 2 */
1277
0
  ret = drbg_hash_process_addtl (drbg, addtl);
1278
0
  if (ret)
1279
0
    return ret;
1280
  /* 10.1.1.4 step 3 -- invocation of the Hashgen function defined in
1281
   * 10.1.1.4 */
1282
0
  ret = drbg_hash_hashgen (drbg, buf, buflen);
1283
0
  if (ret)
1284
0
    return ret;
1285
1286
  /* 10.1.1.4 step 4 */
1287
0
  drbg_string_fill (&data1, &prefix, 1);
1288
0
  drbg_string_fill (&data2, drbg->V, drbg_statelen (drbg));
1289
0
  data1.next = &data2;
1290
1291
  /* this is the value H as documented in 10.1.1.4 */
1292
0
  retval = drbg_hash (drbg, &data1);
1293
1294
  /* 10.1.1.4 step 5 */
1295
0
  drbg_add_buf (drbg->V, drbg_statelen (drbg), retval, drbg_blocklen (drbg));
1296
0
  drbg_add_buf (drbg->V, drbg_statelen (drbg), drbg->C, drbg_statelen (drbg));
1297
0
  u.req_int = be_bswap64 (drbg->reseed_ctr);
1298
0
  drbg_add_buf (drbg->V, drbg_statelen (drbg), u.req, sizeof (u.req));
1299
1300
0
  return ret;
1301
0
}
1302
1303
/*
1304
 * scratchpad usage: as update and generate are used isolated, both
1305
 * can use the scratchpad
1306
 */
1307
static struct drbg_state_ops_s drbg_hash_ops = {
1308
  drbg_hash_update,
1309
  drbg_hash_generate,
1310
  drbg_hash_init,
1311
  drbg_hash_fini,
1312
};
1313
1314
/******************************************************************
1315
 * Functions common for DRBG implementations
1316
 ******************************************************************/
1317
1318
/*
1319
 * Seeding or reseeding of the DRBG
1320
 *
1321
 * @drbg: DRBG state struct
1322
 * @pers: personalization / additional information buffer
1323
 * @reseed: 0 for initial seed process, 1 for reseeding
1324
 *
1325
 * return:
1326
 *  0 on success
1327
 *  error value otherwise
1328
 */
1329
static gpg_err_code_t
1330
drbg_seed (drbg_state_t drbg, drbg_string_t *pers, int reseed)
1331
0
{
1332
0
  gpg_err_code_t ret = 0;
1333
0
  unsigned char *entropy = NULL;
1334
0
  size_t entropylen = 0;
1335
0
  drbg_string_t data1;
1336
1337
  /* 9.1 / 9.2 / 9.3.1 step 3 */
1338
0
  if (pers && pers->len > (drbg_max_addtl ()))
1339
0
    {
1340
0
      dbg (("DRBG: personalization string too long %lu\n", pers->len));
1341
0
      return GPG_ERR_INV_ARG;
1342
0
    }
1343
0
  if (drbg->test_data && drbg->test_data->testentropy)
1344
0
    {
1345
0
      drbg_string_fill (&data1, drbg->test_data->testentropy->buf,
1346
0
           drbg->test_data->testentropy->len);
1347
0
      dbg (("DRBG: using test entropy\n"));
1348
0
    }
1349
0
  else
1350
0
    {
1351
      /* Gather entropy equal to the security strength of the DRBG.
1352
       * With a derivation function, a nonce is required in addition
1353
       * to the entropy. A nonce must be at least 1/2 of the security
1354
       * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1355
       * of the strength. The consideration of a nonce is only
1356
       * applicable during initial seeding. */
1357
0
      entropylen = drbg_sec_strength (drbg->core->flags);
1358
0
      if (!entropylen)
1359
0
  return GPG_ERR_GENERAL;
1360
0
      if (0 == reseed)
1361
  /* make sure we round up strength/2 in
1362
   * case it is not divisible by 2 */
1363
0
  entropylen = ((entropylen + 1) / 2) * 3;
1364
0
      dbg (("DRBG: (re)seeding with %lu bytes of entropy\n", entropylen));
1365
0
      entropy = xcalloc_secure (1, entropylen);
1366
0
      if (!entropy)
1367
0
  return GPG_ERR_ENOMEM;
1368
0
      ret = drbg_get_entropy (drbg, entropy, entropylen);
1369
0
      if (ret)
1370
0
  goto out;
1371
0
      drbg_string_fill (&data1, entropy, entropylen);
1372
0
    }
1373
1374
  /* concatenation of entropy with personalization str / addtl input)
1375
   * the variable pers is directly handed by the caller, check its
1376
   * contents whether it is appropriate */
1377
0
  if (pers && pers->buf && 0 < pers->len && NULL == pers->next)
1378
0
    {
1379
0
      data1.next = pers;
1380
0
      dbg (("DRBG: using personalization string\n"));
1381
0
    }
1382
1383
0
  ret = drbg->d_ops->update (drbg, &data1, reseed);
1384
0
  dbg (("DRBG: state updated with seed\n"));
1385
0
  if (ret)
1386
0
    goto out;
1387
0
  drbg->seeded = 1;
1388
  /* 10.1.1.2 / 10.1.1.3 step 5 */
1389
0
  drbg->reseed_ctr = 1;
1390
1391
0
 out:
1392
0
  xfree (entropy);
1393
0
  return ret;
1394
0
}
1395
1396

1397
/*************************************************************************
1398
 * Exported interfaces.
1399
 *************************************************************************/
1400
1401
/*
1402
 * DRBG generate function as required by SP800-90A - this function
1403
 * generates random numbers
1404
 *
1405
 * @drbg   DRBG state handle
1406
 * @buf    Buffer where to store the random numbers -- the buffer must already
1407
 *         be pre-allocated by caller
1408
 * @buflen Length of output buffer - this value defines the number of random
1409
 *     bytes pulled from DRBG
1410
 * @addtl  Additional input that is mixed into state, may be NULL -- note
1411
 *     the entropy is pulled by the DRBG internally unconditionally
1412
 *     as defined in SP800-90A. The additional input is mixed into
1413
 *     the state in addition to the pulled entropy.
1414
 *
1415
 * return: Generated number of bytes.
1416
 */
1417
static gpg_err_code_t
1418
drbg_generate (drbg_state_t drbg,
1419
               unsigned char *buf, unsigned int buflen,
1420
               drbg_string_t *addtl)
1421
0
{
1422
0
  gpg_err_code_t ret = GPG_ERR_INV_ARG;
1423
1424
0
  if (0 == buflen || !buf)
1425
0
    {
1426
0
      dbg (("DRBG: no buffer provided\n"));
1427
0
      return ret;
1428
0
    }
1429
0
  if (addtl && NULL == addtl->buf && 0 < addtl->len)
1430
0
    {
1431
0
      dbg (("DRBG: wrong format of additional information\n"));
1432
0
      return ret;
1433
0
    }
1434
1435
  /* 9.3.1 step 2 */
1436
0
  if (buflen > (drbg_max_request_bytes ()))
1437
0
    {
1438
0
      dbg (("DRBG: requested random numbers too large %u\n", buflen));
1439
0
      return ret;
1440
0
    }
1441
  /* 9.3.1 step 3 is implicit with the chosen DRBG */
1442
  /* 9.3.1 step 4 */
1443
0
  if (addtl && addtl->len > (drbg_max_addtl ()))
1444
0
    {
1445
0
      dbg (("DRBG: additional information string too long %lu\n",
1446
0
      addtl->len));
1447
0
      return ret;
1448
0
    }
1449
  /* 9.3.1 step 5 is implicit with the chosen DRBG */
1450
  /* 9.3.1 step 6 and 9 supplemented by 9.3.2 step c -- the spec is a
1451
   * bit convoluted here, we make it simpler */
1452
0
  if ((drbg_max_requests ()) < drbg->reseed_ctr)
1453
0
    drbg->seeded = 0;
1454
1455
0
  if (drbg->pr || !drbg->seeded)
1456
0
    {
1457
0
      dbg (("DRBG: reseeding before generation (prediction resistance: %s, state %s)\n", drbg->pr ? "true" : "false", drbg->seeded ? "seeded" : "unseeded"));
1458
      /* 9.3.1 steps 7.1 through 7.3 */
1459
0
      ret = drbg_seed (drbg, addtl, 1);
1460
0
      if (ret)
1461
0
  return ret;
1462
      /* 9.3.1 step 7.4 */
1463
0
      addtl = NULL;
1464
0
    }
1465
1466
0
  if (addtl && addtl->buf)
1467
0
    {
1468
0
      dbg (("DRBG: using additional information string\n"));
1469
0
    }
1470
1471
  /* 9.3.1 step 8 and 10 */
1472
0
  ret = drbg->d_ops->generate (drbg, buf, buflen, addtl);
1473
1474
  /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1475
0
  drbg->reseed_ctr++;
1476
0
  if (ret)
1477
0
    return ret;
1478
1479
  /* 11.3.3 -- re-perform self tests after some generated random
1480
   * numbers, the chosen value after which self test is performed
1481
   * is arbitrary, but it should be reasonable */
1482
  /* Here we do not perform the self tests because of the following
1483
   * reasons: it is mathematically impossible that the initial self tests
1484
   * were successfully and the following are not. If the initial would
1485
   * pass and the following would not, the system integrity is violated.
1486
   * In this case, the entire system operation is questionable and it
1487
   * is unlikely that the integrity violation only affects to the
1488
   * correct operation of the DRBG.
1489
   */
1490
#if 0
1491
  if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096))
1492
    {
1493
      dbg (("DRBG: start to perform self test\n"));
1494
      ret = drbg_healthcheck ();
1495
      if (ret)
1496
  {
1497
    log_fatal (("DRBG: self test failed\n"));
1498
    return ret;
1499
  }
1500
      else
1501
  {
1502
    dbg (("DRBG: self test successful\n"));
1503
  }
1504
    }
1505
#endif
1506
1507
0
  return ret;
1508
0
}
1509
1510
/*
1511
 * Wrapper around drbg_generate which can pull arbitrary long strings
1512
 * from the DRBG without hitting the maximum request limitation.
1513
 *
1514
 * Parameters: see drbg_generate
1515
 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1516
 *     the entire drbg_generate_long request fails
1517
 */
1518
static gpg_err_code_t
1519
drbg_generate_long (drbg_state_t drbg,
1520
                    unsigned char *buf, unsigned int buflen,
1521
                    drbg_string_t *addtl)
1522
0
{
1523
0
  gpg_err_code_t ret = 0;
1524
0
  unsigned int slice = 0;
1525
0
  unsigned char *buf_p = buf;
1526
0
  unsigned len = 0;
1527
0
  do
1528
0
    {
1529
0
      unsigned int chunk = 0;
1530
0
      slice = ((buflen - len) / drbg_max_request_bytes ());
1531
0
      chunk = slice ? drbg_max_request_bytes () : (buflen - len);
1532
0
      ret = drbg_generate (drbg, buf_p, chunk, addtl);
1533
0
      if (ret)
1534
0
  return ret;
1535
0
      buf_p += chunk;
1536
0
      len += chunk;
1537
0
    }
1538
0
  while (slice > 0 && (len < buflen));
1539
0
  return ret;
1540
0
}
1541
1542
/*
1543
 * DRBG uninstantiate function as required by SP800-90A - this function
1544
 * frees all buffers and the DRBG handle
1545
 *
1546
 * @drbg DRBG state handle
1547
 *
1548
 * return
1549
 *  0 on success
1550
 */
1551
static gpg_err_code_t
1552
drbg_uninstantiate (drbg_state_t drbg)
1553
0
{
1554
0
  if (!drbg)
1555
0
    return GPG_ERR_INV_ARG;
1556
0
  drbg->d_ops->crypto_fini(drbg);
1557
0
  xfree (drbg->V);
1558
0
  drbg->V = NULL;
1559
0
  xfree (drbg->C);
1560
0
  drbg->C = NULL;
1561
0
  drbg->reseed_ctr = 0;
1562
0
  xfree (drbg->scratchpad);
1563
0
  drbg->scratchpad = NULL;
1564
0
  drbg->seeded = 0;
1565
0
  drbg->pr = 0;
1566
0
  drbg->seed_init_pid = 0;
1567
0
  return 0;
1568
0
}
1569
1570
/*
1571
 * DRBG instantiation function as required by SP800-90A - this function
1572
 * sets up the DRBG handle, performs the initial seeding and all sanity
1573
 * checks required by SP800-90A
1574
 *
1575
 * @drbg memory of state -- if NULL, new memory is allocated
1576
 * @pers Personalization string that is mixed into state, may be NULL -- note
1577
 *   the entropy is pulled by the DRBG internally unconditionally
1578
 *   as defined in SP800-90A. The additional input is mixed into
1579
 *   the state in addition to the pulled entropy.
1580
 * @coreref reference to core
1581
 * @flags Flags defining the requested DRBG type and cipher type. The flags
1582
 *    are defined in drbg.h and may be XORed. Beware, if you XOR multiple
1583
 *    cipher types together, the code picks the core on a first come first
1584
 *    serve basis as it iterates through the available cipher cores and
1585
 *    uses the one with the first match. The minimum required flags are:
1586
 *    cipher type flag
1587
 *
1588
 * return
1589
 *  0 on success
1590
 *  error value otherwise
1591
 */
1592
static gpg_err_code_t
1593
drbg_instantiate (drbg_state_t drbg,
1594
                  drbg_string_t *pers, int coreref, int pr)
1595
0
{
1596
0
  gpg_err_code_t ret = GPG_ERR_ENOMEM;
1597
0
  unsigned int sb_size = 0;
1598
1599
0
  if (!drbg)
1600
0
    return GPG_ERR_INV_ARG;
1601
1602
0
  dbg (("DRBG: Initializing DRBG core %d with prediction resistance %s\n",
1603
0
  coreref, pr ? "enabled" : "disabled"));
1604
0
  drbg->core = &drbg_cores[coreref];
1605
0
  drbg->pr = pr;
1606
0
  drbg->seeded = 0;
1607
0
  if (drbg->core->flags & DRBG_HMAC)
1608
0
    drbg->d_ops = &drbg_hmac_ops;
1609
0
  else if (drbg->core->flags & DRBG_HASH_MASK)
1610
0
    drbg->d_ops = &drbg_hash_ops;
1611
0
  else if (drbg->core->flags & DRBG_CTR_MASK)
1612
0
    drbg->d_ops = &drbg_ctr_ops;
1613
0
  else
1614
0
    return GPG_ERR_GENERAL;
1615
  /* 9.1 step 1 is implicit with the selected DRBG type -- see
1616
   * drbg_sec_strength() */
1617
1618
  /* 9.1 step 2 is implicit as caller can select prediction resistance
1619
   * and the flag is copied into drbg->flags --
1620
   * all DRBG types support prediction resistance */
1621
1622
  /* 9.1 step 4 is implicit in  drbg_sec_strength */
1623
1624
0
  ret = drbg->d_ops->crypto_init(drbg);
1625
0
  if (ret)
1626
0
    goto err;
1627
1628
0
  drbg->V = xcalloc_secure (1, drbg_statelen (drbg));
1629
0
  if (!drbg->V)
1630
0
    goto fini;
1631
0
  drbg->C = xcalloc_secure (1, drbg_statelen (drbg));
1632
0
  if (!drbg->C)
1633
0
    goto fini;
1634
  /* scratchpad is only generated for CTR and Hash */
1635
0
  if (drbg->core->flags & DRBG_HMAC)
1636
0
    sb_size = 0;
1637
0
  else if (drbg->core->flags & DRBG_CTR_MASK)
1638
0
    sb_size = drbg_statelen (drbg) + drbg_blocklen (drbg) + /* temp */
1639
0
      drbg_statelen (drbg) +  /* df_data */
1640
0
      drbg_blocklen (drbg) +  /* pad */
1641
0
      drbg_blocklen (drbg) +  /* iv */
1642
0
      drbg_statelen (drbg) + drbg_blocklen (drbg); /* temp */
1643
0
  else
1644
0
    sb_size = drbg_statelen (drbg);
1645
1646
0
  if (0 < sb_size)
1647
0
    {
1648
0
      drbg->scratchpad = xcalloc_secure (1, sb_size);
1649
0
      if (!drbg->scratchpad)
1650
0
  goto fini;
1651
0
    }
1652
0
  dbg (("DRBG: state allocated with scratchpad size %u bytes\n", sb_size));
1653
1654
  /* 9.1 step 6 through 11 */
1655
0
  ret = drbg_seed (drbg, pers, 0);
1656
0
  if (ret)
1657
0
    goto fini;
1658
1659
0
  dbg (("DRBG: core %d %s prediction resistance successfully initialized\n",
1660
0
  coreref, pr ? "with" : "without"));
1661
0
  return 0;
1662
1663
0
 fini:
1664
0
  drbg->d_ops->crypto_fini(drbg);
1665
0
 err:
1666
0
  drbg_uninstantiate (drbg);
1667
0
  return ret;
1668
0
}
1669
1670
/*
1671
 * DRBG reseed function as required by SP800-90A
1672
 *
1673
 * @drbg DRBG state handle
1674
 * @addtl Additional input that is mixed into state, may be NULL -- note
1675
 *    the entropy is pulled by the DRBG internally unconditionally
1676
 *    as defined in SP800-90A. The additional input is mixed into
1677
 *    the state in addition to the pulled entropy.
1678
 *
1679
 * return
1680
 *  0 on success
1681
 *  error value otherwise
1682
 */
1683
static gpg_err_code_t
1684
drbg_reseed (drbg_state_t drbg,drbg_string_t *addtl)
1685
0
{
1686
0
  gpg_err_code_t ret = 0;
1687
0
  ret = drbg_seed (drbg, addtl, 1);
1688
0
  return ret;
1689
0
}
1690
1691
1692

1693
/******************************************************************
1694
 * Libgcrypt integration code.
1695
 ******************************************************************/
1696
1697
/***************************************************
1698
 * Libgcrypt backend functions to the RNG API code.
1699
 ***************************************************/
1700
1701
static inline void
1702
drbg_lock (void)
1703
0
{
1704
0
  gpg_err_code_t ec;
1705
1706
0
  ec = gpgrt_lock_lock (&drbg_lock_var);
1707
0
  if (ec)
1708
0
    log_fatal ("failed to acquire the RNG lock: %s\n", gpg_strerror (ec));
1709
0
}
1710
1711
static inline void
1712
drbg_unlock (void)
1713
0
{
1714
0
  gpg_err_code_t ec;
1715
1716
0
  ec = gpgrt_lock_unlock (&drbg_lock_var);
1717
0
  if (ec)
1718
0
    log_fatal ("failed to release the RNG lock: %s\n", gpg_strerror (ec));
1719
0
}
1720
1721
/* Basic initialization is required to initialize mutexes and
1722
   do a few checks on the implementation.  */
1723
static void
1724
basic_initialization (void)
1725
0
{
1726
0
  static int initialized;
1727
1728
0
  if (initialized)
1729
0
    return;
1730
0
  initialized = 1;
1731
1732
  /* Make sure that we are still using the values we have
1733
     traditionally used for the random levels.  */
1734
0
  gcry_assert (GCRY_WEAK_RANDOM == 0
1735
0
               && GCRY_STRONG_RANDOM == 1
1736
0
               && GCRY_VERY_STRONG_RANDOM == 2);
1737
0
}
1738
1739
/****** helper functions where lock must be held by caller *****/
1740
1741
/* Check whether given flags are known to point to an applicable DRBG */
1742
static gpg_err_code_t
1743
drbg_algo_available (u32 flags, int *coreref)
1744
0
{
1745
0
  int i = 0;
1746
0
  for (i = 0; ARRAY_SIZE (drbg_cores) > i; i++)
1747
0
    {
1748
0
      if ((drbg_cores[i].flags & DRBG_CIPHER_MASK) ==
1749
0
    (flags & DRBG_CIPHER_MASK))
1750
0
  {
1751
0
    *coreref = i;
1752
0
    return 0;
1753
0
  }
1754
0
    }
1755
0
  return GPG_ERR_GENERAL;
1756
0
}
1757
1758
static gpg_err_code_t
1759
_drbg_init_internal (u32 flags, drbg_string_t *pers)
1760
0
{
1761
0
  static u32 oldflags;
1762
0
  gpg_err_code_t ret = 0;
1763
0
  int coreref = 0;
1764
0
  int pr = 0;
1765
1766
  /* If a caller provides 0 as flags, use the flags of the previous
1767
   * initialization, otherwise use the current flags and remember them
1768
   * for the next invocation.  If no flag is given and no global state
1769
   * is set this is the first initialization and we set the default
1770
   * type.
1771
   */
1772
0
  if (!flags && !drbg_state)
1773
0
    flags = oldflags = DRBG_DEFAULT_TYPE;
1774
0
  else if (!flags)
1775
0
    flags = oldflags;
1776
0
  else
1777
0
    oldflags = flags;
1778
1779
0
  ret = drbg_algo_available (flags, &coreref);
1780
0
  if (ret)
1781
0
    return ret;
1782
1783
0
  if (drbg_state)
1784
0
    {
1785
0
      drbg_uninstantiate (drbg_state);
1786
0
    }
1787
0
  else
1788
0
    {
1789
0
      drbg_state = &drbg_instance;
1790
0
    }
1791
0
  if (flags & DRBG_PREDICTION_RESIST)
1792
0
    pr = 1;
1793
0
  ret = drbg_instantiate (drbg_state, pers, coreref, pr);
1794
0
  if (ret)
1795
0
    fips_signal_error ("DRBG cannot be initialized");
1796
0
  else
1797
0
    drbg_state->seed_init_pid = getpid ();
1798
0
  return ret;
1799
0
}
1800
1801
/************* calls available to common RNG code **************/
1802
1803
/*
1804
 * Initialize one DRBG invoked by the libgcrypt API
1805
 */
1806
void
1807
_gcry_rngdrbg_inititialize (int full)
1808
0
{
1809
0
  basic_initialization ();
1810
0
  if (!full)
1811
0
      return;
1812
0
  drbg_lock ();
1813
0
  if (!drbg_state)
1814
0
    _drbg_init_internal (0, NULL);
1815
0
  drbg_unlock ();
1816
0
}
1817
1818
/*
1819
 * Backend handler function for GCRYCTL_DRBG_REINIT
1820
 *
1821
 * Select a different DRBG type and initialize it.
1822
 * Function checks whether requested DRBG type exists and returns an error in
1823
 * case it does not. In case of an error, the previous instantiated DRBG is
1824
 * left untouched and alive. Thus, in case of an error, a DRBG is always
1825
 * available, even if it is not the chosen one.
1826
 *
1827
 * Re-initialization will be performed in any case regardless whether flags
1828
 * or personalization string are set.
1829
 *
1830
 * If flags is NULL, do not change current DRBG.  If PERS is NULL and
1831
 * NPERS is 0, re-initialize without personalization string.  If PERS
1832
 * is not NULL NPERS must be one and PERS and the first ietm from the
1833
 * bufer is take as personalization string.
1834
 */
1835
gpg_err_code_t
1836
_gcry_rngdrbg_reinit (const char *flagstr, gcry_buffer_t *pers, int npers)
1837
0
{
1838
0
  gpg_err_code_t ret;
1839
0
  unsigned int flags;
1840
1841
  /* If PERS is not given we expect NPERS to be zero; if given we
1842
     expect a one-item array.  */
1843
0
  if ((!pers && npers) || (pers && npers != 1))
1844
0
    return GPG_ERR_INV_ARG;
1845
1846
0
  ret = parse_flag_string (flagstr, &flags);
1847
0
  if (!ret)
1848
0
    {
1849
0
      dbg (("DRBG: reinitialize internal DRBG state with flags %u\n", flags));
1850
0
      drbg_lock ();
1851
0
      if (pers)
1852
0
        {
1853
0
          drbg_string_t persbuf;
1854
1855
0
          drbg_string_fill
1856
0
            (&persbuf, (const unsigned char *)pers[0].data + pers[0].off,
1857
0
             pers[0].len);
1858
0
          ret = _drbg_init_internal (flags, &persbuf);
1859
0
        }
1860
0
      else
1861
0
        ret = _drbg_init_internal (flags, NULL);
1862
0
      drbg_unlock ();
1863
0
    }
1864
0
  return ret;
1865
0
}
1866
1867
/* Release resources used by this DRBG module.  That is, close the FDs
1868
 * of the random gather module (if any), and release memory used.
1869
 */
1870
void
1871
_gcry_rngdrbg_close_fds (void)
1872
0
{
1873
0
  drbg_lock ();
1874
0
#if USE_RNDGETENTROPY
1875
0
  _gcry_rndgetentropy_gather_random (NULL, 0, 0, 0);
1876
0
#endif
1877
#if USE_RNDOLDLINUX
1878
  _gcry_rndoldlinux_gather_random (NULL, 0, 0, 0);
1879
#endif
1880
0
  if (drbg_state)
1881
0
    {
1882
0
      drbg_uninstantiate (drbg_state);
1883
0
      drbg_state = NULL;
1884
0
    }
1885
0
  drbg_unlock ();
1886
0
}
1887
1888
/* Print some statistics about the RNG.  */
1889
void
1890
_gcry_rngdrbg_dump_stats (void)
1891
0
{
1892
  /* Not yet implemented.  */
1893
  /* Maybe dumping of reseed counter? */
1894
0
}
1895
1896
/* This function returns true if no real RNG is available or the
1897
 * quality of the RNG has been degraded for test purposes.  */
1898
int
1899
_gcry_rngdrbg_is_faked (void)
1900
0
{
1901
0
  return 0;     /* Faked random is not allowed.  */
1902
0
}
1903
1904
/* Add BUFLEN bytes from BUF to the internal random pool.  QUALITY
1905
 * should be in the range of 0..100 to indicate the goodness of the
1906
 * entropy added, or -1 for goodness not known. */
1907
gcry_error_t
1908
_gcry_rngdrbg_add_bytes (const void *buf, size_t buflen, int quality)
1909
0
{
1910
0
  gpg_err_code_t ret = 0;
1911
0
  drbg_string_t seed;
1912
0
  (void) quality;
1913
0
  _gcry_rngdrbg_inititialize (1); /* Auto-initialize if needed */
1914
0
  if (!drbg_state)
1915
0
    return GPG_ERR_GENERAL;
1916
0
  drbg_string_fill (&seed, (unsigned char *) buf, buflen);
1917
0
  drbg_lock ();
1918
0
  ret = drbg_reseed (drbg_state, &seed);
1919
0
  drbg_unlock ();
1920
0
  return ret;
1921
0
}
1922
1923
/* This function is to be used for all types of random numbers, including
1924
 * nonces
1925
 */
1926
void
1927
_gcry_rngdrbg_randomize (void *buffer, size_t length,
1928
          enum gcry_random_level level)
1929
0
{
1930
0
  (void) level;
1931
0
  _gcry_rngdrbg_inititialize (1); /* Auto-initialize if needed */
1932
0
  drbg_lock ();
1933
0
  if (!drbg_state)
1934
0
    {
1935
0
      fips_signal_error ("DRBG is not initialized");
1936
0
      goto bailout;
1937
0
    }
1938
1939
  /* As reseeding changes the entire state of the DRBG, including any
1940
   * key, either a re-init or a reseed is sufficient for a fork */
1941
0
  if (drbg_state->seed_init_pid != getpid ())
1942
0
    {
1943
      /* Update the PID recorded.  */
1944
0
      drbg_state->seed_init_pid = getpid ();
1945
1946
      /* We are in a child of us. Perform a reseeding. */
1947
0
      if (drbg_reseed (drbg_state, NULL))
1948
0
  {
1949
0
    fips_signal_error ("reseeding upon fork failed");
1950
0
    log_fatal ("severe error getting random\n");
1951
0
    goto bailout;
1952
0
  }
1953
0
    }
1954
  /* potential integer overflow is covered by drbg_generate which
1955
   * ensures that length cannot overflow an unsigned int */
1956
0
  if (0 < length)
1957
0
    {
1958
0
      if (!buffer)
1959
0
  goto bailout;
1960
0
      if (drbg_generate_long (drbg_state, buffer, (unsigned int) length, NULL))
1961
0
  log_fatal ("No random numbers generated\n");
1962
0
    }
1963
0
  else
1964
0
    {
1965
0
      drbg_gen_t *data = (drbg_gen_t *)buffer;
1966
      /* catch NULL pointer */
1967
0
      if (!data || !data->outbuf)
1968
0
  {
1969
0
    fips_signal_error ("No output buffer provided");
1970
0
    goto bailout;
1971
0
  }
1972
0
      if (drbg_generate_long (drbg_state, data->outbuf, data->outlen,
1973
0
                              data->addtl))
1974
0
  log_fatal ("No random numbers generated\n");
1975
0
    }
1976
1977
0
 bailout:
1978
0
  drbg_unlock ();
1979
0
  return;
1980
1981
0
}
1982
1983
/***************************************************************
1984
 * Self-test code
1985
 ***************************************************************/
1986
1987
/*
1988
 * Test vectors from
1989
 * http://csrc.nist.gov/groups/STM/cavp/documents/drbg/drbgtestvectors.zip
1990
 */
1991
struct gcry_drbg_test_vector drbg_test_pr[] = {
1992
  {
1993
    /* .flags = */ "sha256 pr" /* DRBG_PR_HASHSHA256 */,
1994
    /* .entropy = */ (unsigned char *)
1995
    "\x5d\xf2\x14\xbc\xf6\xb5\x4e\x0b\xf0\x0d\x6f\x2d"
1996
    "\xe2\x01\x66\x7b\xd0\xa4\x73\xa4\x21\xdd\xb0\xc0"
1997
    "\x51\x79\x09\xf4\xea\xa9\x08\xfa\xa6\x67\xe0\xe1"
1998
    "\xd1\x88\xa8\xad\xee\x69\x74\xb3\x55\x06\x9b\xf6",
1999
    /* .entropylen = */ 48,
2000
    /* .entpra = */ (unsigned char *)
2001
    "\xef\x48\x06\xa2\xc2\x45\xf1\x44\xfa\x34\x2c\xeb"
2002
    "\x8d\x78\x3c\x09\x8f\x34\x72\x20\xf2\xe7\xfd\x13"
2003
    "\x76\x0a\xf6\xdc\x3c\xf5\xc0\x15",
2004
    /* .entprb = */ (unsigned char *)
2005
    "\x4b\xbe\xe5\x24\xed\x6a\x2d\x0c\xdb\x73\x5e\x09"
2006
    "\xf9\xad\x67\x7c\x51\x47\x8b\x6b\x30\x2a\xc6\xde"
2007
    "\x76\xaa\x55\x04\x8b\x0a\x72\x95",
2008
    /* .entprlen = */ 32,
2009
    /* .addtla = */ (unsigned char *)
2010
    "\xbe\x13\xdb\x2a\xe9\xa8\xfe\x09\x97\xe1\xce\x5d"
2011
    "\xe8\xbb\xc0\x7c\x4f\xcb\x62\x19\x3f\x0f\xd2\xad"
2012
    "\xa9\xd0\x1d\x59\x02\xc4\xff\x70",
2013
    /* .addtlb = */ (unsigned char *)
2014
    "\x6f\x96\x13\xe2\xa7\xf5\x6c\xfe\xdf\x66\xe3\x31"
2015
    "\x63\x76\xbf\x20\x27\x06\x49\xf1\xf3\x01\x77\x41"
2016
    "\x9f\xeb\xe4\x38\xfe\x67\x00\xcd",
2017
    /* .addtllen = */ 32,
2018
    /* .pers = */ NULL,
2019
    /* .perslen = */ 0,
2020
    /* .expected = */ (unsigned char *)
2021
    "\x3b\x14\x71\x99\xa1\xda\xa0\x42\xe6\xc8\x85\x32"
2022
    "\x70\x20\x32\x53\x9a\xbe\xd1\x1e\x15\xef\xfb\x4c"
2023
    "\x25\x6e\x19\x3a\xf0\xb9\xcb\xde\xf0\x3b\xc6\x18"
2024
    "\x4d\x85\x5a\x9b\xf1\xe3\xc2\x23\x03\x93\x08\xdb"
2025
    "\xa7\x07\x4b\x33\x78\x40\x4d\xeb\x24\xf5\x6e\x81"
2026
    "\x4a\x1b\x6e\xa3\x94\x52\x43\xb0\xaf\x2e\x21\xf4"
2027
    "\x42\x46\x8e\x90\xed\x34\x21\x75\xea\xda\x67\xb6"
2028
    "\xe4\xf6\xff\xc6\x31\x6c\x9a\x5a\xdb\xb3\x97\x13"
2029
    "\x09\xd3\x20\x98\x33\x2d\x6d\xd7\xb5\x6a\xa8\xa9"
2030
    "\x9a\x5b\xd6\x87\x52\xa1\x89\x2b\x4b\x9c\x64\x60"
2031
    "\x50\x47\xa3\x63\x81\x16\xaf\x19",
2032
    /* .expectedlen = */ 128,
2033
    /* .entropyreseed = */ NULL,
2034
    /* .entropyreseed_len = */ 0,
2035
    /* .addtl_reseed = */ NULL,
2036
    /* .addtl_reseed_len = */ 0
2037
  },
2038
  {
2039
    /* flags = */ "hmac sha256 pr" /* DRBG_PR_HMACSHA256 */,
2040
    /* .entropy = */ (unsigned char *)
2041
    "\x13\x54\x96\xfc\x1b\x7d\x28\xf3\x18\xc9\xa7\x89"
2042
    "\xb6\xb3\xc8\x72\xac\x00\xd4\x59\x36\x25\x05\xaf"
2043
    "\xa5\xdb\x96\xcb\x3c\x58\x46\x87\xa5\xaa\xbf\x20"
2044
    "\x3b\xfe\x23\x0e\xd1\xc7\x41\x0f\x3f\xc9\xb3\x67",
2045
    /* .entropylen = */ 48,
2046
    /* .entpra = */ (unsigned char *)
2047
    "\xe2\xbd\xb7\x48\x08\x06\xf3\xe1\x93\x3c\xac\x79"
2048
    "\xa7\x2b\x11\xda\xe3\x2e\xe1\x91\xa5\x02\x19\x57"
2049
    "\x20\x28\xad\xf2\x60\xd7\xcd\x45",
2050
    /* .entprb = */ (unsigned char *)
2051
    "\x8b\xd4\x69\xfc\xff\x59\x95\x95\xc6\x51\xde\x71"
2052
    "\x68\x5f\xfc\xf9\x4a\xab\xec\x5a\xcb\xbe\xd3\x66"
2053
    "\x1f\xfa\x74\xd3\xac\xa6\x74\x60",
2054
    /* .entprlen = */ 32,
2055
    /* .addtla = */ NULL,
2056
    /* .addtlb = */ NULL,
2057
    /* .addtllen = */ 0,
2058
    /* .pers = */ (unsigned char *)
2059
    "\x64\xb6\xfc\x60\xbc\x61\x76\x23\x6d\x3f\x4a\x0f"
2060
    "\xe1\xb4\xd5\x20\x9e\x70\xdd\x03\x53\x6d\xbf\xce"
2061
    "\xcd\x56\x80\xbc\xb8\x15\xc8\xaa",
2062
    /* .perslen = */ 32,
2063
    /* .expected = */ (unsigned char *)
2064
    "\x1f\x9e\xaf\xe4\xd2\x46\xb7\x47\x41\x4c\x65\x99"
2065
    "\x01\xe9\x3b\xbb\x83\x0c\x0a\xb0\xc1\x3a\xe2\xb3"
2066
    "\x31\x4e\xeb\x93\x73\xee\x0b\x26\xc2\x63\xa5\x75"
2067
    "\x45\x99\xd4\x5c\x9f\xa1\xd4\x45\x87\x6b\x20\x61"
2068
    "\x40\xea\x78\xa5\x32\xdf\x9e\x66\x17\xaf\xb1\x88"
2069
    "\x9e\x2e\x23\xdd\xc1\xda\x13\x97\x88\xa5\xb6\x5e"
2070
    "\x90\x14\x4e\xef\x13\xab\x5c\xd9\x2c\x97\x9e\x7c"
2071
    "\xd7\xf8\xce\xea\x81\xf5\xcd\x71\x15\x49\x44\xce"
2072
    "\x83\xb6\x05\xfb\x7d\x30\xb5\x57\x2c\x31\x4f\xfc"
2073
    "\xfe\x80\xb6\xc0\x13\x0c\x5b\x9b\x2e\x8f\x3d\xfc"
2074
    "\xc2\xa3\x0c\x11\x1b\x80\x5f\xf3",
2075
    /* .expectedlen = */ 128,
2076
    /* .entropyreseed = */ NULL,
2077
    /* .entropyreseed_len = */ 0,
2078
    /* .addtl_reseed = */ NULL,
2079
    /* .addtl_reseed_len = */ 0
2080
  },
2081
  {
2082
    /* .flags = */ "aes sym128 pr", /* DRBG_PR_CTRAES128 */
2083
    /* .entropy = */ (unsigned char *)
2084
    "\x92\x89\x8f\x31\xfa\x1c\xff\x6d\x18\x2f\x26\x06"
2085
    "\x43\xdf\xf8\x18\xc2\xa4\xd9\x72\xc3\xb9\xb6\x97",
2086
    /* .entropylen = */ 24,
2087
    /* .entpra = */ (unsigned char *)
2088
    "\x20\x72\x8a\x06\xf8\x6f\x8d\xd4\x41\xe2\x72\xb7"
2089
    "\xc4\x2c\xe8\x10",
2090
    /* .entprb = */ (unsigned char *)
2091
    "\x3d\xb0\xf0\x94\xf3\x05\x50\x33\x17\x86\x3e\x22"
2092
    "\x08\xf7\xa5\x01",
2093
    /* .entprlen = */ 16,
2094
    /* .addtla = */ (unsigned char *)
2095
    "\x1a\x40\xfa\xe3\xcc\x6c\x7c\xa0\xf8\xda\xba\x59"
2096
    "\x23\x6d\xad\x1d",
2097
    /* .addtlb = */ (unsigned char *)
2098
    "\x9f\x72\x76\x6c\xc7\x46\xe5\xed\x2e\x53\x20\x12"
2099
    "\xbc\x59\x31\x8c",
2100
    /* .addtllen = */ 16,
2101
    /* .pers = */ (unsigned char *)
2102
    "\xea\x65\xee\x60\x26\x4e\x7e\xb6\x0e\x82\x68\xc4"
2103
    "\x37\x3c\x5c\x0b",
2104
    /* .perslen = */ 16,
2105
    /* .expected = */ (unsigned char *)
2106
    "\x5a\x35\x39\x87\x0f\x4d\x22\xa4\x09\x24\xee\x71"
2107
    "\xc9\x6f\xac\x72\x0a\xd6\xf0\x88\x82\xd0\x83\x28"
2108
    "\x73\xec\x3f\x93\xd8\xab\x45\x23\xf0\x7e\xac\x45"
2109
    "\x14\x5e\x93\x9f\xb1\xd6\x76\x43\x3d\xb6\xe8\x08"
2110
    "\x88\xf6\xda\x89\x08\x77\x42\xfe\x1a\xf4\x3f\xc4"
2111
    "\x23\xc5\x1f\x68",
2112
    /* .expectedlen = */ 64,
2113
    /* .entropyreseed = */ NULL,
2114
    /* .entropyreseed_len = */ 0,
2115
    /* .addtl_reseed = */ NULL,
2116
    /* .addtl_reseed_len = */ 0
2117
   }
2118
};
2119
2120
struct gcry_drbg_test_vector drbg_test_nopr[] = {
2121
  {
2122
    /* .flags = */ "sha256" /* DRBG_NOPR_HASHSHA256 */,
2123
    /* .entropy = */ (unsigned char *)
2124
    "\x73\xd3\xfb\xa3\x94\x5f\x2b\x5f\xb9\x8f\xf6\x9c"
2125
    "\x8a\x93\x17\xae\x19\xc3\x4c\xc3\xd6\xca\xa3\x2d"
2126
    "\x16\xfc\x42\xd2\x2d\xd5\x6f\x56\xcc\x1d\x30\xff"
2127
    "\x9e\x06\x3e\x09\xce\x58\xe6\x9a\x35\xb3\xa6\x56",
2128
    /* .entropylen = */ 48,
2129
    /* .entpra = */ NULL,
2130
    /* .entprb = */ NULL,
2131
    /* .entprlen = */ 0,
2132
    /* .addtla = */ (unsigned char *)
2133
    "\xf4\xd5\x98\x3d\xa8\xfc\xfa\x37\xb7\x54\x67\x73"
2134
    "\xc7\xc3\xdd\x47\x34\x71\x02\x5d\xc1\xa0\xd3\x10"
2135
    "\xc1\x8b\xbd\xf5\x66\x34\x6f\xdd",
2136
    /* .addtlb = */ (unsigned char *)
2137
    "\xf7\x9e\x6a\x56\x0e\x73\xe9\xd9\x7a\xd1\x69\xe0"
2138
    "\x6f\x8c\x55\x1c\x44\xd1\xce\x6f\x28\xcc\xa4\x4d"
2139
    "\xa8\xc0\x85\xd1\x5a\x0c\x59\x40",
2140
    /* .addtllen = */ 32,
2141
    /* .pers = */ NULL,
2142
    /* .perslen = */ 0,
2143
    /* .expected = */ (unsigned char *)
2144
    "\x71\x7b\x93\x46\x1a\x40\xaa\x35\xa4\xaa\xc5\xe7"
2145
    "\x6d\x5b\x5b\x8a\xa0\xdf\x39\x7d\xae\x71\x58\x5b"
2146
    "\x3c\x7c\xb4\xf0\x89\xfa\x4a\x8c\xa9\x5c\x54\xc0"
2147
    "\x40\xdf\xbc\xce\x26\x81\x34\xf8\xba\x7d\x1c\xe8"
2148
    "\xad\x21\xe0\x74\xcf\x48\x84\x30\x1f\xa1\xd5\x4f"
2149
    "\x81\x42\x2f\xf4\xdb\x0b\x23\xf8\x73\x27\xb8\x1d"
2150
    "\x42\xf8\x44\x58\xd8\x5b\x29\x27\x0a\xf8\x69\x59"
2151
    "\xb5\x78\x44\xeb\x9e\xe0\x68\x6f\x42\x9a\xb0\x5b"
2152
    "\xe0\x4e\xcb\x6a\xaa\xe2\xd2\xd5\x33\x25\x3e\xe0"
2153
    "\x6c\xc7\x6a\x07\xa5\x03\x83\x9f\xe2\x8b\xd1\x1c"
2154
    "\x70\xa8\x07\x59\x97\xeb\xf6\xbe",
2155
    /* .expectedlen = */ 128,
2156
    /* .entropyreseed = */ NULL,
2157
    /* .entropyreseed_len = */ 0,
2158
    /* .addtl_reseed = */ NULL,
2159
    /* .addtl_reseed_len = */ 0
2160
  },
2161
  {
2162
    /* .flags = */ "hmac sha256" /* DRBG_NOPR_HMACSHA256 */,
2163
    /* .entropy = */ (unsigned char *)
2164
    "\x8d\xf0\x13\xb4\xd1\x03\x52\x30\x73\x91\x7d\xdf"
2165
    "\x6a\x86\x97\x93\x05\x9e\x99\x43\xfc\x86\x54\x54"
2166
    "\x9e\x7a\xb2\x2f\x7c\x29\xf1\x22\xda\x26\x25\xaf"
2167
    "\x2d\xdd\x4a\xbc\xce\x3c\xf4\xfa\x46\x59\xd8\x4e",
2168
    /* .entropylen = */ 48,
2169
    /* .entpra = */ NULL,
2170
    /* .entprb = */ NULL,
2171
    /* .entprlen = */ 0,
2172
    /* .addtla = */ NULL,
2173
    /* .addtlb = */ NULL,
2174
    /* .addtllen = */ 0,
2175
    /* .pers = */ (unsigned char *)
2176
    "\xb5\x71\xe6\x6d\x7c\x33\x8b\xc0\x7b\x76\xad\x37"
2177
    "\x57\xbb\x2f\x94\x52\xbf\x7e\x07\x43\x7a\xe8\x58"
2178
    "\x1c\xe7\xbc\x7c\x3a\xc6\x51\xa9",
2179
    /* .perslen = */ 32,
2180
    /* .expected = */ (unsigned char *)
2181
    "\xb9\x1c\xba\x4c\xc8\x4f\xa2\x5d\xf8\x61\x0b\x81"
2182
    "\xb6\x41\x40\x27\x68\xa2\x09\x72\x34\x93\x2e\x37"
2183
    "\xd5\x90\xb1\x15\x4c\xbd\x23\xf9\x74\x52\xe3\x10"
2184
    "\xe2\x91\xc4\x51\x46\x14\x7f\x0d\xa2\xd8\x17\x61"
2185
    "\xfe\x90\xfb\xa6\x4f\x94\x41\x9c\x0f\x66\x2b\x28"
2186
    "\xc1\xed\x94\xda\x48\x7b\xb7\xe7\x3e\xec\x79\x8f"
2187
    "\xbc\xf9\x81\xb7\x91\xd1\xbe\x4f\x17\x7a\x89\x07"
2188
    "\xaa\x3c\x40\x16\x43\xa5\xb6\x2b\x87\xb8\x9d\x66"
2189
    "\xb3\xa6\x0e\x40\xd4\xa8\xe4\xe9\xd8\x2a\xf6\xd2"
2190
    "\x70\x0e\x6f\x53\x5c\xdb\x51\xf7\x5c\x32\x17\x29"
2191
    "\x10\x37\x41\x03\x0c\xcc\x3a\x56",
2192
    /* .expectedlen = */ 128,
2193
    /* .entropyreseed = */ NULL,
2194
    /* .entropyreseed_len = */ 0,
2195
    /* .addtl_reseed = */ NULL,
2196
    /* .addtl_reseed_len = */ 0
2197
  },
2198
  {
2199
    /* .flags = */ "aes sym128" /* DRBG_NOPR_CTRAES128 */,
2200
    /* .entropy = */ (unsigned char *)
2201
    "\xc0\x70\x1f\x92\x50\x75\x8f\xcd\xf2\xbe\x73\x98"
2202
    "\x80\xdb\x66\xeb\x14\x68\xb4\xa5\x87\x9c\x2d\xa6",
2203
    /* .entropylen = */ 24,
2204
    /* .entpra = */ NULL,
2205
    /* .entprb = */ NULL,
2206
    /* .entprlen = */ 0,
2207
    /* .addtla = */ (unsigned char *)
2208
    "\xf9\x01\xf8\x16\x7a\x1d\xff\xde\x8e\x3c\x83\xe2"
2209
    "\x44\x85\xe7\xfe",
2210
    /* .addtlb = */ (unsigned char *)
2211
    "\x17\x1c\x09\x38\xc2\x38\x9f\x97\x87\x60\x55\xb4"
2212
    "\x82\x16\x62\x7f",
2213
    /* .addtllen = */ 16,
2214
    /* .pers = */ (unsigned char *)
2215
    "\x80\x08\xae\xe8\xe9\x69\x40\xc5\x08\x73\xc7\x9f"
2216
    "\x8e\xcf\xe0\x02",
2217
    /* .perslen = */ 16,
2218
    /* .expected = */ (unsigned char *)
2219
    "\x97\xc0\xc0\xe5\xa0\xcc\xf2\x4f\x33\x63\x48\x8a"
2220
    "\xdb\x13\x0a\x35\x89\xbf\x80\x65\x62\xee\x13\x95"
2221
    "\x7c\x33\xd3\x7d\xf4\x07\x77\x7a\x2b\x65\x0b\x5f"
2222
    "\x45\x5c\x13\xf1\x90\x77\x7f\xc5\x04\x3f\xcc\x1a"
2223
    "\x38\xf8\xcd\x1b\xbb\xd5\x57\xd1\x4a\x4c\x2e\x8a"
2224
    "\x2b\x49\x1e\x5c",
2225
    /* .expectedlen = */ 64,
2226
    /* .entropyreseed = */ NULL,
2227
    /* .entropyreseed_len = */ 0,
2228
    /* .addtl_reseed = */ NULL,
2229
    /* .addtl_reseed_len = */ 0
2230
  },
2231
  {
2232
    /* .flags = */ "sha1" /* DRBG_NOPR_HASHSHA1 */,
2233
    /* .entropy = */ (unsigned char *)
2234
    "\x16\x10\xb8\x28\xcc\xd2\x7d\xe0\x8c\xee\xa0\x32"
2235
    "\xa2\x0e\x92\x08\x49\x2c\xf1\x70\x92\x42\xf6\xb5",
2236
    /* .entropylen = */ 24,
2237
    /* .entpra = */ NULL,
2238
    /* .entprb = */ NULL,
2239
    /* .entprlen = */ 0,
2240
    /* .addtla = */ NULL,
2241
    /* .addtlb = */ NULL,
2242
    /* .addtllen = */ 0,
2243
    /* .pers = */ NULL,
2244
    /* .perslen = */ 0,
2245
    /* .expected = */ (unsigned char *)
2246
    "\x56\xf3\x3d\x4f\xdb\xb9\xa5\xb6\x4d\x26\x23\x44"
2247
    "\x97\xe9\xdc\xb8\x77\x98\xc6\x8d\x08\xf7\xc4\x11"
2248
    "\x99\xd4\xbd\xdf\x97\xeb\xbf\x6c\xb5\x55\x0e\x5d"
2249
    "\x14\x9f\xf4\xd5\xbd\x0f\x05\xf2\x5a\x69\x88\xc1"
2250
    "\x74\x36\x39\x62\x27\x18\x4a\xf8\x4a\x56\x43\x35"
2251
    "\x65\x8e\x2f\x85\x72\xbe\xa3\x33\xee\xe2\xab\xff"
2252
    "\x22\xff\xa6\xde\x3e\x22\xac\xa2",
2253
    /* .expectedlen = */ 80,
2254
    /* .entropyreseed = */ (unsigned char *)
2255
    "\x72\xd2\x8c\x90\x8e\xda\xf9\xa4\xd1\xe5\x26\xd8"
2256
    "\xf2\xde\xd5\x44",
2257
    /* .entropyreseed_len = */ 16,
2258
    /* .addtl_reseed = */ NULL,
2259
    /* .addtl_reseed_len = */ 0
2260
  },
2261
  {
2262
    /* .flags = */ "sha1" /* DRBG_NOPR_HASHSHA1 */,
2263
    /* .entropy = */ (unsigned char *)
2264
    "\xd9\xba\xb5\xce\xdc\xa9\x6f\x61\x78\xd6\x45\x09"
2265
    "\xa0\xdf\xdc\x5e\xda\xd8\x98\x94\x14\x45\x0e\x01",
2266
    /* .entropylen = */ 24,
2267
    /* .entpra = */ NULL,
2268
    /* .entprb = */ NULL,
2269
    /* .entprlen = */ 0,
2270
    /* .addtla = */ (unsigned char *)
2271
    "\x04\xfa\x28\x95\xaa\x5a\x6f\x8c\x57\x43\x34\x3b"
2272
    "\x80\x5e\x5e\xa4",
2273
    /* .addtlb = */ (unsigned char *)
2274
    "\xdf\x5d\xc4\x59\xdf\xf0\x2a\xa2\xf0\x52\xd7\x21"
2275
    "\xec\x60\x72\x30",
2276
    /* .addtllen = */ 16,
2277
    /* .pers = */ NULL,
2278
    /* .perslen = */ 0,
2279
    /* .expected = */ (unsigned char *)
2280
    "\xc4\x8b\x89\xf9\xda\x3f\x74\x82\x45\x55\x5d\x5d"
2281
    "\x03\x3b\x69\x3d\xd7\x1a\x4d\xf5\x69\x02\x05\xce"
2282
    "\xfc\xd7\x20\x11\x3c\xc2\x4e\x09\x89\x36\xff\x5e"
2283
    "\x77\xb5\x41\x53\x58\x70\xb3\x39\x46\x8c\xdd\x8d"
2284
    "\x6f\xaf\x8c\x56\x16\x3a\x70\x0a\x75\xb2\x3e\x59"
2285
    "\x9b\x5a\xec\xf1\x6f\x3b\xaf\x6d\x5f\x24\x19\x97"
2286
    "\x1f\x24\xf4\x46\x72\x0f\xea\xbe",
2287
    /* .expectedlen = */ 80,
2288
    /* .entropyreseed = */ (unsigned char *)
2289
    "\xc6\xba\xd0\x74\xc5\x90\x67\x86\xf5\xe1\xf3\x20"
2290
    "\x99\xf5\xb4\x91",
2291
    /* .entropyreseed_len = */ 16,
2292
    /* .addtl_reseed = */ (unsigned char *)
2293
    "\x3e\x6b\xf4\x6f\x4d\xaa\x38\x25\xd7\x19\x4e\x69"
2294
    "\x4e\x77\x52\xf7",
2295
    /* .addtl_reseed_len = */ 16
2296
  }
2297
};
2298
2299
2300
/*
2301
 * Tests implement the CAVS test approach as documented in
2302
 * http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf
2303
 */
2304
2305
/*
2306
 * CAVS test
2307
 *
2308
 * This function is not static as it is needed for as a private API
2309
 * call for the CAVS test tool.
2310
 */
2311
gpg_err_code_t
2312
_gcry_rngdrbg_cavs_test (struct gcry_drbg_test_vector *test, unsigned char *buf)
2313
0
{
2314
0
  gpg_err_code_t ret = 0;
2315
0
  drbg_state_t drbg = NULL;
2316
0
  struct drbg_test_data_s test_data;
2317
0
  drbg_string_t addtl, pers, testentropy;
2318
0
  int coreref = 0;
2319
0
  int pr = 0;
2320
0
  u32 flags;
2321
2322
0
  ret = parse_flag_string (test->flagstr, &flags);
2323
0
  if (ret)
2324
0
    goto outbuf;
2325
2326
0
  ret = drbg_algo_available (flags, &coreref);
2327
0
  if (ret)
2328
0
    goto outbuf;
2329
2330
0
  drbg = xtrycalloc_secure (1, sizeof *drbg);
2331
0
  if (!drbg)
2332
0
    {
2333
0
      ret = gpg_err_code_from_syserror ();
2334
0
      goto outbuf;
2335
0
    }
2336
2337
0
  if ((flags & DRBG_PREDICTION_RESIST))
2338
0
    pr = 1;
2339
2340
0
  test_data.testentropy = &testentropy;
2341
0
  drbg_string_fill (&testentropy, test->entropy, test->entropylen);
2342
0
  drbg->test_data = &test_data;
2343
0
  drbg_string_fill (&pers, test->pers, test->perslen);
2344
0
  ret = drbg_instantiate (drbg, &pers, coreref, pr);
2345
0
  if (ret)
2346
0
    goto outbuf;
2347
2348
0
  if (test->entropyreseed)
2349
0
    {
2350
0
      drbg_string_fill (&testentropy, test->entropyreseed,
2351
0
           test->entropyreseed_len);
2352
0
      drbg_string_fill (&addtl, test->addtl_reseed,
2353
0
           test->addtl_reseed_len);
2354
0
      if (drbg_reseed (drbg, &addtl))
2355
0
  goto outbuf;
2356
0
    }
2357
2358
0
  drbg_string_fill (&addtl, test->addtla, test->addtllen);
2359
0
  if (test->entpra)
2360
0
    {
2361
0
      drbg_string_fill (&testentropy, test->entpra, test->entprlen);
2362
0
      drbg->test_data = &test_data;
2363
0
    }
2364
0
  drbg_generate_long (drbg, buf, test->expectedlen, &addtl);
2365
2366
0
  drbg_string_fill (&addtl, test->addtlb, test->addtllen);
2367
0
  if (test->entprb)
2368
0
    {
2369
0
      drbg_string_fill (&testentropy, test->entprb, test->entprlen);
2370
0
      drbg->test_data = &test_data;
2371
0
    }
2372
0
  drbg_generate_long (drbg, buf, test->expectedlen, &addtl);
2373
0
  drbg_uninstantiate (drbg);
2374
2375
0
 outbuf:
2376
0
  xfree (drbg);
2377
0
  return ret;
2378
0
}
2379
2380
/*
2381
 * Invoke the CAVS test and perform the final check whether the
2382
 * calculated random value matches the expected one.
2383
 *
2384
 * This function is not static as it is needed for as a private API
2385
 * call for the CAVS test tool.
2386
 */
2387
gpg_err_code_t
2388
_gcry_rngdrbg_healthcheck_one (struct gcry_drbg_test_vector * test)
2389
0
{
2390
0
  gpg_err_code_t ret = GPG_ERR_ENOMEM;
2391
0
  unsigned char *buf = xcalloc_secure (1, test->expectedlen);
2392
0
  if (!buf)
2393
0
    return GPG_ERR_ENOMEM;
2394
2395
0
  ret = _gcry_rngdrbg_cavs_test (test, buf);
2396
  /* FIXME: The next line is wrong.   */
2397
0
  ret = memcmp (test->expected, buf, test->expectedlen);
2398
2399
0
  xfree (buf);
2400
0
  return ret;
2401
0
}
2402
2403
/*
2404
 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
2405
 * of the error handling.
2406
 *
2407
 * Note, testing the reseed counter is not done as an automatic reseeding
2408
 * is performed in drbg_generate when the reseed counter is too large.
2409
 */
2410
static gpg_err_code_t
2411
drbg_healthcheck_sanity (struct gcry_drbg_test_vector *test)
2412
0
{
2413
0
  unsigned int len = 0;
2414
0
  drbg_state_t drbg = NULL;
2415
0
  gpg_err_code_t ret = GPG_ERR_GENERAL;
2416
0
  gpg_err_code_t tmpret = GPG_ERR_GENERAL;
2417
0
  struct drbg_test_data_s test_data;
2418
0
  drbg_string_t addtl, testentropy;
2419
0
  int coreref = 0;
2420
0
  unsigned char *buf = NULL;
2421
0
  size_t max_addtllen, max_request_bytes;
2422
0
  u32 flags;
2423
2424
  /* only perform test in FIPS mode */
2425
0
  if (0 == fips_mode ())
2426
0
    return 0;
2427
2428
0
  ret = parse_flag_string (test->flagstr, &flags);
2429
0
  if (ret)
2430
0
    return ret;
2431
0
  ret = GPG_ERR_GENERAL; /* Fixme: Improve handling of RET.  */
2432
2433
0
  buf = xtrycalloc_secure (1, test->expectedlen);
2434
0
  if (!buf)
2435
0
    return gpg_err_code_from_syserror ();
2436
0
  tmpret = drbg_algo_available (flags, &coreref);
2437
0
  if (tmpret)
2438
0
    goto outbuf;
2439
0
  drbg = xtrycalloc_secure (1, sizeof *drbg);
2440
0
  if (!drbg)
2441
0
    {
2442
0
      ret = gpg_err_code_from_syserror ();
2443
0
      goto outbuf;
2444
0
    }
2445
2446
  /* if the following tests fail, it is likely that there is a buffer
2447
   * overflow and we get a SIGSEV */
2448
0
  ret = drbg_instantiate (drbg, NULL, coreref, 1);
2449
0
  if (ret)
2450
0
    goto outbuf;
2451
0
  max_addtllen = drbg_max_addtl ();
2452
0
  max_request_bytes = drbg_max_request_bytes ();
2453
  /* overflow addtllen with additional info string */
2454
0
  drbg_string_fill (&addtl, test->addtla, (max_addtllen + 1));
2455
0
  len = drbg_generate (drbg, buf, test->expectedlen, &addtl);
2456
0
  if (len)
2457
0
    goto outdrbg;
2458
2459
  /* overflow max_bits */
2460
0
  len = drbg_generate (drbg, buf, (max_request_bytes + 1), NULL);
2461
0
  if (len)
2462
0
    goto outdrbg;
2463
0
  drbg_uninstantiate (drbg);
2464
2465
  /* test failing entropy source as defined in 11.3.2 */
2466
0
  test_data.testentropy = NULL;
2467
0
  test_data.fail_seed_source = 1;
2468
0
  drbg->test_data = &test_data;
2469
0
  tmpret = drbg_instantiate (drbg, NULL, coreref, 0);
2470
0
  if (!tmpret)
2471
0
    goto outdrbg;
2472
0
  test_data.fail_seed_source = 0;
2473
2474
0
  test_data.testentropy = &testentropy;
2475
0
  drbg_string_fill (&testentropy, test->entropy, test->entropylen);
2476
  /* overflow max addtllen with personalization string */
2477
0
  tmpret = drbg_instantiate (drbg, &addtl, coreref, 0);
2478
0
  if (!tmpret)
2479
0
    goto outdrbg;
2480
2481
0
  dbg (("DRBG: Sanity tests for failure code paths successfully completed\n"));
2482
0
  ret = 0;
2483
2484
0
 outdrbg:
2485
0
  drbg_uninstantiate (drbg);
2486
0
 outbuf:
2487
0
  xfree (buf);
2488
0
  xfree (drbg);
2489
0
  return ret;
2490
0
}
2491
2492
/*
2493
 * DRBG Healthcheck function as required in SP800-90A
2494
 *
2495
 * return:
2496
 *  0 on success (all tests pass)
2497
 *  >0 on error (return code indicate the number of failures)
2498
 */
2499
static int
2500
drbg_healthcheck (void)
2501
0
{
2502
0
  int ret = 0;
2503
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_nopr[0]);
2504
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_nopr[1]);
2505
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_nopr[2]);
2506
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_nopr[3]);
2507
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_nopr[4]);
2508
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_pr[0]);
2509
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_pr[1]);
2510
0
  ret += _gcry_rngdrbg_healthcheck_one (&drbg_test_pr[2]);
2511
0
  ret += drbg_healthcheck_sanity (&drbg_test_nopr[0]);
2512
0
  return ret;
2513
0
}
2514
2515
/* Run the self-tests.  */
2516
gcry_error_t
2517
_gcry_rngdrbg_selftest (selftest_report_func_t report)
2518
0
{
2519
0
  gcry_err_code_t ec;
2520
0
  const char *errtxt = NULL;
2521
0
  drbg_lock ();
2522
0
  if (0 != drbg_healthcheck ())
2523
0
    errtxt = "RNG output does not match known value";
2524
0
  drbg_unlock ();
2525
0
  if (report && errtxt)
2526
0
    report ("random", 0, "KAT", errtxt);
2527
0
  ec = errtxt ? GPG_ERR_SELFTEST_FAILED : 0;
2528
0
  return gpg_error (ec);
2529
0
}
2530
2531
/***************************************************************
2532
 * Cipher invocations requested by DRBG
2533
 ***************************************************************/
2534
2535
static gpg_err_code_t
2536
drbg_hash_init (drbg_state_t drbg)
2537
0
{
2538
0
  gcry_md_hd_t hd;
2539
0
  gpg_error_t err;
2540
2541
0
  err = _gcry_md_open (&hd, drbg->core->backend_cipher, 0);
2542
0
  if (err)
2543
0
    return err;
2544
2545
0
  drbg->priv_data = hd;
2546
2547
0
  return 0;
2548
0
}
2549
2550
static gpg_err_code_t
2551
drbg_hmac_init (drbg_state_t drbg)
2552
0
{
2553
0
  gcry_md_hd_t hd;
2554
0
  gpg_error_t err;
2555
2556
0
  err = _gcry_md_open (&hd, drbg->core->backend_cipher, GCRY_MD_FLAG_HMAC);
2557
0
  if (err)
2558
0
    return err;
2559
2560
0
  drbg->priv_data = hd;
2561
2562
0
  return 0;
2563
0
}
2564
2565
static gpg_err_code_t
2566
drbg_hmac_setkey (drbg_state_t drbg, const unsigned char *key)
2567
0
{
2568
0
  gcry_md_hd_t hd = (gcry_md_hd_t)drbg->priv_data;
2569
2570
0
  return _gcry_md_setkey (hd, key, drbg_statelen (drbg));
2571
0
}
2572
2573
static void
2574
drbg_hash_fini (drbg_state_t drbg)
2575
0
{
2576
0
  gcry_md_hd_t hd = (gcry_md_hd_t)drbg->priv_data;
2577
2578
0
  _gcry_md_close (hd);
2579
0
}
2580
2581
static byte *
2582
drbg_hash (drbg_state_t drbg, const drbg_string_t *buf)
2583
0
{
2584
0
  gcry_md_hd_t hd = (gcry_md_hd_t)drbg->priv_data;
2585
2586
0
  _gcry_md_reset(hd);
2587
0
  for (; NULL != buf; buf = buf->next)
2588
0
    _gcry_md_write (hd, buf->buf, buf->len);
2589
0
  _gcry_md_final (hd);
2590
0
  return _gcry_md_read (hd, drbg->core->backend_cipher);
2591
0
}
2592
2593
static void
2594
drbg_sym_fini (drbg_state_t drbg)
2595
0
{
2596
0
  gcry_cipher_hd_t hd = (gcry_cipher_hd_t)drbg->priv_data;
2597
2598
0
  if (hd)
2599
0
    _gcry_cipher_close (hd);
2600
0
  if (drbg->ctr_handle)
2601
0
    _gcry_cipher_close (drbg->ctr_handle);
2602
0
}
2603
2604
static gpg_err_code_t
2605
drbg_sym_init (drbg_state_t drbg)
2606
0
{
2607
0
  gcry_cipher_hd_t hd;
2608
0
  gpg_error_t err;
2609
2610
0
  err = _gcry_cipher_open (&hd, drbg->core->backend_cipher,
2611
0
         GCRY_CIPHER_MODE_ECB, 0);
2612
0
  if (err)
2613
0
    {
2614
0
      drbg_sym_fini (drbg);
2615
0
      return err;
2616
0
    }
2617
0
  drbg->priv_data = hd;
2618
2619
0
  err = _gcry_cipher_open (&drbg->ctr_handle, drbg->core->backend_cipher,
2620
0
         GCRY_CIPHER_MODE_CTR, 0);
2621
0
  if (err)
2622
0
    {
2623
0
      drbg_sym_fini (drbg);
2624
0
      return err;
2625
0
    }
2626
2627
2628
0
  if (drbg_blocklen (drbg) !=
2629
0
      _gcry_cipher_get_algo_blklen (drbg->core->backend_cipher))
2630
0
    {
2631
0
      drbg_sym_fini (drbg);
2632
0
      return -GPG_ERR_NO_ERROR;
2633
0
    }
2634
2635
0
  return 0;
2636
0
}
2637
2638
static gpg_err_code_t
2639
drbg_sym_setkey (drbg_state_t drbg, const unsigned char *key)
2640
0
{
2641
0
  gcry_cipher_hd_t hd = (gcry_cipher_hd_t)drbg->priv_data;
2642
2643
0
  return _gcry_cipher_setkey (hd, key, drbg_keylen (drbg));
2644
0
}
2645
2646
static gpg_err_code_t
2647
drbg_sym (drbg_state_t drbg, unsigned char *outval, const drbg_string_t *buf)
2648
0
{
2649
0
  gcry_cipher_hd_t hd = (gcry_cipher_hd_t)drbg->priv_data;
2650
2651
0
  _gcry_cipher_reset(hd);
2652
0
  if (drbg_blocklen (drbg) < buf->len)
2653
0
    return -GPG_ERR_NO_ERROR;
2654
  /* in is only component */
2655
0
  return _gcry_cipher_encrypt (hd, outval, drbg_blocklen (drbg), buf->buf,
2656
0
             buf->len);
2657
0
}
2658
2659
static gpg_err_code_t
2660
drbg_sym_ctr (drbg_state_t drbg,
2661
        const unsigned char *inbuf, unsigned int inbuflen,
2662
        unsigned char *outbuf, unsigned int outbuflen)
2663
0
{
2664
0
  gpg_error_t err;
2665
2666
0
  _gcry_cipher_reset(drbg->ctr_handle);
2667
0
  err = _gcry_cipher_setctr(drbg->ctr_handle, drbg->V, drbg_blocklen (drbg));
2668
0
  if (err)
2669
0
    return err;
2670
2671
0
  while (outbuflen)
2672
0
    {
2673
0
       unsigned int cryptlen = (inbuflen > outbuflen) ? outbuflen : inbuflen;
2674
2675
0
       err = _gcry_cipher_encrypt (drbg->ctr_handle, outbuf, cryptlen, inbuf,
2676
0
           cryptlen);
2677
0
       if (err)
2678
0
         return err;
2679
2680
0
       outbuflen -= cryptlen;
2681
0
       outbuf += cryptlen;
2682
0
    }
2683
0
  return _gcry_cipher_getctr(drbg->ctr_handle, drbg->V, drbg_blocklen (drbg));
2684
0
}