2
0

gcm_filehandle.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include "tomcrypt.h"
  4. /**
  5. @file gcm_filehandle.c
  6. GCM process a filehandle, Steffen Jaeckel
  7. */
  8. #ifdef LTC_GCM_MODE
  9. #ifndef LTC_NO_FILE
  10. #if defined(_MSC_VER)
  11. #define ftruncate _chsize
  12. #else
  13. #include <unistd.h>
  14. #endif
  15. /**
  16. Process a filehandle.
  17. This uses the widely established scheme where the tag is appended
  18. to the ciphertext.
  19. encrypted_file = aesgcm(plain_file) | aesgcm_tag(plain_file)
  20. Depending on 'direction' this function does:
  21. Encrypt file 'in' to 'out' and append the tag of length 'taglen'
  22. to 'out'.
  23. or
  24. Decrypt file 'in' to 'out' and check the tag of length 'taglen'
  25. and strip the tag from 'in'.
  26. In error-cases 'out' will be zeroed out first and then truncated to
  27. a length of 0.
  28. @param cipher Index of cipher to use
  29. @param key The secret key
  30. @param keylen The length of the secret key
  31. @param IV The initial vector
  32. @param IVlen The length of the initial vector
  33. @param adata The additional authentication data (header)
  34. @param adatalen The length of the adata
  35. @param in The input file
  36. @param out The output file
  37. @param taglen The MAC tag length
  38. @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
  39. @param res [out] Result of the operation, 1==valid, 0==invalid
  40. @return CRYPT_OK on success
  41. */
  42. int gcm_filehandle( int cipher,
  43. const unsigned char *key, unsigned long keylen,
  44. const unsigned char *IV, unsigned long IVlen,
  45. const unsigned char *adata, unsigned long adatalen,
  46. FILE *in,
  47. FILE *out,
  48. unsigned long taglen,
  49. int direction,
  50. int *res)
  51. {
  52. void *orig;
  53. gcm_state *gcm;
  54. int err;
  55. unsigned char *buf, tag[16];
  56. size_t x, tot_data;
  57. unsigned long tag_len;
  58. LTC_ARGCHK(in != NULL);
  59. LTC_ARGCHK(out != NULL);
  60. LTC_ARGCHK(res != NULL);
  61. *res = 0;
  62. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  63. return err;
  64. }
  65. #ifndef LTC_GCM_TABLES_SSE2
  66. orig = gcm = XMALLOC(sizeof(*gcm));
  67. #else
  68. orig = gcm = XMALLOC(sizeof(*gcm) + 16);
  69. #endif
  70. if (gcm == NULL) {
  71. return CRYPT_MEM;
  72. }
  73. if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
  74. XFREE(gcm);
  75. return CRYPT_MEM;
  76. }
  77. /* Force GCM to be on a multiple of 16 so we can use 128-bit aligned operations
  78. * note that we only modify gcm and keep orig intact. This code is not portable
  79. * but again it's only for SSE2 anyways, so who cares?
  80. */
  81. #ifdef LTC_GCM_TABLES_SSE2
  82. gcm = LTC_ALIGN_BUF(gcm, 16);
  83. #endif
  84. if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
  85. goto LBL_ERR;
  86. }
  87. if ((err = gcm_add_iv(gcm, IV, IVlen)) != CRYPT_OK) {
  88. goto LBL_ERR;
  89. }
  90. if ((err = gcm_add_aad(gcm, adata, adatalen)) != CRYPT_OK) {
  91. goto LBL_ERR;
  92. }
  93. fseek(in, 0, SEEK_END);
  94. tot_data = ftell(in);
  95. if (direction == GCM_DECRYPT) {
  96. tot_data -= taglen;
  97. }
  98. fseek(in, 0, SEEK_SET);
  99. do {
  100. x = MIN(tot_data, LTC_FILE_READ_BUFSIZE);
  101. x = fread(buf, 1, x, in);
  102. tot_data -= x;
  103. if ((err = gcm_process(gcm, buf, (unsigned long)x, buf, direction)) != CRYPT_OK) {
  104. goto LBL_CLEANBUF;
  105. }
  106. if(fwrite(buf, 1, x, out) != x) {
  107. err = CRYPT_ERROR;
  108. goto LBL_CLEANBUF;
  109. }
  110. } while (x == LTC_FILE_READ_BUFSIZE);
  111. tag_len = taglen;
  112. if ((err = gcm_done(gcm, tag, &tag_len)) != CRYPT_OK) {
  113. goto LBL_CLEANBUF;
  114. }
  115. if (tag_len != taglen) {
  116. err = CRYPT_ERROR;
  117. goto LBL_CLEANBUF;
  118. }
  119. if (direction == GCM_DECRYPT) {
  120. if (feof(in) || ferror(in)) {
  121. err = CRYPT_ERROR;
  122. goto LBL_CLEANBUF;
  123. }
  124. x = fread(buf, 1, taglen, in);
  125. if (x != taglen) {
  126. err = CRYPT_ERROR;
  127. goto LBL_CLEANBUF;
  128. }
  129. if (XMEM_NEQ(buf, tag, taglen) == 0) {
  130. *res = 1;
  131. }
  132. } else {
  133. if(fwrite(tag, 1, taglen, out) != taglen) {
  134. err = CRYPT_ERROR;
  135. goto LBL_CLEANBUF;
  136. }
  137. *res = 1;
  138. }
  139. LBL_CLEANBUF:
  140. zeromem(buf, LTC_FILE_READ_BUFSIZE);
  141. zeromem(tag, sizeof(tag));
  142. LBL_ERR:
  143. #ifdef LTC_CLEAN_STACK
  144. #ifndef LTC_GCM_TABLES_SSE2
  145. zeromem(orig, sizeof(*gcm));
  146. #else
  147. zeromem(orig, sizeof(*gcm) + 16);
  148. #endif
  149. #endif
  150. if(*res == 0) {
  151. x = ftell(out);
  152. fseek(in, 0, SEEK_SET);
  153. while((size_t)ftell(out) < x) {
  154. fwrite(buf, 1, LTC_FILE_READ_BUFSIZE, out);
  155. }
  156. if(ftruncate(fileno(out), 0)) {
  157. /* well, what shall we do here... */
  158. }
  159. }
  160. fflush(out);
  161. XFREE(buf);
  162. XFREE(orig);
  163. return err;
  164. }
  165. #endif
  166. #endif