@@ -40,6 +40,7 @@ enum ltc_pka_id {
4040 LTC_PKA_X25519 ,
4141 LTC_PKA_ED25519 ,
4242 LTC_PKA_DH ,
43+ LTC_PKA_RSA_PSS ,
4344 LTC_PKA_NUM
4445};
4546
@@ -62,7 +63,16 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng);
6263/* ---- RSA ---- */
6364#ifdef LTC_MRSA
6465
65- /** RSA PKCS style key */
66+ typedef struct ltc_rsa_parameters {
67+ /** saltLength for PSS */
68+ unsigned long saltlen ;
69+ /** Hash algorithm index for OAEP/PSS, -1 if unset */
70+ int hash_idx ;
71+ /** MGF1 hash algorithm index, -1 if unset */
72+ int mgf1_hash_idx ;
73+ } ltc_rsa_parameters ;
74+
75+ /** RSA key */
6676typedef struct Rsa_key {
6777 /** Type of key, PK_PRIVATE or PK_PUBLIC */
6878 int type ;
@@ -82,6 +92,10 @@ typedef struct Rsa_key {
8292 void * dP ;
8393 /** The d mod (q - 1) CRT param */
8494 void * dQ ;
95+ /** Key is constrained to PSS/OAEP operations */
96+ int pss_oaep ;
97+ /** PSS/OAEP parameters of the RSA key */
98+ ltc_rsa_parameters params ;
8599} rsa_key ;
86100
87101int rsa_make_key (prng_state * prng , int wprng , int size , long e , rsa_key * key );
@@ -95,50 +109,131 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
95109
96110void rsa_free (rsa_key * key );
97111
98- /* These use PKCS #1 v2.0 padding */
99- #define rsa_encrypt_key (in , inlen , out , outlen , lparam , lparamlen , prng , prng_idx , hash_idx , key ) \
100- rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, -1, LTC_PKCS_1_OAEP, key)
101-
102- #define rsa_decrypt_key (in , inlen , out , outlen , lparam , lparamlen , hash_idx , stat , key ) \
103- rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, -1, LTC_PKCS_1_OAEP, stat, key)
104-
105- #define rsa_sign_hash (in , inlen , out , outlen , prng , prng_idx , hash_idx , saltlen , key ) \
106- rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
107-
108- #define rsa_verify_hash (sig , siglen , hash , hashlen , hash_idx , saltlen , stat , key ) \
109- rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, saltlen, stat, key)
110-
111- #define rsa_sign_saltlen_get_max (hash_idx , key ) \
112- rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
112+ typedef struct ltc_rsa_op_parameters {
113+ ltc_rsa_parameters params ;
114+ /* The padding type */
115+ int padding ;
116+ /* The PRNG to use.
117+ * Only required for signing and encryption. */
118+ int wprng ;
119+ prng_state * prng ;
120+ /* Operation-specific parameters */
121+ union {
122+ struct {
123+ const unsigned char * lparam ;
124+ unsigned long lparamlen ;
125+ } crypt ;
126+ /* let's make space for potential future extensions */
127+ ulong64 dummy [8 ];
128+ } u ;
129+ } ltc_rsa_op_parameters ;
130+
131+ int rsa_encrypt_key_v2 (const unsigned char * in , unsigned long inlen ,
132+ unsigned char * out , unsigned long * outlen ,
133+ ltc_rsa_op_parameters * params ,
134+ const rsa_key * key );
135+
136+ int rsa_decrypt_key_v2 (const unsigned char * in , unsigned long inlen ,
137+ unsigned char * out , unsigned long * outlen ,
138+ ltc_rsa_op_parameters * params ,
139+ int * stat ,
140+ const rsa_key * key );
141+
142+ int rsa_sign_hash_v2 (const unsigned char * hash , unsigned long hashlen ,
143+ unsigned char * sig , unsigned long * siglen ,
144+ ltc_rsa_op_parameters * params ,
145+ const rsa_key * key );
146+
147+ int rsa_verify_hash_v2 (const unsigned char * sig , unsigned long siglen ,
148+ const unsigned char * hash , unsigned long hashlen ,
149+ ltc_rsa_op_parameters * params ,
150+ int * stat ,
151+ const rsa_key * key );
113152
153+ /* These use PKCS #1 v2.0 padding */
154+ #define ltc_rsa_encrypt_key (in , inlen , out , outlen , lp , lplen , prng_ , prng_idx , hash_idx_ , key ) \
155+ rsa_encrypt_key_v2(in, inlen, out, outlen, \
156+ &(ltc_rsa_op_parameters){ \
157+ .u.crypt.lparam = lp, \
158+ .u.crypt.lparamlen = lplen,\
159+ .prng = prng_, \
160+ .wprng = prng_idx, \
161+ .params.mgf1_hash_idx = hash_idx_, \
162+ .params.hash_idx = hash_idx_, \
163+ .padding = LTC_PKCS_1_OAEP, \
164+ }, key)
165+
166+ #define ltc_rsa_decrypt_key (in , inlen , out , outlen , lp , lplen , hash_idx_ , stat , key ) \
167+ rsa_decrypt_key_v2(in, inlen, out, outlen, \
168+ &(ltc_rsa_op_parameters){ \
169+ .u.crypt.lparam = lp, \
170+ .u.crypt.lparamlen = lplen,\
171+ .params.mgf1_hash_idx = hash_idx_, \
172+ .params.hash_idx = hash_idx_, \
173+ .padding = LTC_PKCS_1_OAEP, \
174+ }, stat, key)
175+
176+ #define ltc_rsa_sign_hash (hash , hashlen , sig , siglen , prng_ , prng_idx , hash_idx_ , saltlen_ , key ) \
177+ rsa_sign_hash_v2(hash, hashlen, sig, siglen, \
178+ &(ltc_rsa_op_parameters){ \
179+ .prng = prng_, \
180+ .wprng = prng_idx, \
181+ .params.mgf1_hash_idx = hash_idx_, \
182+ .params.hash_idx = hash_idx_, \
183+ .params.saltlen = saltlen_, \
184+ .padding = LTC_PKCS_1_PSS, \
185+ }, key)
186+
187+ #define ltc_rsa_verify_hash (sig , siglen , hash , hashlen , hash_idx_ , saltlen_ , stat , key ) \
188+ rsa_verify_hash_v2(sig, siglen, hash, hashlen, \
189+ &(ltc_rsa_op_parameters){ \
190+ .params.mgf1_hash_idx = hash_idx_, \
191+ .params.hash_idx = hash_idx_, \
192+ .params.saltlen = saltlen_, \
193+ .padding = LTC_PKCS_1_PSS, \
194+ }, stat, key)
195+
196+ /* If you used those in v1, they're still working */
197+ #define rsa_encrypt_key ltc_rsa_encrypt_key
198+ #define rsa_decrypt_key ltc_rsa_decrypt_key
199+ #define rsa_sign_hash ltc_rsa_sign_hash
200+ #define rsa_verify_hash ltc_rsa_verify_hash
201+
202+ #ifndef LTC_NO_DEPRECATED_APIS
114203/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
204+ LTC_DEPRECATED (rsa_encrypt_key_v2 )
115205int rsa_encrypt_key_ex (const unsigned char * in , unsigned long inlen ,
116206 unsigned char * out , unsigned long * outlen ,
117207 const unsigned char * lparam , unsigned long lparamlen ,
118208 prng_state * prng , int prng_idx ,
119- int mgf_hash , int lparam_hash ,
120- int padding ,
209+ int hash_idx , int padding ,
121210 const rsa_key * key );
122211
212+ LTC_DEPRECATED (rsa_decrypt_key_v2 )
123213int rsa_decrypt_key_ex (const unsigned char * in , unsigned long inlen ,
124214 unsigned char * out , unsigned long * outlen ,
125215 const unsigned char * lparam , unsigned long lparamlen ,
126- int mgf_hash , int lparam_hash ,
127- int padding ,
216+ int hash_idx , int padding ,
128217 int * stat , const rsa_key * key );
129218
219+ LTC_DEPRECATED (rsa_sign_hash_v2 )
130220int rsa_sign_hash_ex (const unsigned char * in , unsigned long inlen ,
131221 unsigned char * out , unsigned long * outlen ,
132222 int padding ,
133- prng_state * prng , int prng_idx ,
223+ prng_state * prng , int prng_idx ,
134224 int hash_idx , unsigned long saltlen ,
135225 const rsa_key * key );
136226
227+ LTC_DEPRECATED (rsa_verify_hash_v2 )
137228int rsa_verify_hash_ex (const unsigned char * sig , unsigned long siglen ,
138229 const unsigned char * hash , unsigned long hashlen ,
139230 int padding ,
140231 int hash_idx , unsigned long saltlen ,
141232 int * stat , const rsa_key * key );
233+ #endif /* LTC_NO_DEPRECATED_APIS */
234+
235+ #define rsa_sign_saltlen_get_max (hash_idx , key ) \
236+ rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
142237
143238int rsa_sign_saltlen_get_max_ex (int padding , int hash_idx , const rsa_key * key );
144239
0 commit comments