Пример #1
0
/*
===============
SV_RSAGenMsg

Generate an encrypted RSA message
===============
*/
int SV_RSAGenMsg( const char *pubkey, char *cleartext, char *encrypted ) {
	struct        rsa_public_key public_key;
	mpz_t         message;
	unsigned char buffer[ RSA_KEY_LENGTH / 8 - 11 ];
	int           retval;

	Com_RandomBytes( buffer, RSA_KEY_LENGTH / 8 - 11 );

	nettle_mpz_init_set_str_256_u( message, RSA_KEY_LENGTH / 8 - 11, buffer );
	mpz_get_str( cleartext, 16, message );
	rsa_public_key_init( &public_key );
	mpz_set_ui( public_key.e, RSA_PUBLIC_EXPONENT );
	retval = mpz_set_str( public_key.n, pubkey, 16 ) + 1;
	
	if ( retval ) {
		rsa_public_key_prepare( &public_key );
		retval = rsa_encrypt( &public_key, NULL, qnettle_random, RSA_KEY_LENGTH / 8 - 11, buffer, message );
	}

	rsa_public_key_clear( &public_key );
	mpz_get_str( encrypted, 16, message );
	mpz_clear( message );

	return retval;
}
int
R_VerifyFinal(R_SIGNATURE_CTX *ctx,
              uint8_t *signature,
              unsigned length,
              R_RSA_PUBLIC_KEY *key)
{
  struct rsa_public_key k;
  int res;

  nettle_mpz_init_set_str_256_u(k.n,
				MAX_RSA_MODULUS_LEN, key->modulus);
  nettle_mpz_init_set_str_256_u(k.e,
				MAX_RSA_MODULUS_LEN, key->exponent);
  
  if (rsa_public_key_prepare(&k) && (k.size == length))
    {
      mpz_t s;
  
      nettle_mpz_init_set_str_256_u(s,
				    k.size, signature);
      res = rsa_md5_verify(&k, &ctx->hash, s)
	? RE_SUCCESS : RE_SIGNATURE;

      mpz_clear(s);
    }
  else
    res = RE_PUBLIC_KEY;

  mpz_clear(k.n);
  mpz_clear(k.e);

  return res;
}
Пример #3
0
int
rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
				  struct rsa_private_key *priv,
				  unsigned limit,
				  struct asn1_der_iterator *i)
{
  /* RSAPrivateKey ::= SEQUENCE {
         version           Version,
	 modulus           INTEGER,  -- n
	 publicExponent    INTEGER,  -- e
	 privateExponent   INTEGER,  -- d
	 prime1            INTEGER,  -- p
	 prime2            INTEGER,  -- q
	 exponent1         INTEGER,  -- d mod (p-1)
	 exponent2         INTEGER,  -- d mod (q-1)
	 coefficient       INTEGER,  -- (inverse of q) mod p
	 otherPrimeInfos   OtherPrimeInfos OPTIONAL
    }
  */

  uint32_t version;
  
  if (i->type != ASN1_SEQUENCE)
    return 0;

  if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
      && i->type == ASN1_INTEGER
      && asn1_der_get_uint32(i, &version)
      && version <= 1
      && GET(i, pub->n, limit)
      && GET(i, pub->e, limit)
      && rsa_public_key_prepare(pub)
      && GET(i, priv->d, limit)
      && GET(i, priv->p, limit)
      && GET(i, priv->q, limit)
      && GET(i, priv->a, limit)
      && GET(i, priv->b, limit)
      && GET(i, priv->c, limit)
      && rsa_private_key_prepare(priv))
    {
      if (version == 1)
	{
	  /* otherPrimeInfos must be present. We ignore the contents */
	  if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
		&& i->type == ASN1_SEQUENCE))
	    return 0;
	}

      return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
    }
  
  return 0;
}
Пример #4
0
int rsa_sha1_verify_fd(libsign_public_key *pub_ctx, libsign_signature *sig_ctx,
                       int fd)
{
    /* hash the data from the given fd and verify the result */
    int ret = -EINVAL;
    int num = 0;
    uint8_t buffer[512];
    struct sha1_ctx hash;
    struct rsa_public_key key;

    rsa_public_key_init(&key);

    mpz_set(key.n, pub_ctx->n);
    mpz_set(key.e, pub_ctx->e);

    rsa_public_key_prepare(&key);

    /* hash the data */
    sha1_init(&hash);
    while((num = read(fd, buffer, 512)) > 0)
        sha1_update(&hash, num, buffer);

    if(num < 0)
        goto exit;

    /* hash the hashed data from the signature */
    sha1_update(&hash, sig_ctx->hashed_data_len,
                sig_ctx->hashed_data);

    /* then hash the trailer */
    if(sig_ctx->version == PGP_SIG_VER4) {
        uint8_t trailer[6];
        /* version */
        trailer[0] = 0x04;

        trailer[1] = 0xff;

        /* big-endian length of the hashed data from
           the signature */
        trailer[5] = sig_ctx->hashed_data_len;
        trailer[4] = sig_ctx->hashed_data_len >> 8;
        trailer[3] = sig_ctx->hashed_data_len >> 16;
        trailer[2] = sig_ctx->hashed_data_len >> 24;

        sha1_update(&hash, 6, trailer);
    }
Пример #5
0
int
rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
				 unsigned limit,
				 struct asn1_der_iterator *i)
{
  /* RSAPublicKey ::= SEQUENCE {
         modulus           INTEGER,  -- n
	 publicExponent    INTEGER   -- e
      }
  */

  return (i->type == ASN1_SEQUENCE
	  && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
	  && asn1_der_get_bignum(i, pub->n, limit) 
	  && mpz_sgn(pub->n) > 0
	  && GET(i, pub->e, limit)
	  && asn1_der_iterator_next(i) == ASN1_ITERATOR_END
	  && rsa_public_key_prepare(pub));
}
Пример #6
0
int
rsa_keypair_from_sexp_alist(struct rsa_public_key *pub,
			    struct rsa_private_key *priv,
			    unsigned limit,
			    struct sexp_iterator *i)
{
  static const uint8_t * const names[8]
    = { "n", "e", "d", "p", "q", "a", "b", "c" };
  struct sexp_iterator values[8];
  unsigned nvalues = priv ? 8 : 2;
  
  if (!sexp_iterator_assoc(i, nvalues, names, values))
    return 0;

  if (priv)
    {
      GET(priv->d, limit, &values[2]);
      GET(priv->p, limit, &values[3]);
      GET(priv->q, limit, &values[4]);
      GET(priv->a, limit, &values[5]);
      GET(priv->b, limit, &values[6]);
      GET(priv->c, limit, &values[7]);

      if (!rsa_private_key_prepare(priv))
	return 0;
    }

  if (pub)
    {
      GET(pub->n, limit, &values[0]);
      GET(pub->e, limit, &values[1]);

      if (!rsa_public_key_prepare(pub))
	return 0;
    }
  
  return 1;
}
Пример #7
0
int
test_main(void)
{
  struct rsa_public_key pub;
  struct rsa_private_key key;

  mpz_t expected;
  
  mpz_init(expected);
  
  rsa_private_key_init(&key);
  rsa_public_key_init(&pub);

  test_rsa_set_key_1(&pub, &key);
  
  /* Test md5 signatures */
  mpz_set_str(expected,
	      "53bf517009fa956e" "3daa6adc95e8663d" "3759002f488bbbad"
	      "e49f62792d85dbcc" "293f68e2b68ef89a" "c5bd42d98f845325"
	      "3e6c1b76fc337db5" "e0053f255c55faf3" "eb6cc568ad7f5013"
	      "5b269a64acb9eaa7" "b7f09d9bd90310e6" "4c58f6dbe673ada2"
	      "67c97a9d99e19f9d" "87960d9ce3f0d5ce" "84f401fe7e10fa24"
	      "28b9bffcf9", 16);

  test_rsa_md5(&pub, &key, expected);

  /* Test sha1 signature */
  mpz_set_str(expected,
	      "129b405ed85db88c" "55d35344c4b52854" "496516b4d63d8211"
	      "80a0c24d6ced9047" "33065a564bbd33d0" "a5cdfd204b9c6d15"
	      "78337207c2f1662d" "c73906c7a0f2bf5c" "af92cef9121957b1"
	      "dcb111ff47b92389" "888e384d0cfd1b1e" "e5d7003a8feff3fd"
	      "dd6a71d242a79272" "25234d67ba369441" "c12ae555c697754e"
	      "a17f93fa92", 16);

  test_rsa_sha1(&pub, &key, expected);

  mpz_set_str(expected,
	      "13f9e43f7a401a73" "0a74985c01520d76" "bf5f2e2dff91e93b"
	      "9267d8c388d6937b" "d4bc6f1fa31618a9" "b5e3a1a875af72f5"
	      "0e805dbfebdf4348" "7d49763f0b365e78" "d2c0ea8fb3785897"
	      "782289a58f998907" "248c9cdf2c643d7e" "6ba6b55026227773"
	      "6f19caa69c4fc6d7" "7e2e5d4cd6b7a82b" "900d201ffd000448"
	      "685e5a4f3e", 16);

  test_rsa_sha256(&pub, &key, expected);
  
  /* 777-bit key, generated by
   *
   *   lsh-keygen -a rsa -l 777 -f advanced-hex
   *
   * Interesting because the size of n doesn't equal the sum of the
   * sizes of p and q. 
   *
   * (private-key (rsa-pkcs1
   *        (n #013b04440e3eef25 d51c738d508a7fa8 b3445180c342af0f
   *            4cb5a789047300e2 cfc5c5450974cfc2 448aeaaa7f43c374
   *            c9a3b038b181f2d1 0f1a2327fd2c087b a49bf1086969fd2c
   *            d1df3fd69f81fa4b 162cc8bbb363fc95 b7b24b9c53d0c67e
   *            f52b#)
   *        (e #3f1a012d#)
   *        (d #f9bae89dacca6cca c21e0412b4df8355 6fe7c5322bbae8ad
   *            3f11494fd12bc076 d4a7da3050fe109d 2074db09cc6a93b4
   *            745479522558379e a0ddfa74f86c9e9e a22c3b0e93d51447
   *            0feb38105dd35395 63b91ee32776f40c 67b2a175690f7abb
   *            25#)
   *        (p #0b73c990eeda0a2a 2c26416052c85560 0c5c0f5ce86a8326
   *            166acea91786237a 7ff884e66dbfdd3a ab9d9801414c1506
   *            8b#)
   *        (q #1b81c19a62802a41 9c99283331b0badb 08eb0c25ffce0fbf
   *            50017850036f32f3 2132a845b91a5236 61f7b451d587383f
   *            e1#)
   *        (a #0a912fc93a6cca6b 3521725a3065b3be 3c9745e29c93303d
   *            7d29316c6cafa4a2 89945f964fcdea59 1f9d248b0b6734be
   *            c9#)
   *        (b #1658eca933251813 1eb19c77aba13d73 e0b8f4ce986d7615
   *            764c6b0b03c18146 46b7f332c43e05c5 351e09006979ca5b
   *            05#)
   *        (c #0114720dace7b27f 2bf2850c1804869f 79a0aad0ec02e6b4
   *            05e1831619db2f10 bb9b6a8fd5c95df2 eb78f303ea0c0cc8
   *            06#)))
   */
  
  mpz_set_str(pub.n,
	      "013b04440e3eef25" "d51c738d508a7fa8" "b3445180c342af0f"
	      "4cb5a789047300e2" "cfc5c5450974cfc2" "448aeaaa7f43c374"
	      "c9a3b038b181f2d1" "0f1a2327fd2c087b" "a49bf1086969fd2c"
	      "d1df3fd69f81fa4b" "162cc8bbb363fc95" "b7b24b9c53d0c67e"
	      "f52b", 16);
  
  mpz_set_str(pub.e, "3f1a012d", 16);

  if (!rsa_public_key_prepare(&pub))
    FAIL();

#if 0  
  mpz_set_str(key.d,
	      "f9bae89dacca6cca" "c21e0412b4df8355" "6fe7c5322bbae8ad"
	      "3f11494fd12bc076" "d4a7da3050fe109d" "2074db09cc6a93b4"
	      "745479522558379e" "a0ddfa74f86c9e9e" "a22c3b0e93d51447"
	      "0feb38105dd35395" "63b91ee32776f40c" "67b2a175690f7abb"
	      "25", 16);
#endif
  
  mpz_set_str(key.p,
	      "0b73c990eeda0a2a" "2c26416052c85560" "0c5c0f5ce86a8326"
	      "166acea91786237a" "7ff884e66dbfdd3a" "ab9d9801414c1506"
	      "8b", 16);
  
  mpz_set_str(key.q,
	      "1b81c19a62802a41" "9c99283331b0badb" "08eb0c25ffce0fbf"
	      "50017850036f32f3" "2132a845b91a5236" "61f7b451d587383f"
	      "e1", 16);
  
  mpz_set_str(key.a,
	      "0a912fc93a6cca6b" "3521725a3065b3be" "3c9745e29c93303d"
	      "7d29316c6cafa4a2" "89945f964fcdea59" "1f9d248b0b6734be"
	      "c9", 16);
  
  mpz_set_str(key.b,
	      "1658eca933251813" "1eb19c77aba13d73" "e0b8f4ce986d7615"
	      "764c6b0b03c18146" "46b7f332c43e05c5" "351e09006979ca5b"
	      "05", 16);
  
  mpz_set_str(key.c,
	      "0114720dace7b27f" "2bf2850c1804869f" "79a0aad0ec02e6b4"
	      "05e1831619db2f10" "bb9b6a8fd5c95df2" "eb78f303ea0c0cc8"
	      "06", 16);

  if (!rsa_private_key_prepare(&key))
    FAIL();

  if (pub.size != key.size)
    FAIL();

  /* Test md5 signatures */
  mpz_set_str(expected,
	      "011b939f6fbacf7f" "7d3217b022d07477" "e582e34d4bbddd4c"
	      "31520647417fc8a6" "18b2e196d799cedd" "d8f5c062fd796b0f"
	      "72ab46db2ac6ec74" "39d856be3f746cc4" "3e0a15429954736a"
	      "60a8b3c6ea93d2cb" "c69085c307d72517" "07d43bf97a3b51eb"
	      "9e89", 16);

  test_rsa_md5(&pub, &key, expected);

  /* Test sha1 signature */
  mpz_set_str(expected,
	      "648c49e0ed045547" "08381d0bcd03b7bd" "b0f80a0e9030525d"
	      "234327a1c96b8660" "f1c01c6f15ae76d0" "4f53a53806b7e4db"
	      "1f789e6e89b538f6" "88fcbd2caa6abef0" "5432d52f3de463a4"
	      "a9e6de94f1b7bb68" "3c07edf0924fc93f" "56e1a0dba8f7491c"
	      "5c", 16);

  test_rsa_sha1(&pub, &key, expected);

  mpz_set_str(expected,
	      "d759bb28b4d249a2" "f8b67bdbb1ab7f50" "c88712fbcabc2956"
	      "1ec6ca3f8fdafe7a" "38433d7da287b8f7" "87857274c1640b2b"
	      "e652cd89c501d570" "3980a0af5c6bb60c" "f84feab25b099d06"
	      "e2519accb73dac43" "fb8bdad28835f3bd" "84c43678fe2ef41f"
	      "af", 16);

  test_rsa_sha256(&pub, &key, expected);

  rsa_private_key_clear(&key);
  rsa_public_key_clear(&pub);
  mpz_clear(expected);

  SUCCESS();
}
Пример #8
0
void
test_rsa_set_key_1(struct rsa_public_key *pub,
		   struct rsa_private_key *key)
{
  /* Initialize key pair for test programs */
  /* 1000-bit key, generated by
   *
   *   lsh-keygen -a rsa -l 1000 -f advanced-hex
   *
   * (private-key (rsa-pkcs1 
   *        (n #69abd505285af665 36ddc7c8f027e6f0 ed435d6748b16088
   *            4fd60842b3a8d7fb bd8a3c98f0cc50ae 4f6a9f7dd73122cc
   *            ec8afa3f77134406 f53721973115fc2d 8cfbba23b145f28d
   *            84f81d3b6ae8ce1e 2850580c026e809b cfbb52566ea3a3b3
   *            df7edf52971872a7 e35c1451b8636d22 279a8fb299368238
   *            e545fbb4cf#)
   *        (e #0db2ad57#)
   *        (d #3240a56f4cd0dcc2 4a413eb4ea545259 5c83d771a1c2ba7b
   *            ec47c5b43eb4b374 09bd2aa1e236dd86 481eb1768811412f
   *            f8d91be3545912af b55c014cb55ceac6 54216af3b85d5c4f
   *            4a32894e3b5dfcde 5b2875aa4dc8d9a8 6afd0ca92ef50d35
   *            bd09f1c47efb4c8d c631e07698d362aa 4a83fd304e66d6c5
   *            468863c307#)
   *        (p #0a66399919be4b4d e5a78c5ea5c85bf9 aba8c013cb4a8732
   *            14557a12bd67711e bb4073fd39ad9a86 f4e80253ad809e5b
   *            f2fad3bc37f6f013 273c9552c9f489#)
   *        (q #0a294f069f118625 f5eae2538db9338c 776a298eae953329
   *            9fd1eed4eba04e82 b2593bc98ba8db27 de034da7daaea795
   *            2d55b07b5f9a5875 d1ca5f6dcab897#)
   *        (a #011b6c48eb592eee e85d1bb35cfb6e07 344ea0b5e5f03a28
   *            5b405396cbc78c5c 868e961db160ba8d 4b984250930cf79a
   *            1bf8a9f28963de53 128aa7d690eb87#)
   *        (b #0409ecf3d2557c88 214f1af5e1f17853 d8b2d63782fa5628
   *            60cf579b0833b7ff 5c0529f2a97c6452 2fa1a8878a9635ab
   *            ce56debf431bdec2 70b308fa5bf387#)
   *        (c #04e103ee925cb5e6 6653949fa5e1a462 c9e65e1adcd60058
   *            e2df9607cee95fa8 daec7a389a7d9afc 8dd21fef9d83805a
   *            40d46f49676a2f6b 2926f70c572c00#)))
   */
  
  mpz_set_str(pub->n,
	      "69abd505285af665" "36ddc7c8f027e6f0" "ed435d6748b16088"
	      "4fd60842b3a8d7fb" "bd8a3c98f0cc50ae" "4f6a9f7dd73122cc"
	      "ec8afa3f77134406" "f53721973115fc2d" "8cfbba23b145f28d"
	      "84f81d3b6ae8ce1e" "2850580c026e809b" "cfbb52566ea3a3b3"
	      "df7edf52971872a7" "e35c1451b8636d22" "279a8fb299368238"
	      "e545fbb4cf", 16);
  mpz_set_str(pub->e, "0db2ad57", 16);

  ASSERT (rsa_public_key_prepare(pub));
  
  /* d is not used */
#if 0  
  mpz_set_str(key->d,
	      "3240a56f4cd0dcc2" "4a413eb4ea545259" "5c83d771a1c2ba7b"
	      "ec47c5b43eb4b374" "09bd2aa1e236dd86" "481eb1768811412f"
	      "f8d91be3545912af" "b55c014cb55ceac6" "54216af3b85d5c4f"
	      "4a32894e3b5dfcde" "5b2875aa4dc8d9a8" "6afd0ca92ef50d35"
	      "bd09f1c47efb4c8d" "c631e07698d362aa" "4a83fd304e66d6c5"
	      "468863c307", 16);
#endif
  
  mpz_set_str(key->p,
	      "0a66399919be4b4d" "e5a78c5ea5c85bf9" "aba8c013cb4a8732"
	      "14557a12bd67711e" "bb4073fd39ad9a86" "f4e80253ad809e5b"
	      "f2fad3bc37f6f013" "273c9552c9f489", 16);

  mpz_set_str(key->q,
	      "0a294f069f118625" "f5eae2538db9338c" "776a298eae953329"
	      "9fd1eed4eba04e82" "b2593bc98ba8db27" "de034da7daaea795"
	      "2d55b07b5f9a5875" "d1ca5f6dcab897", 16);
  
  mpz_set_str(key->a,
	      "011b6c48eb592eee" "e85d1bb35cfb6e07" "344ea0b5e5f03a28"
	      "5b405396cbc78c5c" "868e961db160ba8d" "4b984250930cf79a"
	      "1bf8a9f28963de53" "128aa7d690eb87", 16);
  
  mpz_set_str(key->b,
	      "0409ecf3d2557c88" "214f1af5e1f17853" "d8b2d63782fa5628"
	      "60cf579b0833b7ff" "5c0529f2a97c6452" "2fa1a8878a9635ab"
	      "ce56debf431bdec2" "70b308fa5bf387", 16);
  
  mpz_set_str(key->c,
	      "04e103ee925cb5e6" "6653949fa5e1a462" "c9e65e1adcd60058"
	      "e2df9607cee95fa8" "daec7a389a7d9afc" "8dd21fef9d83805a"
	      "40d46f49676a2f6b" "2926f70c572c00", 16);

  ASSERT (rsa_private_key_prepare(key));
  ASSERT (pub->size == key->size);
}
Пример #9
0
int main(int argc, char **argv)
{
	FILE *input = stdin;

	RSA *openssl_key = NULL;
	struct rsa_private_key *nettle_key = NULL;
	struct rsa_public_key *nettle_pub_key = NULL;
	struct nettle_buffer *sexp_buffer = NULL;

	OpenSSL_add_all_algorithms();
	
	openssl_key = PEM_read_RSAPrivateKey(input, NULL, NULL, NULL);

	if(!openssl_key)
	{
		fprintf(stderr, "OpenSSL failed to read key: %s\n", ERR_error_string(ERR_get_error(), NULL));
		return 1;
	}

# ifdef DEBUG
	fprintf(stderr, "OpenSSL RSA private key:\n");
	fprintf(stderr, "\tPrivate Exponent: %s\n", BN_bn2hex(openssl_key->d)); // d
	fprintf(stderr, "\tPrime 1: %s\n", BN_bn2hex(openssl_key->p));          // p
	fprintf(stderr, "\tPrime 2: %s\n", BN_bn2hex(openssl_key->q));          // q
	fprintf(stderr, "\tExponent 1: %s\n", BN_bn2hex(openssl_key->dmp1));    // a
	fprintf(stderr, "\tExponent 2: %s\n", BN_bn2hex(openssl_key->dmq1));    // b
	fprintf(stderr, "\tCoefficient: %s\n", BN_bn2hex(openssl_key->iqmp));   // c
# endif

	nettle_key = (struct rsa_private_key*)malloc(sizeof(struct rsa_private_key));
	if(!nettle_key)
	{
		fprintf(stderr, "Failed to malloc %zu bytes\n", sizeof(struct rsa_private_key));
		return 4;
	}
	rsa_private_key_init(nettle_key);
	mpz_set_str(nettle_key->d, BN_bn2hex(openssl_key->d), 16);
	mpz_set_str(nettle_key->p, BN_bn2hex(openssl_key->p), 16);
	mpz_set_str(nettle_key->q, BN_bn2hex(openssl_key->q), 16);
	mpz_set_str(nettle_key->a, BN_bn2hex(openssl_key->dmp1), 16);
	mpz_set_str(nettle_key->b, BN_bn2hex(openssl_key->dmq1), 16);
	mpz_set_str(nettle_key->c, BN_bn2hex(openssl_key->iqmp), 16);

	if(rsa_private_key_prepare(nettle_key) != 1)
	{
		fprintf(stderr, "Nettle failed to prepare key\n");
		return 2;
	}

# ifdef DEBUG
	fprintf(stderr, "\tPrivate Exponent: %s\n", mpz_get_str(NULL, 16, nettle_key->d)); // d
	fprintf(stderr, "\tPrime 1: %s\n", mpz_get_str(NULL, 16, nettle_key->p));          // p
	fprintf(stderr, "\tPrime 2: %s\n", mpz_get_str(NULL, 16, nettle_key->q));          // q
	fprintf(stderr, "\tExponent 1: %s\n", mpz_get_str(NULL, 16, nettle_key->a));    // a
	fprintf(stderr, "\tExponent 2: %s\n", mpz_get_str(NULL, 16, nettle_key->b));    // b
	fprintf(stderr, "\tCoefficient: %s\n", mpz_get_str(NULL, 16, nettle_key->c));   // c
# endif

	nettle_pub_key = (struct rsa_public_key*)malloc(sizeof(struct rsa_public_key));
	if(!nettle_pub_key)
	{
		fprintf(stderr, "Failed to malloc %zu bytes\n", sizeof(struct rsa_public_key));
		return 4;
	}
	rsa_public_key_init(nettle_pub_key);
	mpz_set_str(nettle_pub_key->n, BN_bn2hex(openssl_key->n), 16);
	mpz_set_str(nettle_pub_key->e, BN_bn2hex(openssl_key->e), 16);
	
	if(rsa_public_key_prepare(nettle_pub_key) != 1)
	{
		fprintf(stderr, "Nettle failed to prepare public key\n");
		return 2;
	}

	sexp_buffer = (struct nettle_buffer*)malloc(sizeof(struct nettle_buffer));
	if(!sexp_buffer)
	{
		fprintf(stderr, "Failed to malloc %zu bytes\n", sizeof(struct nettle_buffer));
		return 4;
	}
	nettle_buffer_init(sexp_buffer);

	if(rsa_keypair_to_sexp(sexp_buffer, NULL, nettle_pub_key, nettle_key) == 0)
	{
		fprintf(stderr, "Nettle failed to export key to sexp\n");
		return 3;
	}

	size_t bytes_written = fwrite(sexp_buffer->contents, 1, sexp_buffer->size, stdout);
	if(bytes_written < sexp_buffer->size)
	{
		fprintf(stderr, "Failed to write sexp to stdout\n");
		return 5;
	}

	return 0;
}
int
test_main(void)
{
  struct rsa_public_key pub;
  struct rsa_private_key priv;

  struct nettle_buffer buffer;
  
  rsa_public_key_init(&pub);
  rsa_private_key_init(&priv);

  mpz_set_str(pub.n,
	      "085c3408989acae4faec3cbbad91c90d34c1d259cd74121a"
	      "36f38b0b51424a9b2be514a04377113a6cdafe79dd7d5f2e"
	      "cc8b5e9661189b86a7b22239907c25", 16);
  mpz_set_str(pub.e, "36ad4b1d", 16);

  ASSERT(rsa_public_key_prepare(&pub));
  
  mpz_set_str(priv.d,
	      "06ee6d4ff3c239e408150daf8117abfa36a40ad4455d9059"
	      "a86d52f33a2de07418a0a699594588c64810248c9412d554"
	      "f74af947c73c32007e87c92f0937ed", 16);

  mpz_set_str(priv.p,
	      "03259879b24315e9cf14254824c7935d807cdb6990f414a0"
	      "f65e6065130a611f", 16);

  mpz_set_str(priv.q,
	      "02a81ba73bad45fc73b36deffce52d1b73e0747f4d8a8264"
	      "8cecd310448ea63b", 16);

  mpz_set_str(priv.a,
	      "026cbdad5dd0046e093f060ecd5b4ac918e098b0278bb752"
	      "b7cadd6a8944f0b9", 16);

  mpz_set_str(priv.b,
	      "0148751e622d6d58e3bb094afd6edacf7370351d068e2ce9"
	      "f565c5528c4a7473", 16);

  mpz_set_str(priv.c,
	      "f8a458ea73a018dc6fa56863e3bc6de405f364f77dee6f09"
	      "62679ea1a8282e", 16);

  ASSERT(rsa_private_key_prepare(&priv));
  
  nettle_buffer_init(&buffer);
  ASSERT(rsa_keypair_to_sexp(&buffer, "rsa", &pub, &priv));

  if (verbose)
    {
      printf("private:");
      print_hex(buffer.size, buffer.contents);  
    }
  
  ASSERT(MEMEQH(buffer.size, buffer.contents,
		"2831313a707269766174652d6b657928"
		"333a72736128313a6e36333a085c3408"
		"989acae4faec3cbbad91c90d34c1d259"
		"cd74121a36f38b0b51424a9b2be514a0"
		"4377113a6cdafe79dd7d5f2ecc8b5e96"
		"61189b86a7b22239907c252928313a65"
		"343a36ad4b1d2928313a6436333a06ee"
		"6d4ff3c239e408150daf8117abfa36a4"
		"0ad4455d9059a86d52f33a2de07418a0"
		"a699594588c64810248c9412d554f74a"
		"f947c73c32007e87c92f0937ed292831"
		"3a7033323a03259879b24315e9cf1425"
		"4824c7935d807cdb6990f414a0f65e60"
		"65130a611f2928313a7133323a02a81b"
		"a73bad45fc73b36deffce52d1b73e074"
		"7f4d8a82648cecd310448ea63b292831"
		"3a6133323a026cbdad5dd0046e093f06"
		"0ecd5b4ac918e098b0278bb752b7cadd"
		"6a8944f0b92928313a6233323a014875"
		"1e622d6d58e3bb094afd6edacf737035"
		"1d068e2ce9f565c5528c4a7473292831"
		"3a6333323a00f8a458ea73a018dc6fa5"
		"6863e3bc6de405f364f77dee6f096267"
		"9ea1a8282e292929"));

  nettle_buffer_clear(&buffer);
  ASSERT(rsa_keypair_to_sexp(&buffer, NULL, &pub, NULL));

  if (verbose)
    {
      printf("public:");
      print_hex(buffer.size, buffer.contents);  
    }
  ASSERT(MEMEQH(buffer.size, buffer.contents,
		"2831303a7075626c69632d6b65792839"
		"3a7273612d706b63733128313a6e3633"
		"3a085c3408989acae4faec3cbbad91c9"
		"0d34c1d259cd74121a36f38b0b51424a"
		"9b2be514a04377113a6cdafe79dd7d5f"
		"2ecc8b5e9661189b86a7b22239907c25"
		"2928313a65343a36ad4b1d292929"));

  rsa_public_key_clear(&pub);
  rsa_private_key_clear(&priv);
  
  SUCCESS();
}