henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_MESSAGE_DIGEST_H_ |
| 12 | #define RTC_BASE_MESSAGE_DIGEST_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
| 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 20 | namespace rtc { |
| 21 | |
| 22 | // Definitions for the digest algorithms. |
| 23 | extern const char DIGEST_MD5[]; |
| 24 | extern const char DIGEST_SHA_1[]; |
| 25 | extern const char DIGEST_SHA_224[]; |
| 26 | extern const char DIGEST_SHA_256[]; |
| 27 | extern const char DIGEST_SHA_384[]; |
| 28 | extern const char DIGEST_SHA_512[]; |
| 29 | |
| 30 | // A general class for computing hashes. |
| 31 | class MessageDigest { |
| 32 | public: |
| 33 | enum { kMaxSize = 64 }; // Maximum known size (SHA-512) |
| 34 | virtual ~MessageDigest() {} |
| 35 | // Returns the digest output size (e.g. 16 bytes for MD5). |
| 36 | virtual size_t Size() const = 0; |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 37 | // Updates the digest with `len` bytes from `buf`. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 38 | virtual void Update(const void* buf, size_t len) = 0; |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 39 | // Outputs the digest value to `buf` with length `len`. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 40 | // Returns the number of bytes written, i.e., Size(). |
| 41 | virtual size_t Finish(void* buf, size_t len) = 0; |
| 42 | }; |
| 43 | |
| 44 | // A factory class for creating digest objects. |
| 45 | class MessageDigestFactory { |
| 46 | public: |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 47 | static MessageDigest* Create(absl::string_view alg); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 48 | }; |
| 49 | |
Harald Alvestrand | cffaf0a | 2021-01-05 15:55:20 | [diff] [blame] | 50 | // A check that an algorithm is in a list of approved digest algorithms |
| 51 | // from RFC 4572 (FIPS 180). |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 52 | bool IsFips180DigestAlgorithm(absl::string_view alg); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 53 | |
| 54 | // Functions to create hashes. |
| 55 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 56 | // Computes the hash of `in_len` bytes of `input`, using the `digest` hash |
| 57 | // implementation, and outputs the hash to the buffer `output`, which is |
| 58 | // `out_len` bytes long. Returns the number of bytes written to `output` if |
| 59 | // successful, or 0 if `out_len` was too small. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 60 | size_t ComputeDigest(MessageDigest* digest, |
| 61 | const void* input, |
| 62 | size_t in_len, |
| 63 | void* output, |
| 64 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 65 | // Like the previous function, but creates a digest implementation based on |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 66 | // the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 67 | // digest with the given name. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 68 | size_t ComputeDigest(absl::string_view alg, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 69 | const void* input, |
| 70 | size_t in_len, |
| 71 | void* output, |
| 72 | size_t out_len); |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 73 | // Computes the hash of `input` using the `digest` hash implementation, and |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 74 | // returns it as a hex-encoded string. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 75 | std::string ComputeDigest(MessageDigest* digest, absl::string_view input); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 76 | // Like the previous function, but creates a digest implementation based on |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 77 | // the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 78 | // there is no digest with the given name. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 79 | std::string ComputeDigest(absl::string_view alg, absl::string_view input); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 80 | // Like the previous function, but returns an explicit result code. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 81 | bool ComputeDigest(absl::string_view alg, |
| 82 | absl::string_view input, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 83 | std::string* output); |
| 84 | |
| 85 | // Shorthand way to compute a hex-encoded hash using MD5. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 86 | inline std::string MD5(absl::string_view input) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 87 | return ComputeDigest(DIGEST_MD5, input); |
| 88 | } |
| 89 | |
| 90 | // Functions to compute RFC 2104 HMACs. |
| 91 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 92 | // Computes the HMAC of `in_len` bytes of `input`, using the `digest` hash |
| 93 | // implementation and `key_len` bytes of `key` to key the HMAC, and outputs |
| 94 | // the HMAC to the buffer `output`, which is `out_len` bytes long. Returns the |
| 95 | // number of bytes written to `output` if successful, or 0 if `out_len` was too |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 96 | // small. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 97 | size_t ComputeHmac(MessageDigest* digest, |
| 98 | const void* key, |
| 99 | size_t key_len, |
| 100 | const void* input, |
| 101 | size_t in_len, |
| 102 | void* output, |
| 103 | size_t out_len); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 104 | // Like the previous function, but creates a digest implementation based on |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 105 | // the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 106 | // digest with the given name. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 107 | size_t ComputeHmac(absl::string_view alg, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 108 | const void* key, |
| 109 | size_t key_len, |
| 110 | const void* input, |
| 111 | size_t in_len, |
| 112 | void* output, |
| 113 | size_t out_len); |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 114 | // Computes the HMAC of `input` using the `digest` hash implementation and `key` |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 115 | // to key the HMAC, and returns it as a hex-encoded string. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 116 | std::string ComputeHmac(MessageDigest* digest, |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 117 | absl::string_view key, |
| 118 | absl::string_view input); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 119 | // Like the previous function, but creates a digest implementation based on |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 120 | // the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 121 | // there is no digest with the given name. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 122 | std::string ComputeHmac(absl::string_view alg, |
| 123 | absl::string_view key, |
| 124 | absl::string_view input); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 125 | // Like the previous function, but returns an explicit result code. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 126 | bool ComputeHmac(absl::string_view alg, |
| 127 | absl::string_view key, |
| 128 | absl::string_view input, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 129 | std::string* output); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 130 | |
| 131 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 132 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 133 | #endif // RTC_BASE_MESSAGE_DIGEST_H_ |