NoPaste Service
DOWNLOAD
Language: C
Author: Matrix86
Description: Libgcrypt Asymmetric Cryptography
Date: 05/09/08 16:30
  1. /*
  2.  *      Libgcrypt example (Asimmetric Cryptography)
  3.  *              This simple software is a demostration program that use libgcrypt
  4.  *              library with RSA algorithm
  5.  *      
  6.  *      Matrix86 of TuxMeaLux.net
  7.  *              matrix86 [.at.] tuxmealux [.dot.] net
  8.  *      
  9.  *      This program is free software; you can redistribute it and/or modify
  10.  *      it under the terms of the GNU General Public License as published by
  11.  *      the Free Software Foundation; either version 2 of the License, or
  12.  *      (at your option) any later version.
  13.  *      
  14.  *      This program is distributed in the hope that it will be useful,
  15.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *      GNU General Public License for more details.
  18.  *      
  19.  *      You should have received a copy of the GNU General Public License
  20.  *      along with this program; if not, write to the Free Software
  21.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22.  *      MA 02110-1301, USA.
  23.  */
  24.  
  25.  
  26. #include <stdio.h>
  27. #include <gcrypt.h>
  28. #include <assert.h>
  29.  
  30. #define SIZE 6000
  31.  
  32. //plain text -> mpi -> s-expression -> crypt text
  33.  
  34. void
  35. die( char *str ){
  36.         fprintf( stderr, "[ERROR] %s\n",str );
  37.         exit(1);
  38. }
  39.  
  40. void
  41. charTosexp( char *plain, gcry_sexp_t *s_exp ){
  42.         gcry_mpi_t plain_mpi;
  43.        
  44.         if( gcry_mpi_scan( &plain_mpi, GCRYMPI_FMT_USG, plain, strlen(plain), NULL ) ) die( " during char->mpi conversion." );
  45.         if( gcry_sexp_build( s_exp, NULL, "(data(flags raw)(value %m))", plain_mpi ) ) die( " during mpi->s-expression conversion." );
  46.        
  47.         gcry_mpi_release( plain_mpi );
  48.        
  49.         return;
  50. }
  51.  
  52. void
  53. sexpToChar( gcry_sexp_t s_exp, char * txtexp, int lenght ){
  54.         if( gcry_sexp_sprint( s_exp, GCRYSEXP_FMT_DEFAULT, txtexp, lenght ) == 0 ) die( "errore conversione testo" );
  55. }
  56.  
  57.  
  58. void
  59. generate_key( gcry_sexp_t *pkey, gcry_sexp_t *skey )
  60. {
  61.   gcry_sexp_t key_spec, key, pub_key, sec_key;
  62.   int rc;
  63.  
  64.   rc = gcry_sexp_new( &key_spec, "(genkey (rsa (nbits 4:2048)))", 0, 1 );
  65.   if( rc ) die( "error creating S-expression." );
  66.  
  67.   rc = gcry_pk_genkey( &key, key_spec );
  68.   gcry_sexp_release( key_spec );
  69.   if( rc ) die( "error generating RSA key." );
  70.    
  71.   pub_key = gcry_sexp_find_token( key, "public-key", 0 );
  72.   if ( !pub_key ) die( "public part missing in key." );
  73.  
  74.   sec_key = gcry_sexp_find_token( key, "private-key", 0 );
  75.   if ( !sec_key ) die( "private part missing in key." );
  76.  
  77.   gcry_sexp_release( key );
  78.   *pkey = pub_key;
  79.   *skey = sec_key;
  80. }
  81.  
  82. void
  83. printSexp( char *text, int maxlength ) {
  84.         int i;
  85.        
  86.         for( i=0; i < maxlength; i++ ) {
  87.                 if( text[i] == ')' && text[i+1] == '\n' && text[i+2] == '\0' ) break;
  88.                 if( text[i] == '\0' ) text[i] = ' ';
  89.         }
  90.        
  91.         return;
  92. }
  93.  
  94. int
  95. main( int argc, char** argv )
  96. {
  97.         int err;
  98.         int i = 0;
  99.         char *version;
  100.         gcry_sexp_t data_decrypted = NULL;
  101.        
  102.         gcry_sexp_t p_key, s_key, plain_sexp, crypt_sexp;
  103.        
  104.         char plain[300], cryptext[SIZE], decrypt[SIZE], buffer[SIZE];
  105.        
  106.         memset( plain, '\0', sizeof(plain) );
  107.         memset( cryptext, '\0', sizeof(cryptext) );
  108.         memset( decrypt, '\0', sizeof(decrypt) );
  109.        
  110.         gcry_control( GCRYCTL_DISABLE_SECMEM );
  111.         version = strdup( gcry_check_version(NULL) );
  112.         printf( "Libgcrypt Test.\nVersion: %s\n", version );
  113.        
  114.         gcry_control( GCRYCTL_ENABLE_QUICK_RANDOM, 0 );
  115.        
  116.         printf( "Generazione chiavi pubblica e privata.\n" );
  117.         // Generation of public and private keys
  118.         generate_key( &p_key, &s_key );
  119.        
  120.         sexpToChar( p_key, buffer, SIZE );
  121.         printSexp( buffer, SIZE );
  122.         printf( "Chiave pubblica:\n%s\n\n", buffer );
  123.        
  124.         sexpToChar( s_key, buffer, SIZE );
  125.         printSexp( buffer, SIZE );
  126.         printf( "Chiave privata:\n%s\n\n", buffer );
  127.        
  128.         printf( "Inserisci la stringa da codificare: " );
  129.         while( ( plain[i] = getchar() ) != '\n' ){
  130.                 i++;
  131.         }
  132.        
  133.         charTosexp( plain, &plain_sexp ); // From string to s-expression
  134.        
  135.         if( ( err = gcry_pk_encrypt( &crypt_sexp, plain_sexp, p_key ) ) ) die( "during the encryption fase." );
  136.        
  137.         sexpToChar( crypt_sexp, cryptext, SIZE ); // From s-expression to string
  138.         printSexp( cryptext, SIZE );
  139.         printf( "Testo cifrato: \n%s\n\n", cryptext );
  140.        
  141.         if( (  gcry_pk_decrypt( &data_decrypted, crypt_sexp, s_key ) ) ) die( "during the decryption fase." );
  142.        
  143.         sexpToChar( data_decrypted, decrypt, SIZE );
  144.         printf( "Testo in chiaro: \n%s\n\n", decrypt );
  145.        
  146.         gcry_sexp_release( s_key );
  147.         gcry_sexp_release( p_key );
  148.         gcry_sexp_release( crypt_sexp );
  149.         gcry_sexp_release( plain_sexp );
  150.        
  151.         return 0;
  152. }
  153.