crypt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/version.h>
  25. #include <vcc/pretty.h>
  26. /* maybe because of that i'm not good at crypting (in fact, i even don't
  27. * know eular etc. ), i think the libgcrypt is the most ****ingest library
  28. * that i've used. it's a great gnu library, but the document is **too**
  29. * ****ing ****ing, that you can't know what you're doing. */
  30. /* i tried aes256 but sometimes i get something garbled. then i
  31. * tried des, that traditional alghrithm created by ibm and
  32. * it really works. */
  33. #define CIPHER_ALGO GCRY_CIPHER_DES
  34. #define show_err(err) fprintf(stderr, "error: %s: %s\n", \
  35. gcry_strsource(err), gcry_strerror(err))
  36. int keysize, blksize;
  37. int msgsize;
  38. int crypt_disabled = 0;
  39. char *key, *iv;
  40. gcry_cipher_hd_t hd;
  41. int show_algo_info(void) {
  42. keysize = gcry_cipher_get_algo_keylen(CIPHER_ALGO);
  43. blksize = gcry_cipher_get_algo_blklen(CIPHER_ALGO);
  44. msgsize = MSG_SIZE / blksize * blksize;
  45. if (unlikely(!(key = malloc(keysize))) || unlikely(!(iv = malloc(keysize)))) {
  46. fprintf(stderr, "*** malloc() failed\n");
  47. return 1;
  48. }
  49. printf("key size: %d\n", keysize);
  50. printf("block size: %d\n", blksize);
  51. printf("maximum message size: %d\n", msgsize);
  52. return 0;
  53. }
  54. int crypt_init(void *k, void *v) {
  55. gcry_error_t err;
  56. memcpy(key, k, keysize);
  57. memcpy(iv, v, blksize);
  58. printf("libgcrypt %s\n", gcry_check_version(NULL));
  59. if (unlikely((err = gcry_cipher_open(&hd, CIPHER_ALGO, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS)))) {
  60. show_err(err);
  61. return 1;
  62. }
  63. if (unlikely((err = gcry_cipher_setkey(hd, key, keysize)))) {
  64. show_err(err);
  65. return 1;
  66. }
  67. if (unlikely((err = gcry_cipher_setiv(hd, iv, blksize)))) {
  68. show_err(err);
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. int encrypt(char *msg) {
  74. gcry_error_t err;
  75. if (crypt_disabled) {
  76. fprintf(stderr, "error: crypting disabled. \n");
  77. return 1;
  78. }
  79. if (unlikely((err = gcry_cipher_encrypt(hd, msg, msgsize, NULL, 0)))) {
  80. show_err(err);
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. int decrypt(char *msg) {
  86. gcry_error_t err;
  87. if (crypt_disabled) {
  88. fprintf(stderr, "error: crypt disabled. \n");
  89. return 1;
  90. }
  91. if (unlikely((err = gcry_cipher_decrypt(hd, msg, msgsize, NULL, 0)))) {
  92. show_err(err);
  93. return 1;
  94. }
  95. return 0;
  96. }