Make CTR mode behaviour consistent with other modes:
[openssl.git] / crypto / engine / eng_rsax.c
index 7c4fa713139a2bdf807bd6ca4dfba089a94418cb..173aaf8d34ca168474e9e2d839d95ba47a1cc930 100644 (file)
@@ -73,6 +73,7 @@
 #include <openssl/rsa.h>
 #endif
 #include <openssl/bn.h>
+#include <openssl/err.h>
 
 /* RSAX is available **ONLY* on x86_64 CPUs */
 #undef COMPILE_RSAX
@@ -225,10 +226,13 @@ static int e_rsax_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
 
 
 #ifndef OPENSSL_NO_RSA
-#include <stdint.h>
 
-typedef uint64_t UINT64;
-typedef uint16_t UINT16;
+#ifdef _WIN32
+typedef unsigned __int64 UINT64;
+#else
+typedef unsigned long long UINT64;
+#endif
+typedef unsigned short UINT16;
 
 /* Table t is interleaved in the following manner:
  * The order in memory is t[0][0], t[0][1], ..., t[0][7], t[1][0], ...
@@ -284,7 +288,7 @@ static E_RSAX_MOD_CTX *e_rsax_get_ctx(RSA *rsa, int idx, BIGNUM* m)
            RSA_set_ex_data(rsa, rsax_ex_data_idx, hptr);
         }
 
-        if (hptr[idx].type == BN_num_bits(m))
+        if (hptr[idx].type == (UINT64)BN_num_bits(m))
             return hptr+idx;
 
         if (BN_num_bits(m) == 512) {
@@ -301,10 +305,17 @@ static E_RSAX_MOD_CTX *e_rsax_get_ctx(RSA *rsa, int idx, BIGNUM* m)
 static int e_rsax_rsa_finish(RSA *rsa)
        {
        E_RSAX_MOD_CTX *hptr = RSA_get_ex_data(rsa, rsax_ex_data_idx);
-       if(!hptr) return 0;
-
-       OPENSSL_free(hptr);
-       RSA_set_ex_data(rsa, rsax_ex_data_idx, NULL);
+       if(hptr)
+               {
+               OPENSSL_free(hptr);
+               RSA_set_ex_data(rsa, rsax_ex_data_idx, NULL);
+               }
+       if (rsa->_method_mod_n)
+               BN_MONT_CTX_free(rsa->_method_mod_n);
+       if (rsa->_method_mod_p)
+               BN_MONT_CTX_free(rsa->_method_mod_p);
+       if (rsa->_method_mod_q)
+               BN_MONT_CTX_free(rsa->_method_mod_q);
        return 1;
        }
 
@@ -388,7 +399,6 @@ static int mod_exp_pre_compute_data_512(UINT64 *m, struct mod_ctx_512 *data)
     /* Some tmps */
     UINT64 _t[8];
     int i, j, ret = 0;
-
     /* Init _m with m */
     BN_init(&_m);
     interleaved_array_to_bn_512(&_m, m);
@@ -463,7 +473,7 @@ static int mod_exp_pre_compute_data_512(UINT64 *m, struct mod_ctx_512 *data)
 err:
     /* Cleanup */
        if (ctx != NULL) {
-               BN_CTX_end(ctx); }
+               BN_CTX_end(ctx); BN_CTX_free(ctx); }
     BN_free(&two_768);
     BN_free(&two_640);
     BN_free(&two_128);
@@ -482,7 +492,6 @@ static int e_rsax_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx
        BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
        BIGNUM *dmp1,*dmq1,*c,*pr1;
        int ret=0;
-
        BN_CTX_start(ctx);
        r1 = BN_CTX_get(ctx);
        m1 = BN_CTX_get(ctx);