Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 2 | Copyright (C) 2006-2013 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 5 | it under the terms of the GNU Lesser General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /** Copyright (C) 2006, 2007 |
| 19 | * |
| 20 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 21 | */ |
| 22 | |
| 23 | #include <gcrypt.h> |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 24 | #include <crypto/aesCFB.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 25 | |
| 26 | |
| 27 | extern void initializeGcrypt(); |
| 28 | |
| 29 | void aesCfbEncrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data, |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 30 | int32_t dataLength); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 31 | { |
| 32 | gcry_error_t err = 0; |
| 33 | int algo; |
| 34 | |
| 35 | initializeGcrypt(); |
| 36 | |
| 37 | if (keyLength == 16) { |
| 38 | algo = GCRY_CIPHER_AES; |
| 39 | } |
| 40 | else if (keyLength == 32) { |
| 41 | algo = GCRY_CIPHER_AES256; |
| 42 | } |
| 43 | else { |
| 44 | return; |
| 45 | } |
| 46 | gcry_cipher_hd_t tmp; |
| 47 | err = gcry_cipher_open(&tmp, algo, GCRY_CIPHER_MODE_CFB, 0); |
| 48 | err = gcry_cipher_setkey(tmp, key, keyLength); |
| 49 | err = gcry_cipher_setiv (tmp, IV, AES_BLOCK_SIZE); |
| 50 | err = gcry_cipher_encrypt (tmp, data, dataLength, data, dataLength); |
| 51 | gcry_cipher_close(tmp); |
| 52 | } |
| 53 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 54 | void aesCfbDecrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data, |
| 55 | int32_t dataLength); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 56 | { |
| 57 | gcry_error_t err = 0; |
| 58 | int algo; |
| 59 | |
| 60 | initializeGcrypt(); |
| 61 | |
| 62 | if (keyLength == 16) { |
| 63 | algo = GCRY_CIPHER_AES; |
| 64 | } |
| 65 | else if (keyLength == 32) { |
| 66 | algo = GCRY_CIPHER_AES256; |
| 67 | } |
| 68 | else { |
| 69 | return; |
| 70 | } |
| 71 | gcry_cipher_hd_t tmp; |
| 72 | err = gcry_cipher_open(&tmp, algo, GCRY_CIPHER_MODE_CFB, 0); |
| 73 | err = gcry_cipher_setkey(tmp, key, keyLength); |
| 74 | err = gcry_cipher_setiv (tmp, IV, AES_BLOCK_SIZE); |
| 75 | err = gcry_cipher_decrypt (tmp, data, dataLength, data, dataLength); |
| 76 | gcry_cipher_close(tmp); |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 77 | } |