Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2011 by Werner Dittmann |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library 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 GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | |
| 18 | * In addition, as a special exception, the copyright holders give |
| 19 | * permission to link the code of portions of this program with the |
| 20 | * OpenSSL library under certain conditions as described in each |
| 21 | * individual source file, and distribute linked combinations |
| 22 | * including the two. |
| 23 | * You must obey the GNU General Public License in all respects |
| 24 | * for all of the code used other than OpenSSL. If you modify |
| 25 | * file(s) with this exception, you may extend this exception to your |
| 26 | * version of the file(s), but you are not obligated to do so. If you |
| 27 | * do not wish to do so, delete this exception statement from your |
| 28 | * version. If you delete this exception statement from all source |
| 29 | * files in the program, then also delete it here. |
| 30 | */ |
| 31 | |
| 32 | /** Copyright (C) 2011 |
| 33 | * |
| 34 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 35 | */ |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 39 | #include <zrtp/crypto/twoCFB.h> |
| 40 | #include <cryptcommon/twofish.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 41 | |
| 42 | static int initialized = 0; |
| 43 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 44 | void twoCfbEncrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data, int32_t dataLength) |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 45 | { |
| 46 | Twofish_key keyCtx; |
| 47 | int usedBytes = 0; |
| 48 | |
| 49 | if (!initialized) { |
| 50 | Twofish_initialise(); |
| 51 | initialized = 1; |
| 52 | } |
| 53 | |
| 54 | memset(&keyCtx, 0, sizeof(Twofish_key)); |
| 55 | Twofish_prepare_key(key, keyLength, &keyCtx); |
| 56 | |
| 57 | Twofish_cfb128_encrypt(&keyCtx, (Twofish_Byte*)data, (Twofish_Byte*)data, |
| 58 | (size_t)dataLength, (Twofish_Byte*)IV, &usedBytes); |
| 59 | } |
| 60 | |
| 61 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 62 | void twoCfbDecrypt(uint8_t* key, int32_t keyLength, uint8_t* IV, uint8_t *data, int32_t dataLength) |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 63 | { |
| 64 | Twofish_key keyCtx; |
| 65 | int usedBytes = 0; |
| 66 | |
| 67 | if (!initialized) { |
| 68 | Twofish_initialise(); |
| 69 | initialized = 1; |
| 70 | } |
| 71 | |
| 72 | memset(&keyCtx, 0, sizeof(Twofish_key)); |
| 73 | Twofish_prepare_key(key, keyLength, &keyCtx); |
| 74 | |
| 75 | Twofish_cfb128_decrypt(&keyCtx, (Twofish_Byte*)data, (Twofish_Byte*)data, |
| 76 | (size_t)dataLength, (Twofish_Byte*)IV, &usedBytes); |
| 77 | } |