@@ -70,7 +70,10 @@ typedef struct ltc_rsa_parameters {
7070 /** saltLength is only defined for PSS
7171 * If saltLength == 0 -> OAEP, else -> PSS */
7272 unsigned long saltlen ;
73- /** hash and MGF hash algorithms */
73+ /** lparam hash for OAEP
74+ * resp.
75+ * signature hash for PSS
76+ * and MGF hash algorithms */
7477 const char * hash_alg , * mgf1_hash_alg ;
7578} ltc_rsa_parameters ;
7679
@@ -109,52 +112,133 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
109112
110113void rsa_free (rsa_key * key );
111114
112- /* These use PKCS #1 v2.0 padding */
113- #define rsa_encrypt_key (in , inlen , out , outlen , lparam , lparamlen , prng , prng_idx , hash_idx , key ) \
114- rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, -1, LTC_PKCS_1_OAEP, key)
115-
116- #define rsa_decrypt_key (in , inlen , out , outlen , lparam , lparamlen , hash_idx , stat , key ) \
117- rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, -1, LTC_PKCS_1_OAEP, stat, key)
118-
119- #define rsa_sign_hash (in , inlen , out , outlen , prng , prng_idx , hash_idx , saltlen , key ) \
120- rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, hash_idx, saltlen, key)
121-
122- #define rsa_verify_hash (sig , siglen , hash , hashlen , hash_idx , saltlen , stat , key ) \
123- rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, hash_idx, saltlen, stat, key)
124-
125- #define rsa_sign_saltlen_get_max (hash_idx , key ) \
126- rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
115+ typedef struct ltc_rsa_op_parameters {
116+ /* The RSA API will set the `pss_oaep` field for you,
117+ * depending on the value of `padding`. */
118+ ltc_rsa_parameters params ;
119+ /* The padding type */
120+ int padding ;
121+ /* The PRNG to use.
122+ * Only required for signing and encryption. */
123+ int wprng ;
124+ prng_state * prng ;
125+ /* Operation-specific parameters */
126+ union {
127+ struct {
128+ const unsigned char * lparam ;
129+ unsigned long lparamlen ;
130+ } crypt ;
131+ /* let's make space for potential future extensions */
132+ ulong64 dummy [8 ];
133+ } u ;
134+ } ltc_rsa_op_parameters ;
135+
136+ int rsa_encrypt_key_v2 (const unsigned char * in , unsigned long inlen ,
137+ unsigned char * out , unsigned long * outlen ,
138+ ltc_rsa_op_parameters * opts ,
139+ const rsa_key * key );
140+
141+ int rsa_decrypt_key_v2 (const unsigned char * in , unsigned long inlen ,
142+ unsigned char * out , unsigned long * outlen ,
143+ ltc_rsa_op_parameters * opts ,
144+ int * stat ,
145+ const rsa_key * key );
146+
147+ int rsa_sign_hash_v2 (const unsigned char * hash , unsigned long hashlen ,
148+ unsigned char * sig , unsigned long * siglen ,
149+ ltc_rsa_op_parameters * opts ,
150+ const rsa_key * key );
151+
152+ int rsa_verify_hash_v2 (const unsigned char * sig , unsigned long siglen ,
153+ const unsigned char * hash , unsigned long hashlen ,
154+ ltc_rsa_op_parameters * opts ,
155+ int * stat ,
156+ const rsa_key * key );
127157
158+ /* These use PKCS #1 v2.0 padding */
159+ #define ltc_rsa_encrypt_key (in , inlen , out , outlen , lp , lplen , prng_ , prng_idx , hash_idx , key ) \
160+ rsa_encrypt_key_v2(in, inlen, out, outlen, \
161+ &(ltc_rsa_op_parameters){ \
162+ .u.crypt.lparam = lp, \
163+ .u.crypt.lparamlen = lplen,\
164+ .prng = prng_, \
165+ .wprng = prng_idx, \
166+ .params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
167+ .params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
168+ .padding = LTC_PKCS_1_OAEP, \
169+ }, key)
170+
171+ #define ltc_rsa_decrypt_key (in , inlen , out , outlen , lp , lplen , hash_idx , stat , key ) \
172+ rsa_decrypt_key_v2(in, inlen, out, outlen, \
173+ &(ltc_rsa_op_parameters){ \
174+ .u.crypt.lparam = lp, \
175+ .u.crypt.lparamlen = lplen,\
176+ .params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
177+ .params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
178+ .padding = LTC_PKCS_1_OAEP, \
179+ }, stat, key)
180+
181+ #define ltc_rsa_sign_hash (hash , hashlen , sig , siglen , prng_ , prng_idx , hash_idx , saltlen_ , key ) \
182+ rsa_sign_hash_v2(hash, hashlen, sig, siglen, \
183+ &(ltc_rsa_op_parameters){ \
184+ .prng = prng_, \
185+ .wprng = prng_idx, \
186+ .params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
187+ .params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
188+ .params.saltlen = saltlen_, \
189+ .padding = LTC_PKCS_1_PSS, \
190+ }, key)
191+
192+ #define ltc_rsa_verify_hash (sig , siglen , hash , hashlen , hash_idx , saltlen_ , stat , key ) \
193+ rsa_verify_hash_v2(sig, siglen, hash, hashlen, \
194+ &(ltc_rsa_op_parameters){ \
195+ .params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
196+ .params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
197+ .params.saltlen = saltlen_, \
198+ .padding = LTC_PKCS_1_PSS, \
199+ }, stat, key)
200+
201+ /* If you used those in v1, they're still working */
202+ #define rsa_encrypt_key ltc_rsa_encrypt_key
203+ #define rsa_decrypt_key ltc_rsa_decrypt_key
204+ #define rsa_sign_hash ltc_rsa_sign_hash
205+ #define rsa_verify_hash ltc_rsa_verify_hash
206+
207+ #ifndef LTC_NO_DEPRECATED_APIS
128208/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
209+ LTC_DEPRECATED (rsa_encrypt_key_v2 )
129210int rsa_encrypt_key_ex (const unsigned char * in , unsigned long inlen ,
130211 unsigned char * out , unsigned long * outlen ,
131212 const unsigned char * lparam , unsigned long lparamlen ,
132213 prng_state * prng , int prng_idx ,
133- int mgf_hash , int lparam_hash ,
134- int padding ,
214+ int hash_idx , int padding ,
135215 const rsa_key * key );
136216
217+ LTC_DEPRECATED (rsa_decrypt_key_v2 )
137218int rsa_decrypt_key_ex (const unsigned char * in , unsigned long inlen ,
138219 unsigned char * out , unsigned long * outlen ,
139220 const unsigned char * lparam , unsigned long lparamlen ,
140- int mgf_hash , int lparam_hash ,
141- int padding ,
221+ int hash_idx , int padding ,
142222 int * stat , const rsa_key * key );
143223
224+ LTC_DEPRECATED (rsa_sign_hash_v2 )
144225int rsa_sign_hash_ex (const unsigned char * in , unsigned long inlen ,
145226 unsigned char * out , unsigned long * outlen ,
146227 int padding ,
147228 prng_state * prng , int prng_idx ,
148- int hash_idx , int mgf_hash_idx ,
149- unsigned long saltlen ,
229+ int hash_idx , unsigned long saltlen ,
150230 const rsa_key * key );
151231
232+ LTC_DEPRECATED (rsa_verify_hash_v2 )
152233int rsa_verify_hash_ex (const unsigned char * sig , unsigned long siglen ,
153234 const unsigned char * hash , unsigned long hashlen ,
154235 int padding ,
155- int hash_idx , int mgf_hash_idx ,
156- unsigned long saltlen ,
236+ int hash_idx , unsigned long saltlen ,
157237 int * stat , const rsa_key * key );
238+ #endif /* LTC_NO_DEPRECATED_APIS */
239+
240+ #define rsa_sign_saltlen_get_max (hash_idx , key ) \
241+ rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
158242
159243int rsa_sign_saltlen_get_max_ex (int padding , int hash_idx , const rsa_key * key );
160244
0 commit comments