25
26
#include "fpm_crypt.h"
28
void (*fpm_encrypt_block) (void *c, byte *outbuf, byte *inbuf);
29
void (*fpm_decrypt_block) (void *c, byte *outbuf, byte *inbuf);
29
static fpm_cipher ciphers[] = {
30
{ "BLOWFISH", 16, 4 },
34
void (*fpm_encrypt_block) (void *c, byte *outbuf, const byte *inbuf);
35
void (*fpm_decrypt_block) (void *c, byte *outbuf, const byte *inbuf);
31
37
static size_t keylen, blocksize, contextsize;
33
39
static void fpm_hex_to_bin(byte* out, const gchar* in, gint len);
34
40
static void fpm_bin_to_hex(gchar* out, const byte* in, gint len);
37
fpm_crypt_init(gchar* password)
42
void fpm_cipher_init(char *cipher_name) {
44
if(strcmp(cipher_name, "BLOWFISH") == 0) {
45
cipher_algo = BLOWFISH;
47
else if(strcmp(cipher_name, "AES-256") == 0) {
53
if (strcmp("BLOWFISH", blowfish_get_info(4, &keylen, &blocksize,
54
&contextsize, &fpm_setkey,
57
{ g_assert_not_reached(); }
60
if (strcmp("AES256", rijndael_get_info(9, &keylen, &blocksize,
61
&contextsize, &fpm_setkey,
64
{ g_assert_not_reached(); }
67
printf("Unknown cipher algorithm!\n");
70
cipher->name = ciphers[cipher_algo].name;
71
cipher->hash_len = ciphers[cipher_algo].hash_len;
72
cipher->salt_len = ciphers[cipher_algo].salt_len;
73
cipher->keylen = keylen;
74
cipher->blocksize = blocksize;
75
cipher->contextsize = contextsize;
78
void fpm_crypt_init(gchar* password) {
39
79
gchar *prehash1, *prehash2;
42
if (strcmp("BLOWFISH", blowfish_get_info(4, &keylen, &blocksize,
43
&contextsize, &fpm_setkey,
47
g_assert_not_reached();
49
old_context = g_malloc(contextsize);
50
new_context = g_malloc(contextsize);
52
prehash1=g_strdup_printf("%s%s", old_salt, password);
53
prehash2=g_strdup_printf("%s%s", new_salt, password);
56
md5_1=md5((byte *)prehash1, strlen(prehash1));
57
md5_2=md5((byte *)prehash2, strlen(prehash2));
59
fpm_setkey(old_context, md5_1, 16);
60
fpm_setkey(new_context, md5_2, 16);
80
byte *hash_1, *hash_2;
82
old_context = g_malloc(cipher->contextsize);
83
new_context = g_malloc(cipher->contextsize);
85
if(cipher_algo == AES256) {
86
hash_1 = g_malloc(cipher->hash_len);
87
hash_2 = g_malloc(cipher->hash_len);
89
pkcs5_pbkdf2(password, strlen(password), old_salt, cipher->salt_len, hash_1, cipher->hash_len, PBKDF2_ITERATIONS);
90
pkcs5_pbkdf2(password, strlen(password), new_salt, cipher->salt_len, hash_2, cipher->hash_len, PBKDF2_ITERATIONS);
92
prehash1=g_strdup_printf("%s%s", old_salt, password);
93
prehash2=g_strdup_printf("%s%s", new_salt, password);
95
hash_1=md5((byte *)prehash1, strlen(prehash1));
96
hash_2=md5((byte *)prehash2, strlen(prehash2));
99
fpm_setkey(old_context, hash_1, cipher->hash_len);
100
fpm_setkey(new_context, hash_2, cipher->hash_len);
102
wipememory(hash_1, cipher->hash_len);
103
wipememory(hash_2, cipher->hash_len);
108
if (cipher_algo == BLOWFISH) {
109
wipememory(prehash1, strlen(prehash1));
110
wipememory(prehash2, strlen(prehash2));
116
void fpm_crypt_set_password(gchar *password) {
120
new_salt = get_new_salt(cipher->salt_len);
122
if(cipher_algo == AES256) {
123
hash = g_malloc(cipher->hash_len);
124
pkcs5_pbkdf2(password, strlen(password), new_salt, cipher->salt_len, hash, cipher->hash_len, PBKDF2_ITERATIONS);
126
prehash = (guchar *) g_strdup_printf("%s%s", new_salt, password);
127
hash = md5(prehash, strlen((gchar *) prehash));
130
fpm_setkey(new_context, hash, cipher->hash_len);
132
if (cipher_algo == BLOWFISH)
133
wipememory(prehash, strlen(prehash));
135
wipememory(hash, cipher->hash_len);