diff options
Diffstat (limited to 'linden/indra/libgcrypt/libgcrypt-1.2.2/tests/ac.c')
-rwxr-xr-x | linden/indra/libgcrypt/libgcrypt-1.2.2/tests/ac.c | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/ac.c b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/ac.c new file mode 100755 index 0000000..31d393b --- /dev/null +++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/ac.c | |||
@@ -0,0 +1,157 @@ | |||
1 | /* pubkey.c - Public key encryption/decryption tests | ||
2 | * Copyright (C) 2003 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 | #include <stdarg.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <assert.h> | ||
26 | |||
27 | #include "../src/gcrypt.h" | ||
28 | |||
29 | static int verbose; | ||
30 | |||
31 | static void | ||
32 | die (const char *format, ...) | ||
33 | { | ||
34 | va_list arg_ptr ; | ||
35 | |||
36 | va_start( arg_ptr, format ) ; | ||
37 | vfprintf (stderr, format, arg_ptr ); | ||
38 | va_end(arg_ptr); | ||
39 | exit (1); | ||
40 | } | ||
41 | |||
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) | ||
46 | { | ||
47 | gcry_error_t err = 0; | ||
48 | |||
49 | err = gcry_ac_key_init (key_cp, handle, type, | ||
50 | gcry_ac_key_data_get (key)); | ||
51 | |||
52 | assert (! err); | ||
53 | } | ||
54 | |||
55 | void | ||
56 | check_one (gcry_mpi_t x) | ||
57 | { | ||
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; | ||
65 | |||
66 | rsa_spec.e = gcry_mpi_new (0); | ||
67 | gcry_mpi_set_ui (rsa_spec.e, 1); | ||
68 | |||
69 | err = gcry_ac_open (&handle, GCRY_AC_RSA, 0); | ||
70 | assert (! err); | ||
71 | |||
72 | err = gcry_ac_key_pair_generate (handle, 1024, &rsa_spec, &key_pair, NULL); | ||
73 | assert (! err); | ||
74 | |||
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); | ||
77 | |||
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); | ||
80 | |||
81 | err = gcry_ac_data_encrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_pub_cp, x, &data); | ||
82 | assert (! err); | ||
83 | |||
84 | err = gcry_ac_data_decrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_sec_cp, &x2, data); | ||
85 | assert (! err); | ||
86 | |||
87 | assert (! gcry_mpi_cmp (x, x2)); | ||
88 | |||
89 | gcry_ac_data_destroy (data); | ||
90 | |||
91 | err = gcry_ac_data_sign (handle, key_sec, x, &data); | ||
92 | assert (! err); | ||
93 | err = gcry_ac_data_copy (&data2, data); | ||
94 | assert (! err); | ||
95 | gcry_ac_data_destroy (data); | ||
96 | err = gcry_ac_data_copy (&data, data2); | ||
97 | assert (! err); | ||
98 | gcry_ac_data_destroy (data2); | ||
99 | |||
100 | err = gcry_ac_data_verify (handle, key_pub, x, data); | ||
101 | assert (! err); | ||
102 | |||
103 | gcry_ac_data_destroy (data); | ||
104 | |||
105 | err = gcry_ac_data_sign (handle, key_sec, x, &data); | ||
106 | assert (! err); | ||
107 | { | ||
108 | const char *label; | ||
109 | gcry_mpi_t y; | ||
110 | |||
111 | err = gcry_ac_data_get_index (data, 0, 0, &label, &y); | ||
112 | assert (! err); | ||
113 | gcry_mpi_add_ui (y, y, 1); | ||
114 | |||
115 | err = gcry_ac_data_verify (handle, key_pub, x, data); | ||
116 | assert (gcry_err_code (err) == GPG_ERR_BAD_SIGNATURE); | ||
117 | } | ||
118 | |||
119 | gcry_ac_close (handle); | ||
120 | } | ||
121 | |||
122 | void | ||
123 | check_run (void) | ||
124 | { | ||
125 | /*const char *s = "All Hail Discordia."; -- not used */ | ||
126 | unsigned int a = 0x4223; | ||
127 | gcry_mpi_t x; | ||
128 | |||
129 | x = gcry_mpi_new (0); | ||
130 | gcry_mpi_set_ui (x, a); | ||
131 | check_one (x); | ||
132 | gcry_mpi_release (x); | ||
133 | } | ||
134 | |||
135 | int | ||
136 | main (int argc, char **argv) | ||
137 | { | ||
138 | int debug = 0; | ||
139 | int i = 1; | ||
140 | |||
141 | if (argc > 1 && !strcmp (argv[1], "--verbose")) | ||
142 | verbose = 1; | ||
143 | else if (argc > 1 && !strcmp (argv[1], "--debug")) | ||
144 | verbose = debug = 1; | ||
145 | |||
146 | gcry_control (GCRYCTL_DISABLE_SECMEM, 0); | ||
147 | if (!gcry_check_version (GCRYPT_VERSION)) | ||
148 | die ("version mismatch\n"); | ||
149 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | ||
150 | if (debug) | ||
151 | gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0); | ||
152 | |||
153 | for (; i > 0; i--) | ||
154 | check_run (); | ||
155 | |||
156 | return 0; | ||
157 | } | ||