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
27 #include "../src/gcrypt.h"
32 die (const char *format
, ...)
36 va_start( arg_ptr
, format
) ;
37 vfprintf (stderr
, format
, arg_ptr
);
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
)
49 err
= gcry_ac_key_init (key_cp
, handle
, type
,
50 gcry_ac_key_data_get (key
));
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
;
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);
72 err
= gcry_ac_key_pair_generate (handle
, &key_pair
, 1024, (void *) &rsa_spec
);
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
);
85 err
= gcry_ac_data_decrypt (handle
, GCRY_AC_FLAG_DATA_NO_BLINDING
,
86 key_sec_cp
, &x2
, data
);
89 assert (! gcry_mpi_cmp (x
, x2
));
91 gcry_ac_data_destroy (data
);
93 err
= gcry_ac_data_sign (handle
, key_sec
, x
, &data
);
95 err
= gcry_ac_data_copy (&data2
, data
);
97 gcry_ac_data_destroy (data
);
98 err
= gcry_ac_data_copy (&data
, data2
);
100 gcry_ac_data_destroy (data2
);
102 err
= gcry_ac_data_verify (handle
, key_pub
, x
, data
);
105 gcry_ac_data_destroy (data
);
107 err
= gcry_ac_data_sign (handle
, key_sec
, x
, &data
);
113 err
= gcry_ac_data_get_index (data
, 0, &label
, &y
);
115 gcry_mpi_add_ui (y
, y
, 1);
117 err
= gcry_ac_data_set (data
, label
, y
);
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
);
130 const char *s
= "All Hail Discordia.";
131 unsigned int a
= 0x4223;
134 x
= gcry_mpi_new (0);
135 gcry_mpi_set_ui (x
, a
);
137 gcry_mpi_release (x
);
141 main (int argc
, char **argv
)
146 if (argc
> 1 && !strcmp (argv
[1], "--verbose"))
148 else if (argc
> 1 && !strcmp (argv
[1], "--debug"))
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);
156 gcry_control (GCRYCTL_SET_DEBUG_FLAGS
, 1u , 0);