| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2019 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
/** \file |
| 10 |
* Fingerprint (cryptographic hashing) support for generating strong hashes of image |
| 11 |
* data. |
| 12 |
*/ |
| 13 |
|
| 14 |
/*****************************************************************************/ |
| 15 |
|
| 16 |
#ifndef __dng_fingerprint__ |
| 17 |
#define __dng_fingerprint__ |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
#include "dng_exceptions.h" |
| 22 |
#include "dng_types.h" |
| 23 |
#include "dng_stream.h" |
| 24 |
#include "dng_string.h" |
| 25 |
|
| 26 |
#include <cstring> |
| 27 |
#include <unordered_set> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
/*****************************************************************************/ |
| 31 |
|
| 32 |
/// @brief Container fingerprint (MD5 only at present). |
| 33 |
|
| 34 |
class dng_fingerprint |
| 35 |
{ |
| 36 |
|
| 37 |
public: |
| 38 |
|
| 39 |
static const size_t kDNGFingerprintSize = 16; |
| 40 |
|
| 41 |
uint8 data [kDNGFingerprintSize]; |
| 42 |
|
| 43 |
public: |
| 44 |
|
| 45 |
dng_fingerprint (); |
| 46 |
|
| 47 |
explicit dng_fingerprint (const char *hex); |
| 48 |
|
| 49 |
/// Check if fingerprint is all zeros. |
| 50 |
|
| 51 |
bool IsNull () const; |
| 52 |
|
| 53 |
/// Same as IsNull but expresses intention of testing validity. |
| 54 |
|
| 55 |
bool IsValid () const |
| 56 |
{ |
| 57 |
return !IsNull (); |
| 58 |
} |
| 59 |
|
| 60 |
/// Set to all zeros, a value used to indicate an invalid fingerprint. |
| 61 |
|
| 62 |
void Clear () |
| 63 |
{ |
| 64 |
*this = dng_fingerprint (); |
| 65 |
} |
| 66 |
|
| 67 |
/// Test if two fingerprints are equal. |
| 68 |
|
| 69 |
bool operator== (const dng_fingerprint &print) const; |
| 70 |
|
| 71 |
/// Test if two fingerprints are not equal. |
| 72 |
|
| 73 |
bool operator!= (const dng_fingerprint &print) const |
| 74 |
{ |
| 75 |
return !(*this == print); |
| 76 |
} |
| 77 |
|
| 78 |
/// Comparison test for fingerprints. |
| 79 |
|
| 80 |
bool operator< (const dng_fingerprint &print) const; |
| 81 |
|
| 82 |
/// Produce a 32-bit hash value from fingerprint used for faster hashing of |
| 83 |
/// fingerprints. |
| 84 |
|
| 85 |
uint32 Collapse32 () const; |
| 86 |
|
| 87 |
/// Convert fingerprint to UTF-8 string. |
| 88 |
/// |
| 89 |
/// \param resultStr The output array to which the UTF-8 encoding of the |
| 90 |
/// fingerprint will be written. |
| 91 |
|
| 92 |
void ToUtf8HexString (char resultStr [2 * kDNGFingerprintSize + 1]) const; |
| 93 |
|
| 94 |
/// Convert fingerprint to UTF-8 string and return result as a dng_string. |
| 95 |
|
| 96 |
dng_string ToUtf8HexString () const; |
| 97 |
|
| 98 |
/// Convert UTF-8 string to fingerprint. Returns true on success, false on |
| 99 |
/// failure. |
| 100 |
/// |
| 101 |
/// \param inputStr The input array from which the UTF-8 encoding of the |
| 102 |
/// fingerprint will be read. |
| 103 |
/// |
| 104 |
/// \retval True indicates success. |
| 105 |
|
| 106 |
bool FromUtf8HexString (const char inputStr [2 * kDNGFingerprintSize + 1]); |
| 107 |
|
| 108 |
/// Convert dng_string string to fingerprint. Returns true on success, |
| 109 |
/// false on failure. |
| 110 |
/// |
| 111 |
/// \param inputStr The input dng_string from which the UTF-8 encoding of the |
| 112 |
/// fingerprint will be read. |
| 113 |
/// |
| 114 |
/// \retval True indicates success. |
| 115 |
|
| 116 |
bool FromUtf8HexString (const dng_string &inputStr); |
| 117 |
|
| 118 |
}; |
| 119 |
|
| 120 |
/*****************************************************************************/ |
| 121 |
|
| 122 |
/// @brief Utility to compare fingerprints (e.g., for sorting). |
| 123 |
|
| 124 |
struct dng_fingerprint_less_than |
| 125 |
{ |
| 126 |
|
| 127 |
/// Less-than comparison. |
| 128 |
|
| 129 |
bool operator() (const dng_fingerprint &a, |
| 130 |
const dng_fingerprint &b) const |
| 131 |
{ |
| 132 |
|
| 133 |
return memcmp (a.data, |
| 134 |
b.data, |
| 135 |
sizeof (a.data)) < 0; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
}; |
| 140 |
|
| 141 |
/******************************************************************************/ |
| 142 |
|
| 143 |
/// @brief Utility to hash fingerprints (e.g., for hashtables). |
| 144 |
|
| 145 |
struct dng_fingerprint_hash |
| 146 |
{ |
| 147 |
|
| 148 |
/// Hash function. |
| 149 |
|
| 150 |
size_t operator () (const dng_fingerprint &digest) const |
| 151 |
{ |
| 152 |
|
| 153 |
return (size_t) digest.Collapse32 (); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
}; |
| 158 |
|
| 159 |
/******************************************************************************/ |
| 160 |
|
| 161 |
typedef std::unordered_set<dng_fingerprint, |
| 162 |
dng_fingerprint_hash> dng_fingerprint_table; |
| 163 |
|
| 164 |
/******************************************************************************/ |
| 165 |
|
| 166 |
typedef std::vector<dng_fingerprint> dng_fingerprint_vector; |
| 167 |
|
| 168 |
/******************************************************************************/ |
| 169 |
|
| 170 |
// Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm. |
| 171 |
|
| 172 |
// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All |
| 173 |
// rights reserved. |
| 174 |
// |
| 175 |
// License to copy and use this software is granted provided that it |
| 176 |
// is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
| 177 |
// Algorithm" in all material mentioning or referencing this software |
| 178 |
// or this function. |
| 179 |
// |
| 180 |
// License is also granted to make and use derivative works provided |
| 181 |
// that such works are identified as "derived from the RSA Data |
| 182 |
// Security, Inc. MD5 Message-Digest Algorithm" in all material |
| 183 |
// mentioning or referencing the derived work. |
| 184 |
// |
| 185 |
// RSA Data Security, Inc. makes no representations concerning either |
| 186 |
// the merchantability of this software or the suitability of this |
| 187 |
// software for any particular purpose. It is provided "as is" |
| 188 |
// without express or implied warranty of any kind. |
| 189 |
// |
| 190 |
// These notices must be retained in any copies of any part of this |
| 191 |
// documentation and/or software. |
| 192 |
|
| 193 |
/// @brief Class to hash binary data to a fingerprint using the MD5 Message-Digest |
| 194 |
/// Algorithm. |
| 195 |
|
| 196 |
class dng_md5_printer |
| 197 |
{ |
| 198 |
|
| 199 |
public: |
| 200 |
|
| 201 |
dng_md5_printer (); |
| 202 |
|
| 203 |
virtual ~dng_md5_printer () |
| 204 |
{ |
| 205 |
} |
| 206 |
|
| 207 |
/// Reset the fingerprint. |
| 208 |
|
| 209 |
void Reset (); |
| 210 |
|
| 211 |
/// Append the data to the stream to be hashed. |
| 212 |
/// \param data The data to be hashed. |
| 213 |
/// \param inputLen The length of data, in bytes. |
| 214 |
|
| 215 |
void Process (const void *data, |
| 216 |
uint32 inputLen); |
| 217 |
|
| 218 |
/// Append the string to the stream to be hashed. |
| 219 |
/// \param text The string to be hashed. |
| 220 |
|
| 221 |
void Process (const char *text) |
| 222 |
{ |
| 223 |
|
| 224 |
Process (text, (uint32) strlen (text)); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
/// Get the fingerprint (i.e., result of the hash). |
| 229 |
|
| 230 |
const dng_fingerprint & Result (); |
| 231 |
|
| 232 |
private: |
| 233 |
|
| 234 |
static void Encode (uint8 *output, |
| 235 |
const uint32 *input, |
| 236 |
uint32 len); |
| 237 |
|
| 238 |
static void Decode (uint32 *output, |
| 239 |
const uint8 *input, |
| 240 |
uint32 len); |
| 241 |
|
| 242 |
// F, G, H and I are basic MD5 functions. |
| 243 |
|
| 244 |
static inline uint32 F (uint32 x, |
| 245 |
uint32 y, |
| 246 |
uint32 z) |
| 247 |
{ |
| 248 |
return (x & y) | (~x & z); |
| 249 |
} |
| 250 |
|
| 251 |
static inline uint32 G (uint32 x, |
| 252 |
uint32 y, |
| 253 |
uint32 z) |
| 254 |
{ |
| 255 |
return (x & z) | (y & ~z); |
| 256 |
} |
| 257 |
|
| 258 |
static inline uint32 H (uint32 x, |
| 259 |
uint32 y, |
| 260 |
uint32 z) |
| 261 |
{ |
| 262 |
return x ^ y ^ z; |
| 263 |
} |
| 264 |
|
| 265 |
static inline uint32 I (uint32 x, |
| 266 |
uint32 y, |
| 267 |
uint32 z) |
| 268 |
{ |
| 269 |
return y ^ (x | ~z); |
| 270 |
} |
| 271 |
|
| 272 |
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. |
| 273 |
|
| 274 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 275 |
static inline void FF (uint32 &a, |
| 276 |
uint32 b, |
| 277 |
uint32 c, |
| 278 |
uint32 d, |
| 279 |
uint32 x, |
| 280 |
uint32 s, |
| 281 |
uint32 ac) |
| 282 |
{ |
| 283 |
a += F (b, c, d) + x + ac; |
| 284 |
a = (a << s) | (a >> (32 - s)); |
| 285 |
a += b; |
| 286 |
} |
| 287 |
|
| 288 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 289 |
static inline void GG (uint32 &a, |
| 290 |
uint32 b, |
| 291 |
uint32 c, |
| 292 |
uint32 d, |
| 293 |
uint32 x, |
| 294 |
uint32 s, |
| 295 |
uint32 ac) |
| 296 |
{ |
| 297 |
a += G (b, c, d) + x + ac; |
| 298 |
a = (a << s) | (a >> (32 - s)); |
| 299 |
a += b; |
| 300 |
} |
| 301 |
|
| 302 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 303 |
static inline void HH (uint32 &a, |
| 304 |
uint32 b, |
| 305 |
uint32 c, |
| 306 |
uint32 d, |
| 307 |
uint32 x, |
| 308 |
uint32 s, |
| 309 |
uint32 ac) |
| 310 |
{ |
| 311 |
a += H (b, c, d) + x + ac; |
| 312 |
a = (a << s) | (a >> (32 - s)); |
| 313 |
a += b; |
| 314 |
} |
| 315 |
|
| 316 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 317 |
static inline void II (uint32 &a, |
| 318 |
uint32 b, |
| 319 |
uint32 c, |
| 320 |
uint32 d, |
| 321 |
uint32 x, |
| 322 |
uint32 s, |
| 323 |
uint32 ac) |
| 324 |
{ |
| 325 |
a += I (b, c, d) + x + ac; |
| 326 |
a = (a << s) | (a >> (32 - s)); |
| 327 |
a += b; |
| 328 |
} |
| 329 |
|
| 330 |
static void MD5Transform (uint32 state [4], |
| 331 |
const uint8 block [64]); |
| 332 |
|
| 333 |
private: |
| 334 |
|
| 335 |
uint32 state [4]; |
| 336 |
|
| 337 |
uint32 count [2]; |
| 338 |
|
| 339 |
uint8 buffer [64]; |
| 340 |
|
| 341 |
bool final; |
| 342 |
|
| 343 |
dng_fingerprint result; |
| 344 |
|
| 345 |
}; |
| 346 |
|
| 347 |
/*****************************************************************************/ |
| 348 |
|
| 349 |
/// @brief A dng_stream based interface to the MD5 printing logic. |
| 350 |
|
| 351 |
class dng_md5_printer_stream : public dng_stream, dng_md5_printer |
| 352 |
{ |
| 353 |
|
| 354 |
private: |
| 355 |
|
| 356 |
uint64 fNextOffset; |
| 357 |
|
| 358 |
public: |
| 359 |
|
| 360 |
/// Create an empty MD5 printer stream. |
| 361 |
|
| 362 |
dng_md5_printer_stream () |
| 363 |
|
| 364 |
: fNextOffset (0) |
| 365 |
|
| 366 |
{ |
| 367 |
} |
| 368 |
|
| 369 |
virtual uint64 DoGetLength () |
| 370 |
{ |
| 371 |
|
| 372 |
return fNextOffset; |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
virtual void DoRead (void * /* data */, |
| 377 |
uint32 /* count */, |
| 378 |
uint64 /* offset */) |
| 379 |
{ |
| 380 |
|
| 381 |
ThrowProgramError (); |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
virtual void DoSetLength (uint64 length) |
| 386 |
{ |
| 387 |
|
| 388 |
if (length != fNextOffset) |
| 389 |
{ |
| 390 |
ThrowProgramError (); |
| 391 |
} |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
virtual void DoWrite (const void *data, |
| 396 |
uint32 count2, |
| 397 |
uint64 offset) |
| 398 |
{ |
| 399 |
|
| 400 |
if (offset != fNextOffset) |
| 401 |
{ |
| 402 |
ThrowProgramError (); |
| 403 |
} |
| 404 |
|
| 405 |
Process (data, count2); |
| 406 |
|
| 407 |
fNextOffset += count2; |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
const dng_fingerprint & Result () |
| 412 |
{ |
| 413 |
|
| 414 |
Flush (); |
| 415 |
|
| 416 |
return dng_md5_printer::Result (); |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
}; |
| 421 |
|
| 422 |
/*****************************************************************************/ |
| 423 |
|
| 424 |
#endif |
| 425 |
|
| 426 |
/*****************************************************************************/ |