chenqiyang
2022-09-02 6a99d9bf65aa5878cb409945ed2bdbdcb916d047
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
/*
 * Copyright (c) 2020 Belledonne Communications SARL.
 *
 * This file is part of bctoolbox.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#ifndef BCTBX_CRYPTO_HH
#define BCTBX_CRYPTO_HH
 
#include <vector>
#include <memory>
 
namespace bctoolbox {
/**
 * @brief Random number generator interface
 *
 * This wrapper provides an interface to a RNG.
 * Two ways to get some random numbers:
 *  - calling the static class functions(cRandomize) : do not use this to feed cryptographic functions
 *  - instanciate a RNG object and call the randomize method : use this one for cryptographic quality random
 *
 * Any call (including creation), may throw an exception if some error are detected on the random source
 */
class RNG {
    public:
        /**
         * fill a buffer with random numbers
         * @param[in,out]    buffer     The buffer to be filled with random (callers responsability to allocate memory)
         * @param[in]        size    size in bytes of the random generated, buffer must be at least of this size
         **/
        void randomize(uint8_t *buffer, const size_t size);
 
        /**
         * return a random vector of given size
         * @param[in]        size    size in bytes of the random generated
         **/
        std::vector<uint8_t> randomize(const size_t size);
 
 
        /**
         * generates a 32 bits random unsigned number
         **/
        uint32_t randomize();
 
        /**
         * fill a buffer with random numbers
         * @param[in,out]    buffer     The buffer to be filled with random (callers responsability to allocate memory)
         * @param[in]        size    size in bytes of the random generated, buffer must be at least of this size
         *
         * @note This function uses a shared RNG context, do not use it to generate sensitive material
         **/
        static void cRandomize(uint8_t *buffer, size_t size);
        /**
         * generates a 32 bits random unsigned number
         *
         * @note This function uses a shared RNG context, do not use it to generate sensitive material
         **/
        static uint32_t cRandomize();
 
        RNG();
        ~RNG();
    private:
        struct Impl;
        std::unique_ptr<Impl> pImpl;
        static std::unique_ptr<Impl> pImplClass;
}; //class RNG
 
 
/*****************************************************************************/
/***                      Hash related function                            ***/
/*****************************************************************************/
/**
 * @brief SHA256 buffer size definition
 */
struct SHA256 {
    /// maximum output size for SHA256 is 32 bytes
    static constexpr size_t ssize() {return 32;}
};
 
/**
 * @brief SHA384 buffer size definition
 */
struct SHA384 {
    /// maximum output size for SHA384 is 48 bytes
    static constexpr size_t ssize() {return 48;}
};
 
/**
 * @brief SHA512 buffer size definition
 */
struct SHA512 {
    /// maximum output size for SHA512 is 64 bytes
    static constexpr size_t ssize() {return 64;}
};
 
 
/**
 * @brief templated HMAC
 *
 * @tparam    hashAlgo    the hash algorithm used: SHA256, SHA384, SHA512
 *
 * @param[in]    key        HMAC key
 * @param[in]    input        HMAC input
 *
 * @return an array of size matching the selected hash algorithm output size
 *
 */
template <typename hashAlgo>
std::vector<uint8_t> HMAC(const std::vector<uint8_t> &key, const std::vector<uint8_t> &input);
/* declare template specialisations */
template <> std::vector<uint8_t>  HMAC<SHA256>(const std::vector<uint8_t> &key, const std::vector<uint8_t> &input);
template <> std::vector<uint8_t>  HMAC<SHA384>(const std::vector<uint8_t> &key, const std::vector<uint8_t> &input);
template <> std::vector<uint8_t>  HMAC<SHA512>(const std::vector<uint8_t> &key, const std::vector<uint8_t> &input);
 
/**
 * @brief HKDF as described in RFC5869
 *    @par Compute:
 *    @code{.unparsed}
 *        PRK = HMAC-Hash(salt, IKM)
 *
 *        N = ceil(L/HashLen)
 *        T = T(1) | T(2) | T(3) | ... | T(N)
 *        OKM = first L octets of T
 *
 *        where:
 *        T(0) = empty string (zero length)
 *        T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
 *        T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
 *        T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
 *        ...
 *    @endcode
 *
 * @tparam    hashAlgo    the hash algorithm to use
 * @tparam    infoType    the info parameter type : can be passed as a string or a std::vector<uint8_t>
 *
 * @param[in]    salt         salt
 * @param[in]    ikm        input key material
 * @param[in]    info        a info string or buffer
 * @param[in]    okmSize        requested amount of data. (L in the RFC doc)
 *
 * @return the output key material
 *
 */
template <typename hashAlgo>
std::vector<uint8_t> HKDF(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::vector<uint8_t> &info, size_t okmSize);
template <typename hashAlgo>
std::vector<uint8_t> HKDF(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::string &info, size_t okmSize);
/* declare template specialisations */
template <> std::vector<uint8_t> HKDF<SHA256>(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::vector<uint8_t> &info, size_t outputSize);
template <> std::vector<uint8_t> HKDF<SHA256>(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::string &info, size_t outputSize);
template <> std::vector<uint8_t> HKDF<SHA512>(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::vector<uint8_t> &info, size_t outputSize);
template <> std::vector<uint8_t> HKDF<SHA512>(const std::vector<uint8_t> &salt, const std::vector<uint8_t> &ikm, const std::string &info, size_t outputSize);
 
 
/************************ AEAD interface *************************************/
// AEAD function defines
/**
 * @brief AES256GCM buffers size definition
 */
struct AES256GCM128 {
    /// key size is 32 bytes
    static constexpr size_t keySize(void) {return 32;};
    /// tag size is 16 bytes
    static constexpr size_t tagSize(void) {return 16;};
};
 
/**
 * @brief Encrypt and tag using scheme given as template parameter
 *
 * @param[in]    key        Encryption key
 * @param[in]    IV        Initialisation vector
 * @param[in]    plain        Plain text
 * @param[in]    AD        Additional data used in tag computation
 * @param[out]    tag        Generated authentication tag
 * @return    the cipher text
 */
template <typename AEADAlgo>
std::vector<uint8_t> AEADEncrypt(const std::vector<uint8_t> &key, const std::vector<uint8_t> IV, const std::vector<uint8_t> &plain, const std::vector<uint8_t> &AD,
        std::vector<uint8_t> &tag);
 
/**
 * @brief Authenticate and Decrypt using scheme given as template parameter
 *
 * @param[in]    key        Encryption key
 * @param[in]    IV        Initialisation vector
 * @param[in]    cipher        Cipher text
 * @param[in]    AD        Additional data used in tag computation
 * @param[in]    tag        Authentication tag
 * @param[out]    plain        A vector to store the plain text
 *
 * @return true if authentication tag match and decryption was successful
 */
template <typename AEADAlgo>
bool AEADDecrypt(const std::vector<uint8_t> &key, const std::vector<uint8_t> &IV, const std::vector<uint8_t> &cipher, const std::vector<uint8_t> &AD,
        const std::vector<uint8_t> &tag, std::vector<uint8_t> &plain);
 
/* declare AEAD template specialisations : AES256-GCM with 128 bits auth tag*/
template <> std::vector<uint8_t> AEADEncrypt<AES256GCM128>(const std::vector<uint8_t> &key, const std::vector<uint8_t> IV, const std::vector<uint8_t> &plain, const std::vector<uint8_t> &AD,
        std::vector<uint8_t> &tag);
 
template <> bool AEADDecrypt<AES256GCM128>(const std::vector<uint8_t> &key, const std::vector<uint8_t> &IV, const std::vector<uint8_t> &cipher, const std::vector<uint8_t> &AD,
        const std::vector<uint8_t> &tag, std::vector<uint8_t> &plain);
 
} // namespace bctoolbox
#endif // BCTBX_CRYPTO_HH