crypt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* vcc/crypt.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <sys/times.h>
  22. #include <gcrypt.h>
  23. #include <vcc/vcc.h>
  24. #include <vcc/plugin.h>
  25. #include <vcc/version.h>
  26. #include <vcc/pretty.h>
  27. #include <vcc/interfaces.h>
  28. /* maybe because of that i'm not good at crypting (in fact, i even don't
  29. * know eular etc. ), i think the libgcrypt is the most ****ingest library
  30. * that i've used. it's a great gnu library, but the document is **too**
  31. * ****ing ****ing, that you can't know what you're doing. */
  32. #define CIPHER_ALGO GCRY_CIPHER_AES256
  33. #define show_err(err) fprintf(stderr, "error: %s: %s\n", \
  34. gcry_strsource(err), gcry_strerror(err))
  35. int keysize, blksize;
  36. int msgsize;
  37. int crypt_disabled = 0;
  38. char *key, *iv;
  39. gcry_cipher_hd_t hd;
  40. int show_algo_info(void) {
  41. keysize = gcry_cipher_get_algo_keylen(CIPHER_ALGO);
  42. blksize = gcry_cipher_get_algo_blklen(CIPHER_ALGO);
  43. msgsize = MSG_SIZE / blksize * blksize;
  44. if (unlikely(!(key = malloc(keysize))) || unlikely(!(iv = malloc(keysize)))) {
  45. fprintf(stderr, "*** malloc() failed\n");
  46. return 1;
  47. }
  48. printf(_("key size: %d\n"), keysize);
  49. printf(_("block size: %d\n"), blksize);
  50. printf(_("maximum message size: %d\n"), msgsize);
  51. return 0;
  52. }
  53. int crypt_init(void *k, void *v) {
  54. gcry_error_t err;
  55. memcpy(key, k, keysize);
  56. memcpy(iv, v, blksize);
  57. printf("libgcrypt %s\n", gcry_check_version(NULL));
  58. if (unlikely((err = gcry_cipher_open(&hd, CIPHER_ALGO, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS)))) {
  59. show_err(err);
  60. return 1;
  61. }
  62. if (unlikely((err = gcry_cipher_setkey(hd, key, keysize)))) {
  63. show_err(err);
  64. return 1;
  65. }
  66. if (unlikely((err = gcry_cipher_setiv(hd, iv, blksize)))) {
  67. show_err(err);
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. int encrypt(char *msg) {
  73. gcry_error_t err;
  74. if (crypt_disabled) {
  75. fprintf(stderr, _("error: crypting disabled. \n"));
  76. return 1;
  77. }
  78. if (unlikely((err = gcry_cipher_reset(hd)))) {
  79. show_err(err);
  80. return 1;
  81. }
  82. if (unlikely((err = gcry_cipher_encrypt(hd, msg, msgsize, NULL, 0)))) {
  83. show_err(err);
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. int decrypt(char *msg) {
  89. gcry_error_t err;
  90. if (crypt_disabled) {
  91. fprintf(stderr, _("error: crypt disabled. \n"));
  92. return 1;
  93. }
  94. if (unlikely((err = gcry_cipher_reset(hd)))) {
  95. show_err(err);
  96. return 1;
  97. }
  98. if (unlikely((err = gcry_cipher_decrypt(hd, msg, msgsize, NULL, 0)))) {
  99. show_err(err);
  100. return 1;
  101. }
  102. return 0;
  103. }