1 /*
2 * steghide 0.5.1 - a steganography program
3 * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 #include <cstring>
22 #include <cstdlib>
23 #include <vector>
24
25 #include "common.h"
26 #include "error.h"
27 #include "MHashKeyGen.h"
28
MHashKeyGen()29 MHashKeyGen::MHashKeyGen ()
30 {
31 AlgorithmData.count = 0 ;
32 AlgorithmData.salt = NULL ;
33 AlgorithmData.salt_size = 0 ;
34 }
35
MHashKeyGen(keygenid kgalgo,hashid halgo,unsigned int keysize)36 MHashKeyGen::MHashKeyGen (keygenid kgalgo, hashid halgo, unsigned int keysize)
37 {
38 setKeyGenAlgorithm (kgalgo) ;
39 setKeySize (keysize) ;
40 setHashAlgorithm (halgo) ;
41 AlgorithmData.count = 0 ;
42 AlgorithmData.salt = NULL ;
43 AlgorithmData.salt_size = 0 ;
44 }
45
~MHashKeyGen()46 MHashKeyGen::~MHashKeyGen ()
47 {
48 if (AlgorithmData.salt != NULL) {
49 free (AlgorithmData.salt) ;
50 }
51 }
52
createKey(std::string password)53 std::vector<unsigned char> MHashKeyGen::createKey (std::string password)
54 {
55 char *passwd = (char *) s_malloc (password.size() + 1) ;
56 strcpy (passwd, password.c_str()) ;
57 int passwdlen = strlen (passwd) ;
58 unsigned char *key = (unsigned char *) s_malloc (KeySize) ;
59
60 if (mhash_keygen_ext (Algorithm, AlgorithmData, key, KeySize, (unsigned char *) passwd, passwdlen) < 0) {
61 throw SteghideError (_("could not generate key using libmhash.")) ;
62 }
63
64 std::vector<unsigned char> retval (KeySize) ;
65 for (unsigned int i = 0 ; i < KeySize ; i++) {
66 retval[i] = key[i] ;
67 }
68
69 free (passwd) ;
70 free (key) ;
71
72 return retval ;
73 }
74
setKeySize(unsigned int keysize)75 void MHashKeyGen::setKeySize (unsigned int keysize)
76 {
77 KeySize = keysize ;
78 }
79
setKeyGenAlgorithm(keygenid algo)80 void MHashKeyGen::setKeyGenAlgorithm (keygenid algo)
81 {
82 Algorithm = algo ;
83 }
84
setHashAlgorithm(hashid hashalgo)85 void MHashKeyGen::setHashAlgorithm (hashid hashalgo)
86 {
87 AlgorithmData.hash_algorithm[0] = hashalgo ;
88 }
89
setHashAlgorithms(std::vector<hashid> hashalgos)90 void MHashKeyGen::setHashAlgorithms (std::vector<hashid> hashalgos)
91 {
92 myassert (hashalgos.size() <= 2) ;
93 for (unsigned int i = 0 ; i < hashalgos.size() ; i++) {
94 AlgorithmData.hash_algorithm[i] = hashalgos[i] ;
95 }
96 }
97
setSalt(std::vector<unsigned char> salt)98 void MHashKeyGen::setSalt (std::vector<unsigned char> salt)
99 {
100 AlgorithmData.salt_size = salt.size() ;
101 if (AlgorithmData.salt != NULL) {
102 free (AlgorithmData.salt) ;
103 }
104 AlgorithmData.salt = s_malloc (AlgorithmData.salt_size) ;
105 unsigned char *tmp = (unsigned char *) AlgorithmData.salt ;
106 for (int i = 0 ; i < AlgorithmData.salt_size ; i++) {
107 tmp[i] = salt[i] ;
108 }
109 }
110
s_malloc(size_t size)111 void *MHashKeyGen::s_malloc (size_t size)
112 {
113 void *retval = NULL ;
114 if ((retval = malloc (size)) == NULL) {
115 throw SteghideError (_("could not allocate memory.")) ;
116 }
117 return retval ;
118 }
119