Merge pull request #82 from apache/security_checks

Added security checks:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 436c36e..01a2524 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,7 @@
   log(${var})
 endmacro()
 
+
 ##################################################
 # Includes
 ##################################################
diff --git a/include/utils.h b/include/utils.h
index 0297874..34c05ae 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -40,7 +40,7 @@
  * @param dst     Binary string
  * @param src_len length Hex encoded string
  */
-void amcl_hex2bin(const char *src, char *dst, int src_len);
+void amcl_hex2bin(const char *src, char *dst, size_t src_len);
 
 /**
  * @brief Encode binary string
@@ -50,8 +50,9 @@
  * @param src     Binary string
  * @param dst     Hex encoded string
  * @param src_len length binary string
+ * @param dst_len length hex encoded string
  */
-void amcl_bin2hex(char *src, char *dst, int src_len);
+void amcl_bin2hex(char *src, char *dst, size_t src_len, size_t dst_len);
 
 /**
  * @brief Print encoded binary string in hex
@@ -61,7 +62,7 @@
  * @param src     Binary string
  * @param src_len length binary string
  */
-void amcl_print_hex(char *src, int src_len);
+void amcl_print_hex(char *src, size_t src_len);
 
 /**
  * @brief Generate a random Octet
diff --git a/src/oct.c b/src/oct.c
index cb9c067..e768dca 100644
--- a/src/oct.c
+++ b/src/oct.c
@@ -73,6 +73,7 @@
 int OCT_comp(octet *x,octet *y)
 {
     int i;
+
     if (x->len>y->len) return 0;
     if (x->len<y->len) return 0;
     for (i=0; i<x->len; i++)
@@ -351,13 +352,18 @@
 /* Convert an octet to a hex string */
 void OCT_toHex(octet *src,char *dst)
 {
+    const char * hexadecimals = "0123456789abcdef";
     int i;
     unsigned char ch;
     for (i=0; i<src->len; i++)
     {
         ch=src->val[i];
-        sprintf(&dst[i*2],"%02x", ch);
+        uint8_t res = ch / 16;
+        uint8_t mod = ch % 16;
+        dst[i*2] = hexadecimals[res];
+        dst[(i*2)+1] = hexadecimals[mod];
     }
+    dst[i*2] =0;
 }
 
 static int char2int(char input)
diff --git a/src/utils.c b/src/utils.c
index 1a103f5..cc533ce 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -30,15 +30,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 #include "amcl.h"
 #include "utils.h"
 
-/* Decode hex value */
-void amcl_hex2bin(const char *src, char *dst, int src_len)
+/* Decode a byte to a 2 chars string */
+
+/** Decode hex value */
+void amcl_hex2bin(const char *src, char *dst, size_t src_len)
 {
-    int i;
     char v,c;
-    for (i = 0; i < src_len/2; i++)
+    for (size_t i = 0; i < src_len/2; i++)
     {
         c = src[2*i];
         if (c >= '0' && c <= '9')
@@ -80,20 +82,24 @@
 }
 
 /* Encode binary string */
-void amcl_bin2hex(char *src, char *dst, int src_len)
+void amcl_bin2hex(char *src, char *dst, size_t src_len, size_t dst_len)
 {
-    int i;
-    for (i = 0; i < src_len; i++)
+    const char * hexadecimals = "0123456789abcdef";
+    unsigned char ch;
+    for (size_t i = 0; i < src_len && i< dst_len/2; i++)
     {
-        sprintf(&dst[i*2],"%02x", (unsigned char) src[i]);
+       ch=src[i];
+       uint8_t res = ch / 16;
+       uint8_t mod = ch % 16;
+       dst[i*2] = hexadecimals[res];
+       dst[(i*2)+1] = hexadecimals[mod];
     }
 }
 
 /* Print encoded binary string in hex */
-void amcl_print_hex(char *src, int src_len)
+void amcl_print_hex(char *src, size_t src_len)
 {
-    int i;
-    for (i = 0; i < src_len; i++)
+    for (size_t i = 0; i < src_len; i++)
     {
         printf("%02x", (unsigned char) src[i]);
     }
@@ -126,6 +132,7 @@
 void generateRandom(csprng *RNG,octet *randomValue)
 {
     int i;
-    for (i=0; i<randomValue->len; i++)
-        randomValue->val[i]=RAND_byte(RNG);
+    for (i=0; i<randomValue->len; i++) {
+        randomValue->val[i] = RAND_byte(RNG);
+    }
 }
diff --git a/test/test_aes_decrypt.c b/test/test_aes_decrypt.c
index 774c6ee..34d1b53 100644
--- a/test/test_aes_decrypt.c
+++ b/test/test_aes_decrypt.c
@@ -153,9 +153,10 @@
             l1 = strlen(linePtr)-1;
             IVLen = l1/2;
             IV = (char*) malloc (IVLen);
-            if (IV==NULL)
+            if (IV==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // IV binary value
             amcl_hex2bin(linePtr, IV, l1);
         }
@@ -228,6 +229,7 @@
             if (!rc)
             {
                 printf("TEST AES DECRYPT FAILED COMPARE PLAINTEXT LINE %d\n",lineNo);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
diff --git a/test/test_aes_encrypt.c b/test/test_aes_encrypt.c
index 3c61c27..8668a4e 100644
--- a/test/test_aes_encrypt.c
+++ b/test/test_aes_encrypt.c
@@ -152,9 +152,10 @@
             l1 = strlen(linePtr)-1;
             IVLen = l1/2;
             IV = (char*) malloc (IVLen);
-            if (IV==NULL)
+            if (IV==NULL) {
                 exit(EXIT_FAILURE);
-
+                fclose(fp);
+            }
             // IV binary value
             amcl_hex2bin(linePtr, IV, l1);
         }
@@ -171,8 +172,10 @@
             l1 = strlen(linePtr)-1;
             PLAINTEXTLen = l1/2;
             PLAINTEXT = (char*) malloc(PLAINTEXTLen);
-            if (PLAINTEXT==NULL)
+            if (PLAINTEXT==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
+            }
 
             // PLAINTEXT binary value
             amcl_hex2bin(linePtr, PLAINTEXT, l1);
@@ -189,8 +192,10 @@
             // Allocate memory
             l1 = strlen(linePtr);
             CIPHERTEXT1 = (char*) malloc(PLAINTEXTLen+1);
-            if (CIPHERTEXT1==NULL)
+            if (CIPHERTEXT1==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
+            }
 
             // Golden CIPHERTEXT value
             octet CIPHERTEXT1Oct= {PLAINTEXTLen,PLAINTEXTLen,CIPHERTEXT1};
@@ -227,6 +232,7 @@
             if (!rc)
             {
                 printf("TEST AES ENCRYPT FAILED COMPARE CIPHERTEXT LINE %d\n",lineNo);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
diff --git a/test/test_big_arithmetics_XXX.c.in b/test/test_big_arithmetics_XXX.c.in
index 4b5a5a1..a8908d4 100644
--- a/test/test_big_arithmetics_XXX.c.in
+++ b/test/test_big_arithmetics_XXX.c.in
@@ -134,6 +134,7 @@
             if (BIG_XXX_comp(BIG1,BIG2) < 0)
             {
                 printf("ERROR comparing two BIGs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -149,6 +150,7 @@
             if (BIG_XXX_comp(BIGsum,supp) != 0)
             {
                 printf("ERROR adding two BIGs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -164,6 +166,7 @@
             if (BIG_XXX_comp(BIGsub,supp) != 0)
             {
                 printf("ERROR subtracting two BIGs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -180,6 +183,7 @@
             if (BIG_XXX_comp(BIG1mod2,supp) != 0)
             {
                 printf("ERROR reducing modulo BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -196,6 +200,7 @@
             if (BIG_XXX_comp(BIG2mod1,supp) != 0)
             {
                 printf("ERROR reducing modulo BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -211,6 +216,7 @@
             if (BIG_XXX_dcomp(BIGmul,dsupp) != 0)
             {
                 printf("ERROR multiplication BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -226,6 +232,7 @@
             if (BIG_XXX_dcomp(BIG1sqr,dsupp) != 0)
             {
                 printf("ERROR squaring BIG 1, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -241,6 +248,7 @@
             if (BIG_XXX_dcomp(BIG2sqr,dsupp) != 0)
             {
                 printf("ERROR squaring BIG 2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -257,6 +265,7 @@
             if (BIG_XXX_comp(BIG1sqrmod2,supp) != 0)
             {
                 printf("ERROR reducing squaring modulo BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -274,6 +283,7 @@
             if (BIG_XXX_comp(BIG1modneg2,supp) != 0)
             {
                 printf("ERROR negative reduced modulo BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -287,6 +297,7 @@
             if (nbitBIG != bitlen)
             {
                 printf("ERROR counting bit BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -300,6 +311,7 @@
             if (nbitDBIG != bitlen)
             {
                 printf("ERROR counting bit DBIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -317,6 +329,7 @@
             if (BIG_XXX_comp(BIGdiv,supp) != 0)
             {
                 printf("ERROR division BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -335,6 +348,7 @@
             if (BIG_XXX_comp(BIGdivmod,supp) != 0)
             {
                 printf("ERROR division modulo BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -350,6 +364,7 @@
             if (BIG_XXX_dcomp(BIGpxmul,dsupp) != 0)
             {
                 printf("ERROR small multiplication BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_ecdh_ZZZ.c.in b/test/test_ecdh_ZZZ.c.in
index c364a99..1250718 100644
--- a/test/test_ecdh_ZZZ.c.in
+++ b/test/test_ecdh_ZZZ.c.in
@@ -150,8 +150,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             dIUT = (char*) malloc (l2);
-            if (dIUT==NULL)
+            if (dIUT==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
+            }
 
             // dIUT binary value
             amcl_hex2bin(linePtr, dIUT, l1);
@@ -205,9 +207,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             ZIUT = (char*) malloc (l2);
-            if (ZIUT==NULL)
+            if (ZIUT==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // ZIUT binary value
             amcl_hex2bin(linePtr, ZIUT, l1);
 
@@ -255,6 +258,7 @@
                 OCT_output(&QIUTOct);
                 printf("\n");
 #endif
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
@@ -272,6 +276,7 @@
                 OCT_output(&ZIUTOct);
                 printf("\n");
 #endif
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             free(dIUT);
diff --git a/test/test_ecdsa_keypair_ZZZ.c.in b/test/test_ecdsa_keypair_ZZZ.c.in
index 0f07527..e1f3245 100644
--- a/test/test_ecdsa_keypair_ZZZ.c.in
+++ b/test/test_ecdsa_keypair_ZZZ.c.in
@@ -89,9 +89,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             d = (char*) malloc (l2);
-            if (d==NULL)
+            if (d==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // d binary value
             amcl_hex2bin(linePtr, d, l1);
 
@@ -154,6 +155,7 @@
             if (!rc)
             {
                 printf("TEST ECDSA KEYPAIR FAILED LINE %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             free(d);
diff --git a/test/test_ecdsa_sign_ZZZ.c.in b/test/test_ecdsa_sign_ZZZ.c.in
index 6324b4a..6cb5b5f 100644
--- a/test/test_ecdsa_sign_ZZZ.c.in
+++ b/test/test_ecdsa_sign_ZZZ.c.in
@@ -121,9 +121,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             Msg = (char*) malloc (l2);
-            if (Msg==NULL)
+            if (Msg==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // Msg binary value
             amcl_hex2bin(linePtr, Msg, l1);
 
@@ -144,9 +145,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             d = (char*) malloc (l2);
-            if (d==NULL)
+            if (d==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // d binary value
             amcl_hex2bin(linePtr, d, l1);
 
@@ -197,9 +199,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             k = (char*) malloc (l2);
-            if (k==NULL)
+            if (k==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // k binary value
             amcl_hex2bin(linePtr, k, l1);
 
@@ -220,9 +223,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             R = (char*) malloc (l2);
-            if (R==NULL)
+            if (R==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // R binary value
             amcl_hex2bin(linePtr, R, l1);
 
@@ -243,9 +247,9 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             S = (char*) malloc (l2);
-            if (S==NULL)
+            if (S==NULL) {
                 exit(EXIT_FAILURE);
-
+            }
             // S binary value
             amcl_hex2bin(linePtr, S, l1);
 
@@ -303,6 +307,7 @@
                 OCT_output(&S2Oct);
                 printf("\n");
 #endif
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
diff --git a/test/test_ecdsa_verify_ZZZ.c.in b/test/test_ecdsa_verify_ZZZ.c.in
index f30be5e..0ec3829 100644
--- a/test/test_ecdsa_verify_ZZZ.c.in
+++ b/test/test_ecdsa_verify_ZZZ.c.in
@@ -114,9 +114,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             Msg = (char*) malloc (l2);
-            if (Msg==NULL)
+            if (Msg==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // Msg binary value
             amcl_hex2bin(linePtr, Msg, l1);
 
@@ -167,9 +168,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             R = (char*) malloc (l2);
-            if (R==NULL)
+            if (R==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // R binary value
             amcl_hex2bin(linePtr, R, l1);
 
@@ -190,9 +192,10 @@
             l1 = strlen(linePtr)-1;
             l2 = l1/2;
             S = (char*) malloc (l2);
-            if (S==NULL)
+            if (S==NULL) {
+                fclose(fp);
                 exit(EXIT_FAILURE);
-
+            }
             // S binary value
             amcl_hex2bin(linePtr, S, l1);
 
@@ -234,6 +237,7 @@
             if ( pass && rc )
             {
                 printf("TEST ECDSA VERIFY FAILED LINE %d pass %d rc %d\n",i,pass,rc);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
@@ -241,6 +245,7 @@
             if ( !pass && !rc )
             {
                 printf("TEST ECDSA VERIFY FAILED LINE %d pass %d rc %d\n",i,pass,rc);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
diff --git a/test/test_ecp2_arithmetics_ZZZ.c.in b/test/test_ecp2_arithmetics_ZZZ.c.in
index 7832229..717c2cd 100644
--- a/test/test_ecp2_arithmetics_ZZZ.c.in
+++ b/test/test_ecp2_arithmetics_ZZZ.c.in
@@ -150,6 +150,7 @@
             if(!read_ECP2_ZZZ(&ecp2[0],linePtr) || ECP2_ZZZ_isinf(&ecp2[0]))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_get(&FP2aux1,&FP2aux2,&ecp2[0]);
@@ -158,6 +159,7 @@
             if (!FP2_YYY_equals(&FP2aux1,&FP2aux2))
             {
                 printf("ERROR computing right hand side of equation ECP, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_toOctet(&OCTaux,&ecp2[0]);
@@ -165,6 +167,7 @@
             if(!ECP2_ZZZ_equals(&ECP2aux1,&ecp2[0]))
             {
                 printf("ERROR converting ECP2_ZZZ to/from OCTET, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -175,6 +178,7 @@
             if(!read_ECP2_ZZZ(&ecp2[1],linePtr) || ECP2_ZZZ_isinf(&ecp2[1]))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -185,6 +189,7 @@
             if(!read_ECP2_ZZZ(&ecp2[2],linePtr) || ECP2_ZZZ_isinf(&ecp2[2]))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -195,6 +200,7 @@
             if(!read_ECP2_ZZZ(&ecp2[3],linePtr) || ECP2_ZZZ_isinf(&ecp2[3]))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -205,6 +211,7 @@
             if(!read_ECP2_ZZZ(&ecp2sum,linePtr))
             {
                 printf("ERROR reading test vector input ECP2_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_copy(&ECP2aux1,&ecp2[0]);
@@ -216,6 +223,7 @@
             if(!ECP2_ZZZ_equals(&ECP2aux1,&ecp2sum) || !ECP2_ZZZ_equals(&ECP2aux2,&ecp2sum))
             {
                 printf("ERROR adding two ECP2_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_copy(&ECP2aux1,&ecp2[0]); // testing associativity (P+Q)+R = P+(Q+R)
@@ -229,6 +237,7 @@
             if(!ECP2_ZZZ_equals(&ECP2aux1,&ECP2aux2))
             {
                 printf("ERROR testing associativity bewtween three ECP2_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -239,6 +248,7 @@
             if(!read_ECP2_ZZZ(&ecp2neg,linePtr))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_copy(&ECP2aux1,&ecp2[0]);
@@ -247,6 +257,7 @@
             if(!ECP2_ZZZ_equals(&ECP2aux1,&ecp2neg))
             {
                 printf("ERROR computing negative of ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -257,6 +268,7 @@
             if(!read_ECP2_ZZZ(&ecp2sub,linePtr))
             {
                 printf("ERROR getting test vector input ECP2_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP2_ZZZ_copy(&ECP2aux1,&ecp2[0]);
diff --git a/test/test_ecp4_arithmetics_ZZZ.c.in b/test/test_ecp4_arithmetics_ZZZ.c.in
index 572ae81..ce9b470 100644
--- a/test/test_ecp4_arithmetics_ZZZ.c.in
+++ b/test/test_ecp4_arithmetics_ZZZ.c.in
@@ -153,6 +153,7 @@
             if(!read_ECP4_ZZZ(&ecp41,linePtr) || ECP4_ZZZ_isinf(&ecp41))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_get(&FP4aux1,&FP4aux2,&ecp41);
@@ -161,6 +162,7 @@
             if (!FP4_YYY_equals(&FP4aux1,&FP4aux2))
             {
                 printf("ERROR computing right hand side of equation ECP, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_toOctet(&OCTaux,&ecp41);
@@ -169,6 +171,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp41))
             {
                 printf("ERROR converting ECP4_ZZZ to/from OCTET, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(ecp4frobs,&ecp41);
@@ -185,6 +188,7 @@
             if(!read_ECP4_ZZZ(&ecp42,linePtr) || ECP4_ZZZ_isinf(&ecp42))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -195,6 +199,7 @@
             if(!read_ECP4_ZZZ(&ecp4sum,linePtr))
             {
                 printf("ERROR reading test vector input ECP4_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(&ECP4aux1,&ecp41);
@@ -208,6 +213,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4sum) || !ECP4_ZZZ_equals(&ECP4aux2,&ecp4sum))
             {
                 printf("ERROR adding two ECP4_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(&ECP4aux1,&ecp41); // testing associativity (P+Q)+R = P+(Q+R)
@@ -221,6 +227,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ECP4aux2))
             {
                 printf("ERROR testing associativity bewtween three ECP4_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -239,6 +246,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4neg))
             {
                 printf("ERROR computing negative of ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -249,6 +257,7 @@
             if(!read_ECP4_ZZZ(&ecp4sub,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(&ECP4aux1,&ecp41);
@@ -257,6 +266,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4sub))
             {
                 printf("ERROR performing subtraction bewtween two ECP4_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -267,6 +277,7 @@
             if(!read_ECP4_ZZZ(&ecp4dbl,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(&ECP4aux1,&ecp41);
@@ -275,6 +286,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4dbl))
             {
                 printf("ERROR computing double of ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -294,6 +306,7 @@
             if(!read_ECP4_ZZZ(&ecp4mul,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_copy(&ECP4aux1,&ecp41);
@@ -302,6 +315,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4mul))
             {
                 printf("ERROR computing multiplication of ECP4_ZZZ by a scalar, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -316,6 +330,7 @@
                 if(!FP2_YYY_equals(&fp2fr,F+k))
                 {
                     printf("ERROR computing %d-th frobenius constant, line %d\n",k+1,i);
+                    fclose(fp);
                     exit(EXIT_FAILURE);
                 }
             }
@@ -332,6 +347,7 @@
             if(!ECP4_ZZZ_equals(&ecp4frob,&ECP4aux1))
             {
                 printf("ERROR computing frobenius action (P^3), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -342,6 +358,7 @@
             if(!read_ECP4_ZZZ(&ecp4mul8,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
@@ -351,6 +368,7 @@
             if(!ECP4_ZZZ_equals(&ECP4aux1,&ecp4mul8))
             {
                 printf("ERROR computing linear combination of 8 ECP4_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -361,6 +379,7 @@
             if(read_ECP4_ZZZ(&ecp4wrong,linePtr) || !ECP4_ZZZ_isinf(&ecp4wrong) || !ECP4_ZZZ_equals(&ecp4wrong,&inf))
             {
                 printf("ERROR identifying a wrong ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             if(ECP4_ZZZ_setx(&ECP4aux1,&ecp4wrong.x))
@@ -375,6 +394,7 @@
             if(read_ECP4_ZZZ(&ecp4inf,linePtr) || !ECP4_ZZZ_isinf(&ecp4inf) || !ECP4_ZZZ_equals(&ecp4inf,&inf))
             {
                 printf("ERROR identifying infinite point ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -385,6 +405,7 @@
             if(!read_ECP4_ZZZ(&ecp4set1,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP4_ZZZ_get(&FP4aux1,&FP4aux2,&ecp41);
@@ -397,11 +418,13 @@
             if(!read_ECP4_ZZZ(&ecp4set2,linePtr))
             {
                 printf("ERROR getting test vector input ECP4_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             if((!ECP4_ZZZ_equals(&ECP4aux1,&ecp4set2)) && (!ECP4_ZZZ_equals(&ECP4aux1,&ecp4set1)))
             {
                 printf("ERROR computing ECP4_ZZZ from coordinate x and with y set2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_ecp8_arithmetics_ZZZ.c.in b/test/test_ecp8_arithmetics_ZZZ.c.in
index f974406..43640fd 100644
--- a/test/test_ecp8_arithmetics_ZZZ.c.in
+++ b/test/test_ecp8_arithmetics_ZZZ.c.in
@@ -161,6 +161,7 @@
             if(!read_ECP8_ZZZ(&ecp81,linePtr) || ECP8_ZZZ_isinf(&ecp81))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_get(&FP8aux1,&FP8aux2,&ecp81);
@@ -169,6 +170,7 @@
             if (!FP8_YYY_equals(&FP8aux1,&FP8aux2))
             {
                 printf("ERROR computing right hand side of equation ECP, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_toOctet(&OCTaux,&ecp81);
@@ -176,6 +178,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp81))
             {
                 printf("ERROR converting ECP8_ZZZ to/from OCTET, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(ecp8frobs,&ecp81);
@@ -192,6 +195,7 @@
             if(!read_ECP8_ZZZ(&ecp82,linePtr) || ECP8_ZZZ_isinf(&ecp82))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -202,6 +206,7 @@
             if(!read_ECP8_ZZZ(&ecp8sum,linePtr))
             {
                 printf("ERROR reading test vector input ECP8_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81);
@@ -215,6 +220,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8sum) || !ECP8_ZZZ_equals(&ECP8aux2,&ecp8sum))
             {
                 printf("ERROR adding two ECP8_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81); // testing associativity (P+Q)+R = P+(Q+R)
@@ -228,6 +234,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ECP8aux2))
             {
                 printf("ERROR testing associativity bewtween three ECP8_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -238,6 +245,7 @@
             if(!read_ECP8_ZZZ(&ecp8neg,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81);
@@ -246,6 +254,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8neg))
             {
                 printf("ERROR computing negative of ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -256,6 +265,7 @@
             if(!read_ECP8_ZZZ(&ecp8sub,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81);
@@ -264,6 +274,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8sub))
             {
                 printf("ERROR performing subtraction bewtween two ECP8_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -274,6 +285,7 @@
             if(!read_ECP8_ZZZ(&ecp8dbl,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81);
@@ -282,6 +294,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8dbl))
             {
                 printf("ERROR computing double of ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -301,6 +314,7 @@
             if(!read_ECP8_ZZZ(&ecp8mul,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_copy(&ECP8aux1,&ecp81);
@@ -309,6 +323,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8mul))
             {
                 printf("ERROR computing multiplication of ECP8_ZZZ by a scalar, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -323,6 +338,7 @@
                 if(!FP2_YYY_equals(&fp2fr,F+k))
                 {
                     printf("ERROR computing %d-th frobenius constant, line %d\n",k+1,i);
+                    fclose(fp);
                     exit(EXIT_FAILURE);
                 }
             }
@@ -339,6 +355,7 @@
             if(!ECP8_ZZZ_equals(&ecp8frob,&ECP8aux1))
             {
                 printf("ERROR computing frobenius action (P^3), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -349,6 +366,7 @@
             if(!read_ECP8_ZZZ(&ecp8mul16,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 
@@ -358,6 +376,7 @@
             if(!ECP8_ZZZ_equals(&ECP8aux1,&ecp8mul16))
             {
                 printf("ERROR computing linear combination of 8 ECP8_ZZZs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -368,6 +387,7 @@
             if(read_ECP8_ZZZ(&ecp8wrong,linePtr) || !ECP8_ZZZ_isinf(&ecp8wrong) || !ECP8_ZZZ_equals(&ecp8wrong,&inf))
             {
                 printf("ERROR identifying a wrong ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -378,6 +398,7 @@
             if(read_ECP8_ZZZ(&ecp8inf,linePtr) || !ECP8_ZZZ_isinf(&ecp8inf) || !ECP8_ZZZ_equals(&ecp8inf,&inf))
             {
                 printf("ERROR identifying infinite point ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -388,6 +409,7 @@
             if(!read_ECP8_ZZZ(&ecp8set1,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP8_ZZZ_get(&FP8aux1,&FP8aux2,&ecp81);
@@ -400,11 +422,13 @@
             if(!read_ECP8_ZZZ(&ecp8set2,linePtr))
             {
                 printf("ERROR getting test vector input ECP8_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             if((!ECP8_ZZZ_equals(&ECP8aux1,&ecp8set2)) && (!ECP8_ZZZ_equals(&ECP8aux1,&ecp8set1)))
             {
                 printf("ERROR computing ECP8_ZZZ from coordinate x and with y set2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_ecp_arithmetics_ZZZ.c.in b/test/test_ecp_arithmetics_ZZZ.c.in
index b877474..bbdbe11 100644
--- a/test/test_ecp_arithmetics_ZZZ.c.in
+++ b/test/test_ecp_arithmetics_ZZZ.c.in
@@ -153,6 +153,7 @@
             if(!read_ECP_ZZZ(&ecp1,linePtr) || ECP_ZZZ_isinf(&ecp1))
             {
                 printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 #if CURVETYPE_ZZZ!=MONTGOMERY
@@ -174,6 +175,7 @@
             if(!ECP_ZZZ_equals(&ECPaux1,&ecp1)) // test octet conversion
             {
                 printf("ERROR converting ECP_ZZZ to/from OCTET, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -264,6 +266,7 @@
             if(!read_ECP_ZZZ(&ecpdbl,linePtr) || ECP_ZZZ_isinf(&ecpdbl))
             {
                 printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP_ZZZ_copy(&ECPaux1,&ecp1);
@@ -274,6 +277,7 @@
                 ECP_ZZZ_outputxyz(&ECPaux1);
                 ECP_ZZZ_outputxyz(&ecpdbl);
                 printf("ERROR computing double of ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -285,6 +289,7 @@
             if(!read_ECP_ZZZ(&ecpmul3,linePtr) || ECP_ZZZ_isinf(&ecpmul3))
             {
                 printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             BIG_XXX_one(BIGaux1);
@@ -296,6 +301,7 @@
             if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul3))
             {
                 printf("ERROR computing multiplication of ECP_ZZZ by 3, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP_ZZZ_copy(&ECPaux1,&ecpdbl);
@@ -303,6 +309,7 @@
             if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul3))
             {
                 printf("ERROR computing multiplication of ECP_ZZZ by 3, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -320,6 +327,7 @@
             if(!read_ECP_ZZZ(&ecpmul,linePtr))
             {
                 printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             ECP_ZZZ_copy(&ECPaux1,&ecp1);
@@ -331,6 +339,7 @@
                 ECP_ZZZ_outputxyz(&ECPaux1);
                 ECP_ZZZ_outputxyz(&ecpmul);
                 printf("ERROR computing multiplication of ECP_ZZZ by a scalar, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -386,6 +395,7 @@
             if(read_ECP_ZZZ(&ecpwrong,linePtr) || !ECP_ZZZ_isinf(&ecpwrong) || !ECP_ZZZ_equals(&ecpwrong,&inf))
             {
                 printf("ERROR identifying wrong ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -396,6 +406,7 @@
             if(read_ECP_ZZZ(&ecpinf,linePtr) || !ECP_ZZZ_isinf(&ecpinf) || !ECP_ZZZ_equals(&ecpinf,&inf))
             {
                 printf("ERROR identifying infinite point ECP_ZZZ, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_fp2_arithmetics_YYY.c.in b/test/test_fp2_arithmetics_YYY.c.in
index df9662d..e0d646b 100644
--- a/test/test_fp2_arithmetics_YYY.c.in
+++ b/test/test_fp2_arithmetics_YYY.c.in
@@ -167,18 +167,21 @@
             if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 0)
             {
                 printf("ERROR in conditional copy of FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             FP2_YYY_cmove(&FP2aux1,&FP2_1,1);
             if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 1)
             {
                 printf("ERROR in conditional copy of FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             FP2_YYY_from_FPs(&FP2aux1,&FP2_1.a,&FP2_1.b);
             if(FP2_YYY_equals(&FP2aux1,&FP2_1) != 1)
             {
                 printf("ERROR in generating FP_YYY from two FPs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             FP_YYY_redc(BIGaux1,&FP2_1.a);
@@ -194,6 +197,7 @@
                 FP2_YYY_output(&FP2_1);
                 printf("\n");
                 printf("ERROR in generating FP_YYY from two BIGs, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             FP2_YYY_from_FP(&FP2aux1,&FP2_1.a);
@@ -202,6 +206,7 @@
             if(FP2_YYY_equals(&FP2aux1,&FP2aux2) != 1)
             {
                 printf("ERROR in generating FP_YYY from one FP, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             FP_YYY_redc(BIGaux1,&FP2_1.a);
@@ -211,6 +216,7 @@
             if(FP2_YYY_equals(&FP2aux1,&FP2aux2) != 1)
             {
                 printf("ERROR in generating FP_YYY from one BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -242,6 +248,7 @@
                 printf("\nExp: ");
                 FP2_YYY_output(&FP2add);
                 printf("ERROR adding two FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
 // test associativity (P+Q)+R = P+(Q+R)
@@ -256,6 +263,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2aux2))
             {
                 printf("ERROR testing associativity between three FP2s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -276,6 +284,7 @@
                 printf("\nExp: ");
                 FP2_YYY_output(&FP2neg);
                 printf("ERROR in computing negative of FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -291,6 +300,7 @@
             if(FP2_YYY_equals(&FP2aux1,&FP2sub) == 0)
             {
                 printf("ERROR subtraction between two FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -305,6 +315,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2conj))
             {
                 printf("ERROR computing conjugate of FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -326,6 +337,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2pmul))
             {
                 printf("ERROR in multiplication by BIG, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -353,6 +365,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2imul))
             {
                 printf("ERROR in multiplication by small integer, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -374,6 +387,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2_1) && !FP2_YYY_equals(&FP2aux2,&FP2_1))
             {
                 printf("ERROR square/square root consistency FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -387,6 +401,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2mul))
             {
                 printf("ERROR in multiplication between two FP2s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -401,6 +416,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2inv))
             {
                 printf("ERROR in computing inverse of FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -414,6 +430,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2div2))
             {
                 printf("ERROR in computing division FP_YYY by 2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -428,6 +445,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2_YYY_mulip))
             {
                 printf("ERROR in computing multiplication of FP_YYY by (1+sqrt(-1)), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -442,6 +460,7 @@
             if(!FP2_YYY_equals(&FP2aux1,&FP2_divip))
             {
                 printf("ERROR in computing division of FP_YYY by (1+sqrt(-1)), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_fp4_arithmetics_YYY.c.in b/test/test_fp4_arithmetics_YYY.c.in
index 8c8fe69..9de820a 100644
--- a/test/test_fp4_arithmetics_YYY.c.in
+++ b/test/test_fp4_arithmetics_YYY.c.in
@@ -192,6 +192,7 @@
     if(FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_isunity(&FP4aux1) || FP4_YYY_isunity(&FP4aux2) || FP4_YYY_iszilch(&FP4aux1) || !FP4_YYY_isreal(&FP4aux1))
     {
         printf("ERROR comparing FP4s or setting FP4 to unity FP\n");
+
         exit(EXIT_FAILURE);
     }
 
@@ -217,6 +218,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4_1))
             {
                 printf("ERROR in generating FP4 from two FP2s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test FP4_from_FP2
@@ -226,6 +228,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4aux2))
             {
                 printf("ERROR in generating FP4 from one FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test FP4_from_FP2H
@@ -235,6 +238,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4aux2))
             {
                 printf("ERROR in generating \"complex\" FP4 from one FP2 , line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -288,6 +292,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4add) || !FP4_YYY_equals(&FP4aux2,&FP4add))
             {
                 printf("ERROR adding two FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test associativity (P+Q)+R = P+(Q+R)
@@ -304,6 +309,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4aux2))
             {
                 printf("ERROR testing associativity between three FP4s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -318,6 +324,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4neg))
             {
                 printf("ERROR in computing negative of FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -333,6 +340,7 @@
             if(FP4_YYY_equals(&FP4aux1,&FP4sub) == 0)
             {
                 printf("ERROR subtraction between two FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -347,6 +355,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4conj))
             {
                 printf("ERROR computing conjugate of FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -361,6 +370,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4nconj))
             {
                 printf("ERROR computing negative conjugate of FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -381,6 +391,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4pmul))
             {
                 printf("ERROR in multiplication by FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -395,6 +406,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4imul))
             {
                 printf("ERROR in multiplication by small integer, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -409,6 +421,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4sqr))
             {
                 printf("ERROR in squaring FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -422,6 +435,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4mul))
             {
                 printf("ERROR in multiplication between two FP4s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -436,6 +450,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4inv))
             {
                 printf("ERROR in computing inverse of FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -450,6 +465,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4mulj))
             {
                 printf("ERROR in  multiplication of an FP4 instance by sqrt(1+sqrt(-1)), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -476,6 +492,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4pow))
             {
                 printf("ERROR in raising FP4 by BIG power, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -489,6 +506,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrA))
             {
                 printf("ERROR in testing the XTR addition function r=w*x-conj(x)*y+z, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -502,6 +520,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrD))
             {
                 printf("ERROR in testing the XTR doubling function r=x^2-2*conj(x), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -515,6 +534,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrpow))
             {
                 printf("ERROR computing FP4 trace of an FP12 raised to the power of a BIG number, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -529,6 +549,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrpow2))
             {
                 printf("ERROR computing FP4 trace of an FP12 raised to the power of a BIG number (Double), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -541,6 +562,7 @@
             if(!FP4_YYY_equals(&FP4aux1,&FP4div2))
             {
                 printf("ERROR dividing FP4 by sqrt(sqrt(1+sqrt(-1)))/2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_fp8_arithmetics_YYY.c.in b/test/test_fp8_arithmetics_YYY.c.in
index 58ddb06..b8dfde6 100644
--- a/test/test_fp8_arithmetics_YYY.c.in
+++ b/test/test_fp8_arithmetics_YYY.c.in
@@ -267,6 +267,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8_1))
             {
                 printf("ERROR in generating FP8 from two FP4s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test FP8_from_FP4
@@ -276,6 +277,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8aux2))
             {
                 printf("ERROR in generating FP8 from one FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test FP8_from_FP4
@@ -285,6 +287,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8aux2))
             {
                 printf("ERROR in generating FP8 from one FP4 as high part, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -310,6 +313,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8add) || !FP8_YYY_equals(&FP8aux2,&FP8add))
             {
                 printf("ERROR adding two FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
             // test associativity (P+Q)+R = P+(Q+R)
@@ -326,6 +330,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8aux2))
             {
                 printf("ERROR testing associativity between three FP8s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -340,6 +345,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8neg))
             {
                 printf("ERROR in computing negative of FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -355,6 +361,7 @@
             if(FP8_YYY_equals(&FP8aux1,&FP8sub) == 0)
             {
                 printf("ERROR subtraction between two FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -369,6 +376,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8conj))
             {
                 printf("ERROR computing conjugate of FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -383,6 +391,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8nconj))
             {
                 printf("ERROR computing negative conjugate of FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -418,6 +427,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8pmul))
             {
                 printf("ERROR in multiplication by FP4, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -431,6 +441,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8qmul))
             {
                 printf("ERROR in multiplication by FP2, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -444,6 +455,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8tmul))
             {
                 printf("ERROR in multiplication by FP, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -458,6 +470,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8imul))
             {
                 printf("ERROR in multiplication by small integer, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -471,6 +484,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8sqr))
             {
                 printf("ERROR in squaring FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -484,6 +498,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8mul))
             {
                 printf("ERROR in multiplication between two FP8s, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -497,6 +512,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8inv))
             {
                 printf("ERROR in computing inverse of FP8, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -511,6 +527,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8mulj))
             {
                 printf("ERROR in  multiplication of an FP8 instance by sqrt(sqrt(1+sqrt(-1))), line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
@@ -537,6 +554,7 @@
             if(!FP8_YYY_equals(&FP8aux1,&FP8pow))
             {
                 printf("ERROR in raising FP8 by BIG power, line %d\n",i);
+                fclose(fp);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_octet_consistency.c b/test/test_octet_consistency.c
index d45f3b2..19ad0d4 100644
--- a/test/test_octet_consistency.c
+++ b/test/test_octet_consistency.c
@@ -39,7 +39,7 @@
     char raw[256], bytes[len+1], bytes64[len64+1], bytesHex[lenHex+1], v[len], w[len];
     octet V= {0,sizeof(v),v}, W= {0,sizeof(w),w};
     csprng rng;
-
+    char originByteHex[lenHex+1];
     /* Fake random source */
     RAND_clean(&rng);
     for (i=0; i<256; i++) raw[i]=(char)i;
@@ -103,10 +103,15 @@
             OCT_rand(&W,&rng,len);
             OCT_copy(&V,&W);
             OCT_toHex(&W,bytesHex);
+
             OCT_fromHex(&W,bytesHex);
+            // originByteHex
+            OCT_toHex(&W,originByteHex);
+            printf("Bucket %d\n", strcmp(bytesHex, originByteHex));
+
             if(!OCT_comp(&V,&W))
             {
-                printf("ERROR converting to and from Hex OCTET\n");
+                printf("ERROR converting to and from Hex OCTET : %s %s\n", bytesHex, originByteHex);
                 exit(EXIT_FAILURE);
             }
         }
diff --git a/test/test_utils.c b/test/test_utils.c
index 9693fc2..20f4d92 100644
--- a/test/test_utils.c
+++ b/test/test_utils.c
@@ -92,11 +92,11 @@
 
     for (i=0; i<nIter; i++)
     {
-        amcl_hex2bin(V2[i], bintemp, 64);
-        amcl_bin2hex(bintemp, temp, 32);
-        if (strncmp(V2[i], temp, 64))
+        amcl_hex2bin(V2[i], bintemp, 32);
+        amcl_bin2hex(bintemp, temp, sizeof(bintemp), sizeof(temp));
+        if (strncmp(V2[i], temp, 32))
         {
-            printf("FAILURE conversion hex/bin\n");
+            printf("FAILURE conversion hex/bin %s %s\n", V2[i], temp);
             return 1;
         }
     }