blob: 02e0bfd561f67785942e6eb09fb7dfb2465b052d [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;
Artem Titov96e3b992021-07-26 16:03:14 +020035 // Updates the digest with `len` bytes from `buf`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036 virtual void Update(const void* buf, size_t len) = 0;
Artem Titov96e3b992021-07-26 16:03:14 +020037 // Outputs the digest value to `buf` with length `len`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 // 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
Harald Alvestrandcffaf0a2021-01-05 15:55:20 +000048// A check that an algorithm is in a list of approved digest algorithms
49// from RFC 4572 (FIPS 180).
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050bool IsFips180DigestAlgorithm(const std::string& alg);
51
52// Functions to create hashes.
53
Artem Titov96e3b992021-07-26 16:03:14 +020054// Computes the hash of `in_len` bytes of `input`, using the `digest` hash
55// implementation, and outputs the hash to the buffer `output`, which is
56// `out_len` bytes long. Returns the number of bytes written to `output` if
57// successful, or 0 if `out_len` was too small.
Yves Gerey665174f2018-06-19 15:03:05 +020058size_t ComputeDigest(MessageDigest* digest,
59 const void* input,
60 size_t in_len,
61 void* output,
62 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 16:03:14 +020064// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +020066size_t ComputeDigest(const std::string& alg,
67 const void* input,
68 size_t in_len,
69 void* output,
70 size_t out_len);
Artem Titov96e3b992021-07-26 16:03:14 +020071// Computes the hash of `input` using the `digest` hash implementation, and
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072// returns it as a hex-encoded string.
73std::string ComputeDigest(MessageDigest* digest, const std::string& input);
74// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 16:03:14 +020075// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076// there is no digest with the given name.
77std::string ComputeDigest(const std::string& alg, const std::string& input);
78// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +020079bool ComputeDigest(const std::string& alg,
80 const std::string& input,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 std::string* output);
82
83// Shorthand way to compute a hex-encoded hash using MD5.
84inline std::string MD5(const std::string& input) {
85 return ComputeDigest(DIGEST_MD5, input);
86}
87
88// Functions to compute RFC 2104 HMACs.
89
Artem Titov96e3b992021-07-26 16:03:14 +020090// Computes the HMAC of `in_len` bytes of `input`, using the `digest` hash
91// implementation and `key_len` bytes of `key` to key the HMAC, and outputs
92// the HMAC to the buffer `output`, which is `out_len` bytes long. Returns the
93// number of bytes written to `output` if successful, or 0 if `out_len` was too
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094// small.
Yves Gerey665174f2018-06-19 15:03:05 +020095size_t ComputeHmac(MessageDigest* digest,
96 const void* key,
97 size_t key_len,
98 const void* input,
99 size_t in_len,
100 void* output,
101 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 16:03:14 +0200103// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns 0 if there is no
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200105size_t ComputeHmac(const std::string& alg,
106 const void* key,
107 size_t key_len,
108 const void* input,
109 size_t in_len,
110 void* output,
111 size_t out_len);
Artem Titov96e3b992021-07-26 16:03:14 +0200112// Computes the HMAC of `input` using the `digest` hash implementation and `key`
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113// to key the HMAC, and returns it as a hex-encoded string.
Yves Gerey665174f2018-06-19 15:03:05 +0200114std::string ComputeHmac(MessageDigest* digest,
115 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 const std::string& input);
117// Like the previous function, but creates a digest implementation based on
Artem Titov96e3b992021-07-26 16:03:14 +0200118// the desired digest name `alg`, e.g. DIGEST_SHA_1. Returns empty string if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119// there is no digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200120std::string ComputeHmac(const std::string& alg,
121 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 const std::string& input);
123// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +0200124bool ComputeHmac(const std::string& alg,
125 const std::string& key,
126 const std::string& input,
127 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_MESSAGE_DIGEST_H_