| 137 | } |
| 138 | |
| 139 | static int aes256CryptCbc( |
| 140 | const uint8_t key[32], |
| 141 | int mode, |
| 142 | size_t length, |
| 143 | unsigned char iv[16], |
| 144 | const unsigned char* input, |
| 145 | unsigned char* output |
| 146 | ) { |
| 147 | check(key && iv && input && output); |
| 148 | |
| 149 | if ((length % 16) || (length == 0)) { |
| 150 | return -1; // TODO: Proper error code from mbed lib? |
| 151 | } |
| 152 | |
| 153 | mbedtls_aes_context master; |
| 154 | mbedtls_aes_init(&master); |
| 155 | if (mode == MBEDTLS_AES_ENCRYPT) { |
| 156 | mbedtls_aes_setkey_enc(&master, key, 256); |
| 157 | } else { |
| 158 | mbedtls_aes_setkey_dec(&master, key, 256); |
| 159 | } |
| 160 | int result = mbedtls_aes_crypt_cbc(&master, mode, length, iv, input, output); |
| 161 | mbedtls_aes_free(&master); |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | int encrypt(const uint8_t iv[16], const uint8_t* inData, uint8_t* outData, size_t dataLength) { |
| 166 | check(dataLength % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256)"); |