Lines 108-114
int my_aes_encrypt(const unsigned char *source, uint32 source_length,
Link Here
|
108 |
const unsigned char *key, uint32 key_length, |
108 |
const unsigned char *key, uint32 key_length, |
109 |
enum my_aes_opmode mode, const unsigned char *iv) |
109 |
enum my_aes_opmode mode, const unsigned char *iv) |
110 |
{ |
110 |
{ |
111 |
EVP_CIPHER_CTX ctx; |
111 |
EVP_CIPHER_CTX *ctx; |
112 |
const EVP_CIPHER *cipher= aes_evp_type(mode); |
112 |
const EVP_CIPHER *cipher= aes_evp_type(mode); |
113 |
int u_len, f_len; |
113 |
int u_len, f_len; |
114 |
/* The real key to be used for encryption */ |
114 |
/* The real key to be used for encryption */ |
Lines 118-140
int my_aes_encrypt(const unsigned char *source, uint32 source_length,
Link Here
|
118 |
if (!cipher || (EVP_CIPHER_iv_length(cipher) > 0 && !iv)) |
118 |
if (!cipher || (EVP_CIPHER_iv_length(cipher) > 0 && !iv)) |
119 |
return MY_AES_BAD_DATA; |
119 |
return MY_AES_BAD_DATA; |
120 |
|
120 |
|
121 |
if (!EVP_EncryptInit(&ctx, cipher, rkey, iv)) |
121 |
if (!EVP_EncryptInit(ctx, cipher, rkey, iv)) |
122 |
goto aes_error; /* Error */ |
122 |
goto aes_error; /* Error */ |
123 |
if (!EVP_CIPHER_CTX_set_padding(&ctx, 1)) |
123 |
if (!EVP_CIPHER_CTX_set_padding(ctx, 1)) |
124 |
goto aes_error; /* Error */ |
124 |
goto aes_error; /* Error */ |
125 |
if (!EVP_EncryptUpdate(&ctx, dest, &u_len, source, source_length)) |
125 |
if (!EVP_EncryptUpdate(ctx, dest, &u_len, source, source_length)) |
126 |
goto aes_error; /* Error */ |
126 |
goto aes_error; /* Error */ |
127 |
|
127 |
|
128 |
if (!EVP_EncryptFinal(&ctx, dest + u_len, &f_len)) |
128 |
if (!EVP_EncryptFinal(ctx, dest + u_len, &f_len)) |
129 |
goto aes_error; /* Error */ |
129 |
goto aes_error; /* Error */ |
130 |
|
130 |
|
131 |
EVP_CIPHER_CTX_cleanup(&ctx); |
131 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
|
|
132 |
EVP_CIPHER_CTX_cleanup(ctx); |
133 |
#else |
134 |
EVP_CIPHER_CTX_free(ctx); |
135 |
#endif |
132 |
return u_len + f_len; |
136 |
return u_len + f_len; |
133 |
|
137 |
|
134 |
aes_error: |
138 |
aes_error: |
135 |
/* need to explicitly clean up the error if we want to ignore it */ |
139 |
/* need to explicitly clean up the error if we want to ignore it */ |
136 |
ERR_clear_error(); |
140 |
ERR_clear_error(); |
137 |
EVP_CIPHER_CTX_cleanup(&ctx); |
141 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
|
|
142 |
EVP_CIPHER_CTX_cleanup(ctx); |
143 |
#else |
144 |
EVP_CIPHER_CTX_free(ctx); |
145 |
#endif |
138 |
return MY_AES_BAD_DATA; |
146 |
return MY_AES_BAD_DATA; |
139 |
} |
147 |
} |
140 |
|
148 |
|
Lines 145-151
int my_aes_decrypt(const unsigned char *source, uint32 source_length,
Link Here
|
145 |
enum my_aes_opmode mode, const unsigned char *iv) |
153 |
enum my_aes_opmode mode, const unsigned char *iv) |
146 |
{ |
154 |
{ |
147 |
|
155 |
|
148 |
EVP_CIPHER_CTX ctx; |
156 |
EVP_CIPHER_CTX *ctx; |
149 |
const EVP_CIPHER *cipher= aes_evp_type(mode); |
157 |
const EVP_CIPHER *cipher= aes_evp_type(mode); |
150 |
int u_len, f_len; |
158 |
int u_len, f_len; |
151 |
|
159 |
|
Lines 156-179
int my_aes_decrypt(const unsigned char *source, uint32 source_length,
Link Here
|
156 |
if (!cipher || (EVP_CIPHER_iv_length(cipher) > 0 && !iv)) |
164 |
if (!cipher || (EVP_CIPHER_iv_length(cipher) > 0 && !iv)) |
157 |
return MY_AES_BAD_DATA; |
165 |
return MY_AES_BAD_DATA; |
158 |
|
166 |
|
159 |
EVP_CIPHER_CTX_init(&ctx); |
167 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
|
|
168 |
EVP_CIPHER_CTX_init(ctx); |
169 |
#endif |
160 |
|
170 |
|
161 |
if (!EVP_DecryptInit(&ctx, aes_evp_type(mode), rkey, iv)) |
171 |
if (!EVP_DecryptInit(ctx, aes_evp_type(mode), rkey, iv)) |
162 |
goto aes_error; /* Error */ |
172 |
goto aes_error; /* Error */ |
163 |
if (!EVP_CIPHER_CTX_set_padding(&ctx, 1)) |
173 |
if (!EVP_CIPHER_CTX_set_padding(ctx, 1)) |
164 |
goto aes_error; /* Error */ |
174 |
goto aes_error; /* Error */ |
165 |
if (!EVP_DecryptUpdate(&ctx, dest, &u_len, source, source_length)) |
175 |
if (!EVP_DecryptUpdate(ctx, dest, &u_len, source, source_length)) |
166 |
goto aes_error; /* Error */ |
176 |
goto aes_error; /* Error */ |
167 |
if (!EVP_DecryptFinal_ex(&ctx, dest + u_len, &f_len)) |
177 |
if (!EVP_DecryptFinal_ex(ctx, dest + u_len, &f_len)) |
168 |
goto aes_error; /* Error */ |
178 |
goto aes_error; /* Error */ |
169 |
|
179 |
|
170 |
EVP_CIPHER_CTX_cleanup(&ctx); |
180 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
|
|
181 |
EVP_CIPHER_CTX_cleanup(ctx); |
182 |
#else |
183 |
EVP_CIPHER_CTX_free(ctx); |
184 |
#endif |
171 |
return u_len + f_len; |
185 |
return u_len + f_len; |
172 |
|
186 |
|
173 |
aes_error: |
187 |
aes_error: |
174 |
/* need to explicitly clean up the error if we want to ignore it */ |
188 |
/* need to explicitly clean up the error if we want to ignore it */ |
175 |
ERR_clear_error(); |
189 |
ERR_clear_error(); |
176 |
EVP_CIPHER_CTX_cleanup(&ctx); |
190 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
|
|
191 |
EVP_CIPHER_CTX_cleanup(ctx); |
192 |
#else |
193 |
EVP_CIPHER_CTX_free(ctx); |
194 |
#endif |
177 |
return MY_AES_BAD_DATA; |
195 |
return MY_AES_BAD_DATA; |
178 |
} |
196 |
} |
179 |
|
197 |
|