1313 * permissions and limitations under the License.
1414 */
1515
16+ #include "crypto/s2n_rsa.h"
17+
1618#include <openssl/evp.h>
1719#include <openssl/rsa.h>
1820#include <stdint.h>
1921
20- #include "error/s2n_errno.h"
21-
22- #include "stuffer/s2n_stuffer.h"
23-
24- #include "crypto/s2n_hash.h"
2522#include "crypto/s2n_drbg.h"
26- #include "crypto/s2n_rsa.h"
27- #include "crypto/s2n_rsa_signing.h"
23+ #include "crypto/s2n_hash.h"
2824#include "crypto/s2n_pkey.h"
29-
30- #include "utils/s2n_safety .h"
31- #include "utils/s2n_random .h"
25+ #include "crypto/s2n_rsa_signing.h"
26+ #include "error/s2n_errno .h"
27+ #include "stuffer/s2n_stuffer .h"
3228#include "utils/s2n_blob.h"
29+ #include "utils/s2n_random.h"
30+ #include "utils/s2n_result.h"
31+ #include "utils/s2n_safety.h"
3332
34- static int s2n_rsa_modulus_check (RSA * rsa )
33+ static S2N_RESULT s2n_rsa_modulus_check (RSA * rsa )
3534{
36- /* RSA was made opaque starting in Openssl 1.1.0 */
37- #if S2N_OPENSSL_VERSION_AT_LEAST (1 ,1 , 0 ) && !defined(LIBRESSL_VERSION_NUMBER )
38- const BIGNUM * n = NULL ;
39- /* RSA still owns the memory for n */
40- RSA_get0_key (rsa , & n , NULL , NULL );
41- notnull_check (n );
42- #else
43- notnull_check (rsa -> n );
44- #endif
45- return 0 ;
35+ /* RSA was made opaque starting in Openssl 1.1.0 */
36+ #if S2N_OPENSSL_VERSION_AT_LEAST (1 , 1 , 0 ) && !defined(LIBRESSL_VERSION_NUMBER )
37+ const BIGNUM * n = NULL ;
38+ /* RSA still owns the memory for n */
39+ RSA_get0_key (rsa , & n , NULL , NULL );
40+ ENSURE_REF (n );
41+ #else
42+ ENSURE_REF (rsa -> n );
43+ #endif
44+ return S2N_RESULT_OK ;
4645}
4746
48- static int s2n_rsa_encrypted_size (const struct s2n_pkey * key )
47+ static S2N_RESULT s2n_rsa_encrypted_size (const struct s2n_pkey * key , uint32_t * size_out )
4948{
49+ ENSURE_REF (key );
50+ ENSURE_REF (size_out );
51+
5052 const struct s2n_rsa_key * rsa_key = & key -> key .rsa_key ;
51- notnull_check (rsa_key -> rsa );
52- GUARD (s2n_rsa_modulus_check (rsa_key -> rsa ));
53+ ENSURE_REF (rsa_key -> rsa );
54+ GUARD_RESULT (s2n_rsa_modulus_check (rsa_key -> rsa ));
5355
54- return RSA_size (rsa_key -> rsa );
56+ const int size = RSA_size (rsa_key -> rsa );
57+ GUARD_AS_RESULT (size );
58+ * size_out = size ;
59+
60+ return S2N_RESULT_OK ;
5561}
5662
57- static int s2n_rsa_sign (const struct s2n_pkey * priv , s2n_signature_algorithm sig_alg ,
58- struct s2n_hash_state * digest , struct s2n_blob * signature )
63+ static int s2n_rsa_sign (const struct s2n_pkey * priv , s2n_signature_algorithm sig_alg , struct s2n_hash_state * digest ,
64+ struct s2n_blob * signature )
5965{
60- switch (sig_alg ) {
66+ switch (sig_alg ) {
6167 case S2N_SIGNATURE_RSA :
6268 return s2n_rsa_pkcs1v15_sign (priv , digest , signature );
6369 case S2N_SIGNATURE_RSA_PSS_RSAE :
@@ -69,10 +75,10 @@ static int s2n_rsa_sign(const struct s2n_pkey *priv, s2n_signature_algorithm sig
6975 return S2N_SUCCESS ;
7076}
7177
72- static int s2n_rsa_verify (const struct s2n_pkey * pub , s2n_signature_algorithm sig_alg ,
73- struct s2n_hash_state * digest , struct s2n_blob * signature )
78+ static int s2n_rsa_verify (const struct s2n_pkey * pub , s2n_signature_algorithm sig_alg , struct s2n_hash_state * digest ,
79+ struct s2n_blob * signature )
7480{
75- switch (sig_alg ) {
81+ switch (sig_alg ) {
7682 case S2N_SIGNATURE_RSA :
7783 return s2n_rsa_pkcs1v15_verify (pub , digest , signature );
7884 case S2N_SIGNATURE_RSA_PSS_RSAE :
@@ -86,28 +92,32 @@ static int s2n_rsa_verify(const struct s2n_pkey *pub, s2n_signature_algorithm si
8692
8793static int s2n_rsa_encrypt (const struct s2n_pkey * pub , struct s2n_blob * in , struct s2n_blob * out )
8894{
89- S2N_ERROR_IF (out -> size < s2n_rsa_encrypted_size (pub ), S2N_ERR_NOMEM );
95+ uint32_t size = 0 ;
96+ GUARD_AS_POSIX (s2n_rsa_encrypted_size (pub , & size ));
97+ S2N_ERROR_IF (out -> size < size , S2N_ERR_NOMEM );
9098
9199 const s2n_rsa_public_key * key = & pub -> key .rsa_key ;
92- int r = RSA_public_encrypt (in -> size , (unsigned char * )in -> data , (unsigned char * )out -> data , key -> rsa , RSA_PKCS1_PADDING );
100+ int r = RSA_public_encrypt (in -> size , ( unsigned char * )in -> data , ( unsigned char * )out -> data , key -> rsa ,
101+ RSA_PKCS1_PADDING );
93102 S2N_ERROR_IF (r != out -> size , S2N_ERR_SIZE_MISMATCH );
94103
95104 return 0 ;
96105}
97106
98107static int s2n_rsa_decrypt (const struct s2n_pkey * priv , struct s2n_blob * in , struct s2n_blob * out )
99108{
100- unsigned char intermediate [4096 ];
101- const int expected_size = s2n_rsa_encrypted_size (priv );
109+ unsigned char intermediate [ 4096 ];
110+ uint32_t expected_size = 0 ;
111+
112+ GUARD_AS_POSIX (s2n_rsa_encrypted_size (priv , & expected_size ));
102113
103- GUARD (expected_size );
104114 S2N_ERROR_IF (expected_size > sizeof (intermediate ), S2N_ERR_NOMEM );
105115 S2N_ERROR_IF (out -> size > sizeof (intermediate ), S2N_ERR_NOMEM );
106116
107117 GUARD_AS_POSIX (s2n_get_public_random_data (out ));
108118
109119 const s2n_rsa_private_key * key = & priv -> key .rsa_key ;
110- int r = RSA_private_decrypt (in -> size , (unsigned char * )in -> data , intermediate , key -> rsa , RSA_NO_PADDING );
120+ int r = RSA_private_decrypt (in -> size , ( unsigned char * )in -> data , intermediate , key -> rsa , RSA_NO_PADDING );
111121 S2N_ERROR_IF (r != expected_size , S2N_ERR_SIZE_MISMATCH );
112122
113123 s2n_constant_time_pkcs1_unpad_or_dont (out -> data , intermediate , r , out -> size );
@@ -117,14 +127,14 @@ static int s2n_rsa_decrypt(const struct s2n_pkey *priv, struct s2n_blob *in, str
117127
118128static int s2n_rsa_keys_match (const struct s2n_pkey * pub , const struct s2n_pkey * priv )
119129{
120- uint8_t plain_inpad [36 ] = {1 }, plain_outpad [36 ] = {0 }, encpad [8192 ];
130+ uint8_t plain_inpad [ 36 ] = { 1 }, plain_outpad [ 36 ] = { 0 }, encpad [ 8192 ];
121131 struct s2n_blob plain_in = { 0 }, plain_out = { 0 }, enc = { 0 };
122132
123133 plain_in .data = plain_inpad ;
124134 plain_in .size = sizeof (plain_inpad );
125135
126136 enc .data = encpad ;
127- enc . size = s2n_rsa_encrypted_size (pub );
137+ GUARD_AS_POSIX ( s2n_rsa_encrypted_size (pub , & enc . size ) );
128138 lte_check (enc .size , sizeof (encpad ));
129139 GUARD (s2n_rsa_encrypt (pub , & plain_in , & enc ));
130140
@@ -140,9 +150,7 @@ static int s2n_rsa_keys_match(const struct s2n_pkey *pub, const struct s2n_pkey
140150static int s2n_rsa_key_free (struct s2n_pkey * pkey )
141151{
142152 struct s2n_rsa_key * rsa_key = & pkey -> key .rsa_key ;
143- if (rsa_key -> rsa == NULL ) {
144- return 0 ;
145- }
153+ if (rsa_key -> rsa == NULL ) { return 0 ; }
146154
147155 RSA_free (rsa_key -> rsa );
148156 rsa_key -> rsa = NULL ;
@@ -177,13 +185,13 @@ int s2n_evp_pkey_to_rsa_private_key(s2n_rsa_private_key *rsa_key, EVP_PKEY *evp_
177185
178186int s2n_rsa_pkey_init (struct s2n_pkey * pkey )
179187{
180- pkey -> size = & s2n_rsa_encrypted_size ;
181- pkey -> sign = & s2n_rsa_sign ;
182- pkey -> verify = & s2n_rsa_verify ;
183- pkey -> encrypt = & s2n_rsa_encrypt ;
184- pkey -> decrypt = & s2n_rsa_decrypt ;
185- pkey -> match = & s2n_rsa_keys_match ;
186- pkey -> free = & s2n_rsa_key_free ;
188+ pkey -> size = & s2n_rsa_encrypted_size ;
189+ pkey -> sign = & s2n_rsa_sign ;
190+ pkey -> verify = & s2n_rsa_verify ;
191+ pkey -> encrypt = & s2n_rsa_encrypt ;
192+ pkey -> decrypt = & s2n_rsa_decrypt ;
193+ pkey -> match = & s2n_rsa_keys_match ;
194+ pkey -> free = & s2n_rsa_key_free ;
187195 pkey -> check_key = & s2n_rsa_check_key_exists ;
188196 return 0 ;
189197}
0 commit comments