OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hmac-compat.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 OpenSSL HMAC API defined in <openssl/hmac.h> so
13// that it can be used as part of the crypto layer of the OpenVPN core.
14
15#ifndef OPENVPN_OPENSSL_CRYPTO_HMAC_H
16#define OPENVPN_OPENSSL_CRYPTO_HMAC_H
17
18#include <string>
19
23
25
26namespace openvpn::OpenSSLCrypto {
28{
29 public:
30 HMACContext(const HMACContext &) = delete;
31 HMACContext &operator=(const HMACContext &) = delete;
32
34 {
35 erase();
36 ctx = rhs.ctx;
37 rhs.ctx = nullptr;
38 return *this;
39 }
40
41 OPENVPN_SIMPLE_EXCEPTION(openssl_hmac_uninitialized);
42 OPENVPN_EXCEPTION(openssl_hmac_error);
43
44 enum
45 {
46 MAX_HMAC_SIZE = EVP_MAX_MD_SIZE
47 };
48
49 HMACContext() = default;
50
51 HMACContext(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
52 {
53 init(digest, key, key_size);
54 }
55
57 {
58 erase();
59 }
60
61 void init(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
62 {
63 erase();
64 ctx = HMAC_CTX_new();
65 if (!HMAC_Init_ex(ctx, key, int(key_size), DigestContext::digest_type(digest, nullptr), nullptr))
66 {
68 HMAC_CTX_free(ctx);
69 ctx = nullptr;
70 throw openssl_hmac_error("HMAC_Init_ex (init)");
71 }
72 }
73
74 void reset()
75 {
77 if (!HMAC_Init_ex(ctx, nullptr, 0, nullptr, nullptr))
78 {
80 throw openssl_hmac_error("HMAC_Init_ex (reset)");
81 }
82 }
83
84 void update(const unsigned char *in, const size_t size)
85 {
87
88 if (!HMAC_Update(ctx, in, int(size)))
89 {
91 throw openssl_hmac_error("HMAC_Update");
92 }
93 }
94
95 size_t final(unsigned char *out)
96 {
98 unsigned int outlen;
99 if (!HMAC_Final(ctx, out, &outlen))
100 {
102 throw openssl_hmac_error("HMAC_Final");
103 }
104 return outlen;
105 }
106
107 size_t size() const
108 {
110 return size_();
111 }
112
113 bool is_initialized() const
114 {
115 return ctx != nullptr;
116 }
117
118 private:
119 void erase()
120 {
121 HMAC_CTX_free(ctx);
122 ctx = nullptr;
123 }
124
125 size_t size_() const
126 {
127 return HMAC_size(ctx);
128 }
129
130 void check_initialized() const
131 {
132#ifdef OPENVPN_ENABLE_ASSERT
133 if (!ctx)
134 throw openssl_hmac_uninitialized();
135#endif
136 }
137
138 HMAC_CTX *ctx = nullptr;
139};
140} // namespace openvpn::OpenSSLCrypto
141
142#endif
static evp_md_type * digest_type(const CryptoAlgs::Type alg, SSLLib::Ctx libctx)
Definition digest.hpp:102
HMACContext(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
HMACContext(const HMACContext &)=delete
OPENVPN_EXCEPTION(openssl_hmac_error)
void update(const unsigned char *in, const size_t size)
void init(const CryptoAlgs::Type digest, const unsigned char *key, const size_t key_size)
OPENVPN_SIMPLE_EXCEPTION(openssl_hmac_uninitialized)
HMACContext & operator=(HMACContext &&rhs)
HMACContext & operator=(const HMACContext &)=delete
void openssl_clear_error_stack()
Definition error.hpp:247
static std::stringstream out
Definition test_path.cpp:10