Skip to content

Commit 312f8c3

Browse files
committed
chore: move aead to separate file
1 parent 50b68a6 commit 312f8c3

6 files changed

Lines changed: 448 additions & 293 deletions

File tree

include/aead.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#pragma once
2+
3+
#include "ncrypto.h"
4+
5+
#ifdef OPENSSL_IS_BORINGSSL
6+
7+
namespace ncrypto {
8+
9+
class AeadCtxPointer;
10+
11+
class Aead final {
12+
private:
13+
// BoringSSL does not keep a list of AEADs, so we need to maintain our own.
14+
struct AeadInfo {
15+
std::string name;
16+
int mode;
17+
int nid = 0; // Note: BoringSSL only defines NIDs for some AEADs
18+
};
19+
20+
public:
21+
Aead() = default;
22+
Aead(const AeadInfo* info, const EVP_AEAD* aead) : info_(info), aead_(aead) {}
23+
Aead(const Aead&) = default;
24+
Aead& operator=(const Aead&) = default;
25+
NCRYPTO_DISALLOW_MOVE(Aead)
26+
27+
inline const EVP_AEAD* get() const { return aead_; }
28+
std::string_view getModeLabel() const;
29+
inline operator const EVP_AEAD*() const { return aead_; }
30+
inline operator bool() const { return aead_ != nullptr; }
31+
32+
int getMode() const;
33+
int getNonceLength() const;
34+
int getKeyLength() const;
35+
int getBlockSize() const;
36+
int getMaxOverhead() const;
37+
int getMaxTagLength() const;
38+
std::string_view getName() const;
39+
40+
static const Aead FromName(std::string_view name);
41+
42+
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
43+
// This method is included only for implementing getCipherInfo and can't be
44+
// used to construct an Aead instance.
45+
int getNid() const;
46+
// static const AEAD FromNid(int nid);
47+
48+
static const Aead FromCtx(std::string_view name, const AeadCtxPointer& ctx);
49+
50+
using AeadNameCallback = std::function<void(std::string_view name)>;
51+
52+
// Iterates the known ciphers if the underlying implementation
53+
// is able to do so.
54+
static void ForEach(AeadNameCallback callback);
55+
56+
// Utilities to get various AEADs by type.
57+
58+
static const Aead EMPTY;
59+
static const Aead AES_128_GCM;
60+
static const Aead AES_192_GCM;
61+
static const Aead AES_256_GCM;
62+
static const Aead CHACHA20_POLY1305;
63+
static const Aead XCHACHA20_POLY1305;
64+
static const Aead AES_128_CTR_HMAC_SHA256;
65+
static const Aead AES_256_CTR_HMAC_SHA256;
66+
static const Aead AES_128_GCM_SIV;
67+
static const Aead AES_256_GCM_SIV;
68+
static const Aead AES_128_GCM_RANDNONCE;
69+
static const Aead AES_256_GCM_RANDNONCE;
70+
static const Aead AES_128_CCM_BLUETOOTH;
71+
static const Aead AES_128_CCM_BLUETOOTH_8;
72+
static const Aead AES_128_CCM_MATTER;
73+
static const Aead AES_128_EAX;
74+
static const Aead AES_256_EAX;
75+
76+
private:
77+
const EVP_AEAD* aead_ = nullptr;
78+
const AeadInfo* info_ = nullptr;
79+
80+
using AeadConstructor = const EVP_AEAD* (*)();
81+
static const std::unordered_map<AeadConstructor, AeadInfo> aeadIndex;
82+
static const Aead FromConstructor(AeadConstructor construct);
83+
};
84+
85+
class AeadCtxPointer final {
86+
public:
87+
static AeadCtxPointer New(
88+
const Aead& aead,
89+
bool encrypt,
90+
const unsigned char* key = nullptr,
91+
size_t keyLen = 0,
92+
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
93+
94+
AeadCtxPointer() = default;
95+
explicit AeadCtxPointer(EVP_AEAD_CTX* ctx);
96+
AeadCtxPointer(AeadCtxPointer&& other) noexcept;
97+
AeadCtxPointer& operator=(AeadCtxPointer&& other) noexcept;
98+
NCRYPTO_DISALLOW_COPY(AeadCtxPointer)
99+
~AeadCtxPointer();
100+
101+
inline bool operator==(std::nullptr_t) const noexcept {
102+
return ctx_ == nullptr;
103+
}
104+
inline operator bool() const { return ctx_ != nullptr; }
105+
inline EVP_AEAD_CTX* get() const { return ctx_.get(); }
106+
inline operator EVP_AEAD_CTX*() const { return ctx_.get(); }
107+
void reset(EVP_AEAD_CTX* ctx = nullptr);
108+
EVP_AEAD_CTX* release();
109+
110+
bool init(const Aead& aead,
111+
bool encrypt,
112+
const unsigned char* key = nullptr,
113+
size_t keyLen = 0,
114+
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
115+
116+
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
117+
// Decide if we will even implement this method.
118+
// int getNid() const;
119+
120+
bool encrypt(const Buffer<const unsigned char>& in,
121+
Buffer<unsigned char>& out,
122+
Buffer<unsigned char>& tag,
123+
const Buffer<const unsigned char>& nonce,
124+
const Buffer<const unsigned char>& aad);
125+
126+
bool decrypt(const Buffer<const unsigned char>& in,
127+
Buffer<unsigned char>& out,
128+
const Buffer<const unsigned char>& tag,
129+
const Buffer<const unsigned char>& nonce,
130+
const Buffer<const unsigned char>& aad);
131+
132+
private:
133+
DeleteFnPtr<EVP_AEAD_CTX, EVP_AEAD_CTX_free> ctx_;
134+
};
135+
} // namespace ncrypto
136+
137+
#endif // OPENSSL_IS_BORINGSSL

include/ncrypto.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ namespace ncrypto {
8181
// ============================================================================
8282
// Utility macros
8383

84+
inline bool EqualNoCase(const std::string_view a, const std::string_view b) {
85+
if (a.size() != b.size()) return false;
86+
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) {
87+
return std::tolower(a) == std::tolower(b);
88+
});
89+
}
90+
8491
#if NCRYPTO_DEVELOPMENT_CHECKS
8592
#define NCRYPTO_STR(x) #x
8693
#define NCRYPTO_REQUIRE(EXPR) \

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(ncrypto ncrypto.cpp engine.cpp)
1+
add_library(ncrypto ncrypto.cpp engine.cpp aead.cpp)
22
target_link_libraries(ncrypto PUBLIC ssl crypto)
33

44
if (NCRYPTO_BSSL_LIBDECREPIT_MISSING)

0 commit comments

Comments
 (0)