aesgcm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /**
  10. @file aesgcm.c
  11. AES128-GCM demo - file en-&decryption, Steffen Jaeckel
  12. Uses the format: |ciphertext|tag-16-bytes|
  13. */
  14. #define _GNU_SOURCE
  15. #include <tomcrypt.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <dirent.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include "gcm-file/gcm_filehandle.c"
  26. #include "gcm-file/gcm_file.c"
  27. static off_t fsize(const char *filename)
  28. {
  29. struct stat st;
  30. if (stat(filename, &st) == 0) return st.st_size;
  31. return -1;
  32. }
  33. #if defined(__linux__) && defined(__GLIBC_PREREQ)
  34. #if __GLIBC_PREREQ(2, 14)
  35. #define HAS_SYNCFS
  36. #endif
  37. #endif
  38. static int mv(const char *old_name, const char *new_name)
  39. {
  40. int fd;
  41. if (rename(old_name, new_name) == -1) return -1;
  42. fd = open(new_name, 0);
  43. if (fd == -1) return -1;
  44. #if !defined(_WIN32)
  45. if (fsync(fd) != 0) goto OUT;
  46. #if defined(HAS_SYNCFS)
  47. syncfs(fd);
  48. #else
  49. sync();
  50. #endif
  51. OUT:
  52. #endif
  53. close(fd);
  54. return 0;
  55. }
  56. /* https://stackoverflow.com/a/23898449 */
  57. static void scan_hex(const char* str, uint8_t* bytes, size_t blen)
  58. {
  59. uint8_t pos;
  60. uint8_t idx0;
  61. uint8_t idx1;
  62. const uint8_t hashmap[] =
  63. {
  64. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
  65. 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 89:;<=>? */
  66. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* @ABCDEFG */
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* HIJKLMNO */
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* PQRSTUVW */
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* XYZ[\]^_ */
  70. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* `abcdefg */
  71. };
  72. for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)
  73. {
  74. idx0 = (uint8_t)(str[pos+0] & 0x1F) ^ 0x10;
  75. idx1 = (uint8_t)(str[pos+1] & 0x1F) ^ 0x10;
  76. bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
  77. }
  78. }
  79. static void die(int ret)
  80. {
  81. fprintf(stderr, "Usage: aesgcm <-e|-d> <infile> <outfile> <96 char hex-string 'IV | key'>\n");
  82. exit(ret);
  83. }
  84. int main(int argc, char **argv)
  85. {
  86. int ret = 0, err, arg, direction, res, tmp;
  87. size_t keylen;
  88. uint8_t keybuf[48] = {0};
  89. char *out = NULL;
  90. const char *mode, *in_file, *out_file, *key_string;
  91. if (argc < 5) die(__LINE__);
  92. arg = 1;
  93. mode = argv[arg++];
  94. in_file = argv[arg++];
  95. out_file = argv[arg++];
  96. key_string = argv[arg++];
  97. if(strcmp(mode, "-d") == 0) direction = GCM_DECRYPT;
  98. else if(strcmp(mode, "-e") == 0) direction = GCM_ENCRYPT;
  99. else die(__LINE__);
  100. if (fsize(in_file) <= 0) die(__LINE__);
  101. keylen = strlen(key_string);
  102. if (keylen != 96) die(__LINE__);
  103. scan_hex(key_string, keybuf, sizeof(keybuf));
  104. register_all_ciphers();
  105. if(asprintf(&out, "%s-XXXXXX", out_file) < 0) die(__LINE__);
  106. if((tmp = mkstemp(out)) == -1) {
  107. ret = __LINE__;
  108. goto cleanup;
  109. }
  110. close(tmp);
  111. if((err = gcm_file(find_cipher("aes"), &keybuf[16], 32, keybuf, 16, NULL, 0, in_file, out, 16, direction, &res)) != CRYPT_OK) {
  112. fprintf(stderr, "boooh %s\n", error_to_string(err));
  113. ret = __LINE__;
  114. goto cleanup;
  115. }
  116. if(res != 1) {
  117. ret = __LINE__;
  118. }
  119. else
  120. {
  121. if (mv(out, out_file) != 0) ret = __LINE__;
  122. }
  123. cleanup:
  124. if(ret != 0) unlink(out);
  125. free(out);
  126. return ret;
  127. }
  128. /* ref: $Format:%D$ */
  129. /* git commit: $Format:%H$ */
  130. /* commit time: $Format:%ai$ */