gcm_file.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @param res [out] Result of the operation, 1==valid, 0==invalid
  34. @return CRYPT_OK on success
  35. */
  36. int gcm_file( int cipher,
  37. const unsigned char *key, unsigned long keylen,
  38. const unsigned char *IV, unsigned long IVlen,
  39. const unsigned char *adata, unsigned long adatalen,
  40. const char *in,
  41. const char *out,
  42. unsigned long taglen,
  43. int direction,
  44. int *res)
  45. {
  46. int err;
  47. FILE *f_in = NULL, *f_out = NULL;
  48. LTC_ARGCHK(in != NULL);
  49. LTC_ARGCHK(out != NULL);
  50. LTC_ARGCHK(res != NULL);
  51. *res = 0;
  52. f_in = fopen(in, "rb");
  53. if (f_in == NULL) {
  54. err = CRYPT_FILE_NOTFOUND;
  55. goto LBL_ERR;
  56. }
  57. f_out = fopen(out, "w+b");
  58. if (f_out == NULL) {
  59. err = CRYPT_FILE_NOTFOUND;
  60. goto LBL_ERR;
  61. }
  62. err = gcm_filehandle(cipher, key, keylen, IV, IVlen, adata, adatalen, f_in, f_out, taglen, direction, res);
  63. LBL_ERR:
  64. if (f_out != NULL && fclose(f_out) != 0) {
  65. err = CRYPT_ERROR;
  66. }
  67. if (*res != 1) {
  68. remove(out);
  69. }
  70. if (f_in != NULL && fclose(f_in) != 0) {
  71. err = CRYPT_ERROR;
  72. }
  73. return err;
  74. }
  75. #endif
  76. #endif
  77. /* ref: $Format:%D$ */
  78. /* git commit: $Format:%H$ */
  79. /* commit time: $Format:%ai$ */