gcm_filehandle.c 5.1 KB

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