blob: 36f00b527339cc4e887718891c6d98e7f4f4889a [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20// Definitions for the digest algorithms.
21extern const char DIGEST_MD5[];
22extern const char DIGEST_SHA_1[];
23extern const char DIGEST_SHA_224[];
24extern const char DIGEST_SHA_256[];
25extern const char DIGEST_SHA_384[];
26extern const char DIGEST_SHA_512[];
27
28// A general class for computing hashes.
29class MessageDigest {
30 public:
31 enum { kMaxSize = 64 }; // Maximum known size (SHA-512)
32 virtual ~MessageDigest() {}
33 // Returns the digest output size (e.g. 16 bytes for MD5).
34 virtual size_t Size() const = 0;
35 // Updates the digest with |len| bytes from |buf|.
36 virtual void Update(const void* buf, size_t len) = 0;
37 // Outputs the digest value to |buf| with length |len|.
38 // Returns the number of bytes written, i.e., Size().
39 virtual size_t Finish(void* buf, size_t len) = 0;
40};
41
42// A factory class for creating digest objects.
43class MessageDigestFactory {
44 public:
45 static MessageDigest* Create(const std::string& alg);
46};
47
48// A whitelist of approved digest algorithms from RFC 4572 (FIPS 180).
49bool IsFips180DigestAlgorithm(const std::string& alg);
50
51// Functions to create hashes.
52
53// Computes the hash of |in_len| bytes of |input|, using the |digest| hash
54// implementation, and outputs the hash to the buffer |output|, which is
55// |out_len| bytes long. Returns the number of bytes written to |output| if
56// successful, or 0 if |out_len| was too small.
Yves Gerey665174f2018-06-19 15:03:05 +020057size_t ComputeDigest(MessageDigest* digest,
58 const void* input,
59 size_t in_len,
60 void* output,
61 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062// Like the previous function, but creates a digest implementation based on
63// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
64// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +020065size_t ComputeDigest(const std::string& alg,
66 const void* input,
67 size_t in_len,
68 void* output,
69 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070// Computes the hash of |input| using the |digest| hash implementation, and
71// returns it as a hex-encoded string.
72std::string ComputeDigest(MessageDigest* digest, const std::string& input);
73// Like the previous function, but creates a digest implementation based on
74// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
75// there is no digest with the given name.
76std::string ComputeDigest(const std::string& alg, const std::string& input);
77// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +020078bool ComputeDigest(const std::string& alg,
79 const std::string& input,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080 std::string* output);
81
82// Shorthand way to compute a hex-encoded hash using MD5.
83inline std::string MD5(const std::string& input) {
84 return ComputeDigest(DIGEST_MD5, input);
85}
86
87// Functions to compute RFC 2104 HMACs.
88
89// Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
90// implementation and |key_len| bytes of |key| to key the HMAC, and outputs
91// the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
92// number of bytes written to |output| if successful, or 0 if |out_len| was too
93// small.
Yves Gerey665174f2018-06-19 15:03:05 +020094size_t ComputeHmac(MessageDigest* digest,
95 const void* key,
96 size_t key_len,
97 const void* input,
98 size_t in_len,
99 void* output,
100 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101// Like the previous function, but creates a digest implementation based on
102// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
103// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200104size_t ComputeHmac(const std::string& alg,
105 const void* key,
106 size_t key_len,
107 const void* input,
108 size_t in_len,
109 void* output,
110 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111// Computes the HMAC of |input| using the |digest| hash implementation and |key|
112// to key the HMAC, and returns it as a hex-encoded string.
Yves Gerey665174f2018-06-19 15:03:05 +0200113std::string ComputeHmac(MessageDigest* digest,
114 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 const std::string& input);
116// Like the previous function, but creates a digest implementation based on
117// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
118// there is no digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200119std::string ComputeHmac(const std::string& alg,
120 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 const std::string& input);
122// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +0200123bool ComputeHmac(const std::string& alg,
124 const std::string& key,
125 const std::string& input,
126 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127
128} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129
Steve Anton10542f22019-01-11 09:11:00 -0800130#endif // RTC_BASE_MESSAGE_DIGEST_H_