Public Key encryption

Timo Schulz twoaday@freakmail.de
Sat, 22 Jun 2002 17:17:02 +0200


On Sat Jun 22 2002; 16:38, Rüdiger Sonderfeld wrote:

>   gcry_sexp_build( &parms, NULL, "(genkey(rsa(nbits %d)))", bits);
>   ret=gcry_pk_genkey( key,parms );
>   gcry_sexp_release( parms );
>   return ret;

> now there should be a public and a private key at the adress of key. 

Right.

Okay, let me try with another code snippet from GnuPG 1.1.2a. I
would recommend you download this version because it contains a
lot of code how to convert the libgcrypt sexp expressions into
GCRY_MPI (GcryMPI) structs.

keygen.c:
rc = key_from_sexp( your_pkey_struct, key, "public-key", "ne" );

Sorry that it is so much code but otherwise it's difficult to read it.

--
key_from_sexp( GCRY_MPI *array,
               GCRY_SEXP sexp, const char *topname, const char *elems )
{
    GCRY_SEXP list, l2;
    const char *s;
    int i, idx;

    list = gcry_sexp_find_token( sexp, topname, 0 );
    if( !list )
        return GCRYERR_INV_OBJ;
    l2 = gcry_sexp_cadr( list );
    gcry_sexp_release ( list );
    list = l2;
    if( !list )
        return GCRYERR_NO_OBJ;
    idx = 0;
    for(s=elems; *s; s++, idx++ ) {
        l2 = gcry_sexp_find_token( list, s, 1 );
        if( !l2 ) {
            for(i=0; i<idx; i++) {
                gcry_free( array[i] );
                array[i] = NULL;
            }
            gcry_sexp_release ( list );
            return GCRYERR_NO_OBJ; /* required parameter not found */
        }
        array[idx] = gcry_sexp_nth_mpi( l2, 1, GCRYMPI_FMT_USG );
        gcry_sexp_release ( l2 );
        if( !array[idx] ) {
            for(i=0; i<idx; i++) {
                gcry_free( array[i] );
                array[i] = NULL;
            }
            gcry_sexp_release ( list );
            return GCRYERR_INV_OBJ; /* required parameter is invalid */
        }
    gcry_sexp_release ( list );

    return 0;
}
--

Hope this helps.


        Timo