Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2012 18:39
Show Gist options
  • Save anonymous/4382120 to your computer and use it in GitHub Desktop.
Save anonymous/4382120 to your computer and use it in GitHub Desktop.
Testing out libgcrypt for use in a new projecty type thing.
/*
Yeah, so obviously I need to use a better salt for the key derivation, and of course I need to do a better job all together with other things (IV, etc).
This is just a test run for using libgcrypt to do symmetric encryption, however.
GCRY_KDF_PBKDF2
gcry_kdf_derive ( const void *passphrase, size_t passphraselen, int algo, int subalgo, const void *salt, size_t saltlen, unsigned long iterations, size_t keysize, void *keybuffer )
GCRY_CIPHER_MODE_CBC
GCRY_CIPHER_AES256
GCRY_CIPHER_SECURE
gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t h, const void *k, size_t l)
gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t h, const void *k, size_t l)
gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, unsigned char *out, size_t outsize, const unsigned char *in, size_t inlen)
Compile with:
$ gcc -o foo foo.c -l gcrypt
$ ./foo
Password:
Plaintext: the world is mine
Derive: Success
password salted with ab -> 39d89f9c3cddba11db0cf102ddf1134e82eea2de7ac78ded55aa7facc7ff3199
Open: Success
Set key: Success
Set IV: Success
Encrypt: Success
Set IV: Success
decrypt: Success
Plaintext: the world is mine
Encrypted: 6c7d8c7feea42526666f72e629f7912ddcdef112cd4101392225f33d809b5a589988f263dd06b73b3940fbab2ab5cbbe5ea5461683ff1a2bd098e19761eeda71b1ae0319961f62effc8f1c113ec3182bb2e809f28f25b00b89f89f569a35430561560f15f9e76a1551bcc8d7bb8698b08936c5dd764efbaaed13bb71939a96109a806bfa0e63dc6962a0cf75b40d20a3fafc51bf5bd86679f6280dda01dc19b50d9e6baeb0e75264573c7807773eb9c4b8a0e626589ec655b557ebb3445d7b4f632272c119d4e325eed252f55a4ec275fd26cb62c997fcbd96cde683c0ef49f3bb9da06440e68b64b364afebbf2501e3125f475ba49f44226915146d6d429aad65d3551ca970104b66f706499ff3c98985b0a6390d4564e6578f369519e7b14770dba4edec1125da67853e4237ec6cf957d0a22967ca4ae61bae38f7002a0eb419c90bde899a39056436d291b4fca4c2525035db850401c150d5af575ef5723aadefc234368bde50f1fd369e2cd5f49484484a0686dd768c72b4c5dfa3093ec30675320da7c0566c7483d8a39027269aa4edede9d8aa98e3801d3402252dbbd18b5cfe82c8af346e4e998e30c15d0e67a2d2a1c4ad535cc8931214d60bf831ccd9243e81f26825d63cb62e0db1730a0bb06bf204e3bd43d99ed1d4325b8a080fe6423aebf69f65730c0554707a94b816893d6fbeb6d22a6ba445a9454711c1ff
Decrypted: 74686520776f726c64206973206d696e65000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
ASCII: the world is mine
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gcrypt.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
int i;
char *pass;
unsigned char keybuffer[33];
unsigned char plaintext[512];
unsigned char ciphertext[512];
unsigned char deciphertext[512];
char iv[32] = { 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 };
gcry_cipher_hd_t hand;
memset(plaintext, 0, 512);
memset(ciphertext, 0, 512);
memset(deciphertext, 0, 512);
pass = getpass("Password: ");
printf("Plaintext: ");
gets(plaintext);
/* Derive a key! */
printf("Derive: %s\n",
gcry_strerror(
gcry_kdf_derive(pass, strlen(pass), GCRY_KDF_PBKDF2, GCRY_MD_SHA256, "ab", 2, 20, 32, keybuffer)
));
/* Derived key! */
printf("%s salted with %s -> ", pass, "ab");
for (i = 0; i < 32; i++) {
printf("%.2x", keybuffer[i]);
}
printf("\n");
printf("Open: %s\n",
gcry_strerror(
gcry_cipher_open(&hand, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_SECURE)
));
printf("\n");
printf("Set key: %s\n",
gcry_strerror(
gcry_cipher_setkey(hand, keybuffer, 32)
));
printf("\n");
printf("Set IV: %s\n",
gcry_strerror(
gcry_cipher_setiv(hand, iv, 16)
));
printf("\n");
printf("Encrypt: %s\n",
gcry_strerror(
gcry_cipher_encrypt(hand, ciphertext, 512, plaintext, 512)
));
printf("\n");
printf("Set IV: %s\n",
gcry_strerror(
gcry_cipher_setiv(hand, iv, 16)
));
printf("\n");
printf("decrypt: %s\n",
gcry_strerror(
gcry_cipher_decrypt(hand, deciphertext, 512, ciphertext, 512)
));
printf("\n");
/* Plaintext */
printf("Plaintext: %s\n", plaintext);
printf("Encrypted: ");
/* Encrypted */
for (i = 0; i < 512; i++) {
printf("%.2x", ciphertext[i]);
}
printf("\n");
printf("Decrypted: ");
/* Deciphered */
for (i = 0; i < 512; i++) {
printf("%.2x", deciphertext[i]);
}
printf("\n");
printf("ASCII: %s\n", deciphertext);
memset(pass, 0, strlen(pass));
free(pass);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment