summaryrefslogtreecommitdiff
path: root/tests/ac.c
blob: 75fe2dae43c6a62751668c4b3b24d1bac1d36d46 (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
/* pubkey.c - Public key encryption/decryption tests
 *	Copyright (C) 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 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);
}

void
key_copy (gcry_ac_handle_t handle,
	  gcry_ac_key_type_t type,
	  gcry_ac_key_t *key_cp, gcry_ac_key_t key)
{
  gcry_error_t err = 0;

  err = gcry_ac_key_init (key_cp, handle, type,
			  gcry_ac_key_data_get (key));

  assert (! err);
}

void
check_one (gcry_mpi_t x)
{
  gcry_ac_handle_t handle;
  gcry_ac_key_pair_t key_pair;
  gcry_ac_key_t key_sec, key_sec_cp, key_pub, key_pub_cp;
  gcry_error_t err = 0;
  gcry_mpi_t x2;
  gcry_ac_data_t data, data2;
  gcry_ac_key_spec_rsa_t rsa_spec;

  rsa_spec.e = gcry_mpi_new (0);
  gcry_mpi_set_ui (rsa_spec.e, 1);

  err = gcry_ac_open (&handle, GCRY_AC_RSA, 0);
  assert (! err);

  err = gcry_ac_key_pair_generate (handle, 1024, &rsa_spec, &key_pair, NULL);
  assert (! err);

  key_sec = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_SECRET);
  key_copy (handle, GCRY_AC_KEY_SECRET, &key_sec_cp, key_sec);

  key_pub = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_PUBLIC);
  key_copy (handle, GCRY_AC_KEY_PUBLIC, &key_pub_cp, key_pub);

  err = gcry_ac_data_encrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_pub_cp, x, &data);
  assert (! err);

  err = gcry_ac_data_decrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_sec_cp, &x2, data);
  assert (! err);

  assert (! gcry_mpi_cmp (x, x2));

  gcry_ac_data_destroy (data);

  err = gcry_ac_data_sign (handle, key_sec, x, &data);
  assert (! err);
  err = gcry_ac_data_copy (&data2, data);
  assert (! err);
  gcry_ac_data_destroy (data);
  err = gcry_ac_data_copy (&data, data2);
  assert (! err);
  gcry_ac_data_destroy (data2);

  err = gcry_ac_data_verify (handle, key_pub, x, data);
  assert (! err);

  gcry_ac_data_destroy (data);

  err = gcry_ac_data_sign (handle, key_sec, x, &data);
  assert (! err);
  {
    const char *label;
    gcry_mpi_t y;

    err = gcry_ac_data_get_index (data, 0, 0, &label, &y);
    assert (! err);
    gcry_mpi_add_ui (y, y, 1);
    
    err = gcry_ac_data_verify (handle, key_pub, x, data);
    assert (gcry_err_code (err) == GPG_ERR_BAD_SIGNATURE);
  }

  gcry_ac_close (handle);
}

void
check_run (void)
{
  /*const char *s = "All Hail Discordia."; -- not used */
  unsigned int a = 0x4223;
  gcry_mpi_t x;

  x = gcry_mpi_new (0);
  gcry_mpi_set_ui (x, a);
  check_one (x);
  gcry_mpi_release (x);
}

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);

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