1/* pubkey.c - Public key encryption/decryption tests 2 * Copyright (C) 2003, 2005 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#ifdef HAVE_CONFIG_H 22#include <config.h> 23#endif 24#include <stdarg.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <assert.h> 29 30#include "../src/gcrypt.h" 31 32static int verbose; 33 34static void 35die (const char *format, ...) 36{ 37 va_list arg_ptr ; 38 39 va_start( arg_ptr, format ) ; 40 vfprintf (stderr, format, arg_ptr ); 41 va_end(arg_ptr); 42 exit (1); 43} 44 45void 46key_copy (gcry_ac_handle_t handle, 47 gcry_ac_key_type_t type, 48 gcry_ac_key_t *key_cp, gcry_ac_key_t key) 49{ 50 gcry_error_t err = 0; 51 52 err = gcry_ac_key_init (key_cp, handle, type, 53 gcry_ac_key_data_get (key)); 54 55 assert (! err); 56} 57 58void 59check_one (gcry_mpi_t x) 60{ 61 gcry_ac_handle_t handle; 62 gcry_ac_key_pair_t key_pair; 63 gcry_ac_key_t key_sec, key_sec_cp, key_pub, key_pub_cp; 64 gcry_error_t err = 0; 65 gcry_mpi_t x2; 66 gcry_ac_data_t data, data2; 67 gcry_ac_key_spec_rsa_t rsa_spec; 68 69 rsa_spec.e = gcry_mpi_new (0); 70 gcry_mpi_set_ui (rsa_spec.e, 1); 71 72 err = gcry_ac_open (&handle, GCRY_AC_RSA, 0); 73 assert (! err); 74 75 err = gcry_ac_key_pair_generate (handle, 1024, &rsa_spec, &key_pair, NULL); 76 assert (! err); 77 78 key_sec = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_SECRET); 79 key_copy (handle, GCRY_AC_KEY_SECRET, &key_sec_cp, key_sec); 80 81 key_pub = gcry_ac_key_pair_extract (key_pair, GCRY_AC_KEY_PUBLIC); 82 key_copy (handle, GCRY_AC_KEY_PUBLIC, &key_pub_cp, key_pub); 83 84 err = gcry_ac_data_encrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_pub_cp, x, &data); 85 assert (! err); 86 87 err = gcry_ac_data_decrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key_sec_cp, &x2, data); 88 assert (! err); 89 90 assert (! gcry_mpi_cmp (x, x2)); 91 92 gcry_ac_data_destroy (data); 93 94 err = gcry_ac_data_sign (handle, key_sec, x, &data); 95 assert (! err); 96 err = gcry_ac_data_copy (&data2, data); 97 assert (! err); 98 gcry_ac_data_destroy (data); 99 err = gcry_ac_data_copy (&data, data2); 100 assert (! err); 101 gcry_ac_data_destroy (data2); 102 103 err = gcry_ac_data_verify (handle, key_pub, x, data); 104 assert (! err); 105 106 gcry_ac_data_destroy (data); 107 108 err = gcry_ac_data_sign (handle, key_sec, x, &data); 109 assert (! err); 110 { 111 const char *label; 112 gcry_mpi_t y; 113 114 err = gcry_ac_data_get_index (data, 0, 0, &label, &y); 115 assert (! err); 116 gcry_mpi_add_ui (y, y, 1); 117 118 err = gcry_ac_data_verify (handle, key_pub, x, data); 119 assert (gcry_err_code (err) == GPG_ERR_BAD_SIGNATURE); 120 } 121 122 gcry_ac_close (handle); 123} 124 125void 126check_run (void) 127{ 128 /*const char *s = "All Hail Discordia."; -- not used */ 129 unsigned int a = 0x4223; 130 gcry_mpi_t x; 131 132 x = gcry_mpi_new (0); 133 gcry_mpi_set_ui (x, a); 134 check_one (x); 135 gcry_mpi_release (x); 136} 137 138int 139main (int argc, char **argv) 140{ 141 int debug = 0; 142 int i = 1; 143 144 if (argc > 1 && !strcmp (argv[1], "--verbose")) 145 verbose = 1; 146 else if (argc > 1 && !strcmp (argv[1], "--debug")) 147 verbose = debug = 1; 148 149 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 150 if (!gcry_check_version (GCRYPT_VERSION)) 151 die ("version mismatch\n"); 152 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 153 if (debug) 154 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0); 155 /* No valuable keys are create, so we can speed up our RNG. */ 156 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); 157 158 for (; i > 0; i--) 159 check_run (); 160 161 return 0; 162} 163