File: digest.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 (273 lines) | stat: -rw-r--r-- 8,360 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
261
262
263
264
265
266
267
268
269
270
271
272
273
//    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 Apple digest API defined in <CommonCrypto/CommonDigest.h>
// so that it can be used as part of the crypto layer of the OpenVPN core.

#ifndef OPENVPN_APPLECRYPTO_CRYPTO_DIGEST_H
#define OPENVPN_APPLECRYPTO_CRYPTO_DIGEST_H

#include <string>

#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>

#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/crypto/cryptoalgs.hpp>
#include <openvpn/apple/cf/error.hpp>

/// \cond KNOWN_WARNINGS
/// error: no matching file member found for openvpn::AppleCrypto::DigestCTX::@9::OPENVPN_DIGEST_CONTEXT(MD4)
#define OPENVPN_DIGEST_CONTEXT(TYPE) CC_##TYPE##_CTX TYPE##_ctx

#define OPENVPN_DIGEST_ALG_CLASS(TYPE)                                                    \
    class DigestAlgorithm##TYPE : public DigestAlgorithm                                  \
    {                                                                                     \
      public:                                                                             \
        DigestAlgorithm##TYPE()                                                           \
        {                                                                                 \
        }                                                                                 \
        int init(DigestCTX &ctx) const override                                           \
        {                                                                                 \
            return CC_##TYPE##_Init(&ctx.u.TYPE##_ctx);                                   \
        }                                                                                 \
        int update(DigestCTX &ctx, const unsigned char *data, size_t size) const override \
        {                                                                                 \
            return CC_##TYPE##_Update(&ctx.u.TYPE##_ctx, data, size);                     \
        }                                                                                 \
        int final(DigestCTX &ctx, unsigned char *md) const override                       \
        {                                                                                 \
            return CC_##TYPE##_Final(md, &ctx.u.TYPE##_ctx);                              \
        }                                                                                 \
    }

#define OPENVPN_DIGEST_ALG_DECLARE(TYPE) const DigestAlgorithm##TYPE alg_##TYPE;

#define OPENVPN_DIGEST_INFO_DECLARE(TYPE) const DigestInfo info_##TYPE(CryptoAlgs::TYPE, &alg_##TYPE, kCCHmacAlg##TYPE)

#define OPENVPN_DIGEST_INFO_DECLARE_NO_HMAC(TYPE) const DigestInfo info_##TYPE(CryptoAlgs::TYPE, &alg_##TYPE, DigestInfo::NO_HMAC_ALG)

namespace openvpn::AppleCrypto {
typedef CC_SHA256_CTX CC_SHA224_CTX;
typedef CC_SHA512_CTX CC_SHA384_CTX;

struct DigestCTX
{
    union {
        OPENVPN_DIGEST_CONTEXT(MD4);
        OPENVPN_DIGEST_CONTEXT(MD5);
        OPENVPN_DIGEST_CONTEXT(SHA1);
        OPENVPN_DIGEST_CONTEXT(SHA224);
        OPENVPN_DIGEST_CONTEXT(SHA256);
        OPENVPN_DIGEST_CONTEXT(SHA384);
        OPENVPN_DIGEST_CONTEXT(SHA512);
    } u;
};

struct DigestAlgorithm
{
    virtual int init(DigestCTX &ctx) const = 0;
    virtual int update(DigestCTX &ctx, const unsigned char *data, size_t size) const = 0;
    virtual int final(DigestCTX &ctx, unsigned char *md) const = 0;
};

// individual digest algorithm classes (each inherits from DigestAlgorithm)
OPENVPN_DIGEST_ALG_CLASS(MD4);
OPENVPN_DIGEST_ALG_CLASS(MD5);
OPENVPN_DIGEST_ALG_CLASS(SHA1);
OPENVPN_DIGEST_ALG_CLASS(SHA224);
OPENVPN_DIGEST_ALG_CLASS(SHA256);
OPENVPN_DIGEST_ALG_CLASS(SHA384);
OPENVPN_DIGEST_ALG_CLASS(SHA512);

/// \endcond

class DigestInfo
{
  public:
    enum
    {
        NO_HMAC_ALG = -1
    };

    DigestInfo(CryptoAlgs::Type type,
               const DigestAlgorithm *digest_alg,
               const CCHmacAlgorithm hmac_alg)
        : type_(type),
          digest_alg_(digest_alg),
          hmac_alg_(hmac_alg)
    {
    }

    CryptoAlgs::Type type() const
    {
        return type_;
    }
    const char *name() const
    {
        return CryptoAlgs::name(type_);
    }
    size_t size() const
    {
        return CryptoAlgs::size(type_);
    }
    const DigestAlgorithm *digest_alg() const
    {
        return digest_alg_;
    }
    CCHmacAlgorithm hmac_alg() const
    {
        return hmac_alg_;
    }

  private:
    CryptoAlgs::Type type_;
    const DigestAlgorithm *digest_alg_;
    CCHmacAlgorithm hmac_alg_;
};

// instantiate individual digest algorithm class instances (each inherits from DigestAlgorithm),
// naming convention is alg_TYPE
OPENVPN_DIGEST_ALG_DECLARE(MD4);
OPENVPN_DIGEST_ALG_DECLARE(MD5);
OPENVPN_DIGEST_ALG_DECLARE(SHA1);
OPENVPN_DIGEST_ALG_DECLARE(SHA224);
OPENVPN_DIGEST_ALG_DECLARE(SHA256);
OPENVPN_DIGEST_ALG_DECLARE(SHA384);
OPENVPN_DIGEST_ALG_DECLARE(SHA512);

// instantiate individual digest info class instances (each is a DigestInfo),
// naming convention is info_TYPE
OPENVPN_DIGEST_INFO_DECLARE_NO_HMAC(MD4);
OPENVPN_DIGEST_INFO_DECLARE(MD5);
OPENVPN_DIGEST_INFO_DECLARE(SHA1);
OPENVPN_DIGEST_INFO_DECLARE(SHA224);
OPENVPN_DIGEST_INFO_DECLARE(SHA256);
OPENVPN_DIGEST_INFO_DECLARE(SHA384);
OPENVPN_DIGEST_INFO_DECLARE(SHA512);

class HMACContext;

class DigestContext
{
    DigestContext(const DigestContext &) = delete;
    DigestContext &operator=(const DigestContext &) = delete;

  public:
    friend class HMACContext;

    OPENVPN_SIMPLE_EXCEPTION(apple_digest_uninitialized);
    OPENVPN_SIMPLE_EXCEPTION(apple_digest_final_overflow);
    OPENVPN_EXCEPTION(apple_digest_error);

    enum
    {
        MAX_DIGEST_SIZE = CC_SHA512_DIGEST_LENGTH // largest known is SHA512
    };

    DigestContext()
    {
        clear();
    }

    DigestContext(const CryptoAlgs::Type alg)
    {
        init(alg);
    }

    void init(const CryptoAlgs::Type alg)
    {
        clear();
        info = digest_type(alg);
        meth = info->digest_alg();
        if (meth->init(ctx) != 1)
            throw apple_digest_error("init");
        initialized = true;
    }

    void update(const unsigned char *in, const size_t size)
    {
        check_initialized();
        if (meth->update(ctx, in, size) != 1)
            throw apple_digest_error("update");
    }

    size_t final(unsigned char *out)
    {
        check_initialized();
        if (meth->final(ctx, out) != 1)
            throw apple_digest_error("final");
        return info->size();
    }

    size_t size() const
    {
        check_initialized();
        return info->size();
    }

    bool is_initialized() const
    {
        return initialized;
    }

  private:
    static const DigestInfo *digest_type(const CryptoAlgs::Type alg)
    {
        switch (alg)
        {
        case CryptoAlgs::MD4:
            return &info_MD4;
        case CryptoAlgs::MD5:
            return &info_MD5;
        case CryptoAlgs::SHA1:
            return &info_SHA1;
        case CryptoAlgs::SHA224:
            return &info_SHA224;
        case CryptoAlgs::SHA256:
            return &info_SHA256;
        case CryptoAlgs::SHA384:
            return &info_SHA384;
        case CryptoAlgs::SHA512:
            return &info_SHA512;
        default:
            OPENVPN_THROW(apple_digest_error, CryptoAlgs::name(alg) << ": not usable");
        }
    }

    void clear()
    {
        initialized = false;
    }

    void check_initialized() const
    {
#ifdef OPENVPN_ENABLE_ASSERT
        if (!initialized)
            throw apple_digest_uninitialized();
#endif
    }

    bool initialized;
    const DigestInfo *info;
    const DigestAlgorithm *meth;
    DigestCTX ctx;
};
} // namespace openvpn::AppleCrypto

#undef OPENVPN_DIGEST_CONTEXT
#undef OPENVPN_DIGEST_ALG_CLASS
#undef OPENVPN_DIGEST_ALG_DECLARE
#undef OPENVPN_DIGEST_INFO_DECLARE

#endif