Mercurial > dropbear
diff src/pk/rsa/rsa_export.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05
Import of libtomcrypt 1.05
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 Jul 2005 03:53:40 +0000 |
parents | 1c15b283127b |
children |
line wrap: on
line diff
--- a/src/pk/rsa/rsa_export.c Fri May 06 13:23:02 2005 +0000 +++ b/src/pk/rsa/rsa_export.c Wed Jul 06 03:53:40 2005 +0000 @@ -27,7 +27,8 @@ */ int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) { - int err, x; + int err; + unsigned long zero=0; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); @@ -37,62 +38,39 @@ if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) { return CRYPT_PK_INVALID_TYPE; } - if (*outlen < 4) { - return CRYPT_BUFFER_OVERFLOW; - } - - /* Mental Note: push space for the header 0x30 0x82 LL LL (LL = length of packet EXcluding 4 bytes) - * we assume LL > 255 which is true since the smallest RSA key has a 128-byte modulus (1024-bit) - */ - *outlen -= 4; 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 */ - if ((err = der_put_multi_integer( - out+4, outlen, &zero, &key->N, &key->e, - &key->d, &key->p, &key->q, &key->dP, - &key->dQ, &key->qP, NULL)) != CRYPT_OK) { - mp_clear(&zero); + if ((err = der_encode_sequence_multi(out, outlen, + LTC_ASN1_SHORT_INTEGER, 1UL, &zero, + LTC_ASN1_INTEGER, 1UL, &key->N, + LTC_ASN1_INTEGER, 1UL, &key->e, + LTC_ASN1_INTEGER, 1UL, &key->d, + LTC_ASN1_INTEGER, 1UL, &key->p, + LTC_ASN1_INTEGER, 1UL, &key->q, + LTC_ASN1_INTEGER, 1UL, &key->dP, + LTC_ASN1_INTEGER, 1UL, &key->dQ, + LTC_ASN1_INTEGER, 1UL, &key->qP, + LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { return err; } /* clear zero and return */ - mp_clear(&zero); + return CRYPT_OK; } else { /* public key */ - if ((err = der_put_multi_integer(out+4, outlen, &key->N, &key->e, NULL)) != CRYPT_OK) { - return err; - } + return der_encode_sequence_multi(out, outlen, + LTC_ASN1_INTEGER, 1UL, &key->N, + LTC_ASN1_INTEGER, 1UL, &key->e, + LTC_ASN1_EOL, 0UL, NULL); } - - /* store the header */ - out[0] = 0x30; - if (*outlen < 256) { - /* shift the output up one byte if the header is only 3 bytes */ - for (x = 0; x < *outlen; x++) { - out[x+3] = out[x+4]; - } - out[1] = 0x81; - out[2] = (*outlen & 255); - *outlen += 3; - } else { - out[1] = 0x82; - out[2] = (*outlen >> 8) & 255; - out[3] = (*outlen & 255); - *outlen += 4; - } - return err; } #endif /* MRSA */ +/* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_export.c,v $ */ +/* $Revision: 1.11 $ */ +/* $Date: 2005/06/04 01:42:48 $ */