blob: bbb22cf84e4717f88b3c7aa1d111d587f3e8274b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#ifndef RTC_BASE_MESSAGE_DIGEST_H_
12#define RTC_BASE_MESSAGE_DIGEST_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
19// Definitions for the digest algorithms.
20extern const char DIGEST_MD5[];
21extern const char DIGEST_SHA_1[];
22extern const char DIGEST_SHA_224[];
23extern const char DIGEST_SHA_256[];
24extern const char DIGEST_SHA_384[];
25extern const char DIGEST_SHA_512[];
26
27// A general class for computing hashes.
28class 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.
42class 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).
48bool 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 Gerey665174f2018-06-19 15:03:05 +020056size_t ComputeDigest(MessageDigest* digest,
57 const void* input,
58 size_t in_len,
59 void* output,
60 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061// 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 Gerey665174f2018-06-19 15:03:05 +020064size_t ComputeDigest(const std::string& alg,
65 const void* input,
66 size_t in_len,
67 void* output,
68 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069// Computes the hash of |input| using the |digest| hash implementation, and
70// returns it as a hex-encoded string.
71std::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.
75std::string ComputeDigest(const std::string& alg, const std::string& input);
76// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +020077bool ComputeDigest(const std::string& alg,
78 const std::string& input,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 std::string* output);
80
81// Shorthand way to compute a hex-encoded hash using MD5.
82inline 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 Gerey665174f2018-06-19 15:03:05 +020093size_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 Kjellanderec78f1c2017-06-29 07:52:50 +0200100// 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 Gerey665174f2018-06-19 15:03:05 +0200103size_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 Kjellanderec78f1c2017-06-29 07:52:50 +0200110// 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 Gerey665174f2018-06-19 15:03:05 +0200112std::string ComputeHmac(MessageDigest* digest,
113 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 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 Gerey665174f2018-06-19 15:03:05 +0200118std::string ComputeHmac(const std::string& alg,
119 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 const std::string& input);
121// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +0200122bool ComputeHmac(const std::string& alg,
123 const std::string& key,
124 const std::string& input,
125 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126
127} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128
Steve Anton10542f22019-01-11 09:11:00 -0800129#endif // RTC_BASE_MESSAGE_DIGEST_H_