~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to tests/basic.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2009-02-21 13:46:58 UTC
  • mto: (1.1.6 upstream) (2.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090221134658-855twvcr4ezk2ron
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* basic.c  -  basic regression tests
2
 
 *      Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2002, 2003, 2005, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of Libgcrypt.
5
5
 *
14
14
 * GNU Lesser General Public License for more details.
15
15
 *
16
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
 
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#ifdef HAVE_CONFIG_H
50
49
 
51
50
static int verbose;
52
51
static int error_count;
 
52
static int in_fips_mode;
53
53
 
54
54
static void
55
55
fail (const char *format, ...)
127
127
 
128
128
  for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
129
129
    {
 
130
      if (gcry_cipher_test_algo (tv[i].algo) && in_fips_mode)
 
131
        {
 
132
          if (verbose)
 
133
            fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
134
                     tv[i].algo);
 
135
          continue;
 
136
        }
 
137
 
130
138
      err = gcry_cipher_open (&hd,
131
139
                              tv[i].algo,
132
140
                              GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_MAC);
1034
1042
    fprintf (stderr, "Starting Cipher checks.\n");
1035
1043
  for (i = 0; algos[i]; i++)
1036
1044
    {
 
1045
      if (gcry_cipher_test_algo (algos[i]) && in_fips_mode)
 
1046
        {
 
1047
          if (verbose)
 
1048
            fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
1049
                     algos[i]);
 
1050
          continue;
 
1051
        }
1037
1052
      if (verbose)
1038
1053
        fprintf (stderr, "  checking %s [%i]\n",
1039
1054
                 gcry_cipher_algo_name (algos[i]),
1049
1064
 
1050
1065
  for (i = 0; algos2[i]; i++)
1051
1066
    {
 
1067
      if (gcry_cipher_test_algo (algos[i]) && in_fips_mode)
 
1068
        {
 
1069
          if (verbose)
 
1070
            fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
1071
                     algos[i]);
 
1072
          continue;
 
1073
        }
1052
1074
      if (verbose)
1053
1075
        fprintf (stderr, "  checking `%s'\n",
1054
1076
                 gcry_cipher_algo_name (algos2[i]));
1294
1316
 
1295
1317
  for (i = 0; algos[i].md; i++)
1296
1318
    {
 
1319
      if ((gcry_md_test_algo (algos[i].md) || algos[i].md == GCRY_MD_MD5)
 
1320
          && in_fips_mode)
 
1321
        {
 
1322
          if (verbose)
 
1323
            fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
1324
                     algos[i].md);
 
1325
          continue;
 
1326
        }
1297
1327
      if (verbose)
1298
1328
        fprintf (stderr, "  checking %s [%i] for length %zi\n", 
1299
1329
                 gcry_md_algo_name (algos[i].md),
1346
1376
  gcry_md_close (hd);
1347
1377
 
1348
1378
  p = gcry_md_read (hd2, algo);
1349
 
  if (0 == p)
 
1379
  if (!p)
1350
1380
    fail("algo %d, hmac gcry_md_read failed\n", algo);
1351
1381
 
1352
1382
  if (memcmp (p, expect, mdlen))
1656
1686
 
1657
1687
  for (i = 0; algos[i].md; i++)
1658
1688
    {
 
1689
      if ((gcry_md_test_algo (algos[i].md) || algos[i].md == GCRY_MD_MD5)
 
1690
          && in_fips_mode)
 
1691
        {
 
1692
          if (verbose)
 
1693
            fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
1694
                     algos[i].md);
 
1695
          continue;
 
1696
        }
1659
1697
      if (verbose)
1660
 
        fprintf (stderr, "  checking %s [%i] for length %zi\n", 
 
1698
        fprintf (stderr, 
 
1699
                 "  checking %s [%i] for %zi byte key and %zi byte data\n", 
1661
1700
                 gcry_md_algo_name (algos[i].md),
1662
1701
                 algos[i].md,
1663
 
                 strlen(algos[i].data));
 
1702
                 strlen(algos[i].key), strlen(algos[i].data));
1664
1703
 
1665
1704
      check_one_hmac (algos[i].md, algos[i].data, strlen (algos[i].data),
1666
1705
                      algos[i].key, strlen(algos[i].key),
1824
1863
  if (verbose)
1825
1864
    fprintf (stderr, "  generating RSA key:");  
1826
1865
  rc = gcry_sexp_new (&key_spec,
1827
 
                      "(genkey (rsa (nbits 4:1024)))", 0, 1);
 
1866
                      in_fips_mode ? "(genkey (rsa (nbits 4:1024)))"
 
1867
                      : "(genkey (rsa (nbits 4:1024)(transient-key)))", 
 
1868
                      0, 1);
1828
1869
  if (rc)
1829
1870
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
1830
1871
  rc = gcry_pk_genkey (&key, key_spec);
1968
2009
    fprintf (stderr, "Starting public key checks.\n");
1969
2010
  for (i = 0; i < sizeof (pubkeys) / sizeof (*pubkeys); i++)
1970
2011
    if (pubkeys[i].id)
1971
 
      check_one_pubkey (i, pubkeys[i]);
 
2012
      {
 
2013
        if (gcry_pk_test_algo (pubkeys[i].id) && in_fips_mode)
 
2014
          {
 
2015
            if (verbose)
 
2016
              fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
2017
                       pubkeys[i].id);
 
2018
            continue;
 
2019
          }
 
2020
        check_one_pubkey (i, pubkeys[i]);
 
2021
      }
1972
2022
  if (verbose)
1973
2023
    fprintf (stderr, "Completed public key checks.\n");
1974
2024
 
1976
2026
    fprintf (stderr, "Starting additional public key checks.\n");
1977
2027
  for (i = 0; i < sizeof (pubkeys) / sizeof (*pubkeys); i++)
1978
2028
    if (pubkeys[i].id)
1979
 
      check_one_pubkey_new (i);
 
2029
      {
 
2030
        if (gcry_pk_test_algo (pubkeys[i].id) && in_fips_mode)
 
2031
          {
 
2032
            if (verbose)
 
2033
              fprintf (stderr, "  algorithm %d not available in fips mode\n",
 
2034
                       pubkeys[i].id);
 
2035
            continue;
 
2036
          }
 
2037
        check_one_pubkey_new (i);
 
2038
      }
1980
2039
  if (verbose)
1981
2040
    fprintf (stderr, "Completed additional public key checks.\n");
1982
2041
 
1984
2043
 
1985
2044
int
1986
2045
main (int argc, char **argv)
1987
 
{
 
2046
 
2047
  gpg_error_t err;
 
2048
  int last_argc = -1;
1988
2049
  int debug = 0;
1989
 
 
1990
 
  if (argc > 1 && !strcmp (argv[1], "--verbose"))
1991
 
    verbose = 1;
1992
 
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
1993
 
    verbose = debug = 1;
 
2050
  int use_fips = 0;
 
2051
  int selftest_only = 0;
 
2052
 
 
2053
  if (argc)
 
2054
    { argc--; argv++; }
 
2055
 
 
2056
  while (argc && last_argc != argc )
 
2057
    {
 
2058
      last_argc = argc;
 
2059
      if (!strcmp (*argv, "--"))
 
2060
        {
 
2061
          argc--; argv++;
 
2062
          break;
 
2063
        }
 
2064
      else if (!strcmp (*argv, "--verbose"))
 
2065
        {
 
2066
          verbose++;
 
2067
          argc--; argv++;
 
2068
        }
 
2069
      else if (!strcmp (*argv, "--debug"))
 
2070
        {
 
2071
          verbose = debug = 1;
 
2072
          argc--; argv++;
 
2073
        }
 
2074
      else if (!strcmp (*argv, "--fips"))
 
2075
        {
 
2076
          use_fips = 1;
 
2077
          argc--; argv++;
 
2078
        }
 
2079
      else if (!strcmp (*argv, "--selftest"))
 
2080
        {
 
2081
          selftest_only = 1;
 
2082
          verbose += 2;
 
2083
          argc--; argv++;
 
2084
        }
 
2085
    }          
 
2086
 
 
2087
  gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
 
2088
 
 
2089
  if (use_fips)
 
2090
    gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
1994
2091
 
1995
2092
  if (!gcry_check_version (GCRYPT_VERSION))
1996
2093
    die ("version mismatch\n");
1997
2094
 
 
2095
  if ( gcry_fips_mode_active () )
 
2096
    in_fips_mode = 1;
 
2097
 
 
2098
  if (!in_fips_mode)
 
2099
    gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
 
2100
 
1998
2101
  if (verbose)
1999
2102
    gcry_set_progress_handler (progress_handler, NULL);
2000
 
  
2001
 
  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
 
2103
 
2002
2104
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
2003
2105
  if (debug)
2004
2106
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
2005
2107
  /* No valuable keys are create, so we can speed up our RNG. */
2006
2108
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
2007
2109
 
2008
 
  check_ciphers ();
2009
 
  check_aes128_cbc_cts_cipher ();
2010
 
  check_cbc_mac_cipher ();
2011
 
  check_ctr_cipher ();
2012
 
  check_cfb_cipher ();
2013
 
  check_ofb_cipher ();
2014
 
  check_digests ();
2015
 
  check_hmac ();
2016
 
  check_pubkey ();
 
2110
  if (!selftest_only)
 
2111
    {
 
2112
      check_ciphers ();
 
2113
      check_aes128_cbc_cts_cipher ();
 
2114
      check_cbc_mac_cipher ();
 
2115
      check_ctr_cipher ();
 
2116
      check_cfb_cipher ();
 
2117
      check_ofb_cipher ();
 
2118
      check_digests ();
 
2119
      check_hmac ();
 
2120
      check_pubkey ();
 
2121
    }
 
2122
 
 
2123
 
 
2124
  if (in_fips_mode && !selftest_only)
 
2125
    {
 
2126
      /* If we are in fips mode do some more tests. */
 
2127
      gcry_md_hd_t md;
 
2128
 
 
2129
      /* First trigger a self-test.  */
 
2130
      gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
 
2131
      if (!gcry_control (GCRYCTL_OPERATIONAL_P, 0))
 
2132
        fail ("not in operational state after self-test\n");
 
2133
      
 
2134
      /* Get us into the error state.  */
 
2135
      err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
 
2136
      if (err)
 
2137
        fail ("failed to open SHA-1 hash context: %s\n", gpg_strerror (err));
 
2138
      else
 
2139
        {
 
2140
          err = gcry_md_enable (md, GCRY_MD_SHA256);
 
2141
          if (err)
 
2142
            fail ("failed to add SHA-256 hash context: %s\n",
 
2143
                  gpg_strerror (err));
 
2144
          else
 
2145
            {
 
2146
              /* gcry_md_get_algo is only defined for a context with
 
2147
                 just one digest algorithm.  With our setup it should
 
2148
                 put the oibrary intoerror state.  */
 
2149
              fputs ("Note: Two lines with error messages follow "
 
2150
                     "- this is expected\n", stderr);
 
2151
              gcry_md_get_algo (md);
 
2152
              gcry_md_close (md);
 
2153
              if (gcry_control (GCRYCTL_OPERATIONAL_P, 0))
 
2154
                fail ("expected error state but still in operational state\n");
 
2155
              else
 
2156
                {
 
2157
                  /* Now run a self-test and to get back into
 
2158
                     operational state.  */
 
2159
                  gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
 
2160
                  if (!gcry_control (GCRYCTL_OPERATIONAL_P, 0))
 
2161
                    fail ("did not reach operational after error "
 
2162
                          "and self-test\n");
 
2163
                }
 
2164
            }
 
2165
        }
 
2166
      
 
2167
    }
 
2168
  else
 
2169
    {
 
2170
      /* If in standard mode, run selftests.  */
 
2171
      if (gcry_control (GCRYCTL_SELFTEST, 0))
 
2172
        fail ("running self-test failed\n");
 
2173
    }
2017
2174
 
2018
2175
  if (verbose)
2019
2176
    fprintf (stderr, "\nAll tests completed. Errors: %i\n", error_count);
2020
2177
 
 
2178
  if (in_fips_mode && !gcry_fips_mode_active ())
 
2179
    fprintf (stderr, "FIPS mode is not anymore active\n");
 
2180
 
2021
2181
  return error_count ? 1 : 0;
2022
2182
}