gcm_file.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include "tomcrypt.h"
  10. /**
  11. @file gcm_file.c
  12. GCM process a file, Steffen Jaeckel
  13. */
  14. #ifdef LTC_GCM_MODE
  15. #ifndef LTC_NO_FILE
  16. /**
  17. Process a file.
  18. c.f. gcm_filehandle() for basic documentation.
  19. It is possible, that in error-cases the 'out' file
  20. will be created and after the error occurred it will
  21. be removed again.
  22. @param cipher Index of cipher to use
  23. @param key The secret key
  24. @param keylen The length of the secret key
  25. @param IV The initial vector
  26. @param IVlen The length of the initial vector
  27. @param adata The additional authentication data (header)
  28. @param adatalen The length of the adata
  29. @param in The input file
  30. @param out The output file
  31. @param taglen The MAC tag length
  32. @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
  33. @return CRYPT_OK on success
  34. */
  35. int gcm_file( int cipher,
  36. const unsigned char *key, unsigned long keylen,
  37. const unsigned char *IV, unsigned long IVlen,
  38. const unsigned char *adata, unsigned long adatalen,
  39. const char *in,
  40. const char *out,
  41. unsigned long taglen,
  42. int direction,
  43. int *res)
  44. {
  45. int err;
  46. FILE *f_in = NULL, *f_out = NULL;
  47. LTC_ARGCHK(in != NULL);
  48. LTC_ARGCHK(out != NULL);
  49. LTC_ARGCHK(res != NULL);
  50. f_in = fopen(in, "rb");
  51. if (f_in == NULL) {
  52. err = CRYPT_FILE_NOTFOUND;
  53. goto LBL_ERR;
  54. }
  55. f_out = fopen(out, "w+b");
  56. if (f_out == NULL) {
  57. err = CRYPT_FILE_NOTFOUND;
  58. goto LBL_ERR;
  59. }
  60. err = gcm_filehandle(cipher, key, keylen, IV, IVlen, adata, adatalen, f_in, f_out, taglen, direction, res);
  61. LBL_ERR:
  62. if (f_out != NULL && fclose(f_out) != 0) {
  63. err = CRYPT_ERROR;
  64. }
  65. if (*res != 1) {
  66. remove(out);
  67. }
  68. if (f_in != NULL && fclose(f_in) != 0) {
  69. err = CRYPT_ERROR;
  70. }
  71. return err;
  72. }
  73. #endif
  74. #endif
  75. /* ref: $Format:%D$ */
  76. /* git commit: $Format:%H$ */
  77. /* commit time: $Format:%ai$ */