1 /* 2 * Copyright (c) 2017-2021 Ribose Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef CRYPTO_HASH_H_ 28 #define CRYPTO_HASH_H_ 29 30 #include <repgp/repgp_def.h> 31 #include "types.h" 32 #include "config.h" 33 34 /** 35 * Output size (in bytes) of biggest supported hash algo 36 */ 37 #define PGP_MAX_HASH_SIZE (64) 38 39 namespace rnp { 40 class Hash { 41 protected: 42 void * handle_; 43 size_t size_; 44 pgp_hash_alg_t alg_; 45 46 public: 47 pgp_hash_alg_t alg() const; 48 size_t size() const; 49 Hash()50 Hash() : handle_(NULL), size_(0), alg_(PGP_HASH_UNKNOWN){}; 51 Hash(pgp_hash_alg_t alg); 52 Hash(Hash &&src); 53 54 virtual void add(const void *buf, size_t len); 55 virtual void add(uint32_t val); 56 virtual void add(const pgp_mpi_t &mpi); 57 virtual size_t finish(uint8_t *digest = NULL); 58 virtual void clone(Hash &dst) const; 59 60 Hash &operator=(const Hash &src); 61 Hash &operator=(Hash &&src); 62 63 virtual ~Hash(); 64 65 /* Hash algorithm by string representation from cleartext-signed text */ 66 static pgp_hash_alg_t alg(const char *name); 67 /* Hash algorithm representation for cleartext-signed text */ 68 static const char *name(pgp_hash_alg_t alg); 69 /* Hash algorithm representation for the backend functions */ 70 static const char *name_backend(pgp_hash_alg_t alg); 71 /* Size of the hash algorithm output or 0 if algorithm is unknown */ 72 static size_t size(pgp_hash_alg_t alg); 73 }; 74 75 #if defined(CRYPTO_BACKEND_BOTAN) 76 class CRC24 : public Hash { 77 public: 78 CRC24(); 79 }; 80 #endif 81 #if defined(CRYPTO_BACKEND_OPENSSL) 82 class CRC24 { 83 uint32_t state_; 84 85 public: 86 CRC24(); 87 88 void add(const void *buf, size_t len); 89 size_t finish(uint8_t *crc); 90 }; 91 #endif 92 93 class HashList { 94 std::vector<Hash> hashes_; 95 96 public: 97 void add_alg(pgp_hash_alg_t alg); 98 const Hash * get(pgp_hash_alg_t alg) const; 99 void add(const void *buf, size_t len); 100 bool empty() const; 101 std::vector<Hash> &hashes(); 102 }; 103 104 } // namespace rnp 105 106 #endif 107