henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_MESSAGEDIGEST_H_ |
| 12 | #define RTC_BASE_MESSAGEDIGEST_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | namespace rtc { |
| 18 | |
| 19 | // Definitions for the digest algorithms. |
| 20 | extern const char DIGEST_MD5[]; |
| 21 | extern const char DIGEST_SHA_1[]; |
| 22 | extern const char DIGEST_SHA_224[]; |
| 23 | extern const char DIGEST_SHA_256[]; |
| 24 | extern const char DIGEST_SHA_384[]; |
| 25 | extern const char DIGEST_SHA_512[]; |
| 26 | |
| 27 | // A general class for computing hashes. |
| 28 | class MessageDigest { |
| 29 | public: |
| 30 | enum { kMaxSize = 64 }; // Maximum known size (SHA-512) |
| 31 | virtual ~MessageDigest() {} |
| 32 | // Returns the digest output size (e.g. 16 bytes for MD5). |
| 33 | virtual size_t Size() const = 0; |
| 34 | // Updates the digest with |len| bytes from |buf|. |
| 35 | virtual void Update(const void* buf, size_t len) = 0; |
| 36 | // Outputs the digest value to |buf| with length |len|. |
| 37 | // Returns the number of bytes written, i.e., Size(). |
| 38 | virtual size_t Finish(void* buf, size_t len) = 0; |
| 39 | }; |
| 40 | |
| 41 | // A factory class for creating digest objects. |
| 42 | class MessageDigestFactory { |
| 43 | public: |
| 44 | static MessageDigest* Create(const std::string& alg); |
| 45 | }; |
| 46 | |
| 47 | // A whitelist of approved digest algorithms from RFC 4572 (FIPS 180). |
| 48 | bool IsFips180DigestAlgorithm(const std::string& alg); |
| 49 | |
| 50 | // Functions to create hashes. |
| 51 | |
| 52 | // Computes the hash of |in_len| bytes of |input|, using the |digest| hash |
| 53 | // implementation, and outputs the hash to the buffer |output|, which is |
| 54 | // |out_len| bytes long. Returns the number of bytes written to |output| if |
| 55 | // successful, or 0 if |out_len| was too small. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | size_t ComputeDigest(MessageDigest* digest, |
| 57 | const void* input, |
| 58 | size_t in_len, |
| 59 | void* output, |
| 60 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | // Like the previous function, but creates a digest implementation based on |
| 62 | // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no |
| 63 | // digest with the given name. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | size_t ComputeDigest(const std::string& alg, |
| 65 | const void* input, |
| 66 | size_t in_len, |
| 67 | void* output, |
| 68 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | // Computes the hash of |input| using the |digest| hash implementation, and |
| 70 | // returns it as a hex-encoded string. |
| 71 | std::string ComputeDigest(MessageDigest* digest, const std::string& input); |
| 72 | // Like the previous function, but creates a digest implementation based on |
| 73 | // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if |
| 74 | // there is no digest with the given name. |
| 75 | std::string ComputeDigest(const std::string& alg, const std::string& input); |
| 76 | // Like the previous function, but returns an explicit result code. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | bool ComputeDigest(const std::string& alg, |
| 78 | const std::string& input, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | std::string* output); |
| 80 | |
| 81 | // Shorthand way to compute a hex-encoded hash using MD5. |
| 82 | inline std::string MD5(const std::string& input) { |
| 83 | return ComputeDigest(DIGEST_MD5, input); |
| 84 | } |
| 85 | |
| 86 | // Functions to compute RFC 2104 HMACs. |
| 87 | |
| 88 | // Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash |
| 89 | // implementation and |key_len| bytes of |key| to key the HMAC, and outputs |
| 90 | // the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the |
| 91 | // number of bytes written to |output| if successful, or 0 if |out_len| was too |
| 92 | // small. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 93 | size_t ComputeHmac(MessageDigest* digest, |
| 94 | const void* key, |
| 95 | size_t key_len, |
| 96 | const void* input, |
| 97 | size_t in_len, |
| 98 | void* output, |
| 99 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 100 | // Like the previous function, but creates a digest implementation based on |
| 101 | // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no |
| 102 | // digest with the given name. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | size_t ComputeHmac(const std::string& alg, |
| 104 | const void* key, |
| 105 | size_t key_len, |
| 106 | const void* input, |
| 107 | size_t in_len, |
| 108 | void* output, |
| 109 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | // Computes the HMAC of |input| using the |digest| hash implementation and |key| |
| 111 | // to key the HMAC, and returns it as a hex-encoded string. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 112 | std::string ComputeHmac(MessageDigest* digest, |
| 113 | const std::string& key, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 114 | const std::string& input); |
| 115 | // Like the previous function, but creates a digest implementation based on |
| 116 | // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if |
| 117 | // there is no digest with the given name. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | std::string ComputeHmac(const std::string& alg, |
| 119 | const std::string& key, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | const std::string& input); |
| 121 | // Like the previous function, but returns an explicit result code. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | bool ComputeHmac(const std::string& alg, |
| 123 | const std::string& key, |
| 124 | const std::string& input, |
| 125 | std::string* output); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | |
| 127 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 129 | #endif // RTC_BASE_MESSAGEDIGEST_H_ |