Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MiniTLS-GPL by
crypto_aes_128_cbc.c
00001 /* 00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices 00003 Author: Donatien Garnier 00004 Copyright (C) 2013-2014 AppNearMe Ltd 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *//** 00020 * \file crypto_aes_128_cbc.c 00021 * \copyright Copyright (c) AppNearMe Ltd 2013 00022 * \author Donatien Garnier 00023 */ 00024 00025 #define __DEBUG__ 0 00026 #ifndef __MODULE__ 00027 #define __MODULE__ "crypto_aes_128_cbc.c" 00028 #endif 00029 00030 00031 #include "core/fwk.h" 00032 #include "inc/minitls_errors.h" 00033 00034 #include "crypto_aes_128_cbc.h" 00035 00036 minitls_err_t crypto_aes_128_cbc_encrypt(crypto_aes_128_t* cipher, buffer_t* initialization_vector, buffer_t* buffer) 00037 { 00038 uint8_t* text = buffer_current_read_position(buffer); 00039 size_t size = buffer_length(buffer); 00040 00041 uint8_t* previous = buffer_current_read_position(initialization_vector); 00042 00043 if( (size % AES_128_BLOCK_SIZE) || (buffer_length(initialization_vector) != AES_128_BLOCK_SIZE) ) 00044 { 00045 return MINITLS_ERR_WRONG_ALIGNMENT_FOR_CIPHER; 00046 } 00047 00048 uint8_t xored_input[AES_128_BLOCK_SIZE]; //Xored input 00049 00050 while(size > 0) 00051 { 00052 for(int i = 0; i < AES_128_BLOCK_SIZE / sizeof(uint32_t); i++) 00053 { 00054 ((uint32_t*)xored_input)[i] = ((uint32_t*)previous)[i] ^ ((uint32_t*)text)[i]; 00055 } 00056 00057 crypto_aes_128_encrypt(cipher, xored_input, text); 00058 00059 previous = text; //Save previous ciphertext to use as initialization vector 00060 00061 text += AES_128_BLOCK_SIZE; 00062 size -= AES_128_BLOCK_SIZE; 00063 } 00064 00065 return MINITLS_OK; 00066 } 00067 00068 minitls_err_t crypto_aes_128_cbc_decrypt(crypto_aes_128_t* cipher, buffer_t* initialization_vector, buffer_t* buffer) 00069 { 00070 uint8_t* text = buffer_current_read_position(buffer); 00071 size_t size = buffer_length(buffer); 00072 00073 if( (size % AES_128_BLOCK_SIZE) || (buffer_length(initialization_vector) != AES_128_BLOCK_SIZE) ) 00074 { 00075 return MINITLS_ERR_WRONG_ALIGNMENT_FOR_CIPHER; 00076 } 00077 00078 uint8_t xored_output[AES_128_BLOCK_SIZE]; //Xored input 00079 uint8_t previous[AES_128_BLOCK_SIZE*2]; 00080 00081 memcpy(previous, buffer_current_read_position(initialization_vector), AES_128_BLOCK_SIZE); 00082 00083 while(size > 0) 00084 { 00085 //Save cipher text (to use as initialization vector) 00086 memcpy(previous + AES_128_BLOCK_SIZE, text, AES_128_BLOCK_SIZE); 00087 00088 crypto_aes_128_decrypt(cipher, text, xored_output); 00089 00090 for(int i = 0; i < AES_128_BLOCK_SIZE / sizeof(uint32_t); i++) 00091 { 00092 ((uint32_t*)text)[i] = ((uint32_t*)previous)[i] ^ ((uint32_t*)xored_output)[i]; 00093 } 00094 00095 memmove(previous, previous + AES_128_BLOCK_SIZE, AES_128_BLOCK_SIZE); 00096 00097 text += AES_128_BLOCK_SIZE; 00098 size -= AES_128_BLOCK_SIZE; 00099 } 00100 00101 return MINITLS_OK; 00102 } 00103
Generated on Tue Jul 12 2022 19:20:10 by
1.7.2
