Skip to content

Commit 687954c

Browse files
committed
common: add RSA SPKI encryption helper
1 parent 57b87bb commit 687954c

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/common/crypto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], cons
3838
/* Derives key material with PBKDF2-HMAC-SHA512. */
3939
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint32_t rounds, uint8_t *out, size_t out_len);
4040

41+
/*
42+
Imports an RSA public key from SubjectPublicKeyInfo DER and encrypts 'in'
43+
with PKCS#1 v1.5 padding, writing the ciphertext to 'out'.
44+
*/
45+
int encrypt_rsa_pkcs1_spki_der(uint8_t *out, size_t *out_len, const uint8_t *der, size_t der_len, const void *in, size_t in_len);
46+
4147
/*
4248
Generates a Diffie-Hellman public-private keypair using the generator value 'gen' and prime modulo
4349
'prime', writing the result to 'pub_out' and 'priv_out', which must be 'keylen' in size.

src/common/crypto_included.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len, const uint8
102102
return 0;
103103
}
104104

105+
int encrypt_rsa_pkcs1_spki_der(uint8_t *out, size_t *out_len, const uint8_t *der, size_t der_len, const void *in, size_t in_len)
106+
{
107+
(void)out;
108+
(void)out_len;
109+
(void)der;
110+
(void)der_len;
111+
(void)in;
112+
(void)in_len;
113+
return 0;
114+
}
115+
105116
int dh_generate_keypair(uint8_t *priv_out, uint8_t *pub_out, const uint8_t *gen, const size_t gen_len, const uint8_t *prime, const size_t keylen)
106117
{
107118
return 0;

src/common/crypto_libgcrypt.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len,
234234
return gcry_err_code(error) == GPG_ERR_NO_ERROR;
235235
}
236236

237+
int encrypt_rsa_pkcs1_spki_der(uint8_t *out, size_t *out_len,
238+
const uint8_t *der, size_t der_len,
239+
const void *in, size_t in_len)
240+
{
241+
(void)out;
242+
(void)out_len;
243+
(void)der;
244+
(void)der_len;
245+
(void)in;
246+
(void)in_len;
247+
return 0;
248+
}
249+
237250
int dh_generate_keypair(uint8_t *priv_out, uint8_t *pub_out, const uint8_t *gen, const size_t gen_len, const uint8_t *prime, const size_t keylen)
238251
{
239252
int result = 0;

src/common/crypto_openssl.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <openssl/dh.h>
2929
#include <openssl/evp.h>
3030
#include <openssl/rand.h>
31+
#include <openssl/rsa.h>
32+
#include <openssl/x509.h>
3133
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
3234
#include <openssl/provider.h>
3335
#endif
@@ -210,6 +212,46 @@ int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len,
210212
EVP_sha512(), (int)out_len, out) == 1;
211213
}
212214

215+
int encrypt_rsa_pkcs1_spki_der(uint8_t *out, size_t *out_len,
216+
const uint8_t *der, size_t der_len,
217+
const void *in, size_t in_len)
218+
{
219+
int result = 0;
220+
EVP_PKEY *pkey = NULL;
221+
EVP_PKEY_CTX *ctx = NULL;
222+
const unsigned char *derp = der;
223+
size_t required_len = 0;
224+
225+
if (!out || !out_len || !der || !in)
226+
goto out;
227+
228+
pkey = d2i_PUBKEY(NULL, &derp, (long)der_len);
229+
if (!pkey || EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA)
230+
goto out;
231+
232+
ctx = EVP_PKEY_CTX_new(pkey, NULL);
233+
if (!ctx)
234+
goto out;
235+
if (EVP_PKEY_encrypt_init(ctx) <= 0)
236+
goto out;
237+
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
238+
goto out;
239+
if (EVP_PKEY_encrypt(ctx, NULL, &required_len, in, in_len) <= 0)
240+
goto out;
241+
if (required_len > *out_len)
242+
goto out;
243+
if (EVP_PKEY_encrypt(ctx, out, &required_len, in, in_len) <= 0)
244+
goto out;
245+
246+
*out_len = required_len;
247+
result = 1;
248+
249+
out:
250+
EVP_PKEY_CTX_free(ctx);
251+
EVP_PKEY_free(pkey);
252+
return result;
253+
}
254+
213255
static void pad_leading_zeros(uint8_t *out, const size_t current_len, const size_t expected_len) {
214256
if (current_len >= expected_len || expected_len < 1)
215257
return;

0 commit comments

Comments
 (0)