File: cipher.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (260 lines) | stat: -rw-r--r-- 6,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

// Wrap the mbed TLS cipher API defined in <mbedtls/cipher.h> so
// that it can be used as part of the crypto layer of the OpenVPN core.

#ifndef OPENVPN_MBEDTLS_CRYPTO_CIPHER_H
#define OPENVPN_MBEDTLS_CRYPTO_CIPHER_H

#include <string>

#include <mbedtls/cipher.h>

#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/crypto/static_key.hpp>
#include <openvpn/crypto/cryptoalgs.hpp>

namespace openvpn::MbedTLSCrypto {
class CipherContextCommon
{
  public:
    OPENVPN_SIMPLE_EXCEPTION(mbedtls_cipher_mode_error);
    OPENVPN_SIMPLE_EXCEPTION(mbedtls_cipher_uninitialized);

    // mode parameter for constructor
    enum
    {
        MODE_UNDEF = MBEDTLS_OPERATION_NONE,
        ENCRYPT = MBEDTLS_ENCRYPT,
        DECRYPT = MBEDTLS_DECRYPT
    };

    /**
     * This crypto library requires the auth tag to be at the end for
     * encryption and decryption
     * @return
     */
    bool constexpr requires_authtag_at_end()
    {
        return true;
    }

  protected:
    CipherContextCommon() = default;

    virtual ~CipherContextCommon()
    {
        erase();
    }

    CipherContextCommon(const CipherContextCommon &other) = delete;
    CipherContextCommon &operator=(const CipherContextCommon &other) = delete;


    CipherContextCommon(CipherContextCommon &&other) noexcept
    {
        ctx = other.ctx;
        initialized = other.initialized;
        other.ctx = {};
        other.initialized = false;
    }

    CipherContextCommon &operator=(CipherContextCommon &&other)
    {
        ctx = other.ctx;
        initialized = other.initialized;
        other.ctx = {};
        other.initialized = false;
        return *this;
    }

    static void check_mode(int mode)
    {
        // check that mode is valid
        if (!(mode == ENCRYPT || mode == DECRYPT))
            throw mbedtls_cipher_mode_error();
    }

    void erase()
    {
        if (initialized)
        {
            mbedtls_cipher_free(&ctx);
            initialized = false;
        }
    }

    void check_initialized() const
    {
        if (unlikely(!initialized))
            throw mbedtls_cipher_uninitialized();
    }

    bool initialized = false;
    mbedtls_cipher_context_t ctx;
};

class CipherContext
    : public CipherContextCommon
{
    CipherContext(const CipherContext &) = delete;
    CipherContext &operator=(const CipherContext &) = delete;

  public:
    OPENVPN_EXCEPTION(mbedtls_cipher_error);

    // mode parameter for constructor
    enum
    {
        MODE_UNDEF = MBEDTLS_OPERATION_NONE,
        ENCRYPT = MBEDTLS_ENCRYPT,
        DECRYPT = MBEDTLS_DECRYPT
    };

    // mbed TLS cipher constants
    enum
    {
        MAX_IV_LENGTH = MBEDTLS_MAX_IV_LENGTH,
        CIPH_CBC_MODE = MBEDTLS_MODE_CBC
    };

    CipherContext() = default;

    ~CipherContext()
    {
        erase();
    }

    static bool is_supported(SSLLib::Ctx libctx, const CryptoAlgs::Type alg)
    {
        return (cipher_type(alg) != nullptr);
    }

    void init(SSLLib::Ctx libctx,
              const CryptoAlgs::Type alg,
              const unsigned char *key,
              const int mode)
    {
        erase();

        check_mode(mode);

        // get cipher type
        const mbedtls_cipher_info_t *ci = cipher_type(alg);
        if (!ci)
            OPENVPN_THROW(mbedtls_cipher_error, CryptoAlgs::name(alg) << ": not usable");

        // initialize cipher context with cipher type
        if (mbedtls_cipher_setup(&ctx, ci) < 0)
            throw mbedtls_cipher_error("mbedtls_cipher_setup");

        // set key and encrypt/decrypt mode
        if (mbedtls_cipher_setkey(&ctx, key, mbedtls_cipher_get_key_bitlen(&ctx), (mbedtls_operation_t)mode) < 0)
            throw mbedtls_cipher_error("mbedtls_cipher_setkey");

        initialized = true;
    }

    void reset(const unsigned char *iv)
    {
        check_initialized();
        if (mbedtls_cipher_reset(&ctx) < 0)
            throw mbedtls_cipher_error("mbedtls_cipher_reset");
        if (mbedtls_cipher_set_iv(&ctx, iv, iv_length()))
            throw mbedtls_cipher_error("mbedtls_cipher_set_iv");
    }

    bool update(unsigned char *out,
                const size_t max_out_size,
                const unsigned char *in,
                const size_t in_size,
                size_t &out_acc)
    {
        check_initialized();
        size_t outlen;
        if (mbedtls_cipher_update(&ctx, in, in_size, out, &outlen) >= 0)
        {
            out_acc += outlen;
            return true;
        }
        else
            return false;
    }

    bool final(unsigned char *out, const size_t max_out_size, size_t &out_acc)
    {
        check_initialized();
        size_t outlen;
        if (mbedtls_cipher_finish(&ctx, out, &outlen) >= 0)
        {
            out_acc += outlen;
            return true;
        }
        else
            return false;
    }

    bool is_initialized() const
    {
        return initialized;
    }

    size_t iv_length() const
    {
        check_initialized();
        return mbedtls_cipher_get_iv_size(&ctx);
    }

    size_t block_size() const
    {
        check_initialized();
        return mbedtls_cipher_get_block_size(&ctx);
    }

    // return cipher mode (such as CIPH_CBC_MODE, etc.)
    int cipher_mode() const
    {
        check_initialized();
        return mbedtls_cipher_get_cipher_mode(&ctx);
    }

  private:
    static const mbedtls_cipher_info_t *cipher_type(const CryptoAlgs::Type alg)
    {
        switch (alg)
        {
        case CryptoAlgs::AES_128_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
        case CryptoAlgs::AES_192_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_192_CBC);
        case CryptoAlgs::AES_256_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC);
        case CryptoAlgs::AES_256_CTR:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CTR);
        case CryptoAlgs::DES_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_CBC);
        case CryptoAlgs::DES_EDE3_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_CBC);
#if MBEDTLS_VERSION_NUMBER < 0x03000000
            /* no longer supported in newer mbed TLS versions */
        case CryptoAlgs::BF_CBC:
            return mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_BLOWFISH_CBC);
#endif
        default:
            return nullptr;
        }
    }
};
} // namespace openvpn::MbedTLSCrypto

#endif