Skip to content

Commit 737833c

Browse files
authored
chore: move aead to separate file (#20)
1 parent afc7e12 commit 737833c

6 files changed

Lines changed: 448 additions & 424 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 & 131 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) \
@@ -1753,137 +1760,6 @@ class KEM final {
17531760

17541761
#endif // OPENSSL_VERSION_MAJOR >= 3
17551762

1756-
// ============================================================================
1757-
// AEAD (Authenticated Encryption with Associated Data)
1758-
// Note that the underlying EVP_AEAD interface is specific to BoringSSL. AEAD
1759-
// primitives are accessed through the Cipher class instead, if using OpenSSL.
1760-
1761-
#ifdef OPENSSL_IS_BORINGSSL
1762-
class Aead final : public ModeMixin<Aead> {
1763-
private:
1764-
// BoringSSL does not keep a list of AEADs, so we need to maintain our own.
1765-
struct AeadInfo {
1766-
std::string name;
1767-
int mode;
1768-
int nid = 0; // Note: BoringSSL only defines NIDs for some AEADs
1769-
};
1770-
1771-
public:
1772-
Aead() = default;
1773-
Aead(const AeadInfo* info, const EVP_AEAD* aead) : info_(info), aead_(aead) {}
1774-
Aead(const Aead&) = default;
1775-
Aead& operator=(const Aead&) = default;
1776-
NCRYPTO_DISALLOW_MOVE(Aead)
1777-
1778-
inline const EVP_AEAD* get() const { return aead_; }
1779-
inline operator const EVP_AEAD*() const { return aead_; }
1780-
inline operator bool() const { return aead_ != nullptr; }
1781-
1782-
int getMode() const;
1783-
int getNonceLength() const;
1784-
int getKeyLength() const;
1785-
int getBlockSize() const;
1786-
int getMaxOverhead() const;
1787-
int getMaxTagLength() const;
1788-
std::string_view getName() const;
1789-
1790-
static const Aead FromName(std::string_view name);
1791-
1792-
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
1793-
// This method is included only for implementing getCipherInfo and can't be
1794-
// used to construct an Aead instance.
1795-
int getNid() const;
1796-
// static const AEAD FromNid(int nid);
1797-
1798-
static const Aead FromCtx(std::string_view name, const AeadCtxPointer& ctx);
1799-
1800-
using AeadNameCallback = std::function<void(std::string_view name)>;
1801-
1802-
// Iterates the known ciphers if the underlying implementation
1803-
// is able to do so.
1804-
static void ForEach(AeadNameCallback callback);
1805-
1806-
// Utilities to get various AEADs by type.
1807-
1808-
static const Aead EMPTY;
1809-
static const Aead AES_128_GCM;
1810-
static const Aead AES_192_GCM;
1811-
static const Aead AES_256_GCM;
1812-
static const Aead CHACHA20_POLY1305;
1813-
static const Aead XCHACHA20_POLY1305;
1814-
static const Aead AES_128_CTR_HMAC_SHA256;
1815-
static const Aead AES_256_CTR_HMAC_SHA256;
1816-
static const Aead AES_128_GCM_SIV;
1817-
static const Aead AES_256_GCM_SIV;
1818-
static const Aead AES_128_GCM_RANDNONCE;
1819-
static const Aead AES_256_GCM_RANDNONCE;
1820-
static const Aead AES_128_CCM_BLUETOOTH;
1821-
static const Aead AES_128_CCM_BLUETOOTH_8;
1822-
static const Aead AES_128_CCM_MATTER;
1823-
static const Aead AES_128_EAX;
1824-
static const Aead AES_256_EAX;
1825-
1826-
private:
1827-
const EVP_AEAD* aead_ = nullptr;
1828-
const AeadInfo* info_ = nullptr;
1829-
1830-
using AeadConstructor = const EVP_AEAD* (*)();
1831-
static const std::unordered_map<AeadConstructor, AeadInfo> aeadIndex;
1832-
static const Aead FromConstructor(AeadConstructor construct);
1833-
};
1834-
1835-
class AeadCtxPointer final {
1836-
public:
1837-
static AeadCtxPointer New(
1838-
const Aead& aead,
1839-
bool encrypt,
1840-
const unsigned char* key = nullptr,
1841-
size_t keyLen = 0,
1842-
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
1843-
1844-
AeadCtxPointer() = default;
1845-
explicit AeadCtxPointer(EVP_AEAD_CTX* ctx);
1846-
AeadCtxPointer(AeadCtxPointer&& other) noexcept;
1847-
AeadCtxPointer& operator=(AeadCtxPointer&& other) noexcept;
1848-
NCRYPTO_DISALLOW_COPY(AeadCtxPointer)
1849-
~AeadCtxPointer();
1850-
1851-
inline bool operator==(std::nullptr_t) const noexcept {
1852-
return ctx_ == nullptr;
1853-
}
1854-
inline operator bool() const { return ctx_ != nullptr; }
1855-
inline EVP_AEAD_CTX* get() const { return ctx_.get(); }
1856-
inline operator EVP_AEAD_CTX*() const { return ctx_.get(); }
1857-
void reset(EVP_AEAD_CTX* ctx = nullptr);
1858-
EVP_AEAD_CTX* release();
1859-
1860-
bool init(const Aead& aead,
1861-
bool encrypt,
1862-
const unsigned char* key = nullptr,
1863-
size_t keyLen = 0,
1864-
size_t tagLen = EVP_AEAD_DEFAULT_TAG_LENGTH /* = 0 */);
1865-
1866-
// TODO(npaun): BoringSSL does not define NIDs for all AEADs.
1867-
// Decide if we will even implement this method.
1868-
// int getNid() const;
1869-
1870-
bool encrypt(const Buffer<const unsigned char>& in,
1871-
Buffer<unsigned char>& out,
1872-
Buffer<unsigned char>& tag,
1873-
const Buffer<const unsigned char>& nonce,
1874-
const Buffer<const unsigned char>& aad);
1875-
1876-
bool decrypt(const Buffer<const unsigned char>& in,
1877-
Buffer<unsigned char>& out,
1878-
const Buffer<const unsigned char>& tag,
1879-
const Buffer<const unsigned char>& nonce,
1880-
const Buffer<const unsigned char>& aad);
1881-
1882-
private:
1883-
DeleteFnPtr<EVP_AEAD_CTX, EVP_AEAD_CTX_free> ctx_;
1884-
};
1885-
#endif
1886-
18871763
#include "version.h"
18881764

18891765
} // namespace ncrypto

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)