summaryrefslogtreecommitdiff
path: root/tests/register.c
blob: 4d8cebe8f35938d38f8efa2b40b89938fdee85ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* register.c - Test for registering of additional cipher modules.
 *	Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "../src/gcrypt.h"

static int verbose;
static int in_fips_mode;

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

  va_start( arg_ptr, format ) ;
  vfprintf (stderr, format, arg_ptr );
  va_end(arg_ptr);
  exit (1);
}

gcry_err_code_t
foo_setkey (void *c, const unsigned char *key, unsigned keylen)
{
  (void)c;
  (void)key;
  (void)keylen;

  return 0;
}

#define FOO_BLOCKSIZE 16

void
foo_encrypt (void *c, unsigned char *outbuf, const unsigned char *inbuf)
{
  int i;

  (void)c;

  for (i = 0; i < FOO_BLOCKSIZE; i++)
    outbuf[i] = inbuf[i] ^ 0x42;
}

void
foo_decrypt (void *c, unsigned char *outbuf, const unsigned char *inbuf)
{
  int i;

  (void)c;

  for (i = 0; i < FOO_BLOCKSIZE; i++)
    outbuf[i] = inbuf[i] ^ 0x42;
}

gcry_cipher_spec_t cipher_spec_foo =
  {
    "FOO", NULL, NULL, 16, 0, 0,
    foo_setkey, foo_encrypt, foo_decrypt,
    NULL, NULL,
  };

int
check_list (int algorithm)
{
  gcry_error_t err = GPG_ERR_NO_ERROR;
  int *list, list_length;
  int i, ret = 0;

  err = gcry_cipher_list (NULL, &list_length);
  assert (! err);
  list = malloc (sizeof (int) * list_length);
  assert (list);
  err = gcry_cipher_list (list, &list_length);

  for (i = 0; i < list_length && (! ret); i++)
    if (list[i] == algorithm)
      ret = 1;

  return ret;
}

void
check_run (void)
{
  int err, algorithm;
  gcry_cipher_hd_t h;
  char plain[16] = "Heil Discordia!";
  char encrypted[16], decrypted[16];
  gcry_module_t module;
  int ret;

  err = gcry_cipher_register (&cipher_spec_foo, &algorithm, &module);
  if (in_fips_mode)
    {
      if (gpg_err_code (err) != GPG_ERR_NOT_SUPPORTED)
        die ("register cipher failed in fips mode: %s\n", gpg_strerror (err));
      return;
    }
  else
    {
      if (err)
        die ("register cipher failed: %s\n", gpg_strerror (err));
    }

  err = gcry_cipher_open (&h, algorithm, GCRY_CIPHER_MODE_CBC, 0);
  if (err)
    die ("gcry_cipher_open failed: %s\n", gpg_strerror (err));

  err = gcry_cipher_encrypt (h,
			     (unsigned char *) encrypted, sizeof (encrypted),
			     (unsigned char *) plain, sizeof (plain));
  assert (! err);
  assert (memcmp ((void *) plain, (void *) encrypted, sizeof (plain)));

  err = gcry_cipher_reset (h);
  assert (! err);

  err = gcry_cipher_decrypt (h,
			     (unsigned char *) decrypted, sizeof (decrypted),
			     (unsigned char *) encrypted, sizeof (encrypted));
  assert (! err);
  assert (! memcmp ((void *) plain, (void *) decrypted, sizeof (plain)));

  ret = check_list (algorithm);
  assert (ret);

  gcry_cipher_close (h);

  gcry_cipher_unregister (module);

  ret = check_list (algorithm);
  assert (! ret);
}

int
main (int argc, char **argv)
{
  int debug = 0;
  int i = 1;

  if (argc > 1 && !strcmp (argv[1], "--verbose"))
    verbose = 1;
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
    verbose = debug = 1;

  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  if (!gcry_check_version (GCRYPT_VERSION))
    die ("version mismatch\n");
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  if (debug)
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);

  if ( gcry_control (GCRYCTL_FIPS_MODE_P, 0) )
    in_fips_mode = 1;

  for (; i > 0; i--)
    check_run ();

  /* In fips mode we let the Makefile skip this test because a PASS
     would not make much sense with all egistering disabled. */
  return in_fips_mode? 77:0;
}