|
|
@@ -23,67 +23,96 @@
|
|
|
#include <string.h>
|
|
|
#include <time.h>
|
|
|
#include <sys/times.h>
|
|
|
+#include <gcrypt.h>
|
|
|
|
|
|
#include <vcc/vcc.h>
|
|
|
#include <vcc/version.h>
|
|
|
#include <vcc/pretty.h>
|
|
|
|
|
|
|
|
|
-#define KEYSIZE 8
|
|
|
-#define BLKSIZE 8
|
|
|
+/* i tried aes256 but sometimes i get something garbled. then i
|
|
|
+ * tried des, that traditional alghrithm created by ibm and
|
|
|
+ * it really works. */
|
|
|
+#define CIPHER_ALGO GCRY_CIPHER_DES
|
|
|
+#define show_err(err) fprintf(stderr, "error: %s: %s\n", \
|
|
|
+ gcry_strsource(err), gcry_strerror(err))
|
|
|
|
|
|
int keysize, blksize;
|
|
|
int msgsize;
|
|
|
-__uint64_t key;
|
|
|
+
|
|
|
+char *key, *iv;
|
|
|
+gcry_cipher_hd_t hd;
|
|
|
|
|
|
|
|
|
int show_algo_info(void) {
|
|
|
- keysize = KEYSIZE;
|
|
|
- blksize = BLKSIZE;
|
|
|
+ keysize = gcry_cipher_get_algo_keylen(CIPHER_ALGO);
|
|
|
+ blksize = gcry_cipher_get_algo_blklen(CIPHER_ALGO);
|
|
|
+
|
|
|
+ msgsize = MSG_SIZE / blksize * blksize;
|
|
|
+
|
|
|
+ if (unlikely(!(key = malloc(keysize))) || unlikely(!(iv = malloc(keysize)))) {
|
|
|
+ fprintf(stderr, "*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
|
|
|
printf("key size: %d\n", keysize);
|
|
|
printf("block size: %d\n", blksize);
|
|
|
- printf("maximum message size: %ld\n", MSG_SIZE);
|
|
|
+ printf("maximum message size: %d\n", msgsize);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int crypt_init(void *k) {
|
|
|
- key = *(__uint64_t *) &k;
|
|
|
+int crypt_init(void *k, void *v) {
|
|
|
+ gcry_error_t err;
|
|
|
+
|
|
|
+ memcpy(key, k, keysize);
|
|
|
+ memcpy(iv, v, blksize);
|
|
|
+
|
|
|
+ printf("libgcrypt %s\n", gcry_check_version(NULL));
|
|
|
+
|
|
|
+ if (unlikely((err = gcry_cipher_open(&hd, CIPHER_ALGO, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS)))) {
|
|
|
+ show_err(err);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely((err = gcry_cipher_setkey(hd, key, keysize)))) {
|
|
|
+ show_err(err);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely((err = gcry_cipher_setiv(hd, iv, blksize)))) {
|
|
|
+ show_err(err);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-char *encrypt(char *msg) {
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; msg[i]; i++) {
|
|
|
- if (msg[i] == 'z')
|
|
|
- msg[i] = 'a';
|
|
|
+int encrypt(char *msg) {
|
|
|
+ gcry_error_t err;
|
|
|
|
|
|
- else if (msg[i] == 'Z')
|
|
|
- msg[i] = 'A';
|
|
|
+ if (unlikely((err = gcry_cipher_encrypt(hd, msg, msgsize, NULL, 0)))) {
|
|
|
+ show_err(err);
|
|
|
|
|
|
- msg[i]++;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
- return msg;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
int decrypt(char *msg) {
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; msg[i]; i++) {
|
|
|
- if (msg[i] == 'a')
|
|
|
- msg[i] = 'z';
|
|
|
+ gcry_error_t err;
|
|
|
|
|
|
- else if (msg[i] == 'A')
|
|
|
- msg[i] = 'Z';
|
|
|
+ if (unlikely((err = gcry_cipher_decrypt(hd, msg, msgsize, NULL, 0)))) {
|
|
|
+ show_err(err);
|
|
|
|
|
|
- else
|
|
|
- msg[i]--;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
return 0;
|