Skip to content

Commit b13a9ae

Browse files
committed
RSA WIP
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent ebb3982 commit b13a9ae

File tree

3 files changed

+129
-10
lines changed

3 files changed

+129
-10
lines changed

src/headers/tomcrypt_pk.h

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,72 @@ int rsa_verify_hash_v2(const unsigned char *sig, unsigned long siglen,
154154
int *stat,
155155
const rsa_key *key);
156156

157+
/* These use PKCS #1 v2.0 padding */
158+
#define ltc_rsa_encrypt_key(in, inlen, out, outlen, lp, lplen, prng_, prng_idx, hash_idx, key) \
159+
rsa_encrypt_key_v2(in, inlen, out, outlen, \
160+
&(ltc_rsa_op_parameters){ \
161+
.u.crypt.lparam = lp, \
162+
.u.crypt.lparamlen = lplen,\
163+
.prng = prng_, \
164+
.wprng = prng_idx, \
165+
.params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
166+
.params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
167+
.padding = LTC_PKCS_1_OAEP, \
168+
}, key)
169+
170+
#define ltc_rsa_decrypt_key(in, inlen, out, outlen, lp, lplen, hash_idx, stat, key) \
171+
rsa_decrypt_key_v2(in, inlen, out, outlen, \
172+
&(ltc_rsa_op_parameters){ \
173+
.u.crypt.lparam = lp, \
174+
.u.crypt.lparamlen = lplen,\
175+
.params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
176+
.params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
177+
.padding = LTC_PKCS_1_OAEP, \
178+
}, stat, key)
179+
180+
#define ltc_rsa_sign_hash(hash, hashlen, sig, siglen, prng_, prng_idx, hash_idx, saltlen_, key) \
181+
rsa_sign_hash_v2(hash, hashlen, sig, siglen, \
182+
&(ltc_rsa_op_parameters){ \
183+
.prng = prng_, \
184+
.wprng = prng_idx, \
185+
.params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
186+
.params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
187+
.params.saltlen = saltlen_, \
188+
.padding = LTC_PKCS_1_PSS, \
189+
}, key)
190+
191+
#define ltc_rsa_verify_hash(sig, siglen, hash, hashlen, hash_idx, saltlen_, stat, key) \
192+
rsa_verify_hash_v2(sig, siglen, hash, hashlen, \
193+
&(ltc_rsa_op_parameters){ \
194+
.params.mgf1_hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
195+
.params.hash_alg = hash_is_valid(hash_idx) == CRYPT_OK ? hash_descriptor[hash_idx].name : NULL, \
196+
.params.saltlen = saltlen_, \
197+
.padding = LTC_PKCS_1_PSS, \
198+
}, stat, key)
199+
200+
//#define LTC_NO_DEPRECATED_APIS
201+
#ifdef LTC_NO_DEPRECATED_APIS
202+
203+
#define rsa_encrypt_key ltc_rsa_encrypt_key
204+
#define rsa_decrypt_key ltc_rsa_decrypt_key
205+
#define rsa_sign_hash ltc_rsa_sign_hash
206+
#define rsa_verify_hash ltc_rsa_verify_hash
207+
208+
#else /* LTC_NO_DEPRECATED_APIS */
209+
157210
/* These use PKCS #1 v2.0 padding */
158211
#define rsa_encrypt_key(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, key) \
159212
rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, LTC_PKCS_1_OAEP, key)
160213

161214
#define rsa_decrypt_key(in, inlen, out, outlen, lparam, lparamlen, hash_idx, stat, key) \
162215
rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, LTC_PKCS_1_OAEP, stat, key)
163216

164-
#define rsa_sign_hash(in, inlen, out, outlen, prng, prng_idx, hash_idx, saltlen, key) \
165-
rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
217+
#define rsa_sign_hash(hash, hashlen, sig, siglen, prng, prng_idx, hash_idx, saltlen, key) \
218+
rsa_sign_hash_ex(hash, hashlen, sig, siglen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
166219

167220
#define rsa_verify_hash(sig, siglen, hash, hashlen, hash_idx, saltlen, stat, key) \
168221
rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, saltlen, stat, key)
169222

170-
#define rsa_sign_saltlen_get_max(hash_idx, key) \
171-
rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
172-
173223
/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
174224
LTC_DEPRECATED(rsa_encrypt_key_v2)
175225
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
@@ -200,6 +250,10 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long sigle
200250
int padding,
201251
int hash_idx, unsigned long saltlen,
202252
int *stat, const rsa_key *key);
253+
#endif /* LTC_NO_DEPRECATED_APIS */
254+
255+
#define rsa_sign_saltlen_get_max(hash_idx, key) \
256+
rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
203257

204258
int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, const rsa_key *key);
205259

tests/deprecated_test.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,46 @@ static void s_ecc_test(void)
3939
}
4040
#endif
4141

42+
#ifdef LTC_MRSA
43+
extern const unsigned char ltc_rsa_private_test_key[];
44+
extern const unsigned long ltc_rsa_private_test_key_sz;
45+
extern const unsigned char ltc_openssl_public_rsa[];
46+
extern const unsigned long ltc_openssl_public_rsa_sz;
47+
static int s_rsa_test(void)
48+
{
49+
rsa_key key, pubkey;
50+
int stat;
51+
const unsigned char tv[] = "test";
52+
unsigned char buf0[1024], buf1[1024];
53+
unsigned long buf0len, buf1len;
54+
55+
DO(rsa_import(ltc_rsa_private_test_key, ltc_rsa_private_test_key_sz, &key));
56+
DO(rsa_import(ltc_openssl_public_rsa, ltc_openssl_public_rsa_sz, &pubkey));
57+
58+
buf0len = sizeof(buf0);
59+
DO(rsa_sign_hash_ex(tv, 4, buf0, &buf0len, LTC_PKCS_1_PSS, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key));
60+
buf1len = sizeof(buf1);
61+
DO(rsa_verify_hash_ex(buf0, buf0len, tv, 4, LTC_PKCS_1_PSS, find_hash("sha1"), 8, &stat, &pubkey));
62+
ENSURE(stat == 1);
63+
64+
buf0len = sizeof(buf0);
65+
DO(rsa_encrypt_key_ex(tv, 4, buf0, &buf0len, NULL, 0, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), LTC_PKCS_1_OAEP, &pubkey));
66+
buf1len = sizeof(buf1);
67+
DO(rsa_decrypt_key_ex(buf0, buf0len, buf1, &buf1len, NULL, 0, find_hash("sha1"), LTC_PKCS_1_OAEP, &stat, &key));
68+
ENSURE(stat == 1);
69+
COMPARE_TESTVECTOR(buf1, buf1len, tv, 4, "s_rsa_test", 0);
70+
71+
return CRYPT_OK;
72+
}
73+
#endif
74+
4275
int deprecated_test(void)
4376
{
4477
#ifdef LTC_MECC
4578
s_ecc_test();
79+
#endif
80+
#ifdef LTC_MRSA
81+
s_rsa_test();
4682
#endif
4783
return 0;
4884
}

tests/rsa_test.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static const char *hex_key[] = {
134134
"DCCC27C8E4DC6248D59BAFF5AB60F621FD53E2B75D09C91AA104A9FC612C5D04583A5A39F14A215667FDCC20A38F78185A793D2E8E7E860AE6A833C104174A9F" };
135135

136136
/*** openssl public RSA key in DER format */
137-
static const unsigned char openssl_public_rsa[] = {
137+
const unsigned char ltc_openssl_public_rsa[] = {
138138
0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
139139
0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
140140
0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
@@ -146,6 +146,7 @@ static const unsigned char openssl_public_rsa[] = {
146146
0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
147147
0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
148148
0x00, 0x01, };
149+
const unsigned long ltc_openssl_public_rsa_sz = sizeof(ltc_openssl_public_rsa);
149150

150151
/* same key but with extra headers stripped */
151152
static const unsigned char openssl_public_rsa_stripped[] = {
@@ -188,7 +189,7 @@ static int rsa_compat_test(void)
188189

189190
/* try reading the key */
190191
DO(rsa_import(ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), &key));
191-
DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &pubkey));
192+
DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &pubkey));
192193

193194
/* sign-verify a message with PKCS #1 v1.5 no ASN.1 */
194195
len = sizeof(buf);
@@ -224,7 +225,7 @@ static int rsa_compat_test(void)
224225
rsa_free(&key);
225226

226227
/* try reading the public key */
227-
DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
228+
DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &key));
228229
len = sizeof(buf);
229230
DO(rsa_export(buf, &len, PK_PUBLIC, &key));
230231
COMPARE_TESTVECTOR(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL)", 0);
@@ -259,10 +260,10 @@ static int rsa_compat_test(void)
259260
rsa_free(&key);
260261

261262
/* try export in SubjectPublicKeyInfo format of the public key */
262-
DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
263+
DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &key));
263264
len = sizeof(buf);
264265
DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
265-
COMPARE_TESTVECTOR(buf, len, openssl_public_rsa, sizeof(openssl_public_rsa), "RSA public export (X.509)", 0);
266+
COMPARE_TESTVECTOR(buf, len, ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), "RSA public export (X.509)", 0);
266267
rsa_free(&key);
267268

268269
return 0;
@@ -452,6 +453,33 @@ static int s_rsa_import_pkcs8(const void *in, unsigned long inlen, void *key)
452453
#endif
453454
#endif
454455

456+
static int s_rsa_macros_test(void)
457+
{
458+
rsa_key key, pubkey;
459+
int stat;
460+
const unsigned char tv[] = "test";
461+
unsigned char buf0[1024], buf1[1024];
462+
unsigned long buf0len, buf1len;
463+
464+
DO(rsa_import(ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), &key));
465+
DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &pubkey));
466+
467+
buf0len = sizeof(buf0);
468+
DO(ltc_rsa_sign_hash(tv, 4, buf0, &buf0len, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key));
469+
buf1len = sizeof(buf1);
470+
DO(ltc_rsa_verify_hash(buf0, buf0len, tv, 4, find_hash("sha1"), 8, &stat, &key));
471+
ENSURE(stat == 1);
472+
473+
buf0len = sizeof(buf0);
474+
DO(ltc_rsa_encrypt_key(tv, 4, buf0, &buf0len, NULL, 0, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), &pubkey));
475+
buf1len = sizeof(buf1);
476+
DO(ltc_rsa_decrypt_key(buf0, buf0len, buf1, &buf1len, NULL, 0, find_hash("sha1"), &stat, &key));
477+
ENSURE(stat == 1);
478+
COMPARE_TESTVECTOR(buf1, buf1len, tv, 4, "s_rsa_macros_test", 0);
479+
480+
return CRYPT_OK;
481+
}
482+
455483
int rsa_test(void)
456484
{
457485
unsigned char in[1024], out[1024], tmp[3072];
@@ -489,6 +517,7 @@ int rsa_test(void)
489517
DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
490518
#endif
491519

520+
DO(s_rsa_macros_test());
492521
DO(s_rsa_cryptx_issue_69());
493522
DO(s_rsa_issue_301(prng_idx));
494523
DO(s_rsa_public_ubin_e(prng_idx));

0 commit comments

Comments
 (0)