1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#ifndef HASHEXTENT_H
#define HASHEXTENT_H
/**
* hashextent: class to track a hash request or value
*
* Simson L. Garfinkel
* 2009-09-18: SLG - Added to repository
* This file is a work of a US government employee and as such is in the Public domain.
* Simson L. Garfinkel, March 12, 2012
*/
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <algorithm>
/* Support OpenSSL before 1.1.0 */
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define EVP_MD_CTX_new EVP_MD_CTX_create
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
#endif
using std::string;
using std::ostream;
using std::vector;
using std::map;
class hashextent {
public:
hashextent():digest(0),digest_bits_(0){}
~hashextent(){
if(digest) free(digest);
}
uint64_t start;
uint64_t bytes;
hashextent(uint64_t aStart,uint64_t aBytes):start(aStart),bytes(aBytes),digest(0){}
hashextent(AFFILE *af,string alg,uint64_t aStart,uint64_t aBytes):start(aStart),bytes(aBytes),digest(0){
compute_digest(af,alg);
}
static bool compare(const hashextent &e1,const hashextent &e2){
return e1.start < e2.start;
}
static int hexcharval(char hex){
if(hex>='0' && hex<='9') return hex-'0';
if(hex>='A' && hex<='F') return hex+10-'A';
if(hex>='a' && hex<='f') return hex+10-'a';
return 0;
}
static string bin2hex(unsigned char *md,int len){
std::stringstream sbuf;
while(len>0){
char buf[3];
snprintf(buf,sizeof(buf),"%02x",md[0]);
sbuf << buf;
md ++;
len --;
}
return sbuf.str();
}
static int hex2bin(unsigned char *binbuf,size_t hexbuf_size,const char *hex){
int bits = 0;
while(hex[0] && hex[1] && hexbuf_size>0){
*binbuf++ = ((hexcharval(hex[0])<<4) |
hexcharval(hex[1]));
hex += 2;
bits += 8;
hexbuf_size -= 1;
}
if(hexbuf_size>0) binbuf[0] = 0; // might as well null-terminate if there is room
return bits;
}
u_char *get_digest(){
if(!digest){
int bytes = hexdigest.size()/2;
digest = (u_char *)malloc(bytes);
digest_bits_ = hex2bin(digest,bytes,hexdigest.c_str());
}
return digest;
}
/* These parameters are for when the structure is read */
int digest_bits() {
if(!digest) get_digest();
return digest_bits_;
}
string digest_name;
string coding;
string hexdigest;
/** Compute the digest from the disk and set all the fields.
* Return 0 if success, -1 if failure.
*/
int compute_digest(AFFILE *af,string digestToUse){
const EVP_MD *md = EVP_get_digestbyname(digestToUse.c_str());
if(!md) return -1; // digest not available
if(af_seek(af,start,0)!=start) return -1; // can't seek
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx,md);
uint64_t bytes_read = 0;
while(bytes_read < this->bytes){
u_char buf[65536];
int to_read = (this->bytes-bytes_read) < sizeof(buf) ? (this->bytes-bytes_read) : sizeof(buf);
if(af_read(af,buf,to_read)!=to_read){
EVP_MD_CTX_free(ctx);
return -1; // error reading
}
/* compute the hash */
EVP_DigestUpdate(ctx,buf,to_read);
bytes_read += to_read;
}
/* Compute the results */
if(digest!=0) free(digest);
u_int digest_bytes = 1024;
digest = (u_char *)malloc(digest_bytes); // big enough for any conceivable digest
EVP_DigestFinal(ctx,digest,&digest_bytes);
EVP_MD_CTX_free(ctx);
digest_bits_ = digest_bytes*8;
digest_name = digestToUse;
hexdigest = bin2hex(digest,digest_bits_/8);
return 0;
}
/** Return XML for the digest */
string toXML(){
std::stringstream sstart,sbytes;
sstart << start;
sbytes << bytes;
return string("<hash coding='base16' start='") + sstart.str() + "' bytes='" + sbytes.str() +
"' alg='"+digest_name+"'>"+hexdigest+"</hash>";
}
private:;
u_char *digest;
u_int digest_bits_;
};
ostream & operator << (ostream &os, const hashextent &he){
os << "[" << he.digest_name << " @ " << he.start << "(" << he.bytes << " bytes) " << he.hexdigest << "]";
return os;
}
bool operator == (const hashextent &h1,const hashextent &h2) {
return h1.start==h2.start && h1.bytes==h2.bytes && h1.hexdigest==h2.hexdigest;
}
class hashvector:public vector<hashextent> {
public:
static int ireverse(int a,int b){
return a<b ? 1 : -1;
}
/**
* return a list of digests, sorted by inverse bitlength,
* in the hashvector
*/
vector<string> digests(){
vector<int> bits_vector;
std::set<int> bits_set; // why isn't find working on vector? set shouldn't be needed
map<int,string> bits_to_hash;
for(hashvector::iterator it = begin();it!=end();it++){
(*it).get_digest(); // parse the digest to determine length
int bits = (*it).digest_bits();
if(bits_set.find(bits)==bits_set.end()){
bits_set.insert(bits);
bits_vector.push_back(bits);
bits_to_hash[bits] = (*it).digest_name;
}
}
/* Now reverse sort it */
sort(bits_vector.begin(),bits_vector.end(),ireverse);
//sort(bits_vector.begin(),bits_vector.end());
//reverse(bits_vector.begin(),bits_vector.end());
/* Generate the result */
vector<string> ret;
for(vector<int>::const_iterator it = bits_vector.begin();it != bits_vector.end(); it++){
ret.push_back(bits_to_hash[*it]);
}
return ret;
}
/**
* Return the strongest digest in the hashvector that OpenSSL
* makes avilable on the runtime system.
*/
const EVP_MD *strongest_available(){
vector<std::string> algs = digests();
for(vector<std::string>::const_iterator it = algs.begin(); it!=algs.end(); it++){
const EVP_MD *ret = EVP_get_digestbyname((*it).c_str());
if(ret) return ret;
}
return 0; // no digest available
}
};
#endif
|