Path: blob/master/libs/tomcrypt/src/pk/rsa/rsa_decrypt_key.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file rsa_decrypt_key.c12RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange13*/1415#ifdef LTC_MRSA1617/**18PKCS #1 decrypt then v1.5 or OAEP depad19@param in The ciphertext20@param inlen The length of the ciphertext (octets)21@param out [out] The plaintext22@param outlen [in/out] The max size and resulting size of the plaintext (octets)23@param lparam The system "lparam" value24@param lparamlen The length of the lparam value (octets)25@param hash_idx The index of the hash desired26@param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5)27@param stat [out] Result of the decryption, 1==valid, 0==invalid28@param key The corresponding private RSA key29@return CRYPT_OK if succcessul (even if invalid)30*/31int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,32unsigned char *out, unsigned long *outlen,33const unsigned char *lparam, unsigned long lparamlen,34int hash_idx, int padding,35int *stat, rsa_key *key)36{37unsigned long modulus_bitlen, modulus_bytelen, x;38int err;39unsigned char *tmp;4041LTC_ARGCHK(out != NULL);42LTC_ARGCHK(outlen != NULL);43LTC_ARGCHK(key != NULL);44LTC_ARGCHK(stat != NULL);4546/* default to invalid */47*stat = 0;4849/* valid padding? */5051if ((padding != LTC_PKCS_1_V1_5) &&52(padding != LTC_PKCS_1_OAEP)) {53return CRYPT_PK_INVALID_PADDING;54}5556if (padding == LTC_PKCS_1_OAEP) {57/* valid hash ? */58if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {59return err;60}61}6263/* get modulus len in bits */64modulus_bitlen = mp_count_bits( (key->N));6566/* outlen must be at least the size of the modulus */67modulus_bytelen = mp_unsigned_bin_size( (key->N));68if (modulus_bytelen != inlen) {69return CRYPT_INVALID_PACKET;70}7172/* allocate ram */73tmp = XMALLOC(inlen);74if (tmp == NULL) {75return CRYPT_MEM;76}7778/* rsa decode the packet */79x = inlen;80if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {81XFREE(tmp);82return err;83}8485if (padding == LTC_PKCS_1_OAEP) {86/* now OAEP decode the packet */87err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,88out, outlen, stat);89} else {90/* now PKCS #1 v1.5 depad the packet */91err = pkcs_1_v1_5_decode(tmp, x, LTC_PKCS_1_EME, modulus_bitlen, out, outlen, stat);92}9394XFREE(tmp);95return err;96}9798#endif /* LTC_MRSA */99100101