-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm_crypto.c
167 lines (153 loc) · 4.56 KB
/
pwm_crypto.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* STANDARD LIBRARIES */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "pwm_crypto.h"
void SHA256_CALC(char *string, unsigned char *hash) {
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(hash, &sha256);
hash[SHA256_DIGEST_LENGTH] = 0;
}
void generate_salted_password_hash(char *password,
unsigned char *salt, unsigned char *buffer) {
unsigned char saltedPassword[500];
int i;
for (i = 0; i < SALT_LEN; i++) {
saltedPassword[i] = salt[i];
}
saltedPassword[i] = 0;
strcat((char *)saltedPassword, (char*) password);
unsigned char saltedPasswordHash[SHA256_DIGEST_LENGTH + 1];
SHA256_CALC((char *)saltedPassword, saltedPasswordHash);
strcpy((char *)buffer, (char *)saltedPasswordHash);
}
void encrypt_to_file(char *plaintext, unsigned char *key,
unsigned char *iv, FILE *outfile) {
int requiredLength = 16 * ((int) ceil((double) strlen(plaintext) / 16));
unsigned char *ciphertext = malloc(requiredLength + 2);
int ciphertext_len = encrypt(plaintext, key, iv, ciphertext);
char *plaintext_out = malloc(sizeof(plaintext));
fwrite(ciphertext, 1, ciphertext_len, outfile);
fflush(outfile);
free(ciphertext);
}
int encrypt(char *plaintext, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx;
int plaintext_len = strlen(plaintext);
int len;
int ciphertext_len;
if (!(ctx = EVP_CIPHER_CTX_new())) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext,
plaintext_len)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
if (!(ctx = EVP_CIPHER_CTX_new())) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext,
ciphertext_len)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
fprintf(stderr, "Encryption failure.\n");
exit(1);
}
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
char random_char(void) {
char buffer[1];
while (1) {
int rc = RAND_bytes(buffer, 1);
if (rc != 1) continue;
if (buffer[0] < 33 || buffer[0] > 126) continue;
break;
}
return buffer[0];
}
unsigned char random_byte(void) {
char buffer[1];
while (1) {
int rc = RAND_bytes(buffer, 1);
if (rc != 1) continue;
break;
}
return buffer[0];
}
char* generate_password(int n) {
char *output = malloc(n + 1);
memset(output, 0, sizeof(output));
int i = 0;
for (i = 0; i < n; i++) {
output[i] = random_char();
}
output[i] = '\0';
return output;
}
char* decrypt_cipherfile(cipherfile *cfile, unsigned char *key,
unsigned char *iv) {
char *output = malloc(cfile->len);
decrypt(cfile->body, cfile->len, key, iv, output);
return output;
}
unsigned char* generate_bytes(int n) {
char *output = malloc(n + 1);
memset(output, 0, sizeof(output));
int i = 0;
for (i = 0; i < n; i++) {
output[i] = random_byte();
}
output[i] = '\0';
return output;
}
cipherfile* read_cipherfile(FILE *file) {
int len = 0;
int c;
int lastupdate = 0;
int numupdates = 0;
int buflen = 1000;
cipherfile *output = malloc(sizeof(cipherfile));
output->body = malloc(buflen);
while ((c = fgetc(file)) != EOF) {
output->body[len++] = (unsigned char) c;
if (len - lastupdate >= 1000) {
output->body = realloc(output->body, buflen * numupdates++);
}
}
output->len = len;
return output;
}