1 /* testapi.c - for libgcrypt
2 * Copyright (C) 2000, 2002 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
26 #define BUG() do {fprintf ( stderr, "Ooops at %s:%d\n", __FILE__ , __LINE__ );\
29 /* an ElGamal public key */
33 "0x9D559F31A6D30492C383213844AEBB7772963A85D3239F3611AAB93A2A985F64FB735B9259EC326BF5720F909980D609D37C288C9223B0350FBE493C3B5AF54CA23031E952E92F8A3DBEDBC5A684993D452CD54F85B85160166FCD25BD7AB6AE9B1EB4FCC9D300DAFF081C4CBA6694906D3E3FF18196A5CCF7F0A6182962166B",
35 "0x9640024BB2A277205813FF685048AA27E2B192B667163E7C59E381E27003D044C700C531CE8FD4AA781B463BC9FFE74956AF09A38A098322B1CF72FC896F009E3A6BFF053D3B1D1E1994BF9CC07FA12963D782F027B51511DDE8C5F43421FBC12734A9C070F158C729A370BEE5FC51A772219438EDA8202C35FA3F5D8CD1997B"
39 test_sexp ( int argc, char **argv )
50 if ( gcry_mpi_scan( &key[0], GCRYMPI_FMT_HEX, elg_testkey1.p, 0, NULL ) )
52 if ( gcry_mpi_scan( &key[1], GCRYMPI_FMT_HEX, elg_testkey1.g, 0, NULL ) )
54 if ( gcry_mpi_scan( &key[2], GCRYMPI_FMT_HEX, elg_testkey1.y, 0, NULL ) )
57 /* get nbits from a key */
58 rc = gcry_sexp_build ( &sexp, NULL,
59 "(public-key(elg(p%m)(g%m)(y%m)))",
60 key[0], key[1], key[2] );
61 fprintf (stderr, "DUMP of PK (rc=%d):\n", rc);
62 gcry_sexp_dump ( sexp );
64 x = gcry_sexp_cdr ( sexp );
65 fputs ( "DUMP of CDR:\n", stderr );
67 gcry_sexp_release ( x );
69 nbits = gcry_pk_get_nbits( sexp );
70 printf ( "elg_testkey1 - nbits=%d\n", nbits );
71 n = gcry_sexp_sprint ( sexp, 0, NULL, 0 );
72 buf = gcry_xmalloc ( n );
73 n = gcry_sexp_sprint ( sexp, 0, buf, n );
74 printf ( "sprint length=%u\n", (unsigned int)n );
76 gcry_sexp_release( sexp );
81 test_genkey ( int argc, char **argv )
84 gcry_sexp_t s_parms, s_key;
89 gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 );
90 rc = gcry_sexp_build ( &s_parms, NULL, "(genkey(dsa(nbits %d)))", nbits );
91 rc = gcry_pk_genkey( &s_key, s_parms );
93 fprintf ( stderr, "genkey failed: %s\n", gpg_strerror (rc) );
96 gcry_sexp_release( s_parms );
97 gcry_sexp_dump ( s_key );
98 gcry_sexp_release( s_key );
102 main( int argc, char **argv )
108 s = gcry_check_version (NULL);
109 printf("%s\n", s? s : "(null)");
111 else if ( !strcmp ( argv[1], "version") )
113 s = gcry_check_version (argc > 2 ? argv[2] : NULL );
114 printf("%s\n", s? s : "(null)");
116 else if ( !strcmp ( argv[1], "sexp" ) )
117 test_sexp ( argc-2, argv+2 );
118 else if ( !strcmp ( argv[1], "genkey" ) )
119 test_genkey ( argc-2, argv+2 );
121 fprintf (stderr, "usage: testapi mode-string [mode-args]\n");