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