Skip to content

Commit c1b3281

Browse files
karel-msjaeckel
authored andcommitted
RSA: no more dual semantics of pss_oaep (removed from ltc_rsa_parameters; now lives on rsa_key where it belongs)
1 parent d2829aa commit c1b3281

4 files changed

Lines changed: 21 additions & 29 deletions

File tree

src/headers/tomcrypt_pk.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng);
6464
#ifdef LTC_MRSA
6565

6666
typedef struct ltc_rsa_parameters {
67-
/** PSS/OAEP or PKCS #1 v1.5 style
68-
* 0 -> PKCS #1 v1.5, 1 -> PSS/OAEP */
69-
int pss_oaep;
70-
/** saltLength is only defined for PSS
71-
* If saltLength == 0 -> OAEP, else -> PSS */
67+
/** saltLength for PSS */
7268
unsigned long saltlen;
7369
/** lparam hash for OAEP
7470
* resp.
@@ -97,7 +93,9 @@ typedef struct Rsa_key {
9793
void *dP;
9894
/** The d mod (q - 1) CRT param */
9995
void *dQ;
100-
/** Further parameters of the RSA key */
96+
/** Key is constrained to PSS/OAEP operations */
97+
int pss_oaep;
98+
/** PSS/OAEP parameters of the RSA key */
10199
ltc_rsa_parameters params;
102100
} rsa_key;
103101

@@ -113,8 +111,6 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
113111
void rsa_free(rsa_key *key);
114112

115113
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`. */
118114
ltc_rsa_parameters params;
119115
/* The padding type */
120116
int padding;

src/pk/rsa/rsa_import_x509.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ static int s_rsa_decode_parameters(const rsa_pss_parameters_data *d, ltc_rsa_par
123123
}
124124
}
125125

126-
rsa_params->pss_oaep = 1;
127-
128126
return CRYPT_OK;
129127
}
130128

@@ -169,7 +167,11 @@ static LTC_INLINE int s_rsa_pss_import_spki(const unsigned char *in, unsigned lo
169167
(public_key_decode_cb)s_rsa_decode, key)) != CRYPT_OK) {
170168
return err;
171169
}
172-
return s_rsa_decode_parameters(&d, &key->params);
170+
if ((err = s_rsa_decode_parameters(&d, &key->params)) != CRYPT_OK) {
171+
return err;
172+
}
173+
key->pss_oaep = 1;
174+
return CRYPT_OK;
173175
}
174176

175177
static LTC_INLINE int s_rsa_import_spki(const unsigned char *in, unsigned long inlen, rsa_key *key)

src/pk/rsa/rsa_key.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void rsa_shrink_key(rsa_key *key)
8585
int rsa_init(rsa_key *key)
8686
{
8787
LTC_ARGCHK(key != NULL);
88+
key->pss_oaep = 0;
8889
XMEMSET(&key->params, 0, sizeof(key->params));
8990
return ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, LTC_NULL);
9091
}
@@ -97,29 +98,27 @@ void rsa_free(rsa_key *key)
9798
{
9899
LTC_ARGCHKVD(key != NULL);
99100
ltc_mp_cleanup_multi(&key->q, &key->p, &key->qP, &key->dP, &key->dQ, &key->N, &key->d, &key->e, LTC_NULL);
101+
key->pss_oaep = 0;
100102
XMEMSET(&key->params, 0, sizeof(key->params));
101103
}
102104

103105
static LTC_INLINE int s_rsa_key_valid_rsa_params(ltc_rsa_op_checked *check)
104106
{
105-
const ltc_rsa_parameters *key_params, *op_params;
107+
const ltc_rsa_parameters *key_params;
106108
/* This is called from PKCS#1 de-/encoder code, so we can't check the key */
107109
if (check->key == NULL) {
108110
return CRYPT_OK;
109111
}
110112
key_params = &check->key->params;
111-
op_params = &check->params->params;
112-
/* The key is restricted to PSS, so check the op's params */
113-
if (key_params->pss_oaep
114-
&& !rsa_params_equal(key_params, op_params)) {
115-
return CRYPT_PK_TYPE_MISMATCH;
116-
}
117-
/* No PSS or OAEP, so we're fine. */
118-
if (!key_params->pss_oaep
119-
|| !op_params->pss_oaep) {
113+
/* Key has no PSS/OAEP constraints */
114+
if (!check->key->pss_oaep) {
120115
return CRYPT_OK;
121116
}
122-
/* Verify hash algs */
117+
/* Key is constrained - operation must use matching PSS/OAEP params */
118+
if (check->params->padding != LTC_PKCS_1_PSS
119+
&& check->params->padding != LTC_PKCS_1_OAEP) {
120+
return CRYPT_PK_TYPE_MISMATCH;
121+
}
123122
if (key_params->hash_alg == NULL
124123
|| find_hash(key_params->hash_alg) != check->hash_alg) {
125124
return CRYPT_INVALID_HASH;
@@ -139,7 +138,7 @@ static LTC_INLINE int s_rsa_key_set_hash_algs(ltc_rsa_op_checked *check)
139138
return CRYPT_INVALID_HASH;
140139
}
141140
if (params->params.mgf1_hash_alg == NULL) {
142-
if (!params->params.pss_oaep)
141+
if (params->padding != LTC_PKCS_1_PSS && params->padding != LTC_PKCS_1_OAEP)
143142
return CRYPT_OK;
144143
} else if ((check->mgf1_hash_alg = find_hash(params->params.mgf1_hash_alg)) != -1) {
145144
return CRYPT_OK;
@@ -211,8 +210,6 @@ int rsa_key_valid_op(ltc_rsa_op op, ltc_rsa_op_checked *check)
211210
/* PKCS#1 ops don't need an RSA key */
212211
LTC_ARGCHK(check->key != NULL);
213212
}
214-
check->params->params.pss_oaep = check->params->padding == LTC_PKCS_1_OAEP
215-
|| check->params->padding == LTC_PKCS_1_PSS;
216213
if ((op & LTC_RSA_OP_SEND) == LTC_RSA_OP_SEND) {
217214
if ((err = s_rsa_check_prng(op, check->params)) != CRYPT_OK) {
218215
return err;
@@ -235,10 +232,6 @@ int rsa_key_valid_op(ltc_rsa_op op, ltc_rsa_op_checked *check)
235232

236233
int rsa_params_equal(const ltc_rsa_parameters *a, const ltc_rsa_parameters *b)
237234
{
238-
if (!a->pss_oaep)
239-
return 0;
240-
if (a->pss_oaep != b->pss_oaep)
241-
return 0;
242235
if (a->saltlen != b->saltlen)
243236
return 0;
244237
if (!a->hash_alg || !b->hash_alg)

tests/rsa_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ static int s_rsa_pss_test(void)
506506
DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
507507
DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
508508
key.params = rsa_oparams.params;
509+
key.pss_oaep = 1;
509510
DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
510511
/* If the key is a PSS key, we must do a PSS operation */
511512
rsa_oparams.padding = LTC_PKCS_1_V1_5;

0 commit comments

Comments
 (0)