LCOV - code coverage report
Current view: top level - tests - aeswrap.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 50 94 53.2 %
Date: 2017-03-02 16:44:37 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* aeswrap.c -  AESWRAP mode regression tests
       2             :  *      Copyright (C) 2009 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       6             :  * Libgcrypt is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * Libgcrypt is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, write to the Free Software
      18             :  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
      19             :  */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include <config.h>
      23             : #endif
      24             : #include <stdio.h>
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : #include <stdarg.h>
      28             : 
      29             : #define PGM "aeswrap"
      30             : #include "t-common.h"
      31             : 
      32             : 
      33             : static void
      34           6 : check (int algo,
      35             :        const void *kek, size_t keklen,
      36             :        const void *data, size_t datalen,
      37             :        const void *expected, size_t expectedlen)
      38             : {
      39             :   gcry_error_t err;
      40             :   gcry_cipher_hd_t hd;
      41             :   unsigned char outbuf[32+8];
      42             :   size_t outbuflen;
      43             : 
      44           6 :   err = gcry_cipher_open (&hd, algo, GCRY_CIPHER_MODE_AESWRAP, 0);
      45           6 :   if (err)
      46             :     {
      47           0 :       fail ("gcry_cipher_open failed: %s\n", gpg_strerror (err));
      48           0 :       return;
      49             :     }
      50             : 
      51           6 :   err = gcry_cipher_setkey (hd, kek, keklen);
      52           6 :   if (err)
      53             :     {
      54           0 :       fail ("gcry_cipher_setkey failed: %s\n", gpg_strerror (err));
      55           0 :       return;
      56             :     }
      57             : 
      58           6 :   outbuflen = datalen + 8;
      59           6 :   if (outbuflen > sizeof outbuf)
      60           0 :     err = gpg_error (GPG_ERR_INTERNAL);
      61             :   else
      62           6 :     err = gcry_cipher_encrypt (hd, outbuf, outbuflen, data, datalen);
      63           6 :   if (err)
      64             :     {
      65           0 :       fail ("gcry_cipher_encrypt failed: %s\n", gpg_strerror (err));
      66           0 :       return;
      67             :     }
      68             : 
      69           6 :   if (outbuflen != expectedlen || memcmp (outbuf, expected, expectedlen))
      70             :     {
      71             :       const unsigned char *s;
      72             :       int i;
      73             : 
      74           0 :       fail ("mismatch at encryption!\n");
      75           0 :       fprintf (stderr, "computed: ");
      76           0 :       for (i = 0; i < outbuflen; i++)
      77           0 :         fprintf (stderr, "%02x ", outbuf[i]);
      78           0 :       fprintf (stderr, "\nexpected: ");
      79           0 :       for (s = expected, i = 0; i < expectedlen; s++, i++)
      80           0 :         fprintf (stderr, "%02x ", *s);
      81           0 :       putc ('\n', stderr);
      82             :     }
      83             : 
      84             : 
      85           6 :   outbuflen = expectedlen - 8;
      86           6 :   if (outbuflen > sizeof outbuf)
      87           0 :     err = gpg_error (GPG_ERR_INTERNAL);
      88             :   else
      89           6 :     err = gcry_cipher_decrypt (hd, outbuf, outbuflen, expected, expectedlen);
      90           6 :   if (err)
      91             :     {
      92           0 :       fail ("gcry_cipher_decrypt failed: %s\n", gpg_strerror (err));
      93           0 :       return;
      94             :     }
      95             : 
      96           6 :   if (outbuflen != datalen || memcmp (outbuf, data, datalen))
      97             :     {
      98             :       const unsigned char *s;
      99             :       int i;
     100             : 
     101           0 :       fail ("mismatch at decryption!\n");
     102           0 :       fprintf (stderr, "computed: ");
     103           0 :       for (i = 0; i < outbuflen; i++)
     104           0 :         fprintf (stderr, "%02x ", outbuf[i]);
     105           0 :       fprintf (stderr, "\nexpected: ");
     106           0 :       for (s = data, i = 0; i < datalen; s++, i++)
     107           0 :         fprintf (stderr, "%02x ", *s);
     108           0 :       putc ('\n', stderr);
     109             :     }
     110             : 
     111             :   /* Now the last step again with a key reset. */
     112           6 :   gcry_cipher_reset (hd);
     113             : 
     114           6 :   outbuflen = expectedlen - 8;
     115           6 :   if (outbuflen > sizeof outbuf)
     116           0 :     err = gpg_error (GPG_ERR_INTERNAL);
     117             :   else
     118           6 :     err = gcry_cipher_decrypt (hd, outbuf, outbuflen, expected, expectedlen);
     119           6 :   if (err)
     120             :     {
     121           0 :       fail ("gcry_cipher_decrypt(2) failed: %s\n", gpg_strerror (err));
     122           0 :       return;
     123             :     }
     124             : 
     125           6 :   if (outbuflen != datalen || memcmp (outbuf, data, datalen))
     126           0 :     fail ("mismatch at decryption(2)!\n");
     127             : 
     128             :   /* And once ore without a key reset. */
     129           6 :   outbuflen = expectedlen - 8;
     130           6 :   if (outbuflen > sizeof outbuf)
     131           0 :     err = gpg_error (GPG_ERR_INTERNAL);
     132             :   else
     133           6 :     err = gcry_cipher_decrypt (hd, outbuf, outbuflen, expected, expectedlen);
     134           6 :   if (err)
     135             :     {
     136           0 :       fail ("gcry_cipher_decrypt(3) failed: %s\n", gpg_strerror (err));
     137           0 :       return;
     138             :     }
     139             : 
     140           6 :   if (outbuflen != datalen || memcmp (outbuf, data, datalen))
     141           0 :     fail ("mismatch at decryption(3)!\n");
     142             : 
     143           6 :   gcry_cipher_close (hd);
     144             : }
     145             : 
     146             : 
     147             : static void
     148           1 : check_all (void)
     149             : {
     150           1 :   if (verbose)
     151           0 :     fprintf (stderr, "4.1 Wrap 128 bits of Key Data with a 128-bit KEK\n");
     152           1 :   check
     153             :     (GCRY_CIPHER_AES128,
     154             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 16,
     155             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF", 16,
     156             :      "\x1F\xA6\x8B\x0A\x81\x12\xB4\x47\xAE\xF3\x4B\xD8\xFB\x5A\x7B\x82"
     157             :      "\x9D\x3E\x86\x23\x71\xD2\xCF\xE5", 24);
     158             : 
     159           1 :   if (verbose)
     160           0 :     fprintf (stderr, "4.2 Wrap 128 bits of Key Data with a 192-bit KEK\n");
     161           1 :   check
     162             :     (GCRY_CIPHER_AES192,
     163             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
     164             :      "\x10\x11\x12\x13\x14\x15\x16\x17", 24,
     165             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF", 16,
     166             :      "\x96\x77\x8B\x25\xAE\x6C\xA4\x35\xF9\x2B\x5B\x97\xC0\x50\xAE\xD2"
     167             :      "\x46\x8A\xB8\xA1\x7A\xD8\x4E\x5D", 24);
     168             : 
     169           1 :   if (verbose)
     170           0 :     fprintf (stderr, "4.3 Wrap 128 bits of Key Data with a 256-bit KEK\n");
     171           1 :   check
     172             :     (GCRY_CIPHER_AES256,
     173             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
     174             :      "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 32,
     175             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF", 16,
     176             :      "\x64\xE8\xC3\xF9\xCE\x0F\x5B\xA2\x63\xE9\x77\x79\x05\x81\x8A\x2A"
     177             :      "\x93\xC8\x19\x1E\x7D\x6E\x8A\xE7", 24);
     178             : 
     179           1 :   if (verbose)
     180           0 :     fprintf (stderr, "4.4 Wrap 192 bits of Key Data with a 192-bit KEK\n");
     181           1 :   check
     182             :     (GCRY_CIPHER_AES192,
     183             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
     184             :      "\x10\x11\x12\x13\x14\x15\x16\x17", 24,
     185             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
     186             :      "\x00\x01\x02\x03\x04\x05\x06\x07", 24,
     187             :      "\x03\x1D\x33\x26\x4E\x15\xD3\x32\x68\xF2\x4E\xC2\x60\x74\x3E\xDC"
     188             :      "\xE1\xC6\xC7\xDD\xEE\x72\x5A\x93\x6B\xA8\x14\x91\x5C\x67\x62\xD2", 32);
     189             : 
     190           1 :   if (verbose)
     191           0 :     fprintf (stderr, "4.5 Wrap 192 bits of Key Data with a 256-bit KEK\n");
     192           1 :   check
     193             :     (GCRY_CIPHER_AES256,
     194             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
     195             :      "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 32,
     196             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
     197             :      "\x00\x01\x02\x03\x04\x05\x06\x07", 24,
     198             :      "\xA8\xF9\xBC\x16\x12\xC6\x8B\x3F\xF6\xE6\xF4\xFB\xE3\x0E\x71\xE4"
     199             :      "\x76\x9C\x8B\x80\xA3\x2C\xB8\x95\x8C\xD5\xD1\x7D\x6B\x25\x4D\xA1", 32);
     200             : 
     201           1 :   if (verbose)
     202           0 :     fprintf (stderr, "4.6 Wrap 256 bits of Key Data with a 256-bit KEK\n");
     203           1 :   check
     204             :     (GCRY_CIPHER_AES,
     205             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
     206             :      "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 32,
     207             :      "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
     208             :      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 32,
     209             :      "\x28\xC9\xF4\x04\xC4\xB8\x10\xF4\xCB\xCC\xB3\x5C\xFB\x87\xF8\x26"
     210             :      "\x3F\x57\x86\xE2\xD8\x0E\xD3\x26\xCB\xC7\xF0\xE7\x1A\x99\xF4\x3B"
     211             :      "\xFB\x98\x8B\x9B\x7A\x02\xDD\x21", 40);
     212           1 : }
     213             : 
     214             : int
     215           1 : main (int argc, char **argv)
     216             : {
     217           1 :   if (argc > 1 && !strcmp (argv[1], "--verbose"))
     218           0 :     verbose = 1;
     219           1 :   else if (argc > 1 && !strcmp (argv[1], "--debug"))
     220           0 :     verbose = debug = 1;
     221             : 
     222           1 :   if (!gcry_check_version (GCRYPT_VERSION))
     223           0 :     die ("version mismatch\n");
     224             : 
     225           1 :   xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     226           1 :   xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     227           1 :   if (debug)
     228           0 :     xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
     229           1 :   check_all ();
     230             : 
     231           1 :   return error_count ? 1 : 0;
     232             : }

Generated by: LCOV version 1.13