Prefer minitasn1 over libtasn1.
[shishi.git] / crypto / tests / ac.c
blob898c60531e23fcf04966338b65673f2a40e5e483
1 /* pubkey.c - Public key encryption/decryption tests
2 * Copyright (C) 2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "../src/gcrypt.h"
29 static int verbose;
31 static void
32 die (const char *format, ...)
34 va_list arg_ptr ;
36 va_start( arg_ptr, format ) ;
37 vfprintf (stderr, format, arg_ptr );
38 va_end(arg_ptr);
39 exit (1);
42 void
43 key_copy (gcry_ac_handle_t handle,
44 gcry_ac_key_type_t type,
45 gcry_ac_key_t *key_cp, gcry_ac_key_t key)
47 gcry_error_t err = 0;
49 err = gcry_ac_key_init (key_cp, handle, type,
50 gcry_ac_key_data_get (key));
52 assert (! err);
55 void
56 check_one (gcry_mpi_t x)
58 gcry_ac_handle_t handle;
59 gcry_ac_key_pair_t key_pair;
60 gcry_ac_key_t key_sec, key_sec_cp, key_pub, key_pub_cp;
61 gcry_error_t err = 0;
62 gcry_mpi_t x2;
63 gcry_ac_data_t data, data2;
64 gcry_ac_key_spec_rsa_t rsa_spec;
66 rsa_spec.e = gcry_mpi_new (0);
67 gcry_mpi_set_ui (rsa_spec.e, 1);
69 err = gcry_ac_open (&handle, GCRY_AC_RSA, 0);
70 assert (! err);
72 err = gcry_ac_key_pair_generate (handle, &key_pair, 1024, (void *) &rsa_spec);
73 assert (! err);
75 key_sec = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_SECRET);
76 key_copy (handle, GCRY_AC_KEY_SECRET, &key_sec_cp, key_sec);
78 key_pub = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_PUBLIC);
79 key_copy (handle, GCRY_AC_KEY_PUBLIC, &key_pub_cp, key_pub);
81 err = gcry_ac_data_encrypt (handle, GCRY_AC_FLAG_DATA_NO_BLINDING,
82 key_pub_cp, x, &data);
83 assert (! err);
85 err = gcry_ac_data_decrypt (handle, GCRY_AC_FLAG_DATA_NO_BLINDING,
86 key_sec_cp, &x2, data);
87 assert (! err);
89 assert (! gcry_mpi_cmp (x, x2));
91 gcry_ac_data_destroy (data);
93 err = gcry_ac_data_sign (handle, key_sec, x, &data);
94 assert (! err);
95 err = gcry_ac_data_copy (&data2, data);
96 assert (! err);
97 gcry_ac_data_destroy (data);
98 err = gcry_ac_data_copy (&data, data2);
99 assert (! err);
100 gcry_ac_data_destroy (data2);
102 err = gcry_ac_data_verify (handle, key_pub, x, data);
103 assert (! err);
105 gcry_ac_data_destroy (data);
107 err = gcry_ac_data_sign (handle, key_sec, x, &data);
108 assert (! err);
110 const char *label;
111 gcry_mpi_t y;
113 err = gcry_ac_data_get_index (data, 0, &label, &y);
114 assert (! err);
115 gcry_mpi_add_ui (y, y, 1);
117 err = gcry_ac_data_set (data, label, y);
118 assert (! err);
120 err = gcry_ac_data_verify (handle, key_pub, x, data);
121 assert (gcry_err_code (err) == GPG_ERR_BAD_SIGNATURE);
124 gcry_ac_close (handle);
127 void
128 check_run (void)
130 const char *s = "All Hail Discordia.";
131 unsigned int a = 0x4223;
132 gcry_mpi_t x;
134 x = gcry_mpi_new (0);
135 gcry_mpi_set_ui (x, a);
136 check_one (x);
137 gcry_mpi_release (x);
141 main (int argc, char **argv)
143 int debug = 0;
144 int i = 1;
146 if (argc > 1 && !strcmp (argv[1], "--verbose"))
147 verbose = 1;
148 else if (argc > 1 && !strcmp (argv[1], "--debug"))
149 verbose = debug = 1;
151 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
152 if (!gcry_check_version (GCRYPT_VERSION))
153 die ("version mismatch\n");
154 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
155 if (debug)
156 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
158 for (; i > 0; i--)
159 check_run ();
161 return 0;