Coverage Report

Created: 2022-12-08 06:09

/src/libgcrypt/cipher/cipher.c
Line
Count
Source (jump to first uncovered line)
1
/* cipher.c  -  cipher dispatcher
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3
 *               2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
4
 * Copyright (C) 2013 g10 Code GmbH
5
 *
6
 * This file is part of Libgcrypt.
7
 *
8
 * Libgcrypt is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser general Public License as
10
 * published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * Libgcrypt is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <errno.h>
27
28
#include "g10lib.h"
29
#include "../src/gcrypt-testapi.h"
30
#include "cipher.h"
31
#include "./cipher-internal.h"
32
33
34
/* This is the list of the default ciphers, which are included in
35
   libgcrypt.  */
36
static gcry_cipher_spec_t * const cipher_list[] =
37
  {
38
#if USE_BLOWFISH
39
     &_gcry_cipher_spec_blowfish,
40
#endif
41
#if USE_DES
42
     &_gcry_cipher_spec_des,
43
     &_gcry_cipher_spec_tripledes,
44
#endif
45
#if USE_ARCFOUR
46
     &_gcry_cipher_spec_arcfour,
47
#endif
48
#if USE_CAST5
49
     &_gcry_cipher_spec_cast5,
50
#endif
51
#if USE_AES
52
     &_gcry_cipher_spec_aes,
53
     &_gcry_cipher_spec_aes192,
54
     &_gcry_cipher_spec_aes256,
55
#endif
56
#if USE_TWOFISH
57
     &_gcry_cipher_spec_twofish,
58
     &_gcry_cipher_spec_twofish128,
59
#endif
60
#if USE_SERPENT
61
     &_gcry_cipher_spec_serpent128,
62
     &_gcry_cipher_spec_serpent192,
63
     &_gcry_cipher_spec_serpent256,
64
#endif
65
#if USE_RFC2268
66
     &_gcry_cipher_spec_rfc2268_40,
67
     &_gcry_cipher_spec_rfc2268_128,
68
#endif
69
#if USE_SEED
70
     &_gcry_cipher_spec_seed,
71
#endif
72
#if USE_CAMELLIA
73
     &_gcry_cipher_spec_camellia128,
74
     &_gcry_cipher_spec_camellia192,
75
     &_gcry_cipher_spec_camellia256,
76
#endif
77
#if USE_IDEA
78
     &_gcry_cipher_spec_idea,
79
#endif
80
#if USE_SALSA20
81
     &_gcry_cipher_spec_salsa20,
82
     &_gcry_cipher_spec_salsa20r12,
83
#endif
84
#if USE_GOST28147
85
     &_gcry_cipher_spec_gost28147,
86
     &_gcry_cipher_spec_gost28147_mesh,
87
#endif
88
#if USE_CHACHA20
89
     &_gcry_cipher_spec_chacha20,
90
#endif
91
#if USE_SM4
92
     &_gcry_cipher_spec_sm4,
93
#endif
94
    NULL
95
  };
96
97
/* Cipher implementations starting with index 0 (enum gcry_cipher_algos) */
98
static gcry_cipher_spec_t * const cipher_list_algo0[] =
99
  {
100
    NULL, /* GCRY_CIPHER_NONE */
101
#if USE_IDEA
102
    &_gcry_cipher_spec_idea,
103
#else
104
    NULL,
105
#endif
106
#if USE_DES
107
    &_gcry_cipher_spec_tripledes,
108
#else
109
    NULL,
110
#endif
111
#if USE_CAST5
112
    &_gcry_cipher_spec_cast5,
113
#else
114
    NULL,
115
#endif
116
#if USE_BLOWFISH
117
    &_gcry_cipher_spec_blowfish,
118
#else
119
    NULL,
120
#endif
121
    NULL, /* GCRY_CIPHER_SAFER_SK128 */
122
    NULL, /* GCRY_CIPHER_DES_SK */
123
#if USE_AES
124
    &_gcry_cipher_spec_aes,
125
    &_gcry_cipher_spec_aes192,
126
    &_gcry_cipher_spec_aes256,
127
#else
128
    NULL,
129
    NULL,
130
    NULL,
131
#endif
132
#if USE_TWOFISH
133
    &_gcry_cipher_spec_twofish
134
#else
135
    NULL
136
#endif
137
  };
138
139
/* Cipher implementations starting with index 301 (enum gcry_cipher_algos) */
140
static gcry_cipher_spec_t * const cipher_list_algo301[] =
141
  {
142
#if USE_ARCFOUR
143
    &_gcry_cipher_spec_arcfour,
144
#else
145
    NULL,
146
#endif
147
#if USE_DES
148
    &_gcry_cipher_spec_des,
149
#else
150
    NULL,
151
#endif
152
#if USE_TWOFISH
153
    &_gcry_cipher_spec_twofish128,
154
#else
155
    NULL,
156
#endif
157
#if USE_SERPENT
158
    &_gcry_cipher_spec_serpent128,
159
    &_gcry_cipher_spec_serpent192,
160
    &_gcry_cipher_spec_serpent256,
161
#else
162
    NULL,
163
    NULL,
164
    NULL,
165
#endif
166
#if USE_RFC2268
167
    &_gcry_cipher_spec_rfc2268_40,
168
    &_gcry_cipher_spec_rfc2268_128,
169
#else
170
    NULL,
171
    NULL,
172
#endif
173
#if USE_SEED
174
    &_gcry_cipher_spec_seed,
175
#else
176
    NULL,
177
#endif
178
#if USE_CAMELLIA
179
    &_gcry_cipher_spec_camellia128,
180
    &_gcry_cipher_spec_camellia192,
181
    &_gcry_cipher_spec_camellia256,
182
#else
183
    NULL,
184
    NULL,
185
    NULL,
186
#endif
187
#if USE_SALSA20
188
    &_gcry_cipher_spec_salsa20,
189
    &_gcry_cipher_spec_salsa20r12,
190
#else
191
    NULL,
192
    NULL,
193
#endif
194
#if USE_GOST28147
195
    &_gcry_cipher_spec_gost28147,
196
#else
197
    NULL,
198
#endif
199
#if USE_CHACHA20
200
    &_gcry_cipher_spec_chacha20,
201
#else
202
    NULL,
203
#endif
204
#if USE_GOST28147
205
    &_gcry_cipher_spec_gost28147_mesh,
206
#else
207
    NULL,
208
#endif
209
#if USE_SM4
210
     &_gcry_cipher_spec_sm4,
211
#else
212
    NULL,
213
#endif
214
  };
215
216
217
static void _gcry_cipher_setup_mode_ops(gcry_cipher_hd_t c, int mode);
218
219

220
static int
221
map_algo (int algo)
222
128k
{
223
128k
  return algo;
224
128k
}
225
226
227
/* Return the spec structure for the cipher algorithm ALGO.  For
228
   an unknown algorithm NULL is returned.  */
229
static gcry_cipher_spec_t *
230
spec_from_algo (int algo)
231
128k
{
232
128k
  gcry_cipher_spec_t *spec = NULL;
233
234
128k
  algo = map_algo (algo);
235
236
128k
  if (algo >= 0 && algo < DIM(cipher_list_algo0))
237
127k
    spec = cipher_list_algo0[algo];
238
323
  else if (algo >= 301 && algo < 301 + DIM(cipher_list_algo301))
239
323
    spec = cipher_list_algo301[algo - 301];
240
241
128k
  if (spec)
242
128k
    gcry_assert (spec->algo == algo);
243
244
0
  return spec;
245
128k
}
246
247
248
/* Lookup a cipher's spec by its name.  */
249
static gcry_cipher_spec_t *
250
spec_from_name (const char *name)
251
0
{
252
0
  gcry_cipher_spec_t *spec;
253
0
  int idx;
254
0
  const char **aliases;
255
256
0
  for (idx=0; (spec = cipher_list[idx]); idx++)
257
0
    {
258
0
      if (!stricmp (name, spec->name))
259
0
        return spec;
260
0
      if (spec->aliases)
261
0
        {
262
0
          for (aliases = spec->aliases; *aliases; aliases++)
263
0
            if (!stricmp (name, *aliases))
264
0
              return spec;
265
0
        }
266
0
    }
267
268
0
  return NULL;
269
0
}
270
271
272
/* Lookup a cipher's spec by its OID.  */
273
static gcry_cipher_spec_t *
274
spec_from_oid (const char *oid)
275
0
{
276
0
  gcry_cipher_spec_t *spec;
277
0
  const gcry_cipher_oid_spec_t *oid_specs;
278
0
  int idx, j;
279
280
0
  for (idx=0; (spec = cipher_list[idx]); idx++)
281
0
    {
282
0
      oid_specs = spec->oids;
283
0
      if (oid_specs)
284
0
        {
285
0
          for (j = 0; oid_specs[j].oid; j++)
286
0
            if (!stricmp (oid, oid_specs[j].oid))
287
0
              return spec;
288
0
        }
289
0
    }
290
291
0
  return NULL;
292
0
}
293
294
295
/* Locate the OID in the oid table and return the spec or NULL if not
296
   found.  An optional "oid." or "OID." prefix in OID is ignored, the
297
   OID is expected to be in standard IETF dotted notation.  A pointer
298
   to the OID specification of the module implementing this algorithm
299
   is return in OID_SPEC unless passed as NULL.*/
300
static gcry_cipher_spec_t *
301
search_oid (const char *oid, gcry_cipher_oid_spec_t *oid_spec)
302
0
{
303
0
  gcry_cipher_spec_t *spec;
304
0
  int i;
305
306
0
  if (!oid)
307
0
    return NULL;
308
309
0
  if (!strncmp (oid, "oid.", 4) || !strncmp (oid, "OID.", 4))
310
0
    oid += 4;
311
312
0
  spec = spec_from_oid (oid);
313
0
  if (spec && spec->oids)
314
0
    {
315
0
      for (i = 0; spec->oids[i].oid; i++)
316
0
  if (!stricmp (oid, spec->oids[i].oid))
317
0
    {
318
0
      if (oid_spec)
319
0
        *oid_spec = spec->oids[i];
320
0
            return spec;
321
0
    }
322
0
    }
323
324
0
  return NULL;
325
0
}
326
327
328
/* Map STRING to the cipher algorithm identifier.  Returns the
329
   algorithm ID of the cipher for the given name or 0 if the name is
330
   not known.  It is valid to pass NULL for STRING which results in a
331
   return value of 0. */
332
int
333
_gcry_cipher_map_name (const char *string)
334
0
{
335
0
  gcry_cipher_spec_t *spec;
336
337
0
  if (!string)
338
0
    return 0;
339
340
  /* If the string starts with a digit (optionally prefixed with
341
     either "OID." or "oid."), we first look into our table of ASN.1
342
     object identifiers to figure out the algorithm */
343
344
0
  spec = search_oid (string, NULL);
345
0
  if (spec)
346
0
    return spec->algo;
347
348
0
  spec = spec_from_name (string);
349
0
  if (spec)
350
0
    return spec->algo;
351
352
0
  return 0;
353
0
}
354
355
356
/* Given a STRING with an OID in dotted decimal notation, this
357
   function returns the cipher mode (GCRY_CIPHER_MODE_*) associated
358
   with that OID or 0 if no mode is known.  Passing NULL for string
359
   yields a return value of 0. */
360
int
361
_gcry_cipher_mode_from_oid (const char *string)
362
0
{
363
0
  gcry_cipher_spec_t *spec;
364
0
  gcry_cipher_oid_spec_t oid_spec;
365
366
0
  if (!string)
367
0
    return 0;
368
369
0
  spec = search_oid (string, &oid_spec);
370
0
  if (spec)
371
0
    return oid_spec.mode;
372
373
0
  return 0;
374
0
}
375
376
377
/* Map the cipher algorithm identifier ALGORITHM to a string
378
   representing this algorithm.  This string is the default name as
379
   used by Libgcrypt.  A "?" is returned for an unknown algorithm.
380
   NULL is never returned. */
381
const char *
382
_gcry_cipher_algo_name (int algorithm)
383
0
{
384
0
  gcry_cipher_spec_t *spec;
385
386
0
  spec = spec_from_algo (algorithm);
387
0
  return spec? spec->name : "?";
388
0
}
389
390
391
/* Flag the cipher algorithm with the identifier ALGORITHM as
392
   disabled.  There is no error return, the function does nothing for
393
   unknown algorithms.  Disabled algorithms are virtually not
394
   available in Libgcrypt.  This is not thread safe and should thus be
395
   called early. */
396
static void
397
disable_cipher_algo (int algo)
398
0
{
399
0
  gcry_cipher_spec_t *spec = spec_from_algo (algo);
400
401
0
  if (spec)
402
0
    spec->flags.disabled = 1;
403
0
}
404
405
406
/* Return 0 if the cipher algorithm with identifier ALGORITHM is
407
   available. Returns a basic error code value if it is not
408
   available.  */
409
static gcry_err_code_t
410
check_cipher_algo (int algorithm)
411
128k
{
412
128k
  gcry_cipher_spec_t *spec;
413
414
128k
  spec = spec_from_algo (algorithm);
415
128k
  if (spec && !spec->flags.disabled && (spec->flags.fips || !fips_mode ()))
416
128k
    return 0;
417
418
0
  return GPG_ERR_CIPHER_ALGO;
419
128k
}
420
421
422
/* Return the standard length in bits of the key for the cipher
423
   algorithm with the identifier ALGORITHM.  */
424
static unsigned int
425
cipher_get_keylen (int algorithm)
426
0
{
427
0
  gcry_cipher_spec_t *spec;
428
0
  unsigned len = 0;
429
430
0
  spec = spec_from_algo (algorithm);
431
0
  if (spec)
432
0
    {
433
0
      len = spec->keylen;
434
0
      if (!len)
435
0
  log_bug ("cipher %d w/o key length\n", algorithm);
436
0
    }
437
438
0
  return len;
439
0
}
440
441
442
/* Return the block length of the cipher algorithm with the identifier
443
   ALGORITHM.  This function return 0 for an invalid algorithm.  */
444
static unsigned int
445
cipher_get_blocksize (int algorithm)
446
0
{
447
0
  gcry_cipher_spec_t *spec;
448
0
  unsigned len = 0;
449
450
0
  spec = spec_from_algo (algorithm);
451
0
  if (spec)
452
0
    {
453
0
      len = spec->blocksize;
454
0
      if (!len)
455
0
        log_bug ("cipher %d w/o blocksize\n", algorithm);
456
0
    }
457
458
0
  return len;
459
0
}
460
461
462
/*
463
   Open a cipher handle for use with cipher algorithm ALGORITHM, using
464
   the cipher mode MODE (one of the GCRY_CIPHER_MODE_*) and return a
465
   handle in HANDLE.  Put NULL into HANDLE and return an error code if
466
   something goes wrong.  FLAGS may be used to modify the
467
   operation.  The defined flags are:
468
469
   GCRY_CIPHER_SECURE:  allocate all internal buffers in secure memory.
470
   GCRY_CIPHER_ENABLE_SYNC:  Enable the sync operation as used in OpenPGP.
471
   GCRY_CIPHER_CBC_CTS:  Enable CTS mode.
472
   GCRY_CIPHER_CBC_MAC:  Enable MAC mode.
473
474
   Values for these flags may be combined using OR.
475
 */
476
gcry_err_code_t
477
_gcry_cipher_open (gcry_cipher_hd_t *handle,
478
                   int algo, int mode, unsigned int flags)
479
0
{
480
0
  gcry_err_code_t rc;
481
0
  gcry_cipher_hd_t h = NULL;
482
483
0
  if (mode >= GCRY_CIPHER_MODE_INTERNAL)
484
0
    rc = GPG_ERR_INV_CIPHER_MODE;
485
0
  else
486
0
    rc = _gcry_cipher_open_internal (&h, algo, mode, flags);
487
488
0
  *handle = rc ? NULL : h;
489
490
0
  return rc;
491
0
}
492
493
494
gcry_err_code_t
495
_gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
496
          int algo, int mode, unsigned int flags)
497
0
{
498
0
  int secure = (flags & GCRY_CIPHER_SECURE);
499
0
  gcry_cipher_spec_t *spec;
500
0
  gcry_cipher_hd_t h = NULL;
501
0
  gcry_err_code_t err;
502
503
  /* If the application missed to call the random poll function, we do
504
     it here to ensure that it is used once in a while. */
505
0
  _gcry_fast_random_poll ();
506
507
0
  spec = spec_from_algo (algo);
508
0
  if (!spec)
509
0
    err = GPG_ERR_CIPHER_ALGO;
510
0
  else if (spec->flags.disabled)
511
0
    err = GPG_ERR_CIPHER_ALGO;
512
0
  else if (!spec->flags.fips && fips_mode ())
513
0
    err = GPG_ERR_CIPHER_ALGO;
514
0
  else
515
0
    err = 0;
516
517
  /* check flags */
518
0
  if ((! err)
519
0
      && ((flags & ~(0
520
0
         | GCRY_CIPHER_SECURE
521
0
         | GCRY_CIPHER_ENABLE_SYNC
522
0
         | GCRY_CIPHER_CBC_CTS
523
0
         | GCRY_CIPHER_CBC_MAC
524
0
                     | GCRY_CIPHER_EXTENDED))
525
0
    || ((flags & GCRY_CIPHER_CBC_CTS) && (flags & GCRY_CIPHER_CBC_MAC))))
526
0
    err = GPG_ERR_CIPHER_ALGO;
527
528
  /* check that a valid mode has been requested */
529
0
  if (! err)
530
0
    switch (mode)
531
0
      {
532
0
      case GCRY_CIPHER_MODE_ECB:
533
0
      case GCRY_CIPHER_MODE_CBC:
534
0
      case GCRY_CIPHER_MODE_CFB:
535
0
      case GCRY_CIPHER_MODE_CFB8:
536
0
      case GCRY_CIPHER_MODE_OFB:
537
0
      case GCRY_CIPHER_MODE_CTR:
538
0
      case GCRY_CIPHER_MODE_AESWRAP:
539
0
      case GCRY_CIPHER_MODE_CMAC:
540
0
      case GCRY_CIPHER_MODE_EAX:
541
0
  if (!spec->encrypt || !spec->decrypt)
542
0
    err = GPG_ERR_INV_CIPHER_MODE;
543
0
  break;
544
545
0
      case GCRY_CIPHER_MODE_CCM:
546
0
  if (!spec->encrypt || !spec->decrypt)
547
0
    err = GPG_ERR_INV_CIPHER_MODE;
548
0
  else if (spec->blocksize != GCRY_CCM_BLOCK_LEN)
549
0
    err = GPG_ERR_INV_CIPHER_MODE;
550
0
  break;
551
552
0
      case GCRY_CIPHER_MODE_XTS:
553
0
  if (!spec->encrypt || !spec->decrypt)
554
0
    err = GPG_ERR_INV_CIPHER_MODE;
555
0
  else if (spec->blocksize != GCRY_XTS_BLOCK_LEN)
556
0
    err = GPG_ERR_INV_CIPHER_MODE;
557
0
  break;
558
559
0
      case GCRY_CIPHER_MODE_GCM:
560
0
  if (!spec->encrypt || !spec->decrypt)
561
0
    err = GPG_ERR_INV_CIPHER_MODE;
562
0
  else if (spec->blocksize != GCRY_GCM_BLOCK_LEN)
563
0
    err = GPG_ERR_INV_CIPHER_MODE;
564
0
  break;
565
566
0
      case GCRY_CIPHER_MODE_SIV:
567
0
      case GCRY_CIPHER_MODE_GCM_SIV:
568
0
  if (!spec->encrypt || !spec->decrypt)
569
0
    err = GPG_ERR_INV_CIPHER_MODE;
570
0
  else if (spec->blocksize != GCRY_SIV_BLOCK_LEN)
571
0
    err = GPG_ERR_INV_CIPHER_MODE;
572
0
  break;
573
574
0
      case GCRY_CIPHER_MODE_POLY1305:
575
0
  if (!spec->stencrypt || !spec->stdecrypt || !spec->setiv)
576
0
    err = GPG_ERR_INV_CIPHER_MODE;
577
0
  else if (spec->algo != GCRY_CIPHER_CHACHA20)
578
0
    err = GPG_ERR_INV_CIPHER_MODE;
579
0
  break;
580
581
0
      case GCRY_CIPHER_MODE_OCB:
582
        /* Note that our implementation allows only for 128 bit block
583
           length algorithms.  Lower block lengths would be possible
584
           but we do not implement them because they limit the
585
           security too much.  */
586
0
  if (!spec->encrypt || !spec->decrypt)
587
0
    err = GPG_ERR_INV_CIPHER_MODE;
588
0
  else if (spec->blocksize != GCRY_OCB_BLOCK_LEN)
589
0
    err = GPG_ERR_INV_CIPHER_MODE;
590
0
  break;
591
592
0
      case GCRY_CIPHER_MODE_STREAM:
593
0
  if (!spec->stencrypt || !spec->stdecrypt)
594
0
    err = GPG_ERR_INV_CIPHER_MODE;
595
0
  break;
596
597
0
      case GCRY_CIPHER_MODE_NONE:
598
        /* This mode may be used for debugging.  It copies the main
599
           text verbatim to the ciphertext.  We do not allow this in
600
           fips mode or if no debug flag has been set.  */
601
0
  if (fips_mode () || !_gcry_get_debug_flag (0))
602
0
          err = GPG_ERR_INV_CIPHER_MODE;
603
0
  break;
604
605
0
      default:
606
0
  err = GPG_ERR_INV_CIPHER_MODE;
607
0
      }
608
609
  /* Perform selftest here and mark this with a flag in cipher_table?
610
     No, we should not do this as it takes too long.  Further it does
611
     not make sense to exclude algorithms with failing selftests at
612
     runtime: If a selftest fails there is something seriously wrong
613
     with the system and thus we better die immediately. */
614
615
0
  if (! err)
616
0
    {
617
0
      size_t size = (sizeof (*h)
618
0
                     + 2 * spec->contextsize
619
0
                     - sizeof (cipher_context_alignment_t)
620
0
#ifdef NEED_16BYTE_ALIGNED_CONTEXT
621
0
                     + 15  /* Space for leading alignment gap.  */
622
0
#endif /*NEED_16BYTE_ALIGNED_CONTEXT*/
623
0
                     );
624
625
      /* Space needed per mode.  */
626
0
      switch (mode)
627
0
  {
628
0
  case GCRY_CIPHER_MODE_XTS:
629
0
  case GCRY_CIPHER_MODE_SIV:
630
    /* Additional cipher context for tweak. */
631
0
    size += 2 * spec->contextsize + 15;
632
0
    break;
633
634
0
  default:
635
0
    break;
636
0
  }
637
638
0
      if (secure)
639
0
  h = xtrycalloc_secure (1, size);
640
0
      else
641
0
  h = xtrycalloc (1, size);
642
643
0
      if (! h)
644
0
  err = gpg_err_code_from_syserror ();
645
0
      else
646
0
  {
647
0
          size_t off = 0;
648
0
    char *tc;
649
650
0
#ifdef NEED_16BYTE_ALIGNED_CONTEXT
651
0
          if ( ((uintptr_t)h & 0x0f) )
652
0
            {
653
              /* The malloced block is not aligned on a 16 byte
654
                 boundary.  Correct for this.  */
655
0
              off = 16 - ((uintptr_t)h & 0x0f);
656
0
              h = (void*)((char*)h + off);
657
0
            }
658
0
#endif /*NEED_16BYTE_ALIGNED_CONTEXT*/
659
660
0
    h->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
661
0
          h->actual_handle_size = size - off;
662
0
          h->handle_offset = off;
663
0
    h->spec = spec;
664
0
          h->algo = algo;
665
0
    h->mode = mode;
666
0
    h->flags = flags;
667
668
          /* Setup mode routines. */
669
0
          _gcry_cipher_setup_mode_ops(h, mode);
670
671
          /* Setup defaults depending on the mode.  */
672
0
          switch (mode)
673
0
            {
674
0
            case GCRY_CIPHER_MODE_OCB:
675
0
              h->u_mode.ocb.taglen = 16; /* Bytes.  */
676
0
              break;
677
678
0
      case GCRY_CIPHER_MODE_XTS:
679
0
        tc = h->context.c + spec->contextsize * 2;
680
0
        tc += (16 - (uintptr_t)tc % 16) % 16;
681
0
        h->u_mode.xts.tweak_context = tc;
682
0
        break;
683
684
0
      case GCRY_CIPHER_MODE_SIV:
685
0
        tc = h->context.c + spec->contextsize * 2;
686
0
        tc += (16 - (uintptr_t)tc % 16) % 16;
687
0
        h->u_mode.siv.ctr_context = tc;
688
0
        break;
689
690
0
            default:
691
0
              break;
692
0
            }
693
0
        }
694
0
    }
695
696
  /* Done.  */
697
698
0
  *handle = err ? NULL : h;
699
700
0
  return err;
701
0
}
702
703
704
/* Release all resources associated with the cipher handle H. H may be
705
   NULL in which case this is a no-operation. */
706
void
707
_gcry_cipher_close (gcry_cipher_hd_t h)
708
0
{
709
0
  size_t off;
710
711
0
  if (!h)
712
0
    return;
713
714
0
  if ((h->magic != CTX_MAGIC_SECURE)
715
0
      && (h->magic != CTX_MAGIC_NORMAL))
716
0
    _gcry_fatal_error(GPG_ERR_INTERNAL,
717
0
          "gcry_cipher_close: already closed/invalid handle");
718
0
  else
719
0
    h->magic = 0;
720
721
  /* We always want to wipe out the memory even when the context has
722
     been allocated in secure memory.  The user might have disabled
723
     secure memory or is using his own implementation which does not
724
     do the wiping.  To accomplish this we need to keep track of the
725
     actual size of this structure because we have no way to known
726
     how large the allocated area was when using a standard malloc. */
727
0
  off = h->handle_offset;
728
0
  wipememory (h, h->actual_handle_size);
729
730
0
  xfree ((char*)h - off);
731
0
}
732
733
734
/* Set the key to be used for the encryption context C to KEY with
735
   length KEYLEN.  The length should match the required length. */
736
static gcry_err_code_t
737
cipher_setkey (gcry_cipher_hd_t c, byte *key, size_t keylen)
738
0
{
739
0
  gcry_err_code_t rc;
740
741
0
  if (c->mode == GCRY_CIPHER_MODE_XTS)
742
0
    {
743
      /* XTS uses two keys. */
744
0
      if (keylen % 2)
745
0
  return GPG_ERR_INV_KEYLEN;
746
0
      keylen /= 2;
747
748
0
      if (fips_mode ())
749
0
  {
750
    /* Reject key if subkeys Key_1 and Key_2 are equal.
751
       See "Implementation Guidance for FIPS 140-2, A.9 XTS-AES
752
       Key Generation Requirements" for details.  */
753
0
    if (buf_eq_const (key, key + keylen, keylen))
754
0
      return GPG_ERR_WEAK_KEY;
755
0
  }
756
0
    }
757
0
  else if (c->mode == GCRY_CIPHER_MODE_SIV)
758
0
    {
759
      /* SIV uses two keys. */
760
0
      if (keylen % 2)
761
0
  return GPG_ERR_INV_KEYLEN;
762
0
      keylen /= 2;
763
0
    }
764
765
0
  rc = c->spec->setkey (&c->context.c, key, keylen, &c->bulk);
766
0
  if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
767
0
    {
768
      /* Duplicate initial context.  */
769
0
      memcpy ((void *) ((char *) &c->context.c + c->spec->contextsize),
770
0
              (void *) &c->context.c,
771
0
              c->spec->contextsize);
772
0
      c->marks.key = 1;
773
774
0
      switch (c->mode)
775
0
        {
776
0
        case GCRY_CIPHER_MODE_CMAC:
777
0
          rc = _gcry_cipher_cmac_set_subkeys (c);
778
0
          break;
779
780
0
        case GCRY_CIPHER_MODE_EAX:
781
0
          rc = _gcry_cipher_eax_setkey (c);
782
0
          break;
783
784
0
        case GCRY_CIPHER_MODE_GCM:
785
0
          _gcry_cipher_gcm_setkey (c);
786
0
          break;
787
788
0
        case GCRY_CIPHER_MODE_GCM_SIV:
789
0
          rc = _gcry_cipher_gcm_siv_setkey (c, keylen);
790
0
          if (rc)
791
0
      c->marks.key = 0;
792
0
          break;
793
794
0
        case GCRY_CIPHER_MODE_OCB:
795
0
          _gcry_cipher_ocb_setkey (c);
796
0
          break;
797
798
0
        case GCRY_CIPHER_MODE_POLY1305:
799
0
          _gcry_cipher_poly1305_setkey (c);
800
0
          break;
801
802
0
  case GCRY_CIPHER_MODE_XTS:
803
    /* Setup tweak cipher with second part of XTS key. */
804
0
    rc = c->spec->setkey (c->u_mode.xts.tweak_context, key + keylen,
805
0
        keylen, &c->bulk);
806
0
    if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
807
0
      {
808
        /* Duplicate initial tweak context.  */
809
0
        memcpy (c->u_mode.xts.tweak_context + c->spec->contextsize,
810
0
          c->u_mode.xts.tweak_context, c->spec->contextsize);
811
0
      }
812
0
    else
813
0
      c->marks.key = 0;
814
0
    break;
815
816
0
        case GCRY_CIPHER_MODE_SIV:
817
    /* Setup CTR cipher with second part of SIV key. */
818
0
          rc = _gcry_cipher_siv_setkey (c, key + keylen, keylen);
819
0
    if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
820
0
      {
821
        /* Duplicate initial CTR context.  */
822
0
        memcpy (c->u_mode.siv.ctr_context + c->spec->contextsize,
823
0
          c->u_mode.siv.ctr_context, c->spec->contextsize);
824
0
      }
825
0
    else
826
0
      c->marks.key = 0;
827
0
          break;
828
829
0
        default:
830
0
          break;
831
0
        }
832
0
    }
833
0
  else
834
0
    c->marks.key = 0;
835
836
0
  return rc;
837
0
}
838
839
840
/* Set the IV to be used for the encryption context C to IV with
841
   length IVLEN.  The length should match the required length. */
842
static gcry_err_code_t
843
cipher_setiv (gcry_cipher_hd_t c, const byte *iv, size_t ivlen)
844
0
{
845
  /* If the cipher has its own IV handler, we use only this one.  This
846
     is currently used for stream ciphers requiring a nonce.  */
847
0
  if (c->spec->setiv)
848
0
    {
849
0
      c->spec->setiv (&c->context.c, iv, ivlen);
850
0
      return 0;
851
0
    }
852
853
0
  memset (c->u_iv.iv, 0, c->spec->blocksize);
854
0
  if (iv)
855
0
    {
856
0
      if (ivlen != c->spec->blocksize)
857
0
        {
858
0
          log_info ("WARNING: cipher_setiv: ivlen=%u blklen=%u\n",
859
0
                    (unsigned int)ivlen, (unsigned int)c->spec->blocksize);
860
0
          fips_signal_error ("IV length does not match blocklength");
861
0
        }
862
0
      if (ivlen > c->spec->blocksize)
863
0
        ivlen = c->spec->blocksize;
864
0
      memcpy (c->u_iv.iv, iv, ivlen);
865
0
      c->marks.iv = 1;
866
0
    }
867
0
  else
868
0
      c->marks.iv = 0;
869
0
  c->unused = 0;
870
871
0
  return 0;
872
0
}
873
874
875
/* Reset the cipher context to the initial context.  This is basically
876
   the same as an release followed by a new. */
877
static void
878
cipher_reset (gcry_cipher_hd_t c)
879
0
{
880
0
  unsigned int marks_key, marks_allow_weak_key;
881
882
0
  marks_key = c->marks.key;
883
0
  marks_allow_weak_key = c->marks.allow_weak_key;
884
885
0
  memcpy (&c->context.c,
886
0
    (char *) &c->context.c + c->spec->contextsize,
887
0
    c->spec->contextsize);
888
0
  memset (&c->marks, 0, sizeof c->marks);
889
0
  memset (c->u_iv.iv, 0, c->spec->blocksize);
890
0
  memset (c->lastiv, 0, c->spec->blocksize);
891
0
  memset (c->u_ctr.ctr, 0, c->spec->blocksize);
892
0
  c->unused = 0;
893
894
0
  c->marks.key = marks_key;
895
0
  c->marks.allow_weak_key = marks_allow_weak_key;
896
897
0
  switch (c->mode)
898
0
    {
899
0
    case GCRY_CIPHER_MODE_CMAC:
900
0
      _gcry_cmac_reset(&c->u_mode.cmac);
901
0
      break;
902
903
0
    case GCRY_CIPHER_MODE_EAX:
904
0
      _gcry_cmac_reset(&c->u_mode.eax.cmac_header);
905
0
      _gcry_cmac_reset(&c->u_mode.eax.cmac_ciphertext);
906
0
      break;
907
908
0
    case GCRY_CIPHER_MODE_GCM:
909
0
    case GCRY_CIPHER_MODE_GCM_SIV:
910
      /* Only clear head of u_mode, keep ghash_key and gcm_table. */
911
0
      {
912
0
        byte *u_mode_pos = (void *)&c->u_mode;
913
0
        byte *ghash_key_pos = c->u_mode.gcm.u_ghash_key.key;
914
0
        size_t u_mode_head_length = ghash_key_pos - u_mode_pos;
915
916
0
        memset (&c->u_mode, 0, u_mode_head_length);
917
0
      }
918
0
      break;
919
920
0
    case GCRY_CIPHER_MODE_POLY1305:
921
0
      memset (&c->u_mode.poly1305, 0, sizeof c->u_mode.poly1305);
922
0
      break;
923
924
0
    case GCRY_CIPHER_MODE_CCM:
925
0
      memset (&c->u_mode.ccm, 0, sizeof c->u_mode.ccm);
926
0
      break;
927
928
0
    case GCRY_CIPHER_MODE_OCB:
929
0
      {
930
0
  const size_t table_maxblks = 1 << OCB_L_TABLE_SIZE;
931
0
  byte *u_mode_head_pos = (void *)&c->u_mode.ocb;
932
0
  byte *u_mode_tail_pos = (void *)&c->u_mode.ocb.tag;
933
0
  size_t u_mode_head_length = u_mode_tail_pos - u_mode_head_pos;
934
0
  size_t u_mode_tail_length = sizeof(c->u_mode.ocb) - u_mode_head_length;
935
936
0
  if (c->u_mode.ocb.aad_nblocks < table_maxblks)
937
0
    {
938
      /* Precalculated L-values are still ok after reset, no need
939
       * to clear. */
940
0
      memset (u_mode_tail_pos, 0, u_mode_tail_length);
941
0
    }
942
0
  else
943
0
    {
944
      /* Reinitialize L table. */
945
0
      memset (&c->u_mode.ocb, 0, sizeof(c->u_mode.ocb));
946
0
      _gcry_cipher_ocb_setkey (c);
947
0
    }
948
949
  /* Setup default taglen.  */
950
0
  c->u_mode.ocb.taglen = 16;
951
0
      }
952
0
      break;
953
954
0
    case GCRY_CIPHER_MODE_XTS:
955
0
      memcpy (c->u_mode.xts.tweak_context,
956
0
        c->u_mode.xts.tweak_context + c->spec->contextsize,
957
0
        c->spec->contextsize);
958
0
      break;
959
960
0
    case GCRY_CIPHER_MODE_SIV:
961
      /* Only clear head of u_mode, keep s2v_cmac and ctr_context. */
962
0
      {
963
0
        byte *u_mode_pos = (void *)&c->u_mode;
964
0
        byte *tail_pos = (void *)&c->u_mode.siv.s2v_cmac;
965
0
        size_t u_mode_head_length = tail_pos - u_mode_pos;
966
967
0
        memset (&c->u_mode, 0, u_mode_head_length);
968
969
0
  memcpy (c->u_mode.siv.ctr_context,
970
0
    c->u_mode.siv.ctr_context + c->spec->contextsize,
971
0
    c->spec->contextsize);
972
973
0
  memcpy (c->u_mode.siv.s2v_d, c->u_mode.siv.s2v_zero_block,
974
0
    GCRY_SIV_BLOCK_LEN);
975
0
      }
976
0
      break;
977
978
0
    default:
979
0
      break; /* u_mode unused by other modes. */
980
0
    }
981
0
}
982
983
984

985
static gcry_err_code_t
986
do_ecb_crypt (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen,
987
        const unsigned char *inbuf, size_t inbuflen, int encrypt)
988
0
{
989
0
  unsigned int blocksize = c->spec->blocksize;
990
0
  size_t n, nblocks;
991
992
0
  if (outbuflen < inbuflen)
993
0
    return GPG_ERR_BUFFER_TOO_SHORT;
994
0
  if ((inbuflen % blocksize))
995
0
    return GPG_ERR_INV_LENGTH;
996
997
0
  nblocks = inbuflen / blocksize;
998
999
0
  if (nblocks == 0)
1000
0
    return 0;
1001
1002
0
  if (c->bulk.ecb_crypt)
1003
0
    {
1004
0
      c->bulk.ecb_crypt (&c->context.c, outbuf, inbuf, nblocks, encrypt);
1005
0
    }
1006
0
  else
1007
0
    {
1008
0
      gcry_cipher_encrypt_t crypt_fn =
1009
0
          encrypt ? c->spec->encrypt : c->spec->decrypt;
1010
0
      unsigned int burn = 0;
1011
0
      unsigned int nburn;
1012
1013
0
      for (n = 0; n < nblocks; n++)
1014
0
  {
1015
0
    nburn = crypt_fn (&c->context.c, outbuf, inbuf);
1016
0
    burn = nburn > burn ? nburn : burn;
1017
0
    inbuf  += blocksize;
1018
0
    outbuf += blocksize;
1019
0
  }
1020
1021
0
      if (burn > 0)
1022
0
  _gcry_burn_stack (burn + 4 * sizeof(void *));
1023
0
    }
1024
1025
0
  return 0;
1026
0
}
1027
1028
static gcry_err_code_t
1029
do_ecb_encrypt (gcry_cipher_hd_t c,
1030
                unsigned char *outbuf, size_t outbuflen,
1031
                const unsigned char *inbuf, size_t inbuflen)
1032
0
{
1033
0
  return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 1);
1034
0
}
1035
1036
static gcry_err_code_t
1037
do_ecb_decrypt (gcry_cipher_hd_t c,
1038
                unsigned char *outbuf, size_t outbuflen,
1039
                const unsigned char *inbuf, size_t inbuflen)
1040
0
{
1041
0
  return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 0);
1042
0
}
1043
1044
1045
static gcry_err_code_t
1046
do_stream_encrypt (gcry_cipher_hd_t c,
1047
                unsigned char *outbuf, size_t outbuflen,
1048
                const unsigned char *inbuf, size_t inbuflen)
1049
0
{
1050
0
  (void)outbuflen;
1051
0
  c->spec->stencrypt (&c->context.c, outbuf, (void *)inbuf, inbuflen);
1052
0
  return 0;
1053
0
}
1054
1055
static gcry_err_code_t
1056
do_stream_decrypt (gcry_cipher_hd_t c,
1057
                unsigned char *outbuf, size_t outbuflen,
1058
                const unsigned char *inbuf, size_t inbuflen)
1059
0
{
1060
0
  (void)outbuflen;
1061
0
  c->spec->stdecrypt (&c->context.c, outbuf, (void *)inbuf, inbuflen);
1062
0
  return 0;
1063
0
}
1064
1065
1066
static gcry_err_code_t
1067
do_encrypt_none_unknown (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
1068
                         const byte *inbuf, size_t inbuflen)
1069
0
{
1070
0
  gcry_err_code_t rc;
1071
1072
0
  (void)outbuflen;
1073
1074
0
  switch (c->mode)
1075
0
    {
1076
0
    case GCRY_CIPHER_MODE_CMAC:
1077
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1078
0
      break;
1079
1080
0
    case GCRY_CIPHER_MODE_NONE:
1081
0
      if (fips_mode () || !_gcry_get_debug_flag (0))
1082
0
        {
1083
0
          fips_signal_error ("cipher mode NONE used");
1084
0
          rc = GPG_ERR_INV_CIPHER_MODE;
1085
0
        }
1086
0
      else
1087
0
        {
1088
0
          if (inbuf != outbuf)
1089
0
            memmove (outbuf, inbuf, inbuflen);
1090
0
          rc = 0;
1091
0
        }
1092
0
      break;
1093
1094
0
    default:
1095
0
      log_fatal ("cipher_encrypt: invalid mode %d\n", c->mode );
1096
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1097
0
      break;
1098
0
    }
1099
1100
0
  return rc;
1101
0
}
1102
1103
static gcry_err_code_t
1104
do_decrypt_none_unknown (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
1105
                         const byte *inbuf, size_t inbuflen)
1106
0
{
1107
0
  gcry_err_code_t rc;
1108
1109
0
  (void)outbuflen;
1110
1111
0
  switch (c->mode)
1112
0
    {
1113
0
    case GCRY_CIPHER_MODE_CMAC:
1114
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1115
0
      break;
1116
1117
0
    case GCRY_CIPHER_MODE_NONE:
1118
0
      if (fips_mode () || !_gcry_get_debug_flag (0))
1119
0
        {
1120
0
          fips_signal_error ("cipher mode NONE used");
1121
0
          rc = GPG_ERR_INV_CIPHER_MODE;
1122
0
        }
1123
0
      else
1124
0
        {
1125
0
          if (inbuf != outbuf)
1126
0
            memmove (outbuf, inbuf, inbuflen);
1127
0
          rc = 0;
1128
0
        }
1129
0
      break;
1130
1131
0
    default:
1132
0
      log_fatal ("cipher_decrypt: invalid mode %d\n", c->mode );
1133
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1134
0
      break;
1135
0
    }
1136
1137
0
  return rc;
1138
0
}
1139
1140
1141
/****************
1142
 * Encrypt IN and write it to OUT.  If IN is NULL, in-place encryption has
1143
 * been requested.
1144
 */
1145
gcry_err_code_t
1146
_gcry_cipher_encrypt (gcry_cipher_hd_t h, void *out, size_t outsize,
1147
                      const void *in, size_t inlen)
1148
0
{
1149
0
  gcry_err_code_t rc;
1150
1151
0
  if (!in)  /* Caller requested in-place encryption.  */
1152
0
    {
1153
0
      in = out;
1154
0
      inlen = outsize;
1155
0
    }
1156
1157
0
  if (h->mode != GCRY_CIPHER_MODE_NONE && !h->marks.key)
1158
0
    {
1159
0
      log_error ("cipher_encrypt: key not set\n");
1160
0
      return GPG_ERR_MISSING_KEY;
1161
0
    }
1162
1163
0
  rc = h->mode_ops.encrypt (h, out, outsize, in, inlen);
1164
1165
  /* Failsafe: Make sure that the plaintext will never make it into
1166
     OUT if the encryption returned an error.  */
1167
0
  if (rc && out)
1168
0
    memset (out, 0x42, outsize);
1169
1170
0
  return rc;
1171
0
}
1172
1173
1174
/****************
1175
 * Decrypt IN and write it to OUT.  If IN is NULL, in-place encryption has
1176
 * been requested.
1177
 */
1178
gcry_err_code_t
1179
_gcry_cipher_decrypt (gcry_cipher_hd_t h, void *out, size_t outsize,
1180
                      const void *in, size_t inlen)
1181
0
{
1182
0
  if (!in) /* Caller requested in-place encryption. */
1183
0
    {
1184
0
      in = out;
1185
0
      inlen = outsize;
1186
0
    }
1187
1188
0
  if (h->mode != GCRY_CIPHER_MODE_NONE && !h->marks.key)
1189
0
    {
1190
0
      log_error ("cipher_decrypt: key not set\n");
1191
0
      return GPG_ERR_MISSING_KEY;
1192
0
    }
1193
1194
0
  return h->mode_ops.decrypt (h, out, outsize, in, inlen);
1195
0
}
1196
1197
1198
/****************
1199
 * Used for PGP's somewhat strange CFB mode. Only works if
1200
 * the corresponding flag is set.
1201
 */
1202
static void
1203
cipher_sync (gcry_cipher_hd_t c)
1204
0
{
1205
0
  if ((c->flags & GCRY_CIPHER_ENABLE_SYNC) && c->unused)
1206
0
    {
1207
0
      memmove (c->u_iv.iv + c->unused,
1208
0
               c->u_iv.iv, c->spec->blocksize - c->unused);
1209
0
      memcpy (c->u_iv.iv,
1210
0
              c->lastiv + c->spec->blocksize - c->unused, c->unused);
1211
0
      c->unused = 0;
1212
0
    }
1213
0
}
1214
1215
1216
gcry_err_code_t
1217
_gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen)
1218
0
{
1219
0
  return cipher_setkey (hd, (void*)key, keylen);
1220
0
}
1221
1222
1223
gcry_err_code_t
1224
_gcry_cipher_setiv (gcry_cipher_hd_t c, const void *iv, size_t ivlen)
1225
0
{
1226
0
  if (c->mode == GCRY_CIPHER_MODE_GCM)
1227
0
    {
1228
0
      c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 0;
1229
1230
0
      if (fips_mode ())
1231
0
        {
1232
          /* Direct invocation of GCM setiv in FIPS mode disables encryption. */
1233
0
          c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 1;
1234
0
        }
1235
0
    }
1236
1237
0
  return c->mode_ops.setiv (c, iv, ivlen);
1238
0
}
1239
1240
1241
/* Set counter for CTR mode.  (CTR,CTRLEN) must denote a buffer of
1242
   block size length, or (NULL,0) to set the CTR to the all-zero
1243
   block. */
1244
gpg_err_code_t
1245
_gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen)
1246
0
{
1247
0
  if (ctr && ctrlen == hd->spec->blocksize)
1248
0
    {
1249
0
      memcpy (hd->u_ctr.ctr, ctr, hd->spec->blocksize);
1250
0
      hd->unused = 0;
1251
0
    }
1252
0
  else if (!ctr || !ctrlen)
1253
0
    {
1254
0
      memset (hd->u_ctr.ctr, 0, hd->spec->blocksize);
1255
0
      hd->unused = 0;
1256
0
    }
1257
0
  else
1258
0
    return GPG_ERR_INV_ARG;
1259
1260
0
  return 0;
1261
0
}
1262
1263
gpg_err_code_t
1264
_gcry_cipher_getctr (gcry_cipher_hd_t hd, void *ctr, size_t ctrlen)
1265
0
{
1266
0
  if (ctr && ctrlen == hd->spec->blocksize)
1267
0
    memcpy (ctr, hd->u_ctr.ctr, hd->spec->blocksize);
1268
0
  else
1269
0
    return GPG_ERR_INV_ARG;
1270
1271
0
  return 0;
1272
0
}
1273
1274
1275
gcry_err_code_t
1276
_gcry_cipher_setup_geniv (gcry_cipher_hd_t hd, int method,
1277
                          const void *fixed_iv, size_t fixed_iv_len,
1278
                          const void *dyn_iv, size_t dyn_iv_len)
1279
0
{
1280
0
  gcry_err_code_t rc = 0;
1281
1282
0
  if (method != GCRY_CIPHER_GENIV_METHOD_CONCAT)
1283
0
    return GPG_ERR_INV_ARG;
1284
1285
0
  if (fixed_iv_len + dyn_iv_len > MAX_BLOCKSIZE)
1286
0
    return GPG_ERR_INV_ARG;
1287
1288
0
  hd->aead.geniv_method = GCRY_CIPHER_GENIV_METHOD_CONCAT;
1289
0
  hd->aead.fixed_iv_len = fixed_iv_len;
1290
0
  hd->aead.dynamic_iv_len = dyn_iv_len;
1291
0
  memset (hd->aead.fixed, 0, MAX_BLOCKSIZE);
1292
0
  memset (hd->aead.dynamic, 0, MAX_BLOCKSIZE);
1293
0
  memcpy (hd->aead.fixed, fixed_iv, fixed_iv_len);
1294
0
  memcpy (hd->aead.dynamic, dyn_iv, dyn_iv_len);
1295
1296
0
  return rc;
1297
0
}
1298
1299
1300
gcry_err_code_t
1301
_gcry_cipher_geniv (gcry_cipher_hd_t hd, void *iv, size_t iv_len)
1302
0
{
1303
0
  gcry_err_code_t rc = 0;
1304
0
  int i;
1305
1306
0
  if (hd->aead.geniv_method != GCRY_CIPHER_GENIV_METHOD_CONCAT)
1307
0
    return GPG_ERR_INV_ARG;
1308
1309
0
  if (iv_len != hd->aead.fixed_iv_len + hd->aead.dynamic_iv_len)
1310
0
    return GPG_ERR_INV_ARG;
1311
1312
0
  memcpy (iv, hd->aead.fixed, hd->aead.fixed_iv_len);
1313
0
  memcpy ((byte *)iv+hd->aead.fixed_iv_len,
1314
0
          hd->aead.dynamic, hd->aead.dynamic_iv_len);
1315
0
  rc = hd->mode_ops.setiv (hd, iv, iv_len);
1316
1317
0
  for (i = hd->aead.dynamic_iv_len; i > 0; i--)
1318
0
    if (++hd->aead.dynamic[i - 1] != 0)
1319
0
      break;
1320
1321
0
  return rc;
1322
0
}
1323
1324
1325
gcry_err_code_t
1326
_gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf,
1327
                           size_t abuflen)
1328
0
{
1329
0
  gcry_err_code_t rc;
1330
1331
0
  if (hd->mode_ops.authenticate)
1332
0
    {
1333
0
      rc = hd->mode_ops.authenticate (hd, abuf, abuflen);
1334
0
    }
1335
0
  else
1336
0
    {
1337
0
      log_error ("gcry_cipher_authenticate: invalid mode %d\n", hd->mode);
1338
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1339
0
    }
1340
1341
0
  return rc;
1342
0
}
1343
1344
1345
gcry_err_code_t
1346
_gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, size_t taglen)
1347
0
{
1348
0
  gcry_err_code_t rc;
1349
1350
0
  if (hd->mode_ops.get_tag)
1351
0
    {
1352
0
      rc = hd->mode_ops.get_tag (hd, outtag, taglen);
1353
0
    }
1354
0
  else
1355
0
    {
1356
0
      log_error ("gcry_cipher_gettag: invalid mode %d\n", hd->mode);
1357
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1358
0
    }
1359
1360
0
  return rc;
1361
0
}
1362
1363
1364
gcry_err_code_t
1365
_gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, size_t taglen)
1366
0
{
1367
0
  gcry_err_code_t rc;
1368
1369
0
  if (hd->mode_ops.check_tag)
1370
0
    {
1371
0
      rc = hd->mode_ops.check_tag (hd, intag, taglen);
1372
0
    }
1373
0
  else
1374
0
    {
1375
0
      log_error ("gcry_cipher_checktag: invalid mode %d\n", hd->mode);
1376
0
      rc = GPG_ERR_INV_CIPHER_MODE;
1377
0
    }
1378
1379
0
  return rc;
1380
0
}
1381
1382
1383
1384
static void
1385
_gcry_cipher_setup_mode_ops(gcry_cipher_hd_t c, int mode)
1386
0
{
1387
  /* Setup encryption and decryption routines. */
1388
0
  switch (mode)
1389
0
    {
1390
0
    case GCRY_CIPHER_MODE_STREAM:
1391
0
      c->mode_ops.encrypt = do_stream_encrypt;
1392
0
      c->mode_ops.decrypt = do_stream_decrypt;
1393
0
      break;
1394
1395
0
    case GCRY_CIPHER_MODE_ECB:
1396
0
      c->mode_ops.encrypt = do_ecb_encrypt;
1397
0
      c->mode_ops.decrypt = do_ecb_decrypt;
1398
0
      break;
1399
1400
0
    case GCRY_CIPHER_MODE_CBC:
1401
0
      if (!(c->flags & GCRY_CIPHER_CBC_CTS))
1402
0
        {
1403
0
          c->mode_ops.encrypt = _gcry_cipher_cbc_encrypt;
1404
0
          c->mode_ops.decrypt = _gcry_cipher_cbc_decrypt;
1405
0
        }
1406
0
      else
1407
0
        {
1408
0
          c->mode_ops.encrypt = _gcry_cipher_cbc_cts_encrypt;
1409
0
          c->mode_ops.decrypt = _gcry_cipher_cbc_cts_decrypt;
1410
0
        }
1411
0
      break;
1412
1413
0
    case GCRY_CIPHER_MODE_CFB:
1414
0
      c->mode_ops.encrypt = _gcry_cipher_cfb_encrypt;
1415
0
      c->mode_ops.decrypt = _gcry_cipher_cfb_decrypt;
1416
0
      break;
1417
1418
0
    case GCRY_CIPHER_MODE_CFB8:
1419
0
      c->mode_ops.encrypt = _gcry_cipher_cfb8_encrypt;
1420
0
      c->mode_ops.decrypt = _gcry_cipher_cfb8_decrypt;
1421
0
      break;
1422
1423
0
    case GCRY_CIPHER_MODE_OFB:
1424
0
      c->mode_ops.encrypt = _gcry_cipher_ofb_encrypt;
1425
0
      c->mode_ops.decrypt = _gcry_cipher_ofb_encrypt;
1426
0
      break;
1427
1428
0
    case GCRY_CIPHER_MODE_CTR:
1429
0
      c->mode_ops.encrypt = _gcry_cipher_ctr_encrypt;
1430
0
      c->mode_ops.decrypt = _gcry_cipher_ctr_encrypt;
1431
0
      break;
1432
1433
0
    case GCRY_CIPHER_MODE_AESWRAP:
1434
0
      c->mode_ops.decrypt = _gcry_cipher_keywrap_decrypt_auto;
1435
0
      if (!(c->flags & GCRY_CIPHER_EXTENDED))
1436
0
        c->mode_ops.encrypt = _gcry_cipher_keywrap_encrypt;
1437
0
      else
1438
0
        c->mode_ops.encrypt = _gcry_cipher_keywrap_encrypt_padding;
1439
0
      break;
1440
1441
0
    case GCRY_CIPHER_MODE_CCM:
1442
0
      c->mode_ops.encrypt = _gcry_cipher_ccm_encrypt;
1443
0
      c->mode_ops.decrypt = _gcry_cipher_ccm_decrypt;
1444
0
      break;
1445
1446
0
    case GCRY_CIPHER_MODE_EAX:
1447
0
      c->mode_ops.encrypt = _gcry_cipher_eax_encrypt;
1448
0
      c->mode_ops.decrypt = _gcry_cipher_eax_decrypt;
1449
0
      break;
1450
1451
0
    case GCRY_CIPHER_MODE_GCM:
1452
0
      c->mode_ops.encrypt = _gcry_cipher_gcm_encrypt;
1453
0
      c->mode_ops.decrypt = _gcry_cipher_gcm_decrypt;
1454
0
      break;
1455
1456
0
    case GCRY_CIPHER_MODE_POLY1305:
1457
0
      c->mode_ops.encrypt = _gcry_cipher_poly1305_encrypt;
1458
0
      c->mode_ops.decrypt = _gcry_cipher_poly1305_decrypt;
1459
0
      break;
1460
1461
0
    case GCRY_CIPHER_MODE_OCB:
1462
0
      c->mode_ops.encrypt = _gcry_cipher_ocb_encrypt;
1463
0
      c->mode_ops.decrypt = _gcry_cipher_ocb_decrypt;
1464
0
      break;
1465
1466
0
    case GCRY_CIPHER_MODE_XTS:
1467
0
      c->mode_ops.encrypt = _gcry_cipher_xts_encrypt;
1468
0
      c->mode_ops.decrypt = _gcry_cipher_xts_decrypt;
1469
0
      break;
1470
1471
0
    case GCRY_CIPHER_MODE_SIV:
1472
0
      c->mode_ops.encrypt = _gcry_cipher_siv_encrypt;
1473
0
      c->mode_ops.decrypt = _gcry_cipher_siv_decrypt;
1474
0
      break;
1475
1476
0
    case GCRY_CIPHER_MODE_GCM_SIV:
1477
0
      c->mode_ops.encrypt = _gcry_cipher_gcm_siv_encrypt;
1478
0
      c->mode_ops.decrypt = _gcry_cipher_gcm_siv_decrypt;
1479
0
      break;
1480
1481
0
    default:
1482
0
      c->mode_ops.encrypt = do_encrypt_none_unknown;
1483
0
      c->mode_ops.decrypt = do_decrypt_none_unknown;
1484
0
      break;
1485
0
    }
1486
1487
  /* Setup IV setting routine. */
1488
0
  switch (mode)
1489
0
    {
1490
0
    case GCRY_CIPHER_MODE_CCM:
1491
0
      c->mode_ops.setiv = _gcry_cipher_ccm_set_nonce;
1492
0
      break;
1493
1494
0
    case GCRY_CIPHER_MODE_EAX:
1495
0
      c->mode_ops.setiv = _gcry_cipher_eax_set_nonce;
1496
0
      break;
1497
1498
0
    case GCRY_CIPHER_MODE_GCM:
1499
0
      c->mode_ops.setiv =  _gcry_cipher_gcm_setiv;
1500
0
      break;
1501
1502
0
    case GCRY_CIPHER_MODE_POLY1305:
1503
0
      c->mode_ops.setiv = _gcry_cipher_poly1305_setiv;
1504
0
      break;
1505
1506
0
    case GCRY_CIPHER_MODE_OCB:
1507
0
      c->mode_ops.setiv = _gcry_cipher_ocb_set_nonce;
1508
0
      break;
1509
1510
0
    case GCRY_CIPHER_MODE_SIV:
1511
0
      c->mode_ops.setiv = _gcry_cipher_siv_set_nonce;
1512
0
      break;
1513
1514
0
    case GCRY_CIPHER_MODE_GCM_SIV:
1515
0
      c->mode_ops.setiv = _gcry_cipher_gcm_siv_set_nonce;
1516
0
      break;
1517
1518
0
    default:
1519
0
      c->mode_ops.setiv = cipher_setiv;
1520
0
      break;
1521
0
    }
1522
1523
1524
  /* Setup authentication routines for AEAD modes. */
1525
0
  switch (mode)
1526
0
    {
1527
0
    case GCRY_CIPHER_MODE_CCM:
1528
0
      c->mode_ops.authenticate = _gcry_cipher_ccm_authenticate;
1529
0
      c->mode_ops.get_tag      = _gcry_cipher_ccm_get_tag;
1530
0
      c->mode_ops.check_tag    = _gcry_cipher_ccm_check_tag;
1531
0
      break;
1532
1533
0
    case GCRY_CIPHER_MODE_CMAC:
1534
0
      c->mode_ops.authenticate = _gcry_cipher_cmac_authenticate;
1535
0
      c->mode_ops.get_tag      = _gcry_cipher_cmac_get_tag;
1536
0
      c->mode_ops.check_tag    = _gcry_cipher_cmac_check_tag;
1537
0
      break;
1538
1539
0
    case GCRY_CIPHER_MODE_EAX:
1540
0
      c->mode_ops.authenticate = _gcry_cipher_eax_authenticate;
1541
0
      c->mode_ops.get_tag      = _gcry_cipher_eax_get_tag;
1542
0
      c->mode_ops.check_tag    = _gcry_cipher_eax_check_tag;
1543
0
      break;
1544
1545
0
    case GCRY_CIPHER_MODE_GCM:
1546
0
      c->mode_ops.authenticate = _gcry_cipher_gcm_authenticate;
1547
0
      c->mode_ops.get_tag      = _gcry_cipher_gcm_get_tag;
1548
0
      c->mode_ops.check_tag    = _gcry_cipher_gcm_check_tag;
1549
0
      break;
1550
1551
0
    case GCRY_CIPHER_MODE_POLY1305:
1552
0
      c->mode_ops.authenticate = _gcry_cipher_poly1305_authenticate;
1553
0
      c->mode_ops.get_tag      = _gcry_cipher_poly1305_get_tag;
1554
0
      c->mode_ops.check_tag    = _gcry_cipher_poly1305_check_tag;
1555
0
      break;
1556
1557
0
    case GCRY_CIPHER_MODE_OCB:
1558
0
      c->mode_ops.authenticate = _gcry_cipher_ocb_authenticate;
1559
0
      c->mode_ops.get_tag      = _gcry_cipher_ocb_get_tag;
1560
0
      c->mode_ops.check_tag    = _gcry_cipher_ocb_check_tag;
1561
0
      break;
1562
1563
0
    case GCRY_CIPHER_MODE_SIV:
1564
0
      c->mode_ops.authenticate = _gcry_cipher_siv_authenticate;
1565
0
      c->mode_ops.get_tag      = _gcry_cipher_siv_get_tag;
1566
0
      c->mode_ops.check_tag    = _gcry_cipher_siv_check_tag;
1567
0
      break;
1568
1569
0
    case GCRY_CIPHER_MODE_GCM_SIV:
1570
0
      c->mode_ops.authenticate = _gcry_cipher_gcm_siv_authenticate;
1571
0
      c->mode_ops.get_tag      = _gcry_cipher_gcm_siv_get_tag;
1572
0
      c->mode_ops.check_tag    = _gcry_cipher_gcm_siv_check_tag;
1573
0
      break;
1574
1575
0
    default:
1576
0
      c->mode_ops.authenticate = NULL;
1577
0
      c->mode_ops.get_tag      = NULL;
1578
0
      c->mode_ops.check_tag    = NULL;
1579
0
      break;
1580
0
    }
1581
0
}
1582
1583
1584
gcry_err_code_t
1585
_gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
1586
0
{
1587
0
  gcry_err_code_t rc = 0;
1588
1589
0
  switch (cmd)
1590
0
    {
1591
0
    case GCRYCTL_RESET:
1592
0
      cipher_reset (h);
1593
0
      break;
1594
1595
0
    case GCRYCTL_FINALIZE:
1596
0
      if (!h || buffer || buflen)
1597
0
  return GPG_ERR_INV_ARG;
1598
0
      h->marks.finalize = 1;
1599
0
      break;
1600
1601
0
    case GCRYCTL_CFB_SYNC:
1602
0
      cipher_sync( h );
1603
0
      break;
1604
1605
0
    case GCRYCTL_SET_CBC_CTS:
1606
0
      if (buflen)
1607
0
  if (h->flags & GCRY_CIPHER_CBC_MAC)
1608
0
    rc = GPG_ERR_INV_FLAG;
1609
0
  else
1610
0
    h->flags |= GCRY_CIPHER_CBC_CTS;
1611
0
      else
1612
0
  h->flags &= ~GCRY_CIPHER_CBC_CTS;
1613
0
      break;
1614
1615
0
    case GCRYCTL_SET_CBC_MAC:
1616
0
      if (buflen)
1617
0
  if (h->flags & GCRY_CIPHER_CBC_CTS)
1618
0
    rc = GPG_ERR_INV_FLAG;
1619
0
  else
1620
0
    h->flags |= GCRY_CIPHER_CBC_MAC;
1621
0
      else
1622
0
  h->flags &= ~GCRY_CIPHER_CBC_MAC;
1623
0
      break;
1624
1625
0
    case GCRYCTL_SET_CCM_LENGTHS:
1626
0
      {
1627
0
        u64 params[3];
1628
0
        size_t encryptedlen;
1629
0
        size_t aadlen;
1630
0
        size_t authtaglen;
1631
1632
0
        if (h->mode != GCRY_CIPHER_MODE_CCM)
1633
0
          return GPG_ERR_INV_CIPHER_MODE;
1634
1635
0
        if (!buffer || buflen != 3 * sizeof(u64))
1636
0
          return GPG_ERR_INV_ARG;
1637
1638
        /* This command is used to pass additional length parameters needed
1639
           by CCM mode to initialize CBC-MAC.  */
1640
0
        memcpy (params, buffer, sizeof(params));
1641
0
        encryptedlen = params[0];
1642
0
        aadlen = params[1];
1643
0
        authtaglen = params[2];
1644
1645
0
        rc = _gcry_cipher_ccm_set_lengths (h, encryptedlen, aadlen, authtaglen);
1646
0
      }
1647
0
      break;
1648
1649
0
    case GCRYCTL_SET_DECRYPTION_TAG:
1650
0
      {
1651
0
        if (!buffer)
1652
0
          return GPG_ERR_INV_ARG;
1653
1654
0
        if (h->mode == GCRY_CIPHER_MODE_SIV)
1655
0
          rc = _gcry_cipher_siv_set_decryption_tag (h, buffer, buflen);
1656
0
        else if (h->mode == GCRY_CIPHER_MODE_GCM_SIV)
1657
0
          rc = _gcry_cipher_gcm_siv_set_decryption_tag (h, buffer, buflen);
1658
0
        else
1659
0
          rc = GPG_ERR_INV_CIPHER_MODE;
1660
0
      }
1661
0
      break;
1662
1663
0
    case GCRYCTL_SET_TAGLEN:
1664
0
      if (!h || !buffer || buflen != sizeof(int) )
1665
0
  return GPG_ERR_INV_ARG;
1666
0
      switch (h->mode)
1667
0
        {
1668
0
        case GCRY_CIPHER_MODE_OCB:
1669
0
          switch (*(int*)buffer)
1670
0
            {
1671
0
            case 8: case 12: case 16:
1672
0
              h->u_mode.ocb.taglen = *(int*)buffer;
1673
0
              break;
1674
0
            default:
1675
0
              rc = GPG_ERR_INV_LENGTH; /* Invalid tag length. */
1676
0
              break;
1677
0
            }
1678
0
          break;
1679
1680
0
        default:
1681
0
          rc =GPG_ERR_INV_CIPHER_MODE;
1682
0
          break;
1683
0
        }
1684
0
      break;
1685
1686
0
    case GCRYCTL_DISABLE_ALGO:
1687
      /* This command expects NULL for H and BUFFER to point to an
1688
         integer with the algo number.  */
1689
0
      if( h || !buffer || buflen != sizeof(int) )
1690
0
  return GPG_ERR_CIPHER_ALGO;
1691
0
      disable_cipher_algo( *(int*)buffer );
1692
0
      break;
1693
1694
0
    case PRIV_CIPHERCTL_DISABLE_WEAK_KEY:  /* (private)  */
1695
0
      if (h->spec->set_extra_info)
1696
0
        rc = h->spec->set_extra_info
1697
0
          (&h->context.c, CIPHER_INFO_NO_WEAK_KEY, NULL, 0);
1698
0
      else
1699
0
        rc = GPG_ERR_NOT_SUPPORTED;
1700
0
      break;
1701
1702
0
    case PRIV_CIPHERCTL_GET_INPUT_VECTOR: /* (private)  */
1703
      /* This is the input block as used in CFB and OFB mode which has
1704
         initially been set as IV.  The returned format is:
1705
           1 byte  Actual length of the block in bytes.
1706
           n byte  The block.
1707
         If the provided buffer is too short, an error is returned. */
1708
0
      if (buflen < (1 + h->spec->blocksize))
1709
0
        rc = GPG_ERR_TOO_SHORT;
1710
0
      else
1711
0
        {
1712
0
          unsigned char *ivp;
1713
0
          unsigned char *dst = buffer;
1714
0
          int n = h->unused;
1715
1716
0
          if (!n)
1717
0
            n = h->spec->blocksize;
1718
0
          gcry_assert (n <= h->spec->blocksize);
1719
0
          *dst++ = n;
1720
0
          ivp = h->u_iv.iv + h->spec->blocksize - n;
1721
0
          while (n--)
1722
0
            *dst++ = *ivp++;
1723
0
        }
1724
0
      break;
1725
1726
0
    case PRIV_CIPHERCTL_GET_COUNTER: /* (private)  */
1727
      /* This is the input block as used in CTR mode which has
1728
         initially been set as IV.  The returned format is:
1729
           1 byte  Actual length of the block in bytes.
1730
           n byte  The block.
1731
         If the provided buffer is too short, an error is returned. */
1732
0
      if (buflen < (1 + h->spec->blocksize))
1733
0
        rc = GPG_ERR_TOO_SHORT;
1734
0
      else
1735
0
        {
1736
0
          unsigned char *ctrp;
1737
0
          unsigned char *dst = buffer;
1738
0
          int n = h->unused;
1739
1740
0
          if (!n)
1741
0
            n = h->spec->blocksize;
1742
0
          gcry_assert (n <= h->spec->blocksize);
1743
0
          *dst++ = n;
1744
0
          ctrp = h->u_ctr.ctr + h->spec->blocksize - n;
1745
0
          while (n--)
1746
0
            *dst++ = *ctrp++;
1747
0
        }
1748
0
      break;
1749
1750
0
    case GCRYCTL_SET_SBOX:
1751
0
      if (h->spec->set_extra_info)
1752
0
        rc = h->spec->set_extra_info
1753
0
          (&h->context.c, GCRYCTL_SET_SBOX, buffer, buflen);
1754
0
      else
1755
0
        rc = GPG_ERR_NOT_SUPPORTED;
1756
0
      break;
1757
1758
0
    case GCRYCTL_SET_ALLOW_WEAK_KEY:
1759
      /* Expecting BUFFER to be NULL and buflen to be on/off flag (0 or 1). */
1760
0
      if (!h || buffer || buflen > 1)
1761
0
  return GPG_ERR_CIPHER_ALGO;
1762
0
      h->marks.allow_weak_key = buflen ? 1 : 0;
1763
0
      break;
1764
1765
0
    default:
1766
0
      rc = GPG_ERR_INV_OP;
1767
0
    }
1768
1769
0
  return rc;
1770
0
}
1771
1772
1773
/* Return information about the cipher handle H.  CMD is the kind of
1774
 * information requested.
1775
 *
1776
 * CMD may be one of:
1777
 *
1778
 *  GCRYCTL_GET_TAGLEN:
1779
 *      Return the length of the tag for an AE algorithm mode.  An
1780
 *      error is returned for modes which do not support a tag.
1781
 *      BUFFER must be given as NULL.  On success the result is stored
1782
 *      at NBYTES.  The taglen is returned in bytes.
1783
 *
1784
 *  GCRYCTL_GET_KEYLEN:
1785
 *      Return the length of the key wrapped for AES-WRAP mode.  The
1786
 *      length is encoded in big-endian 4 bytes, when the key is
1787
 *      unwrapped with KWP.  Return 00 00 00 00, when the key is
1788
 *      unwrapped with KW.
1789
 *
1790
 * The function returns 0 on success or an error code.
1791
 */
1792
gcry_err_code_t
1793
_gcry_cipher_info (gcry_cipher_hd_t h, int cmd, void *buffer, size_t *nbytes)
1794
0
{
1795
0
  gcry_err_code_t rc = 0;
1796
1797
0
  switch (cmd)
1798
0
    {
1799
0
    case GCRYCTL_GET_TAGLEN:
1800
0
      if (!h || buffer || !nbytes)
1801
0
  rc = GPG_ERR_INV_ARG;
1802
0
      else
1803
0
  {
1804
0
          switch (h->mode)
1805
0
            {
1806
0
            case GCRY_CIPHER_MODE_OCB:
1807
0
              *nbytes = h->u_mode.ocb.taglen;
1808
0
              break;
1809
1810
0
            case GCRY_CIPHER_MODE_CCM:
1811
0
              *nbytes = h->u_mode.ccm.authlen;
1812
0
              break;
1813
1814
0
            case GCRY_CIPHER_MODE_EAX:
1815
0
              *nbytes = h->spec->blocksize;
1816
0
              break;
1817
1818
0
            case GCRY_CIPHER_MODE_GCM:
1819
0
              *nbytes = GCRY_GCM_BLOCK_LEN;
1820
0
              break;
1821
1822
0
            case GCRY_CIPHER_MODE_POLY1305:
1823
0
              *nbytes = POLY1305_TAGLEN;
1824
0
              break;
1825
1826
0
            case GCRY_CIPHER_MODE_SIV:
1827
0
              *nbytes = GCRY_SIV_BLOCK_LEN;
1828
0
              break;
1829
1830
0
            case GCRY_CIPHER_MODE_GCM_SIV:
1831
0
              *nbytes = GCRY_SIV_BLOCK_LEN;
1832
0
              break;
1833
1834
0
            default:
1835
0
              rc = GPG_ERR_INV_CIPHER_MODE;
1836
0
              break;
1837
0
            }
1838
0
        }
1839
0
      break;
1840
1841
0
    case GCRYCTL_GET_KEYLEN:
1842
0
      if (!h || !buffer || !nbytes)
1843
0
  rc = GPG_ERR_INV_ARG;
1844
0
      else
1845
0
        {
1846
0
          switch (h->mode)
1847
0
            {
1848
0
            case GCRY_CIPHER_MODE_AESWRAP:
1849
0
              *nbytes = 4;
1850
0
              memcpy (buffer, h->u_mode.wrap.plen, 4);
1851
0
              break;
1852
1853
0
            default:
1854
0
              rc = GPG_ERR_INV_CIPHER_MODE;
1855
0
              break;
1856
0
            }
1857
0
        }
1858
0
      break;
1859
1860
0
    default:
1861
0
      rc = GPG_ERR_INV_OP;
1862
0
    }
1863
1864
0
  return rc;
1865
0
}
1866
1867
/* Return information about the given cipher algorithm ALGO.
1868
1869
   WHAT select the kind of information returned:
1870
1871
    GCRYCTL_GET_KEYLEN:
1872
    Return the length of the key.  If the algorithm ALGO
1873
    supports multiple key lengths, the maximum supported key length
1874
    is returned.  The key length is returned as number of octets.
1875
    BUFFER and NBYTES must be zero.
1876
1877
    GCRYCTL_GET_BLKLEN:
1878
    Return the blocklength of the algorithm ALGO counted in octets.
1879
    BUFFER and NBYTES must be zero.
1880
1881
    GCRYCTL_TEST_ALGO:
1882
    Returns 0 if the specified algorithm ALGO is available for use.
1883
    BUFFER and NBYTES must be zero.
1884
1885
   Note: Because this function is in most cases used to return an
1886
   integer value, we can make it easier for the caller to just look at
1887
   the return value.  The caller will in all cases consult the value
1888
   and thereby detecting whether a error occurred or not (i.e. while
1889
   checking the block size)
1890
 */
1891
gcry_err_code_t
1892
_gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1893
128k
{
1894
128k
  gcry_err_code_t rc = 0;
1895
128k
  unsigned int ui;
1896
1897
128k
  switch (what)
1898
128k
    {
1899
0
    case GCRYCTL_GET_KEYLEN:
1900
0
      if (buffer || (! nbytes))
1901
0
  rc = GPG_ERR_CIPHER_ALGO;
1902
0
      else
1903
0
  {
1904
0
    ui = cipher_get_keylen (algo);
1905
0
    if ((ui > 0) && (ui <= 512))
1906
0
      *nbytes = (size_t) ui / 8;
1907
0
    else
1908
      /* The only reason for an error is an invalid algo.  */
1909
0
      rc = GPG_ERR_CIPHER_ALGO;
1910
0
  }
1911
0
      break;
1912
1913
0
    case GCRYCTL_GET_BLKLEN:
1914
0
      if (buffer || (! nbytes))
1915
0
  rc = GPG_ERR_CIPHER_ALGO;
1916
0
      else
1917
0
  {
1918
0
    ui = cipher_get_blocksize (algo);
1919
0
    if ((ui > 0) && (ui < 10000))
1920
0
      *nbytes = ui;
1921
0
    else
1922
0
            {
1923
              /* The only reason is an invalid algo or a strange
1924
                 blocksize.  */
1925
0
              rc = GPG_ERR_CIPHER_ALGO;
1926
0
            }
1927
0
  }
1928
0
      break;
1929
1930
128k
    case GCRYCTL_TEST_ALGO:
1931
128k
      if (buffer || nbytes)
1932
0
  rc = GPG_ERR_INV_ARG;
1933
128k
      else
1934
128k
  rc = check_cipher_algo (algo);
1935
128k
      break;
1936
1937
0
      default:
1938
0
  rc = GPG_ERR_INV_OP;
1939
128k
    }
1940
1941
128k
  return rc;
1942
128k
}
1943
1944
1945
/* This function returns length of the key for algorithm ALGO.  If the
1946
   algorithm supports multiple key lengths, the maximum supported key
1947
   length is returned.  On error 0 is returned.  The key length is
1948
   returned as number of octets.
1949
1950
   This is a convenience functions which should be preferred over
1951
   gcry_cipher_algo_info because it allows for proper type
1952
   checking.  */
1953
size_t
1954
_gcry_cipher_get_algo_keylen (int algo)
1955
0
{
1956
0
  size_t n;
1957
1958
0
  if (_gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &n))
1959
0
    n = 0;
1960
0
  return n;
1961
0
}
1962
1963
1964
/* This functions returns the blocklength of the algorithm ALGO
1965
   counted in octets.  On error 0 is returned.
1966
1967
   This is a convenience functions which should be preferred over
1968
   gcry_cipher_algo_info because it allows for proper type
1969
   checking.  */
1970
size_t
1971
_gcry_cipher_get_algo_blklen (int algo)
1972
0
{
1973
0
  size_t n;
1974
1975
0
  if (_gcry_cipher_algo_info( algo, GCRYCTL_GET_BLKLEN, NULL, &n))
1976
0
    n = 0;
1977
0
  return n;
1978
0
}
1979
1980
1981
/* Explicitly initialize this module.  */
1982
gcry_err_code_t
1983
_gcry_cipher_init (void)
1984
1
{
1985
1
  return 0;
1986
1
}
1987
1988
1989
/* Run the selftests for cipher algorithm ALGO with optional reporting
1990
   function REPORT.  */
1991
gpg_error_t
1992
_gcry_cipher_selftest (int algo, int extended, selftest_report_func_t report)
1993
0
{
1994
0
  gcry_err_code_t ec = 0;
1995
0
  gcry_cipher_spec_t *spec;
1996
1997
0
  spec = spec_from_algo (algo);
1998
0
  if (spec && !spec->flags.disabled
1999
0
      && (spec->flags.fips || !fips_mode ())
2000
0
      && spec->selftest)
2001
0
    ec = spec->selftest (algo, extended, report);
2002
0
  else
2003
0
    {
2004
0
      ec = GPG_ERR_CIPHER_ALGO;
2005
0
      if (report)
2006
0
        report ("cipher", algo, "module",
2007
0
                spec && !spec->flags.disabled
2008
0
                && (spec->flags.fips || !fips_mode ())?
2009
0
                "no selftest available" :
2010
0
                spec? "algorithm disabled" : "algorithm not found");
2011
0
    }
2012
2013
0
  return gpg_error (ec);
2014
0
}