diff -uNr a/eucrypt/manifest b/eucrypt/manifest --- a/eucrypt/manifest e1a9fb1198b0f060412bb794b454ac7cc62a29e3730256d8e0d30bbd403b6b68a2f267514294478b33039eda10aa334e450f159f876f34c192bf58ea4eca48b0 +++ b/eucrypt/manifest 4983488ac0cc9138c74af005f6d96d29bf589323d25ae661408ae12f5e49de0f6ae8cc9530d6559e265351f506f41ec54865e6c878936f256fdba606bc7561f0 @@ -19,3 +19,4 @@ 527560 eucrypt_manifest diana_coman Adds this manifest file that should be modified each time a new patch is added to EuCrypt. 543780 eucrypt_fix_256 diana_coman Fix the error in smg_oaep.adb that used 255 instead of 256 when calculating/retrieving length stored on 2 octets. 545170 eucrypt_ch14_crc32 diana_coman A simple implementation of CRC32 checksum using a lookup table. The CRC32 lib can be compiled on its own or together with the whole EuCrypt. +552693 eucrypt_ch15_arbitrary_e diana_coman Changes to allow the user to pick their desired length for the public exponent when generating a new pair of RSA keys. diff -uNr a/eucrypt/smg_rsa/include/smg_rsa.h b/eucrypt/smg_rsa/include/smg_rsa.h --- a/eucrypt/smg_rsa/include/smg_rsa.h dab95b2f666390284b7ba75171f6836e16847e0b910755599c439f5792e1cdf83ca43c198d03a19491faf213313d34635c373bfc13a97ed432c1ca4faaefdf18 +++ b/eucrypt/smg_rsa/include/smg_rsa.h 167507120c4c88aca1148faef1afef73d8618bd35da845eabf96a1b7413eaa181d89391cf47d8ddb35d668ac5fec00bbc10df326f08a3da8d6c05f5632ef86a6 @@ -21,6 +21,16 @@ */ static const int KEY_LENGTH_OCTETS = 512; +/** + * This is the length of the public exponent e, given in octets. + * TMSR standard e has KEY_LENGTH_OCTETS / 2 octets. + * Eulora's communication protocol uses however e with 8 octets length. + * New keypairs generated will have e precisely this length. + * Change this to your preferred size of e for generating new keys with that size of e. + * NB: this impacts key generation ONLY! (i.e. NOT encrypt/decrypt). + */ +static const int E_LENGTH_OCTETS = 256; + /* * This is the maximum length of a plain-text message (in octets) that can be * oeap+rsa encrypted in a single block. Its value is defined in smg_oaep.ads @@ -224,6 +234,8 @@ output! * @param pk the public key that will be used to encrypt input * + * NB: ALL MPIs (key, input) should be normalized (i.e. NO leading 0s) as otherwise + * underlying MPI operations may take a long time/never return! * Precondition: * output != input * Output and input have to be two distinct MPIs because of the sorry state of @@ -250,6 +262,8 @@ your needs though! * NB: it is the caller's responsibility to allocate memory for output! * NB: NO checks are made on input! + * NB: ALL MPIs (key, input) should be normalized (i.e. NO leading 0s) as otherwise + * underlying MPI operations may take a long time/never return! * * @param output MPI with enough allocated memory to hold result of decryption * @param input MPI containing content to decrypt diff -uNr a/eucrypt/smg_rsa/rsa.c b/eucrypt/smg_rsa/rsa.c --- a/eucrypt/smg_rsa/rsa.c c506f05a540f43ff34059cc5398431aa58b1f4f2294c38a1a793b19d9ccdc9b21c21a06577c95623401ef4154009e77301b640fd323fdea9e3570c1407368bd6 +++ b/eucrypt/smg_rsa/rsa.c a8521afbd09107ddce7ee4683886c44f1a8c2a17b29cb7583f18cb858b8d7833e446ec1aa2c5371246759e99a163df5676686ebdf202075ef50a34d5cdfabcc7 @@ -52,7 +52,7 @@ /* choose random prime e, public exponent, with 3 < e < phi */ /* because e is prime, gcd(e, phi) is always 1 so no need to check it */ do { - gen_random_prime( noctets_pq, sk->e); + gen_random_prime( E_LENGTH_OCTETS, sk->e); } while ( (mpi_cmp_ui(sk->e, 3) < 0) || (mpi_cmp(sk->e, phi) > 0)); /* calculate private exponent d, 1 < d < phi, where e * d = 1 mod phi */ diff -uNr a/eucrypt/smg_rsa/tests/tests.c b/eucrypt/smg_rsa/tests/tests.c --- a/eucrypt/smg_rsa/tests/tests.c 89b1c405d92e249341bdc0bddc5a98d8ae8dd0349e04a165063d938f01df557a057be544c51b0efb6d72d505f1424a1aa3a6d7957e83f229711677240123e2fd +++ b/eucrypt/smg_rsa/tests/tests.c 07cdfd546dcaa1b548e7a1daee2091450a84f21710bafbb9e6e794c9a11f7a7b081bdb0c5a1a8841a35699a6cf3f19b6274073690e01b6ced048b4b5ab3afe3f @@ -305,6 +305,104 @@ } +void test_rsa_8e(int nruns) { + RSA_secret_key sk; + int noctets = KEY_LENGTH_OCTETS; + int noctets_pq = noctets / 2; + int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq); + + sk.n = mpi_alloc(0); + sk.e = mpi_alloc(0); + sk.d = mpi_alloc(0); + sk.p = mpi_alloc(0); + sk.q = mpi_alloc(0); + sk.u = mpi_alloc(0); + + mpi_fromstr(sk.n, "0x\ +B51BE851F39159EAC714F3E0376713A84DAD36A82D446D0A257A391870F45FAE13C4CC\ +F400DDA9F604991134C0934161554EEFEAA3147BF0EADC77B99E2B9B6E4EE942EA9D07\ +5F015EE2465B491F4130E04E1BBB6CCDC98F6E8789D4F7FCA3E3FF83C6100CAF2B764E\ +A5AF7CBA9B27C13EE72EA7A8602F34B32E17C2BA56CFBA4223F7D9A03C23336095D34F\ +BF66E88BF5CE661D66C251DFAD4CB2BA8D1E1669AC927894EA20DABABD2495BC2A4BA3\ +A25C79ABEC2D57F45F0F889D962C777A663D0AB25D3650DFDC6D77C528803C0C6E12BD\ +05281B33C603BEA66A0C2ACBEBD1CA53D32C2269294C9B93E742CA563AF39E939C32CE\ +51D5ED827F9C217EF58CC518B635D0E03BA778BCEBAF9A2CDB493282D751A5977CB907\ +C8708D1EF1CAE644C1F2525DDE98E29761B1ADF0965F08AA856DF540AEFD67F96B92AE\ +83636C31A507C59635C6D435C5E7EE333DC2257C07BC0FCE27CF400F6EB7A6B90FFF00\ +C3C1179615BF5DA6137476926C09D8CCD03257DFCAEF12BE9DC1D3F621D6C97D7F3E6D\ +534337579B4B65AE212ACC26FC3861E24033E6F12A601D473A65EFC5F25ABD5D6049EA\ +DD6D76BA60AA218C5EBE13439AAFFF0088C49ACC0E9F7DE56DB03F585E1AC2862EB990\ +59724FD407C4ACD3DD14A53A6A35F6AFAE03EA53A4E742CC370087692E206A2422FF9D"); + + sk.e = mpi_alloc(nlimbs_pq); + int i; + char echar[109]; + int ne = 109; + for (i=0;i