view rsa_export.c @ 154:b4c6e7f276e9 libtomcrypt

Makefile.in: don't compile things we don't need
author Matt Johnston <matt@ucc.asn.au>
date Mon, 20 Dec 2004 14:46:40 +0000
parents 5d99163f7e32
children
line wrap: on
line source

/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 *
 * LibTomCrypt is a library that provides various cryptographic
 * algorithms in a highly modular and flexible manner.
 *
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, [email protected], http://libtomcrypt.org
 */

#include "mycrypt.h"

#ifdef MRSA

/* This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] */
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key)
{
   int err;

   _ARGCHK(out    != NULL);
   _ARGCHK(outlen != NULL);
   _ARGCHK(key    != NULL);

   /* type valid? */
   if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
      return CRYPT_PK_INVALID_TYPE;
   }
  
   if (type == PK_PRIVATE) {
      /* private key */
      mp_int zero;

      /* first INTEGER == 0 to signify two-prime RSA */
      if ((err = mp_init(&zero)) != MP_OKAY) {
         return mpi_to_ltc_error(err);
      }
 
      /* output is 
            Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p
       */
      err = der_put_multi_integer(out, outlen, &zero, &key->N, &key->e,
                          &key->d, &key->p, &key->q, &key->dP,
                          &key->dQ, &key->qP, NULL);
 
      /* clear zero and return */
      mp_clear(&zero);
      return err;
   } else {
      /* public key */
      return der_put_multi_integer(out, outlen, &key->N, &key->e, NULL);
   }
}

#endif /* MRSA */