blob: 632b9af0753c4510d213795bdfb22f31f98d1beb [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_MESSAGE_DIGEST_H_
12#define RTC_BASE_MESSAGE_DIGEST_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2617
Ali Tofigh7fa90572022-03-17 14:47:4918#include "absl/strings/string_view.h"
19
Henrik Kjellanderec78f1c2017-06-29 05:52:5020namespace rtc {
21
22// Definitions for the digest algorithms.
23extern const char DIGEST_MD5[];
24extern const char DIGEST_SHA_1[];
25extern const char DIGEST_SHA_224[];
26extern const char DIGEST_SHA_256[];
27extern const char DIGEST_SHA_384[];
28extern const char DIGEST_SHA_512[];
29
30// A general class for computing hashes.
31class 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 Titov96e3b992021-07-26 14:03:1437 // Updates the digest with `len` bytes from `buf`.
Henrik Kjellanderec78f1c2017-06-29 05:52:5038 virtual void Update(const void* buf, size_t len) = 0;
Artem Titov96e3b992021-07-26 14:03:1439 // Outputs the digest value to `buf` with length `len`.
Henrik Kjellanderec78f1c2017-06-29 05:52:5040 // 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.
45class MessageDigestFactory {
46 public:
Ali Tofigh7fa90572022-03-17 14:47:4947 static MessageDigest* Create(absl::string_view alg);
Henrik Kjellanderec78f1c2017-06-29 05:52:5048};
49
Harald Alvestrandcffaf0a2021-01-05 15:55:2050// A check that an algorithm is in a list of approved digest algorithms
51// from RFC 4572 (FIPS 180).
Ali Tofigh7fa90572022-03-17 14:47:4952bool IsFips180DigestAlgorithm(absl::string_view alg);
Henrik Kjellanderec78f1c2017-06-29 05:52:5053
54// Functions to create hashes.
55
Artem Titov96e3b992021-07-26 14:03:1456// 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 Gerey665174f2018-06-19 13:03:0560size_t ComputeDigest(MessageDigest* digest,
61 const void* input,
62 size_t in_len,
63 void* output,
64 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 05:52:5065// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 14:03:1466// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no
Henrik Kjellanderec78f1c2017-06-29 05:52:5067// digest with the given name.
Ali Tofigh7fa90572022-03-17 14:47:4968size_t ComputeDigest(absl::string_view alg,
Yves Gerey665174f2018-06-19 13:03:0569 const void* input,
70 size_t in_len,
71 void* output,
72 size_t out_len);
Artem Titov96e3b992021-07-26 14:03:1473// Computes the hash of `input` using the `digest` hash implementation, and
Henrik Kjellanderec78f1c2017-06-29 05:52:5074// returns it as a hex-encoded string.
Ali Tofigh7fa90572022-03-17 14:47:4975std::string ComputeDigest(MessageDigest* digest, absl::string_view input);
Henrik Kjellanderec78f1c2017-06-29 05:52:5076// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 14:03:1477// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if
Henrik Kjellanderec78f1c2017-06-29 05:52:5078// there is no digest with the given name.
Ali Tofigh7fa90572022-03-17 14:47:4979std::string ComputeDigest(absl::string_view alg, absl::string_view input);
Henrik Kjellanderec78f1c2017-06-29 05:52:5080// Like the previous function, but returns an explicit result code.
Ali Tofigh7fa90572022-03-17 14:47:4981bool ComputeDigest(absl::string_view alg,
82 absl::string_view input,
Henrik Kjellanderec78f1c2017-06-29 05:52:5083 std::string* output);
84
85// Shorthand way to compute a hex-encoded hash using MD5.
Ali Tofigh7fa90572022-03-17 14:47:4986inline std::string MD5(absl::string_view input) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5087 return ComputeDigest(DIGEST_MD5, input);
88}
89
90// Functions to compute RFC 2104 HMACs.
91
Artem Titov96e3b992021-07-26 14:03:1492// 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 Kjellanderec78f1c2017-06-29 05:52:5096// small.
Yves Gerey665174f2018-06-19 13:03:0597size_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 Kjellanderec78f1c2017-06-29 05:52:50104// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 14:03:14105// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no
Henrik Kjellanderec78f1c2017-06-29 05:52:50106// digest with the given name.
Ali Tofigh7fa90572022-03-17 14:47:49107size_t ComputeHmac(absl::string_view alg,
Yves Gerey665174f2018-06-19 13:03:05108 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 Titov96e3b992021-07-26 14:03:14114// Computes the HMAC of `input` using the `digest` hash implementation and `key`
Henrik Kjellanderec78f1c2017-06-29 05:52:50115// to key the HMAC, and returns it as a hex-encoded string.
Yves Gerey665174f2018-06-19 13:03:05116std::string ComputeHmac(MessageDigest* digest,
Ali Tofigh7fa90572022-03-17 14:47:49117 absl::string_view key,
118 absl::string_view input);
Henrik Kjellanderec78f1c2017-06-29 05:52:50119// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 14:03:14120// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if
Henrik Kjellanderec78f1c2017-06-29 05:52:50121// there is no digest with the given name.
Ali Tofigh7fa90572022-03-17 14:47:49122std::string ComputeHmac(absl::string_view alg,
123 absl::string_view key,
124 absl::string_view input);
Henrik Kjellanderec78f1c2017-06-29 05:52:50125// Like the previous function, but returns an explicit result code.
Ali Tofigh7fa90572022-03-17 14:47:49126bool ComputeHmac(absl::string_view alg,
127 absl::string_view key,
128 absl::string_view input,
Yves Gerey665174f2018-06-19 13:03:05129 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 05:52:50130
131} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26132
Steve Anton10542f22019-01-11 17:11:00133#endif // RTC_BASE_MESSAGE_DIGEST_H_