summaryrefslogtreecommitdiff
path: root/utils/zenutils/source/shared/crypt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/zenutils/source/shared/crypt.cpp')
-rwxr-xr-xutils/zenutils/source/shared/crypt.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/utils/zenutils/source/shared/crypt.cpp b/utils/zenutils/source/shared/crypt.cpp
new file mode 100755
index 0000000000..9c2d33870c
--- /dev/null
+++ b/utils/zenutils/source/shared/crypt.cpp
@@ -0,0 +1,91 @@
1/* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include "crypt.h"
20#include <stdexcept>
21#include <beecrypt/hmacsha1.h>
22#include <beecrypt/blockmode.h>
23#include <beecrypt/blowfish.h>
24
25
26bool zen::hmac_sha1_calc(const byte* key, size_t keylen, const byte* data,
27 size_t datalen, byte* sig, size_t* siglen)
28{
29 hmacsha1Param param;
30 if (hmacsha1Setup(&param, key, keylen * 8))
31 return false;
32 if (hmacsha1Update(&param, data, datalen))
33 return false;
34 if (hmacsha1Digest(&param, sig))
35 return false;
36 return true;
37}
38
39bool zen::bf_cbc_encrypt(const byte* key, size_t keylen, byte* data,
40 size_t datalen, const byte* iv)
41{
42 if (datalen % blowfish.blocksize)
43 throw std::invalid_argument(
44 "The length must be aligned on a 8 byte boundary.");
45
46 blowfishParam param;
47 if (blowfishSetup(&param, key, keylen * 8, ENCRYPT))
48 return false;
49 if (blowfishSetIV(&param, iv))
50 return false;
51
52 byte* plain = new byte[datalen];
53 memcpy(plain, data, datalen);
54
55 unsigned int nblocks = datalen / blowfish.blocksize;
56 if (blockEncryptCBC(&blowfish, &param, (uint32_t*)data, (uint32_t*)plain,
57 nblocks))
58 {
59 delete [] plain;
60 return false;
61 }
62
63 return true;
64}
65
66bool zen::bf_cbc_decrypt(const byte* key, size_t keylen, byte* data,
67 size_t datalen, const byte* iv)
68{
69 if (datalen % blowfish.blocksize)
70 throw std::invalid_argument(
71 "The length must be aligned on a 8 byte boundary.");
72
73 blowfishParam param;
74 if (blowfishSetup(&param, key, keylen * 8, ENCRYPT))
75 return false;
76 if (blowfishSetIV(&param, iv))
77 return false;
78
79 byte* cipher = new byte[datalen];
80 memcpy(cipher, data, datalen);
81
82 unsigned int nblocks = datalen / blowfish.blocksize;
83 if (blockDecryptCBC(&blowfish, &param, (uint32_t*)data, (uint32_t*)cipher,
84 nblocks))
85 {
86 delete [] cipher;
87 return false;
88 }
89
90 return true;
91}