From aacfec441eb88aced0b8204b6ad779d9ee54f5d8 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 3 Sep 2013 14:25:00 +0200 Subject: [PATCH] add rsa_sign_saltlen_get_max_ex() --- src/headers/tomcrypt_pk.h | 5 ++++ src/pk/rsa/rsa_sign_saltlen_get.c | 49 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/pk/rsa/rsa_sign_saltlen_get.c diff --git a/src/headers/tomcrypt_pk.h b/src/headers/tomcrypt_pk.h index 5d17f29..98c6ce8 100644 --- a/src/headers/tomcrypt_pk.h +++ b/src/headers/tomcrypt_pk.h @@ -72,6 +72,9 @@ void rsa_free(rsa_key *key); #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \ rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key) +#define rsa_sign_saltlen_get_max(_hash_idx, _key) \ + rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key) + /* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, @@ -97,6 +100,8 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, int hash_idx, unsigned long saltlen, int *stat, rsa_key *key); +int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key); + /* LTC_PKCS #1 import/export */ int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); diff --git a/src/pk/rsa/rsa_sign_saltlen_get.c b/src/pk/rsa/rsa_sign_saltlen_get.c new file mode 100644 index 0000000..83dfcf9 --- /dev/null +++ b/src/pk/rsa/rsa_sign_saltlen_get.c @@ -0,0 +1,49 @@ +/* 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. + * + * http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file rsa_sign_saltlen_get_ex.c + Retrieve the maximum size of the salt, Steffen Jaeckel. +*/ + +#ifdef LTC_MRSA + +/** + Retrieve the maximum possible size of the salt when creating a PKCS#1 PSS signature. + @param padding Type of padding (LTC_PKCS_1_PSS only) + @param hash_idx The index of the desired hash + @param key The RSA key + @return The maximum salt length in bytes or INT_MAX on error. +*/ +int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key) +{ + int ret = INT_MAX; + LTC_ARGCHKVD(key != NULL); + + if (hash_is_valid(hash_idx) && + (padding == LTC_PKCS_1_PSS)) + { + ret = rsa_get_size(key); + if (ret < INT_MAX) + { + ret -= (hash_descriptor[hash_idx].hashsize + 2); + } /* if */ + } /* if */ + + return ret; +} + +#endif + +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */