libtomcrypt/rsa_sys.c

272 lines
6.4 KiB
C
Raw Normal View History

2003-03-03 00:59:24 +00:00
/* these are smaller routines written by Clay Culver. They do the same function as the rsa_encrypt/decrypt
* except that they are used to RSA encrypt/decrypt a single value and not a packet.
*/
int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
unsigned char *outkey, unsigned long *outlen,
prng_state *prng, int wprng, rsa_key *key)
{
unsigned char rsa_in[4096], rsa_out[4096];
unsigned long x, y, rsa_size;
2003-03-03 01:02:42 +00:00
int err;
2003-03-03 00:59:24 +00:00
_ARGCHK(inkey != NULL);
_ARGCHK(outkey != NULL);
_ARGCHK(outlen != NULL);
_ARGCHK(key != NULL);
2003-03-13 02:12:16 +00:00
/* only allow keys from 64 to 256 bits */
if (inlen < 8 || inlen > 32) {
return CRYPT_INVALID_ARG;
}
2003-03-03 00:59:24 +00:00
/* are the parameters valid? */
2003-03-03 01:02:42 +00:00
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* rsa_pad the symmetric key */
2003-03-03 01:02:42 +00:00
y = (unsigned long)sizeof(rsa_in);
if ((err = rsa_pad(inkey, inlen, rsa_in, &y, wprng, prng)) != CRYPT_OK) {
2003-03-03 00:59:24 +00:00
return CRYPT_ERROR;
}
/* rsa encrypt it */
2003-03-03 01:02:42 +00:00
rsa_size = (unsigned long)sizeof(rsa_out);
if ((err = rsa_exptmod(rsa_in, y, rsa_out, &rsa_size, PK_PUBLIC, key)) != CRYPT_OK) {
2003-03-03 00:59:24 +00:00
return CRYPT_ERROR;
}
/* check size */
if (*outlen < (PACKET_SIZE+4+rsa_size)) {
return CRYPT_BUFFER_OVERFLOW;
}
/* now lets make the header */
y = PACKET_SIZE;
/* store the size of the RSA value */
STORE32L(rsa_size, (outkey+y));
y += 4;
/* store the rsa value */
for (x = 0; x < rsa_size; x++, y++) {
outkey[y] = rsa_out[x];
}
/* store header */
2003-03-03 01:02:10 +00:00
packet_store_header(outkey, PACKET_SECT_RSA, PACKET_SUB_ENC_KEY);
2003-03-03 00:59:24 +00:00
#ifdef CLEAN_STACK
/* clean up */
zeromem(rsa_in, sizeof(rsa_in));
zeromem(rsa_out, sizeof(rsa_out));
#endif
*outlen = y;
return CRYPT_OK;
}
2003-03-03 01:02:10 +00:00
int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen,
rsa_key *key)
2003-03-03 00:59:24 +00:00
{
unsigned char sym_key[MAXBLOCKSIZE], rsa_in[4096], rsa_out[4096];
unsigned long x, y, z, i, rsa_size;
2003-03-03 01:02:42 +00:00
int err;
2003-03-03 00:59:24 +00:00
_ARGCHK(in != NULL);
_ARGCHK(outkey != NULL);
_ARGCHK(keylen != NULL);
_ARGCHK(key != NULL);
/* right key type? */
if (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED) {
return CRYPT_PK_NOT_PRIVATE;
}
2003-03-03 01:02:10 +00:00
if (inlen < PACKET_SIZE+4) {
return CRYPT_INVALID_PACKET;
} else {
inlen -= PACKET_SIZE+4;
}
2003-03-03 00:59:24 +00:00
/* check the header */
2003-03-03 01:02:42 +00:00
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_RSA, PACKET_SUB_ENC_KEY)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* grab length of the rsa key */
y = PACKET_SIZE;
2003-03-03 01:02:10 +00:00
LOAD32L(rsa_size, (in+y));
if (inlen < rsa_size) {
return CRYPT_INVALID_PACKET;
} else {
inlen -= rsa_size;
}
2003-03-03 00:59:24 +00:00
y += 4;
/* read it in */
for (x = 0; x < rsa_size; x++, y++) {
rsa_in[x] = in[y];
}
/* decrypt it */
2003-03-03 01:02:42 +00:00
x = (unsigned long)sizeof(rsa_out);
if ((err = rsa_exptmod(rsa_in, rsa_size, rsa_out, &x, PK_PRIVATE, key)) != CRYPT_OK) {
return err;
2003-03-03 01:02:10 +00:00
}
2003-03-03 00:59:24 +00:00
/* depad it */
2003-03-03 01:02:42 +00:00
z = (unsigned long)sizeof(sym_key);
if ((err = rsa_depad(rsa_out, x, sym_key, &z)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* check size */
if (*keylen < z) {
return CRYPT_BUFFER_OVERFLOW;
}
for (i = 0; i < z; i++) {
outkey[i] = sym_key[i];
}
#ifdef CLEAN_STACK
/* clean up */
zeromem(sym_key, sizeof(sym_key));
zeromem(rsa_in, sizeof(rsa_in));
zeromem(rsa_out, sizeof(rsa_out));
#endif
*keylen = z;
return CRYPT_OK;
}
int rsa_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
rsa_key *key)
{
unsigned long rsa_size, x, y;
unsigned char rsa_in[4096], rsa_out[4096];
2003-03-03 01:02:42 +00:00
int err;
2003-03-03 00:59:24 +00:00
_ARGCHK(in != NULL);
_ARGCHK(out != NULL);
_ARGCHK(outlen != NULL);
_ARGCHK(key != NULL);
2003-03-13 02:12:16 +00:00
/* reject nonsense sizes */
if (inlen < 16) {
return CRYPT_INVALID_ARG;
}
2003-03-03 00:59:24 +00:00
/* type of key? */
if (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED) {
return CRYPT_PK_NOT_PRIVATE;
}
/* pad it */
2003-03-03 01:02:42 +00:00
x = (unsigned long)sizeof(rsa_out);
if ((err = rsa_signpad(in, inlen, rsa_out, &x)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* sign it */
2003-03-03 01:02:42 +00:00
rsa_size = (unsigned long)sizeof(rsa_in);
if ((err = rsa_exptmod(rsa_out, x, rsa_in, &rsa_size, PK_PRIVATE, key)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* check size */
if (*outlen < (PACKET_SIZE+4+rsa_size)) {
return CRYPT_BUFFER_OVERFLOW;
}
/* now lets output the message */
y = PACKET_SIZE;
/* output the len */
STORE32L(rsa_size, (out+y));
y += 4;
/* store the signature */
for (x = 0; x < rsa_size; x++, y++) {
out[y] = rsa_in[x];
}
/* store header */
2003-03-03 01:02:10 +00:00
packet_store_header(out, PACKET_SECT_RSA, PACKET_SUB_SIGNED);
2003-03-03 00:59:24 +00:00
#ifdef CLEAN_STACK
/* clean up */
zeromem(rsa_in, sizeof(rsa_in));
zeromem(rsa_out, sizeof(rsa_out));
#endif
*outlen = y;
return CRYPT_OK;
}
2003-03-03 01:02:10 +00:00
int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *md, int *stat, rsa_key *key)
2003-03-03 00:59:24 +00:00
{
unsigned long rsa_size, x, y, z;
unsigned char rsa_in[4096], rsa_out[4096];
2003-03-03 01:02:42 +00:00
int err;
2003-03-03 00:59:24 +00:00
_ARGCHK(sig != NULL);
_ARGCHK(md != NULL);
_ARGCHK(stat != NULL);
_ARGCHK(key != NULL);
/* always be incorrect by default */
*stat = 0;
2003-03-03 01:02:10 +00:00
if (siglen < PACKET_SIZE+4) {
return CRYPT_INVALID_PACKET;
} else {
siglen -= PACKET_SIZE+4;
}
2003-03-03 00:59:24 +00:00
/* verify header */
2003-03-03 01:02:42 +00:00
if ((err = packet_valid_header((unsigned char *)sig, PACKET_SECT_RSA, PACKET_SUB_SIGNED)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* get the len */
y = PACKET_SIZE;
LOAD32L(rsa_size, (sig+y));
2003-03-03 01:02:10 +00:00
if (siglen < rsa_size) {
return CRYPT_INVALID_PACKET;
} else {
siglen -= rsa_size;
}
2003-03-03 00:59:24 +00:00
y += 4;
/* load the signature */
for (x = 0; x < rsa_size; x++, y++) {
rsa_in[x] = sig[y];
}
/* exptmod it */
2003-03-03 01:02:42 +00:00
x = (unsigned long)sizeof(rsa_in);
if ((err = rsa_exptmod(rsa_in, rsa_size, rsa_out, &x, PK_PUBLIC, key)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* depad it */
2003-03-03 01:02:42 +00:00
z = (unsigned long)sizeof(rsa_in);
if ((err = rsa_signdepad(rsa_out, x, rsa_in, &z)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
/* check? */
2003-03-03 01:02:42 +00:00
if (memcmp(rsa_in, md, (size_t)z) == 0) {
2003-03-03 00:59:24 +00:00
*stat = 1;
}
#ifdef CLEAN_STACK
zeromem(rsa_in, sizeof(rsa_in));
zeromem(rsa_out, sizeof(rsa_out));
#endif
return CRYPT_OK;
}