-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes.c
120 lines (95 loc) · 2.47 KB
/
aes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include "aes.h"
#include "util.h"
AES_KEY *aes_init(const char* priv_key)
{
/* Exctract binary value */
unsigned char *bin_priv_key = hex2bin(priv_key, OTP_KEY_HEX_LEN);
/* Init key */
AES_KEY *key = calloc(1ul, sizeof(AES_KEY));
if (key == NULL) {
return NULL;
}
AES_set_decrypt_key(bin_priv_key, (int) OTP_KEY_BIN_LEN, key);
/* Clean cache */
free(bin_priv_key);
return key;
}
void aes_clean(AES_KEY *key)
{
free(key);
}
struct otp* extract_otp(char* obfuscated_encrypted_otp, AES_KEY *key)
{
/* De-obfuscate OTP */
modhex2hex(obfuscated_encrypted_otp, OTP_KEY_HEX_LEN);
/* Exctract binary values */
unsigned char *bin_encrypted_otp = hex2bin(obfuscated_encrypted_otp, OTP_KEY_HEX_LEN);;
/* Allocate output buffer */
unsigned char *otp = calloc(sizeof(char), OTP_KEY_BIN_LEN);
if (otp == NULL) {
return NULL;
}
/* Use openssl to decrypt */
AES_decrypt(bin_encrypted_otp, otp, key);
/* Clean cache */
free(bin_encrypted_otp);
return (struct otp*) otp;
}
unsigned char*
compute_hash(const char* digest_name, const char* input, size_t input_len)
{
const EVP_MD *md;
EVP_MD_CTX *mdctx;
unsigned char *md_value;
unsigned int md_len;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(digest_name);
if (md == NULL) {
printf("Unsuporter digest '%s'\n", digest_name);
return NULL;
}
md_value = calloc(sizeof(char), (size_t) EVP_MAX_MD_SIZE);
if (md_value == NULL) {
printf("Malloc error\n");
return NULL;
}
mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, input, input_len);
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
EVP_cleanup();
return md_value;
}
char
check_hash(const char* digest_name, const char* input, size_t input_len, const char* hash, size_t hash_len)
{
const EVP_MD *md;
EVP_MD_CTX *mdctx;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(digest_name);
if (md == NULL) {
return 1;
}
mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, input, input_len);
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
EVP_cleanup();
if (md_len != hash_len) {
return 2;
}
if (memcmp(hash, md_value, hash_len)) {
return 3;
}
return 0;
}