-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.c
More file actions
158 lines (136 loc) · 3.64 KB
/
encrypt.c
File metadata and controls
158 lines (136 loc) · 3.64 KB
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
/* Copyright (C) 2015, Hsiang Kao (e0e1e) <0xe0e1e@gmail.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "highway/encrypt_pub.h"
#include "highway/encrypt_impl.h"
#define ENCRYPT_MAX_KEY_LENGTH 32
#define ENCRYPT_MAX_IV_LENGTH 16
_record(encrypt)
_methodinfo *method;
int keysiz;
char keyiv[ENCRYPT_MAX_KEY_LENGTH + ENCRYPT_MAX_IV_LENGTH];
_cipher *cipher;
_end_record(encrypt)
_record(methodinfo)
_methodinfo_0 *_0;
_end_record(methodinfo)
int
encrypt_iv_length(_encrypt *crypt)
{
_methodinfo *m = crypt->method;
return m->_0->iv_length(m);
}
#include <string.h>
void
encrypt_generatekey(_encrypt *crypt, char *pw)
{
_methodinfo *m = crypt->method;
int keysiz = m->_0->key_length(m);
if (keysiz) // 若keysiz != 0,则使用标准算法生成
encrypt_EVP_BytesToKey(pw, crypt->keyiv, crypt->keysiz = keysiz, 0);
else { // 若keysiz == 0, 此时key就是pw
int i;
for(i=0; pw[i] != '\0'; ++i)
crypt->keyiv[i] = pw[i];
crypt->keysiz = i;
}
}
#include <stdlib.h>
_methodinfo *encrypt_openssl_get_methodbyname(char *);
_methodinfo *encrypt_rc4_md5_get_methodbyname(char *);
static
_methodinfo *(*encrypt_get_methodbyname[])(char *) = {
encrypt_openssl_get_methodbyname,
encrypt_rc4_md5_get_methodbyname,
NULL
};
_decrypt *decrypt_init(char *method, char *pw)
{
int i;
for(i=0; encrypt_get_methodbyname[i] != NULL; ++i) {
_methodinfo *m = encrypt_get_methodbyname[i](method);
if (m != NULL) {
_decrypt *crypt = (_encrypt *)malloc(sizeof(_decrypt));
if (crypt != NULL) {
crypt->method = m;
crypt->cipher = NULL;
if (pw != NULL) encrypt_generatekey(crypt, pw);
return crypt;
}
}
}
return NULL;
}
_encrypt *encrypt_init(char *method, char *pw)
{
_encrypt *crypt = decrypt_init(method, pw);
if (crypt != NULL) {
_methodinfo *m = crypt->method;
encrypt_rand_bytes(m->_0->iv_length(m), crypt->keyiv + crypt->keysiz);
}
return crypt;
}
int encrypt_update(_encrypt *crypt, char *dst, unsigned bufsiz, char *buf)
{
_methodinfo *m = crypt->method;
if (m != NULL) {
int ivsiz;
if (crypt->cipher == NULL) {
char *iv = crypt->keyiv + crypt->keysiz;
// 1 for encryption, 0 for decryption
crypt->cipher = m->_0->cipher_init(m, crypt->keyiv, iv, 1);
memcpy(dst, iv, ivsiz = m->_0->iv_length(m));
dst += ivsiz;
} else ivsiz = 0;
return m->_0->cipher_update(crypt->cipher, dst, bufsiz, buf) + ivsiz;
}
return -1;
}
int decrypt_update(_decrypt *crypt, char *dst, unsigned bufsiz, char *buf)
{
_methodinfo *m = crypt->method;
if (m != NULL) {
if (crypt->cipher == NULL) {
int ivsiz = m->_0->iv_length(m);
if (bufsiz >= ivsiz) {
char *iv = crypt->keyiv + crypt->keysiz;
memcpy(iv, buf, ivsiz);
// 1 for encryption, 0 for decryption
crypt->cipher = m->_0->cipher_init(m, crypt->keyiv, iv, 0);
buf += ivsiz, bufsiz -= ivsiz;
} else return -1;
}
if (bufsiz) return m->_0->cipher_update(crypt->cipher, dst, bufsiz, buf);
else return 0;
}
return -1;
}
int encrypt_final(_encrypt *crypt, char *dst)
{
_methodinfo *m = crypt->method;
if (m != NULL) {
int ret = m->_0->cipher_final(crypt->cipher, dst);
if (ret >= 0) {
m->_0->cipher_exit(crypt->cipher);
crypt->cipher = NULL;
}
return ret;
}
return -1;
}
void encrypt_free(_encrypt *crypt)
{
_methodinfo *m = crypt->method;
if (m != NULL)
if (crypt->cipher != NULL)
m->_0->cipher_exit(crypt->cipher);
free(crypt);
}
void encryptor_openssl_init(void);
void encryptor_rc4_md5_init(void);
void encryptor_init(void)
{
encryptor_openssl_init();
}