<div dir="auto">There are a variety of attacks against RSA when used in this manner. You really should use OAEP ( <a href="https://en.m.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding" target="_blank" rel="noreferrer">https://en.m.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding</a> ) and you almost certainly should use RSA to exchange keys for a symmetric authenticated encryption algorithm (such as ChaCha20-Poly1305 or AES-GCM).<div dir="auto"><br></div><div dir="auto">It goes without saying that playing around with encryption is fun, but for anything serious, use a high-level well-reviewed library implementing well-studied protocols. libgnutls, libgpgme, and libsodium are good choices, depending on your use case. libgcrypt is a low-level library meant as a building block for high-level end-user libraries.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">Cheers,</div><div dir="auto">Karl</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 26, 2018, 23:33 Steffen Bingel, pi4 <<a href="mailto:sbi@pi4.de" target="_blank" rel="noreferrer">sbi@pi4.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
at first, this is the first time for me using a mailing list and I <br>
apologize in advance for any violation of rules I may not know yet.<br>
<br>
I'm playing around with the private/public key functions of libgcrypt <br>
and ran into an behavior I couldn't find an explanation for. If my <br>
message that I try to encrypt is larger than the key I use for <br>
encryption the pk_encrypt seems to generate random data without throwing <br>
an error. The following code is a condensed copy from <br>
<a href="https://github.com/vedantk/gcrypt-example/blob/master/main.cc" rel="noreferrer noreferrer noreferrer" target="_blank">https://github.com/vedantk/gcrypt-example/blob/master/main.cc</a>. If my <br>
message contains 32 characters (256 bit) this works fine but if I pass <br>
33 or more characters the decrypted messages makes no sense at all. I <br>
was also playing around with bigger keys where I could observe the same <br>
behavior (msg bigger than key not working).<br>
<br>
So if the function is not intended to take data larger than the key, why <br>
is it not returning an error?<br>
<br>
What is the correct way to encrypt large, at least larger than the key, <br>
binary data I have in memory?<br>
<br>
Thanks a lot<br>
<br>
gcry_error_t err;<br>
<br>
#define _assert(cmd) {\<br>
err = cmd;\<br>
if (err != GPG_ERR_NO_ERROR) {\<br>
L("ERR: command returned: %s",gcry_strerror(err));\<br>
}}<br>
<br>
/* generate key pair */<br>
gcry_sexp_t rsa_keypair;<br>
gcry_sexp_t parms;<br>
_assert(gcry_sexp_build( &parms, NULL, "(genkey(rsa(nbits %d)))",256));<br>
<br>
_assert(gcry_pk_genkey( &rsa_keypair,parms ));<br>
<br>
gcry_sexp_t pubk = gcry_sexp_find_token(rsa_keypair, "public-key", 0);<br>
gcry_sexp_t privk = gcry_sexp_find_token(rsa_keypair, <br>
"private-key", 0);<br>
<br>
/* Create a message. */<br>
gcry_mpi_t msg;<br>
gcry_sexp_t data;<br>
const unsigned char* s = (const unsigned char*)<br>
"uweoirdnd1iejfkslrm2kdleirjfm3xss";<br>
_assert(gcry_mpi_scan(&msg, GCRYMPI_FMT_USG, s, strlen((const <br>
char*) s), NULL));<br>
<br>
gcry_mpi_dump(msg);<br>
<br>
_assert(gcry_sexp_build(&data, NULL,"(data (flags raw) (value <br>
%m))", msg));<br>
<br>
gcry_sexp_dump(data);<br>
<br>
/* Encrypt the message. */<br>
gcry_sexp_t ciph;<br>
_assert(gcry_pk_encrypt(&ciph, data, pubk));<br>
<br>
gcry_sexp_dump(ciph);<br>
<br>
/* Decrypt the message. */<br>
gcry_sexp_t plain;<br>
_assert(gcry_pk_decrypt(&plain, ciph, privk));<br>
<br>
/* Pretty-print the results. */<br>
gcry_mpi_t out_msg = gcry_sexp_nth_mpi(plain, 0, GCRYMPI_FMT_USG);<br>
L("Original:");<br>
gcry_mpi_dump(msg);<br>
L("\n" "Decrypted:");<br>
gcry_mpi_dump(out_msg);<br>
<br>
if (gcry_mpi_cmp(msg, out_msg)) {<br>
L("data corruption!");<br>
} else {<br>
L("Messages match.\n");<br>
}<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Gcrypt-devel mailing list<br>
<a href="mailto:Gcrypt-devel@gnupg.org" rel="noreferrer noreferrer" target="_blank">Gcrypt-devel@gnupg.org</a><br>
<a href="http://lists.gnupg.org/mailman/listinfo/gcrypt-devel" rel="noreferrer noreferrer noreferrer" target="_blank">http://lists.gnupg.org/mailman/listinfo/gcrypt-devel</a><br>
</blockquote></div>