OpenVPN 3 Core Library
Loading...
Searching...
No Matches
digest.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Wrap the mbed TLS digest API defined in <mbedtls/md.h>
13// so that it can be used as part of the crypto layer of the OpenVPN core.
14
15#ifndef OPENVPN_MBEDTLS_CRYPTO_DIGEST_H
16#define OPENVPN_MBEDTLS_CRYPTO_DIGEST_H
17
18#include <string>
19
20#include <mbedtls/md.h>
21
26
27namespace openvpn::MbedTLSCrypto {
28class HMACContext;
29
31{
32 DigestContext(const DigestContext &) = delete;
34
35 public:
36 friend class HMACContext;
37
38 OPENVPN_SIMPLE_EXCEPTION(mbedtls_digest_uninitialized);
39 OPENVPN_SIMPLE_EXCEPTION(mbedtls_digest_final_overflow);
40 OPENVPN_EXCEPTION(mbedtls_digest_error);
41
42 enum
43 {
44 MAX_DIGEST_SIZE = MBEDTLS_MD_MAX_SIZE
45 };
46
48 : initialized(false)
49 {
50 }
51
53 : initialized(false)
54 {
55 init(alg);
56 }
57
58 // SSLLib::Ctx is unused in mbedtls
60 : initialized(false)
61 {
62 init(alg);
63 }
64
66 {
67 erase();
68 }
69
70 void init(const CryptoAlgs::Type alg)
71 {
72 erase();
73
74 mbedtls_md_init(&ctx);
75 if (mbedtls_md_setup(&ctx, digest_type(alg), 1) < 0)
76 throw mbedtls_digest_error("mbedtls_md_setup");
77 if (mbedtls_md_starts(&ctx) < 0)
78 throw mbedtls_digest_error("mbedtls_md_starts");
79 initialized = true;
80 }
81
82 void update(const unsigned char *in, const size_t size)
83 {
85 if (mbedtls_md_update(&ctx, in, size) < 0)
86 throw mbedtls_digest_error("mbedtls_md_update");
87 }
88
89 size_t final(unsigned char *out)
90 {
92 if (mbedtls_md_finish(&ctx, out) < 0)
93 throw mbedtls_digest_error("mbedtls_md_finish");
94 return size_();
95 }
96
97 size_t size() const
98 {
100 return size_();
101 }
102
103 bool is_initialized() const
104 {
105 return initialized;
106 }
107
108 private:
109 static const mbedtls_md_info_t *digest_type(const CryptoAlgs::Type alg)
110 {
111 switch (alg)
112 {
113#if MBEDTLS_VERSION_NUMBER < 0x03000000
114 case CryptoAlgs::MD4:
115 return mbedtls_md_info_from_type(MBEDTLS_MD_MD4);
116#endif
117 case CryptoAlgs::MD5:
118 return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
119 case CryptoAlgs::SHA1:
120 return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
122 return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
124 return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
126 return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
128 return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
129 default:
130 OPENVPN_THROW(mbedtls_digest_error, CryptoAlgs::name(alg) << ": not usable");
131 }
132 }
133
134 void erase()
135 {
136 if (initialized)
137 {
138 mbedtls_md_free(&ctx);
139 initialized = false;
140 }
141 }
142
143 size_t size_() const
144 {
145 return mbedtls_md_get_size(mbedtls_md_info_from_ctx(&ctx));
146 }
147
148 void check_initialized() const
149 {
150#ifdef OPENVPN_ENABLE_ASSERT
151 if (!initialized)
152 throw mbedtls_digest_uninitialized();
153#endif
154 }
155
157 mbedtls_md_context_t ctx;
158};
159} // namespace openvpn::MbedTLSCrypto
160
161#endif
OPENVPN_SIMPLE_EXCEPTION(mbedtls_digest_final_overflow)
DigestContext & operator=(const DigestContext &)=delete
OPENVPN_SIMPLE_EXCEPTION(mbedtls_digest_uninitialized)
static const mbedtls_md_info_t * digest_type(const CryptoAlgs::Type alg)
Definition digest.hpp:109
DigestContext(const DigestContext &)=delete
void update(const unsigned char *in, const size_t size)
Definition digest.hpp:82
DigestContext(const CryptoAlgs::Type alg, SSLLib::Ctx)
Definition digest.hpp:59
DigestContext(const CryptoAlgs::Type alg)
Definition digest.hpp:52
OPENVPN_EXCEPTION(mbedtls_digest_error)
void init(const CryptoAlgs::Type alg)
Definition digest.hpp:70
#define OPENVPN_THROW(exc, stuff)
static const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
const char * name(const KeyDerivation kd)
static std::stringstream out
Definition test_path.cpp:10