1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
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.
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.
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
21 #include <beecrypt/hmacsha1.h>
22 #include <beecrypt/blockmode.h>
23 #include <beecrypt/blowfish.h>
26 bool zen::hmac_sha1_calc(const byte
* key
, size_t keylen
, const byte
* data
,
27 size_t datalen
, byte
* sig
, size_t* siglen
)
30 if (hmacsha1Setup(¶m
, key
, keylen
* 8))
32 if (hmacsha1Update(¶m
, data
, datalen
))
34 if (hmacsha1Digest(¶m
, sig
))
39 bool zen::bf_cbc_encrypt(const byte
* key
, size_t keylen
, byte
* data
,
40 size_t datalen
, const byte
* iv
)
42 if (datalen
% blowfish
.blocksize
)
43 throw std::invalid_argument(
44 "The length must be aligned on a 8 byte boundary.");
47 if (blowfishSetup(¶m
, key
, keylen
* 8, ENCRYPT
))
49 if (blowfishSetIV(¶m
, iv
))
52 byte
* plain
= new byte
[datalen
];
53 memcpy(plain
, data
, datalen
);
55 unsigned int nblocks
= datalen
/ blowfish
.blocksize
;
56 if (blockEncryptCBC(&blowfish
, ¶m
, (uint32_t*)data
, (uint32_t*)plain
,
66 bool zen::bf_cbc_decrypt(const byte
* key
, size_t keylen
, byte
* data
,
67 size_t datalen
, const byte
* iv
)
69 if (datalen
% blowfish
.blocksize
)
70 throw std::invalid_argument(
71 "The length must be aligned on a 8 byte boundary.");
74 if (blowfishSetup(¶m
, key
, keylen
* 8, ENCRYPT
))
76 if (blowfishSetIV(¶m
, iv
))
79 byte
* cipher
= new byte
[datalen
];
80 memcpy(cipher
, data
, datalen
);
82 unsigned int nblocks
= datalen
/ blowfish
.blocksize
;
83 if (blockDecryptCBC(&blowfish
, ¶m
, (uint32_t*)data
, (uint32_t*)cipher
,