Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
CipherAlgorithm.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_HasComponent_OpenSSL
7#include <openssl/evp.h>
8#endif
9
10#include "Stroika/Foundation/Containers/Common.h"
13#include "Stroika/Foundation/Execution/Common.h"
15
16#include "CipherAlgorithm.h"
17
18using namespace Stroika::Foundation;
21using namespace Stroika::Foundation::Cryptography;
22using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
23using namespace Stroika::Foundation::Memory;
24
25#if qStroika_HasComponent_OpenSSL
26
27/*
28 ********************************************************************************
29 ***************** Cryptography::Providers::OpenSSL::CipherAlgorithms **********************
30 ********************************************************************************
31 */
32CipherAlgorithm::CipherAlgorithm (const ::EVP_CIPHER* cipher)
33 : name{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] const auto* property) -> String {
34 const CipherAlgorithm* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &CipherAlgorithm::name);
35 AssertNotNull (::EVP_CIPHER_name (thisObj->fCipher_));
36 return String{::EVP_CIPHER_name (thisObj->fCipher_)};
37 }}
38 , fCipher_{cipher}
39{
40 RequireNotNull (cipher);
41}
42
43CipherAlgorithm CipherAlgorithm::GetByName (const String& cipherName)
44{
45 static const Execution::RuntimeErrorException kErr_{"No such cipher"sv};
46 auto p = ::EVP_get_cipherbyname (cipherName.AsNarrowSDKString ().c_str ());
47 Execution::ThrowIfNull (p, kErr_);
48 return p;
49}
50
51optional<CipherAlgorithm> CipherAlgorithm::GetByNameQuietly (const String& cipherName)
52{
53 auto tmp = EVP_get_cipherbyname (cipherName.AsNarrowSDKString ().c_str ());
54 return tmp == nullptr ? optional<CipherAlgorithm>{} : tmp;
55}
56
57/*
58 ********************************************************************************
59 ************ Cryptography::Providers::OpenSSL::CipherAlgorithms ****************
60 ********************************************************************************
61 */
62const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_CBC{[] () { return ::EVP_aes_128_cbc (); }};
63const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_ECB{[] () { return ::EVP_aes_128_ecb (); }};
64const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_OFB{[] () { return ::EVP_aes_128_ofb (); }};
65const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_CFB1{[] () { return ::EVP_aes_128_cfb1 (); }};
66const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_CFB8{[] () { return ::EVP_aes_128_cfb8 (); }};
67const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_128_CFB128{[] () { return ::EVP_aes_128_cfb128 (); }};
68const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_CBC{[] () { return ::EVP_aes_192_cbc (); }};
69const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_ECB{[] () { return ::EVP_aes_192_ecb (); }};
70const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_OFB{[] () { return ::EVP_aes_192_ofb (); }};
71const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_CFB1{[] () { return ::EVP_aes_192_cfb1 (); }};
72const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_CFB8{[] () { return ::EVP_aes_192_cfb8 (); }};
73const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_192_CFB128{[] () { return ::EVP_aes_192_cfb128 (); }};
74const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_CBC{[] () { return ::EVP_aes_256_cbc (); }};
75const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_ECB{[] () { return ::EVP_aes_256_ecb (); }};
76const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_OFB{[] () { return ::EVP_aes_256_ofb (); }};
77const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_CFB1{[] () { return ::EVP_aes_256_cfb1 (); }};
78const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_CFB8{[] () { return ::EVP_aes_256_cfb8 (); }};
79const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kAES_256_CFB128{[] () { return ::EVP_aes_256_cfb128 (); }};
80const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kBlowfish_CBC{[] () { return ::EVP_bf_cbc (); }};
81const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kBlowfish{[] () { return CipherAlgorithms::kBlowfish_CBC (); }};
82const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kBlowfish_ECB{[] () { return ::EVP_bf_ecb (); }};
83const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kBlowfish_CFB{[] () { return ::EVP_bf_cfb (); }};
84const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kBlowfish_OFB{[] () { return ::EVP_bf_ofb (); }};
85const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kRC2_CBC{[] () { return ::EVP_rc2_cbc (); }};
86const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kRC2_ECB{[] () { return ::EVP_rc2_ecb (); }};
87const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kRC2_CFB{[] () { return ::EVP_rc2_cfb (); }};
88const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kRC2_OFB{[] () { return ::EVP_rc2_ofb (); }};
89const Execution::LazyInitialized<CipherAlgorithm> CipherAlgorithms::kRC4{[] () { return ::EVP_rc4 (); }};
90#endif
#define AssertNotNull(p)
Definition Assertions.h:333
#define RequireNotNull(p)
Definition Assertions.h:347
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual string AsNarrowSDKString() const
Definition String.inl:834
value-object, where the value construction is delayed until first needed (can be handy to avoid c++ i...