@@ -85,19 +85,18 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
8585 }
8686 }
8787
88- if ((ret == 1 ) && (! aes -> init )) {
88+ if ((ret == 1 ) && (key != NULL )) {
8989 WOLFENGINE_MSG (WE_LOG_CIPHER ,
9090 "Initializing wolfCrypt Aes structure: %p" , & aes -> aes );
9191 rc = wc_AesInit (& aes -> aes , NULL , INVALID_DEVID );
9292 if (rc != 0 ) {
9393 WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER , "wc_AesInit" , rc );
9494 ret = 0 ;
9595 }
96+ aes -> init = 1 ;
9697 }
9798
98- if (ret == 1 ) {
99- /* Must have initialized wolfSSL AES object when here. */
100- aes -> init = 1 ;
99+ if (ret == 1 && (aes -> init == 1 )) {
101100 aes -> over = 0 ;
102101 /* Store whether encrypting. */
103102 aes -> enc = enc ;
@@ -148,87 +147,12 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out,
148147
149148 WOLFENGINE_ENTER (WE_LOG_CIPHER , "we_aes_cbc_encrypt" );
150149
151- /* Length of 0 means Final called. */
152- if (len == 0 ) {
153- if (aes -> over != 0 ) {
154- WOLFENGINE_ERROR_MSG (WE_LOG_CIPHER ,
155- "No Pad - last encrypt block not full" );
156- ret = 0 ;
157- }
158- }
159-
160- if (ret == 1 ) {
161- unsigned int l ;
162-
163- /* Check for cached data. */
164- if (aes -> over > 0 ) {
165- WOLFENGINE_MSG (WE_LOG_CIPHER , "Encrypting leftover cached data, "
166- "aes->over = %d" , aes -> over );
167-
168- /* Partial block not yet encrypted. */
169- l = AES_BLOCK_SIZE - aes -> over ;
170- if (l > len ) {
171- l = (int )len ;
172- }
173-
174- /* Copy as much of input as possible to fill in block. */
175- if (l > 0 ) {
176- XMEMCPY (aes -> lastBlock + aes -> over , in , l );
177- aes -> over += l ;
178- in += l ;
179- len -= l ;
180- }
181- /* Check if we have a complete block to encrypt. */
182- if (aes -> over == AES_BLOCK_SIZE ) {
183- /* Encrypt and return block. */
184- rc = wc_AesCbcEncrypt (& aes -> aes , out , aes -> lastBlock ,
185- AES_BLOCK_SIZE );
186- if (rc != 0 ) {
187- WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER ,
188- "wc_AesCbcEncrypt" , rc );
189- ret = 0 ;
190- }
191- else {
192- WOLFENGINE_MSG_VERBOSE (WE_LOG_CIPHER ,
193- "Encrypted %d bytes (AES-CBC)" ,
194- AES_BLOCK_SIZE );
195- WOLFENGINE_BUFFER (WE_LOG_CIPHER , out , AES_BLOCK_SIZE );
196- }
197-
198- /* Data put to output. */
199- out += AES_BLOCK_SIZE ;
200- /* No more cached data. */
201- aes -> over = 0 ;
202-
203- WOLFENGINE_MSG (WE_LOG_CIPHER , "Encrypted all cached data" );
204- }
205- }
206- /* Encrypt full blocks from remaining input. */
207- if ((ret == 1 ) && (len >= AES_BLOCK_SIZE )) {
208- /* Calculate full blocks. */
209- l = (int )len & (~(AES_BLOCK_SIZE - 1 ));
210-
211- rc = wc_AesCbcEncrypt (& aes -> aes , out , in , l );
212- if (rc != 0 ) {
213- WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER , "wc_AesCbcEncrypt" , rc );
214- ret = 0 ;
215- }
216- else {
217- WOLFENGINE_MSG_VERBOSE (WE_LOG_CIPHER ,
218- "Encrypted %d bytes (AES-CBC)" , l );
219- WOLFENGINE_BUFFER (WE_LOG_CIPHER , out , l );
220- }
221-
222- in += l ;
223- len -= l ;
224- }
225- if ((ret == 1 ) && (len > 0 )) {
226- /* Copy remaining input as incomplete block. */
227- XMEMCPY (aes -> lastBlock , in , len );
228- aes -> over = (int )len ;
229- }
150+ /* padding is handled by OpenSSL before passed to we_aes_cbc_encrypt */
151+ rc = wc_AesCbcEncrypt (& aes -> aes , out , in , (unsigned int )len );
152+ if (rc != 0 ) {
153+ WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER , "wc_AesCbcEncrypt" , rc );
154+ ret = -1 ;
230155 }
231-
232156 WOLFENGINE_LEAVE (WE_LOG_CIPHER , "we_aes_cbc_encrypt" , ret );
233157
234158 return ret ;
@@ -249,94 +173,21 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out,
249173static int we_aes_cbc_decrypt (we_AesBlock * aes , unsigned char * out ,
250174 const unsigned char * in , size_t len )
251175{
252- int ret = 1 ;
176+ int ret = 0 ;
253177 int rc ;
254178
255179 WOLFENGINE_ENTER (WE_LOG_CIPHER , "we_aes_cbc_decrypt" );
256180
257- /* Length of 0 means Final called. */
258- if (len == 0 ) {
259- if (aes -> over != 0 ) {
260- WOLFENGINE_ERROR_MSG (WE_LOG_CIPHER ,
261- "No Pad - last decrypt block not full" );
262- ret = 0 ;
263- }
181+ /* padding is handled by OpenSSL before passed to we_aes_cbc_decrypt */
182+ rc = wc_AesCbcDecrypt (& aes -> aes , out , in , (unsigned int )len );
183+ if (rc == 0 ) {
184+ ret = (int )len ;
264185 }
265- if (ret == 1 ) {
266- unsigned int l ;
267-
268- /* Check for cached data. */
269- if (aes -> over > 0 ) {
270- WOLFENGINE_MSG (WE_LOG_CIPHER , "Decrypting leftover cached data, "
271- "aes->over = %d" , aes -> over );
272-
273- /* Calculate amount of input that can be used. */
274- l = AES_BLOCK_SIZE - aes -> over ;
275- if (l > len ) {
276- l = (int )len ;
277- }
278-
279- if (l > 0 ) {
280- /* Copy as much of input as possible to fill in block. */
281- XMEMCPY (aes -> lastBlock + aes -> over , in , l );
282- aes -> over += l ;
283- in += l ;
284- len -= l ;
285- }
286- /* Padding and not last full block or not padding and full block. */
287- if ((aes -> over == AES_BLOCK_SIZE ) || len > 0 ) {
288- /* Decrypt block cached block. */
289- rc = wc_AesCbcDecrypt (& aes -> aes , out , aes -> lastBlock ,
290- AES_BLOCK_SIZE );
291- if (rc != 0 ) {
292- WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER ,
293- "wc_AesCbcDecrypt" , rc );
294- ret = 0 ;
295- }
296- else {
297- WOLFENGINE_MSG_VERBOSE (WE_LOG_CIPHER ,
298- "Decrypted %d bytes (AES-CBC)" ,
299- AES_BLOCK_SIZE );
300- WOLFENGINE_BUFFER (WE_LOG_CIPHER , out , AES_BLOCK_SIZE );
301- }
302-
303- /* Data put to output. */
304- out += AES_BLOCK_SIZE ;
305- /* No more cached data. */
306- aes -> over = 0 ;
307- }
308- }
309- /* Decrypt full blocks from remaining input. */
310- if ((ret == 1 ) && (len >= AES_BLOCK_SIZE )) {
311- /* Calculate full blocks. */
312- l = (int )len & (~(AES_BLOCK_SIZE - 1 ));
313-
314- if (l > 0 ) {
315- rc = wc_AesCbcDecrypt (& aes -> aes , out , in , l );
316- if (rc != 0 ) {
317- WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER ,
318- "wc_AesCbcDecrypt" , rc );
319- ret = 0 ;
320- }
321- else {
322- WOLFENGINE_MSG_VERBOSE (WE_LOG_CIPHER ,
323- "Decrypted %d bytes (AES-CBC)" , l );
324- WOLFENGINE_BUFFER (WE_LOG_CIPHER , out , l );
325- }
326- }
327-
328- in += l ;
329- len -= l ;
330- }
331- if ((ret == 1 ) && (len > 0 )) {
332- /* Copy remaining input as incomplete block. */
333- XMEMCPY (aes -> lastBlock , in , len );
334- aes -> over = (int )len ;
335- }
186+ else {
187+ ret = -1 ;
336188 }
337189
338190 WOLFENGINE_LEAVE (WE_LOG_CIPHER , "we_aes_cbc_decrypt" , ret );
339-
340191 return ret ;
341192}
342193
@@ -601,19 +452,20 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
601452 ret = 0 ;
602453 }
603454
604- if ((ret == 1 ) && (( key == NULL ) || (! aes -> init ) )) {
455+ if ((ret == 1 ) && (key == NULL )) {
605456 WOLFENGINE_MSG (WE_LOG_CIPHER ,
606457 "Initializing wolfCrypt Aes structure: %p" , & aes -> aes );
607458 rc = wc_AesInit (& aes -> aes , NULL , INVALID_DEVID );
608459 if (rc != 0 ) {
609460 WOLFENGINE_ERROR_FUNC (WE_LOG_CIPHER , "wc_AesInit" , rc );
610461 ret = 0 ;
611462 }
463+ else {
464+ aes -> init = 1 ;
465+ }
612466 }
613467
614468 if (ret == 1 ) {
615- /* Must have initialized wolfSSL AES object when here. */
616- aes -> init = 1 ;
617469 aes -> over = 0 ;
618470 /* Store whether encrypting. */
619471 aes -> enc = enc ;
0 commit comments