瀏覽代碼

Facelift of *_file functions

Karel Miko 8 年之前
父節點
當前提交
336c52ca5f

+ 16 - 10
src/hashes/helper/hash_filehandle.c

@@ -27,7 +27,7 @@
 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
 {
 {
     hash_state md;
     hash_state md;
-    unsigned char buf[512];
+    unsigned char *buf;
     size_t x;
     size_t x;
     int err;
     int err;
 
 
@@ -35,30 +35,36 @@ int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outle
     LTC_ARGCHK(outlen != NULL);
     LTC_ARGCHK(outlen != NULL);
     LTC_ARGCHK(in     != NULL);
     LTC_ARGCHK(in     != NULL);
 
 
+    if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+        return CRYPT_MEM;
+    }
+
     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
-        return err;
+        goto LBL_ERR;
     }
     }
 
 
     if (*outlen < hash_descriptor[hash].hashsize) {
     if (*outlen < hash_descriptor[hash].hashsize) {
        *outlen = hash_descriptor[hash].hashsize;
        *outlen = hash_descriptor[hash].hashsize;
-       return CRYPT_BUFFER_OVERFLOW;
+       err = CRYPT_BUFFER_OVERFLOW;
+       goto LBL_ERR;
     }
     }
     if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
     if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
-       return err;
+       goto LBL_ERR;
     }
     }
 
 
     *outlen = hash_descriptor[hash].hashsize;
     *outlen = hash_descriptor[hash].hashsize;
     do {
     do {
-        x = fread(buf, 1, sizeof(buf), in);
+        x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
         if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
         if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
-           return err;
+           goto LBL_CLEANBUF;
         }
         }
-    } while (x == sizeof(buf));
+    } while (x == LTC_FILE_READ_BUFSIZE);
     err = hash_descriptor[hash].done(&md, out);
     err = hash_descriptor[hash].done(&md, out);
 
 
-#ifdef LTC_CLEAN_STACK
-    zeromem(buf, sizeof(buf));
-#endif
+LBL_CLEANBUF:
+    zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
+    XFREE(buf);
     return err;
     return err;
 }
 }
 #endif /* #ifndef LTC_NO_FILE */
 #endif /* #ifndef LTC_NO_FILE */

+ 30 - 21
src/mac/f9/f9_file.c

@@ -22,14 +22,14 @@
    @param cipher   The index of the cipher desired
    @param cipher   The index of the cipher desired
    @param key      The secret key
    @param key      The secret key
    @param keylen   The length of the secret key (octets)
    @param keylen   The length of the secret key (octets)
-   @param filename The name of the file you wish to f9
+   @param fname    The name of the file you wish to f9
    @param out      [out] Where the authentication tag is to be stored
    @param out      [out] Where the authentication tag is to be stored
    @param outlen   [in/out] The max size and resulting size of the authentication tag
    @param outlen   [in/out] The max size and resulting size of the authentication tag
    @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
    @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
 */
 */
 int f9_file(int cipher,
 int f9_file(int cipher,
               const unsigned char *key, unsigned long keylen,
               const unsigned char *key, unsigned long keylen,
-              const char *filename,
+              const char *fname,
                     unsigned char *out, unsigned long *outlen)
                     unsigned char *out, unsigned long *outlen)
 {
 {
 #ifdef LTC_NO_FILE
 #ifdef LTC_NO_FILE
@@ -39,41 +39,50 @@ int f9_file(int cipher,
    int err;
    int err;
    f9_state f9;
    f9_state f9;
    FILE *in;
    FILE *in;
-   unsigned char buf[512];
+   unsigned char *buf;
 
 
-   LTC_ARGCHK(key      != NULL);
-   LTC_ARGCHK(filename != NULL);
-   LTC_ARGCHK(out      != NULL);
-   LTC_ARGCHK(outlen   != NULL);
+   LTC_ARGCHK(key    != NULL);
+   LTC_ARGCHK(fname  != NULL);
+   LTC_ARGCHK(out    != NULL);
+   LTC_ARGCHK(outlen != NULL);
 
 
-   in = fopen(filename, "rb");
-   if (in == NULL) {
-      return CRYPT_FILE_NOTFOUND;
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
    }
    }
 
 
    if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) {
    if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) {
-      fclose(in);
-      return err;
+      goto LBL_ERR;
+   }
+
+   in = fopen(fname, "rb");
+   if (in == NULL) {
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
    }
    }
 
 
    do {
    do {
-      x = fread(buf, 1, sizeof(buf), in);
+      x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = f9_process(&f9, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = f9_process(&f9, buf, (unsigned long)x)) != CRYPT_OK) {
          fclose(in);
          fclose(in);
-         return err;
+         goto LBL_CLEANBUF;
       }
       }
-   } while (x == sizeof(buf));
-   fclose(in);
+   } while (x == LTC_FILE_READ_BUFSIZE);
 
 
-   if ((err = f9_done(&f9,    out, outlen)) != CRYPT_OK) {
-      return err;
+   if (fclose(in) != 0) {
+      err = CRYPT_ERROR;
+      goto LBL_CLEANBUF;
    }
    }
 
 
+   err = f9_done(&f9, out, outlen);
+
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
-   zeromem(buf, sizeof(buf));
+   zeromem(&f9, sizeof(f9_state));
 #endif
 #endif
-
-   return CRYPT_OK;
+   XFREE(buf);
+   return err;
 #endif
 #endif
 }
 }
 
 

+ 23 - 20
src/mac/hmac/hmac_file.c

@@ -36,7 +36,7 @@ int hmac_file(int hash, const char *fname,
 #else
 #else
    hmac_state hmac;
    hmac_state hmac;
    FILE *in;
    FILE *in;
-   unsigned char buf[512];
+   unsigned char *buf;
    size_t x;
    size_t x;
    int err;
    int err;
 
 
@@ -45,49 +45,52 @@ int hmac_file(int hash, const char *fname,
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
    LTC_ARGCHK(outlen != NULL);
 
 
-   if((err = hash_is_valid(hash)) != CRYPT_OK) {
-       return err;
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
+   }
+
+   if ((err = hash_is_valid(hash)) != CRYPT_OK) {
+      goto LBL_ERR;
    }
    }
 
 
    if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
    if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
-       return err;
+      goto LBL_ERR;
    }
    }
 
 
    in = fopen(fname, "rb");
    in = fopen(fname, "rb");
    if (in == NULL) {
    if (in == NULL) {
-      return CRYPT_FILE_NOTFOUND;
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
    }
    }
 
 
-   /* process the file contents */
    do {
    do {
-      x = fread(buf, 1, sizeof(buf), in);
+      x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
-         /* we don't trap this error since we're already returning an error! */
-         fclose(in);
-         return err;
+         fclose(in); /* we don't trap this error since we're already returning an error! */
+         goto LBL_CLEANBUF;
       }
       }
-   } while (x == sizeof(buf));
+   } while (x == LTC_FILE_READ_BUFSIZE);
 
 
    if (fclose(in) != 0) {
    if (fclose(in) != 0) {
-      return CRYPT_ERROR;
+      err = CRYPT_ERROR;
+      goto LBL_CLEANBUF;
    }
    }
 
 
-   /* get final hmac */
-   if ((err = hmac_done(&hmac, out, outlen)) != CRYPT_OK) {
-      return err;
-   }
+   err = hmac_done(&hmac, out, outlen);
 
 
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
-   /* clear memory */
-   zeromem(buf, sizeof(buf));
+   zeromem(&hmac, sizeof(hmac_state));
 #endif
 #endif
-   return CRYPT_OK;
+   XFREE(buf);
+   return err;
 #endif
 #endif
 }
 }
 
 
 #endif
 #endif
 
 
-
 /* $Source$ */
 /* $Source$ */
 /* $Revision$ */
 /* $Revision$ */
 /* $Date$ */
 /* $Date$ */

+ 24 - 15
src/mac/omac/omac_file.c

@@ -39,41 +39,50 @@ int omac_file(int cipher,
    int err;
    int err;
    omac_state omac;
    omac_state omac;
    FILE *in;
    FILE *in;
-   unsigned char buf[512];
+   unsigned char *buf;
 
 
    LTC_ARGCHK(key      != NULL);
    LTC_ARGCHK(key      != NULL);
    LTC_ARGCHK(filename != NULL);
    LTC_ARGCHK(filename != NULL);
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(outlen   != NULL);
    LTC_ARGCHK(outlen   != NULL);
 
 
-   in = fopen(filename, "rb");
-   if (in == NULL) {
-      return CRYPT_FILE_NOTFOUND;
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
    }
    }
 
 
    if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) {
    if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) {
-      fclose(in);
-      return err;
+      goto LBL_ERR;
+   }
+
+   in = fopen(filename, "rb");
+   if (in == NULL) {
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
    }
    }
 
 
    do {
    do {
-      x = fread(buf, 1, sizeof(buf), in);
+      x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = omac_process(&omac, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = omac_process(&omac, buf, (unsigned long)x)) != CRYPT_OK) {
          fclose(in);
          fclose(in);
-         return err;
+         goto LBL_CLEANBUF;
       }
       }
-   } while (x == sizeof(buf));
-   fclose(in);
+   } while (x == LTC_FILE_READ_BUFSIZE);
 
 
-   if ((err = omac_done(&omac, out, outlen)) != CRYPT_OK) {
-      return err;
+   if (fclose(in) != 0) {
+      err = CRYPT_ERROR;
+      goto LBL_CLEANBUF;
    }
    }
 
 
+   err = omac_done(&omac, out, outlen);
+
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
-   zeromem(buf, sizeof(buf));
+   zeromem(&omac, sizeof(omac_state));
 #endif
 #endif
-
-   return CRYPT_OK;
+   XFREE(buf);
+   return err;
 #endif
 #endif
 }
 }
 
 

+ 24 - 15
src/mac/pmac/pmac_file.c

@@ -39,7 +39,7 @@ int pmac_file(int cipher,
    int err;
    int err;
    pmac_state pmac;
    pmac_state pmac;
    FILE *in;
    FILE *in;
-   unsigned char buf[512];
+   unsigned char *buf;
 
 
 
 
    LTC_ARGCHK(key      != NULL);
    LTC_ARGCHK(key      != NULL);
@@ -47,34 +47,43 @@ int pmac_file(int cipher,
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(outlen   != NULL);
    LTC_ARGCHK(outlen   != NULL);
 
 
-   in = fopen(filename, "rb");
-   if (in == NULL) {
-      return CRYPT_FILE_NOTFOUND;
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
    }
    }
 
 
    if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
    if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
-      fclose(in);
-      return err;
+      goto LBL_ERR;
+   }
+
+   in = fopen(filename, "rb");
+   if (in == NULL) {
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
    }
    }
 
 
    do {
    do {
-      x = fread(buf, 1, sizeof(buf), in);
+      x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) {
          fclose(in);
          fclose(in);
-         return err;
+         goto LBL_CLEANBUF;
       }
       }
-   } while (x == sizeof(buf));
-   fclose(in);
+   } while (x == LTC_FILE_READ_BUFSIZE);
 
 
-   if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) {
-      return err;
+   if (fclose(in) != 0) {
+      err = CRYPT_ERROR;
+      goto LBL_CLEANBUF;
    }
    }
 
 
+   err = pmac_done(&pmac, out, outlen);
+
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
-   zeromem(buf, sizeof(buf));
+   zeromem(&pmac, sizeof(pmac_state));
 #endif
 #endif
-
-   return CRYPT_OK;
+   XFREE(buf);
+   return err;
 #endif
 #endif
 }
 }
 
 

+ 22 - 8
src/mac/poly1305/poly1305_file.c

@@ -21,8 +21,8 @@
   @param fname    The name of the file you wish to POLY1305
   @param fname    The name of the file you wish to POLY1305
   @param key      The secret key
   @param key      The secret key
   @param keylen   The length of the secret key
   @param keylen   The length of the secret key
-  @param out      [out] The POLY1305 authentication tag
-  @param outlen   [in/out]  The max size and resulting size of the authentication tag
+  @param mac      [out] The POLY1305 authentication tag
+  @param maclen   [in/out]  The max size and resulting size of the authentication tag
   @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
   @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
 */
 */
 int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
 int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
@@ -41,23 +41,37 @@ int poly1305_file(const char *fname, const unsigned char *key, unsigned long key
    LTC_ARGCHK(mac    != NULL);
    LTC_ARGCHK(mac    != NULL);
    LTC_ARGCHK(maclen != NULL);
    LTC_ARGCHK(maclen != NULL);
 
 
-   if ((in = fopen(fname, "rb")) == NULL)                   { return CRYPT_FILE_NOTFOUND; }
-   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL)      { return CRYPT_MEM; }
-   if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) { goto LBL_ERR; }
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
+   }
+
+   if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) {
+      goto LBL_ERR;
+   }
+
+   in = fopen(fname, "rb");
+   if (in == NULL) {
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
+   }
 
 
    do {
    do {
       x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
          fclose(in);
          fclose(in);
-         goto LBL_ERR;
+         goto LBL_CLEANBUF;
       }
       }
    } while (x == LTC_FILE_READ_BUFSIZE);
    } while (x == LTC_FILE_READ_BUFSIZE);
-   if (fclose(in) != 0)  {
+
+   if (fclose(in) != 0) {
       err = CRYPT_ERROR;
       err = CRYPT_ERROR;
-      goto LBL_ERR;
+      goto LBL_CLEANBUF;
    }
    }
+
    err = poly1305_done(&st, mac, maclen);
    err = poly1305_done(&st, mac, maclen);
 
 
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
 LBL_ERR:
 LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
    zeromem(&st, sizeof(poly1305_state));
    zeromem(&st, sizeof(poly1305_state));

+ 24 - 15
src/mac/xcbc/xcbc_file.c

@@ -39,41 +39,50 @@ int xcbc_file(int cipher,
    int err;
    int err;
    xcbc_state xcbc;
    xcbc_state xcbc;
    FILE *in;
    FILE *in;
-   unsigned char buf[512];
+   unsigned char *buf;
 
 
    LTC_ARGCHK(key      != NULL);
    LTC_ARGCHK(key      != NULL);
    LTC_ARGCHK(filename != NULL);
    LTC_ARGCHK(filename != NULL);
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(out      != NULL);
    LTC_ARGCHK(outlen   != NULL);
    LTC_ARGCHK(outlen   != NULL);
 
 
-   in = fopen(filename, "rb");
-   if (in == NULL) {
-      return CRYPT_FILE_NOTFOUND;
+   if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
+      return CRYPT_MEM;
    }
    }
 
 
    if ((err = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) {
    if ((err = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) {
-      fclose(in);
-      return err;
+      goto LBL_ERR;
+   }
+
+   in = fopen(filename, "rb");
+   if (in == NULL) {
+      err = CRYPT_FILE_NOTFOUND;
+      goto LBL_ERR;
    }
    }
 
 
    do {
    do {
-      x = fread(buf, 1, sizeof(buf), in);
+      x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
       if ((err = xcbc_process(&xcbc, buf, (unsigned long)x)) != CRYPT_OK) {
       if ((err = xcbc_process(&xcbc, buf, (unsigned long)x)) != CRYPT_OK) {
          fclose(in);
          fclose(in);
-         return err;
+         goto LBL_CLEANBUF;
       }
       }
-   } while (x == sizeof(buf));
-   fclose(in);
+   } while (x == LTC_FILE_READ_BUFSIZE);
 
 
-   if ((err = xcbc_done(&xcbc, out, outlen)) != CRYPT_OK) {
-      return err;
+   if (fclose(in) != 0) {
+      err = CRYPT_ERROR;
+      goto LBL_CLEANBUF;
    }
    }
 
 
+   err = xcbc_done(&xcbc, out, outlen);
+
+LBL_CLEANBUF:
+   zeromem(buf, LTC_FILE_READ_BUFSIZE);
+LBL_ERR:
 #ifdef LTC_CLEAN_STACK
 #ifdef LTC_CLEAN_STACK
-   zeromem(buf, sizeof(buf));
+   zeromem(&xcbc, sizeof(xcbc_state));
 #endif
 #endif
-
-   return CRYPT_OK;
+   XFREE(buf);
+   return err;
 #endif
 #endif
 }
 }