blob: 066035fa93f55998f1e387d171391b42ee9e71d9 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 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 Lision7fd5d3d2013-12-04 13:06:40 -050024#include <crypto/aesCFB.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050025
26
27extern void initializeGcrypt();
28
29void aesCfbEncrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data,
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050030 int32_t dataLength);
Alexandre Lision51140e12013-12-02 10:54:09 -050031{
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 Lision7fd5d3d2013-12-04 13:06:40 -050054void aesCfbDecrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data,
55 int32_t dataLength);
Alexandre Lision51140e12013-12-02 10:54:09 -050056{
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 Lision7fd5d3d2013-12-04 13:06:40 -050077}