Skip to content

Commit 4f43fd0

Browse files
committed
src: move CipherCtx methods to ncrypto
1 parent e6b27ca commit 4f43fd0

File tree

4 files changed

+270
-134
lines changed

4 files changed

+270
-134
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ std::optional<uint32_t> SSLPointer::verifyPeerCertificate() const {
22942294
}
22952295

22962296
const std::string_view SSLPointer::getClientHelloAlpn() const {
2297-
if (ssl_ == nullptr) return std::string_view();
2297+
if (ssl_ == nullptr) return {};
22982298
const unsigned char* buf;
22992299
size_t len;
23002300
size_t rem;
@@ -2305,34 +2305,34 @@ const std::string_view SSLPointer::getClientHelloAlpn() const {
23052305
&buf,
23062306
&rem) ||
23072307
rem < 2) {
2308-
return nullptr;
2308+
return {};
23092309
}
23102310

23112311
len = (buf[0] << 8) | buf[1];
2312-
if (len + 2 != rem) return nullptr;
2312+
if (len + 2 != rem) return {};
23132313
return reinterpret_cast<const char*>(buf + 3);
23142314
}
23152315

23162316
const std::string_view SSLPointer::getClientHelloServerName() const {
2317-
if (ssl_ == nullptr) return std::string_view();
2317+
if (ssl_ == nullptr) return {};
23182318
const unsigned char* buf;
23192319
size_t len;
23202320
size_t rem;
23212321

23222322
if (!SSL_client_hello_get0_ext(get(), TLSEXT_TYPE_server_name, &buf, &rem) ||
23232323
rem <= 2) {
2324-
return nullptr;
2324+
return {};
23252325
}
23262326

23272327
len = (*buf << 8) | *(buf + 1);
2328-
if (len + 2 != rem) return nullptr;
2328+
if (len + 2 != rem) return {};
23292329
rem = len;
23302330

2331-
if (rem == 0 || *(buf + 2) != TLSEXT_NAMETYPE_host_name) return nullptr;
2331+
if (rem == 0 || *(buf + 2) != TLSEXT_NAMETYPE_host_name) return {};
23322332
rem--;
2333-
if (rem <= 2) return nullptr;
2333+
if (rem <= 2) return {};
23342334
len = (*(buf + 3) << 8) | *(buf + 4);
2335-
if (len + 2 > rem) return nullptr;
2335+
if (len + 2 > rem) return {};
23362336
return reinterpret_cast<const char*>(buf + 5);
23372337
}
23382338

@@ -2504,4 +2504,110 @@ bool Cipher::isSupportedAuthenticatedMode() const {
25042504
}
25052505
}
25062506

2507+
// ============================================================================
2508+
2509+
CipherCtxPointer CipherCtxPointer::New() {
2510+
auto ret = CipherCtxPointer(EVP_CIPHER_CTX_new());
2511+
if (!ret) return {};
2512+
EVP_CIPHER_CTX_init(ret.get());
2513+
return ret;
2514+
}
2515+
2516+
CipherCtxPointer::CipherCtxPointer(EVP_CIPHER_CTX* ctx) : ctx_(ctx) {}
2517+
2518+
CipherCtxPointer::CipherCtxPointer(CipherCtxPointer&& other) noexcept
2519+
: ctx_(other.release()) {}
2520+
2521+
CipherCtxPointer& CipherCtxPointer::operator=(
2522+
CipherCtxPointer&& other) noexcept {
2523+
if (this == &other) return *this;
2524+
this->~CipherCtxPointer();
2525+
return *new (this) CipherCtxPointer(std::move(other));
2526+
}
2527+
2528+
CipherCtxPointer::~CipherCtxPointer() {
2529+
reset();
2530+
}
2531+
2532+
void CipherCtxPointer::reset(EVP_CIPHER_CTX* ctx) {
2533+
ctx_.reset(ctx);
2534+
}
2535+
2536+
EVP_CIPHER_CTX* CipherCtxPointer::release() {
2537+
return ctx_.release();
2538+
}
2539+
2540+
void CipherCtxPointer::setFlags(int flags) {
2541+
if (!ctx_) return;
2542+
EVP_CIPHER_CTX_set_flags(ctx_.get(), flags);
2543+
}
2544+
2545+
bool CipherCtxPointer::setKeyLength(size_t length) {
2546+
if (!ctx_) return false;
2547+
return EVP_CIPHER_CTX_set_key_length(ctx_.get(), length);
2548+
}
2549+
2550+
bool CipherCtxPointer::setIvLength(size_t length) {
2551+
if (!ctx_) return false;
2552+
return EVP_CIPHER_CTX_ctrl(
2553+
ctx_.get(), EVP_CTRL_AEAD_SET_IVLEN, length, nullptr);
2554+
}
2555+
2556+
bool CipherCtxPointer::setAeadTag(const Buffer<const char>& tag) {
2557+
if (!ctx_) return false;
2558+
return EVP_CIPHER_CTX_ctrl(
2559+
ctx_.get(), EVP_CTRL_AEAD_SET_TAG, tag.len, const_cast<char*>(tag.data));
2560+
}
2561+
2562+
bool CipherCtxPointer::setAeadTagLength(size_t length) {
2563+
if (!ctx_) return false;
2564+
return EVP_CIPHER_CTX_ctrl(
2565+
ctx_.get(), EVP_CTRL_AEAD_SET_TAG, length, nullptr);
2566+
}
2567+
2568+
bool CipherCtxPointer::setPadding(bool padding) {
2569+
if (!ctx_) return false;
2570+
return EVP_CIPHER_CTX_set_padding(ctx_.get(), padding);
2571+
}
2572+
2573+
int CipherCtxPointer::getBlockSize() const {
2574+
if (!ctx_) return 0;
2575+
return EVP_CIPHER_CTX_block_size(ctx_.get());
2576+
}
2577+
2578+
int CipherCtxPointer::getMode() const {
2579+
if (!ctx_) return 0;
2580+
return EVP_CIPHER_CTX_mode(ctx_.get());
2581+
}
2582+
2583+
int CipherCtxPointer::getNid() const {
2584+
if (!ctx_) return 0;
2585+
return EVP_CIPHER_CTX_nid(ctx_.get());
2586+
}
2587+
2588+
bool CipherCtxPointer::init(const Cipher& cipher,
2589+
bool encrypt,
2590+
const unsigned char* key,
2591+
const unsigned char* iv) {
2592+
if (!ctx_) return false;
2593+
return EVP_CipherInit_ex(
2594+
ctx_.get(), cipher, nullptr, key, iv, encrypt ? 1 : 0) == 1;
2595+
}
2596+
2597+
bool CipherCtxPointer::update(const Buffer<const unsigned char>& in,
2598+
unsigned char* out,
2599+
int* out_len,
2600+
bool finalize) {
2601+
if (!ctx_) return false;
2602+
if (!finalize) {
2603+
return EVP_CipherUpdate(ctx_.get(), out, out_len, in.data, in.len) == 1;
2604+
}
2605+
return EVP_CipherFinal_ex(ctx_.get(), out, out_len) == 1;
2606+
}
2607+
2608+
bool CipherCtxPointer::getAeadTag(size_t len, unsigned char* out) {
2609+
if (!ctx_) return false;
2610+
return EVP_CIPHER_CTX_ctrl(ctx_.get(), EVP_CTRL_AEAD_GET_TAG, len, out);
2611+
}
2612+
25072613
} // namespace ncrypto

deps/ncrypto/ncrypto.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
197197

198198
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
199199
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
200-
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
201200
using DSAPointer = DeleteFnPtr<DSA, DSA_free>;
202201
using DSASigPointer = DeleteFnPtr<DSA_SIG, DSA_SIG_free>;
203202
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
@@ -213,6 +212,8 @@ using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>;
213212
using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
214213
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
215214

215+
class CipherCtxPointer;
216+
216217
struct StackOfXASN1Deleter {
217218
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
218219
sk_ASN1_OBJECT_pop_free(p, ASN1_OBJECT_free);
@@ -425,6 +426,51 @@ class BignumPointer final {
425426
static bool defaultPrimeCheckCallback(int, int) { return 1; }
426427
};
427428

429+
class CipherCtxPointer final {
430+
public:
431+
static CipherCtxPointer New();
432+
433+
CipherCtxPointer() = default;
434+
explicit CipherCtxPointer(EVP_CIPHER_CTX* ctx);
435+
CipherCtxPointer(CipherCtxPointer&& other) noexcept;
436+
CipherCtxPointer& operator=(CipherCtxPointer&& other) noexcept;
437+
NCRYPTO_DISALLOW_COPY(CipherCtxPointer)
438+
~CipherCtxPointer();
439+
440+
inline bool operator==(std::nullptr_t) const noexcept {
441+
return ctx_ == nullptr;
442+
}
443+
inline operator bool() const { return ctx_ != nullptr; }
444+
inline EVP_CIPHER_CTX* get() const { return ctx_.get(); }
445+
inline operator EVP_CIPHER_CTX*() const { return ctx_.get(); }
446+
void reset(EVP_CIPHER_CTX* ctx = nullptr);
447+
EVP_CIPHER_CTX* release();
448+
449+
void setFlags(int flags);
450+
bool setKeyLength(size_t length);
451+
bool setIvLength(size_t length);
452+
bool setAeadTag(const Buffer<const char>& tag);
453+
bool setAeadTagLength(size_t length);
454+
bool setPadding(bool padding);
455+
bool init(const Cipher& cipher,
456+
bool encrypt,
457+
const unsigned char* key = nullptr,
458+
const unsigned char* iv = nullptr);
459+
460+
int getBlockSize() const;
461+
int getMode() const;
462+
int getNid() const;
463+
464+
bool update(const Buffer<const unsigned char>& in,
465+
unsigned char* out,
466+
int* out_len,
467+
bool finalize = false);
468+
bool getAeadTag(size_t len, unsigned char* out);
469+
470+
private:
471+
DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> ctx_;
472+
};
473+
428474
class EVPKeyPointer final {
429475
public:
430476
static EVPKeyPointer New();

0 commit comments

Comments
 (0)