OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hmac.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 HMAC API defined in <mbedtls/md.h> so
13// that it can be used as part of the crypto layer of the OpenVPN core.
14
15#ifndef OPENVPN_MBEDTLS_CRYPTO_HMAC_H
16#define OPENVPN_MBEDTLS_CRYPTO_HMAC_H
17
18#include <string>
19
24
25namespace openvpn::MbedTLSCrypto {
27{
28 HMACContext(const HMACContext &) = delete;
29 HMACContext &operator=(const HMACContext &) = delete;
30
31 public:
32 OPENVPN_SIMPLE_EXCEPTION(mbedtls_hmac_uninitialized);
33 OPENVPN_EXCEPTION(mbedtls_hmac_error);
34
35 enum
36 {
37 MAX_HMAC_SIZE = MBEDTLS_MD_MAX_SIZE
38 };
39
41 : initialized(false)
42 {
43 }
44
45 HMACContext(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
46 : initialized(false)
47 {
48 init(digest, key, key_size);
49 }
50
52 {
53 erase();
54 }
55
56 void init(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
57 {
58 erase();
59
60 mbedtls_md_init(&ctx);
61 if (mbedtls_md_setup(&ctx, DigestContext::digest_type(digest), 1) < 0)
62 throw mbedtls_hmac_error("mbedtls_md_setup");
63 if (mbedtls_md_hmac_starts(&ctx, key, key_size) < 0)
64 throw mbedtls_hmac_error("mbedtls_md_hmac_starts");
65 initialized = true;
66 }
67
68 void reset()
69 {
71 if (mbedtls_md_hmac_reset(&ctx) < 0)
72 throw mbedtls_hmac_error("mbedtls_md_hmac_reset");
73 }
74
75 void update(const unsigned char *in, const size_t size)
76 {
78 if (mbedtls_md_hmac_update(&ctx, in, size) < 0)
79 throw mbedtls_hmac_error("mbedtls_md_hmac_update");
80 }
81
82 size_t final(unsigned char *out)
83 {
85 if (mbedtls_md_hmac_finish(&ctx, out) < 0)
86 throw mbedtls_hmac_error("mbedtls_md_hmac_finish");
87 return size_();
88 }
89
90 size_t size() const
91 {
93 return size_();
94 }
95
96 bool is_initialized() const
97 {
98 return initialized;
99 }
100
101 private:
102 void erase()
103 {
104 if (initialized)
105 {
106 mbedtls_md_free(&ctx);
107 initialized = false;
108 }
109 }
110
111 size_t size_() const
112 {
113 return mbedtls_md_get_size(mbedtls_md_info_from_ctx(&ctx));
114 }
115
116 void check_initialized() const
117 {
118#ifdef OPENVPN_ENABLE_ASSERT
119 if (!initialized)
120 throw mbedtls_hmac_uninitialized();
121#endif
122 }
123
125 mbedtls_md_context_t ctx;
126};
127} // namespace openvpn::MbedTLSCrypto
128
129#endif
static const mbedtls_md_info_t * digest_type(const CryptoAlgs::Type alg)
Definition digest.hpp:109
HMACContext(const HMACContext &)=delete
OPENVPN_EXCEPTION(mbedtls_hmac_error)
void update(const unsigned char *in, const size_t size)
Definition hmac.hpp:75
mbedtls_md_context_t ctx
Definition hmac.hpp:125
HMACContext(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
Definition hmac.hpp:45
OPENVPN_SIMPLE_EXCEPTION(mbedtls_hmac_uninitialized)
HMACContext & operator=(const HMACContext &)=delete
void init(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
Definition hmac.hpp:56
static const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
static std::stringstream out
Definition test_path.cpp:10