XRootD
Loading...
Searching...
No Matches
XrdCryptoLite_BFecb Class Reference

#include <XrdCryptoLite_BFecb.hh>

Collaboration diagram for XrdCryptoLite_BFecb:

Public Member Functions

 XrdCryptoLite_BFecb (bool &aOK, const unsigned char *key=0, unsigned int keylen=0)
 ~XrdCryptoLite_BFecb ()
void Decrypt (const unsigned char *in8, unsigned char *out8)
void Encrypt (const unsigned char *in8, unsigned char *out8)

Static Public Member Functions

static XrdCryptoLite_BFecbInstance (const unsigned char *key=0, unsigned int klen=0)

Detailed Description

Definition at line 46 of file XrdCryptoLite_BFecb.hh.

Constructor & Destructor Documentation

◆ XrdCryptoLite_BFecb()

XrdCryptoLite_BFecb::XrdCryptoLite_BFecb ( bool & aOK,
const unsigned char * key = 0,
unsigned int keylen = 0 )

Construct an ECB encryption/decryption object.

Parameters
aOKUpon return must be true if all went well. It will be false otherwise and this object will not be safely usable.
keyPointer to the encryption key which should be 128 bits. When null, a random 128 bit key is generated for use.
keylenThe length of the key in bytes if key is specified.
Note
When initializing a static pointer you may wish to use the Instance() method that automatically returns a null pointer on a failure.

Definition at line 42 of file XrdCryptoLite_BFecb.cc.

45 : decCTX(0), encCTX(0)
46{
47#if OPENSSL_VERSION_NUMBER >= 0x30000000L
48// With openssl v3 the blowfish cipher is only available via the "legacy"
49// provider. Legacy is typically not enabled by default (but can be via
50// openssl.cnf) so it is loaded here. Explicitly loading a provider will
51// disable the automatic loading of the "default" one. The default might
52// not have already been loaded, or standard algorithms might be available
53// via another configured provider, such as FIPS. So an attempt is made to
54// fetch a common default algorithm, possibly automaticlly loading the
55// default provider. Afterwards the legacy provider is loaded.
56//
57 static struct loadProviders {
58 loadProviders() {
59 EVP_MD *mdp = EVP_MD_fetch(NULL, "SHA2-256", NULL);
60 if (mdp) EVP_MD_free(mdp);
61 // Load legacy provider into the default (NULL) library context
62 (void) OSSL_PROVIDER_load(NULL, "legacy");
63 }
64 } lp;
65#endif
66
67// Handle auto generation of a random key
68//
69 unsigned char bfKey[16];
70 if (!key || !keylen)
71 {XrdOucUtils::Random(bfKey, sizeof(bfKey));
72 key = bfKey;
73 keylen = sizeof(bfKey);
74 }
75
76// The legacy openssl EVP is rather outdated, cumbersome, non thread-safe,
77// and badly documented. Unfortunately, it is the only one generally availabe
78// on all platforms (modern versions like CryptoPP need manual installation).
79// So, we need to construct a decryption context and an encryption context
80// because the context can only do one type of action at a time and resetting
81// the key when switching actions is CPU intensive. What a pain in the but!
82//
83 aOK = false;
84 if (!(decCTX = EVP_CIPHER_CTX_new())) return;
85 if (1 != EVP_DecryptInit_ex(decCTX, EVP_bf_ecb(), NULL, NULL, NULL)) return;
86 EVP_CIPHER_CTX_set_padding(decCTX, 0);
87 EVP_CIPHER_CTX_set_key_length(decCTX, keylen);
88 if (1 != EVP_DecryptInit_ex(decCTX, NULL, NULL, key, NULL)) return;
89
90 if (!(encCTX = EVP_CIPHER_CTX_new())) return;
91 if (1 != EVP_EncryptInit_ex(encCTX, EVP_bf_ecb(), NULL, NULL, NULL)) return;
92 EVP_CIPHER_CTX_set_padding(encCTX, 0);
93 EVP_CIPHER_CTX_set_key_length(encCTX, keylen);
94 if (1 != EVP_EncryptInit_ex(encCTX, NULL, NULL, key, NULL)) return;
95 aOK = true;
96}
static void Random(unsigned char *buff, unsigned int inblen)

References XrdOucUtils::Random().

Referenced by Instance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~XrdCryptoLite_BFecb()

XrdCryptoLite_BFecb::~XrdCryptoLite_BFecb ( )

Definition at line 102 of file XrdCryptoLite_BFecb.cc.

103{
104 EVP_CIPHER_CTX_free(decCTX);
105 EVP_CIPHER_CTX_free(encCTX);
106}

Member Function Documentation

◆ Decrypt()

void XrdCryptoLite_BFecb::Decrypt ( const unsigned char * in8,
unsigned char * out8 )

Decrypt exactly one blowfish block of 8 bytes (64 bits)

Parameters
in8Pointer to exactly 8 bytes of data to be decrypted.
out8Pointer to a 8 bytes or more buffer to hold the result.

Definition at line 112 of file XrdCryptoLite_BFecb.cc.

114{
115 int dlen;
116
117// Perform the action. Since we said padding is zero and the input must be
118// 8 bytes, and we are using blowfish ECB when we decrypt the result will
119// not be buffered but placed in the output buffer upon return.
120//
121 evpMutex.Lock();
122 EVP_DecryptUpdate(decCTX, out8, &dlen, in8, 8);
123 evpMutex.UnLock();
124}

◆ Encrypt()

void XrdCryptoLite_BFecb::Encrypt ( const unsigned char * in8,
unsigned char * out8 )

Encrypt exactly one blowfish block of 8 bytes (64 bits)

Parameters
in8Pointer to exactly 8 bytes of data to be encrypted.
out8Pointer to a 8 bytes or more buffer to hold the result.

Definition at line 130 of file XrdCryptoLite_BFecb.cc.

132{
133 int dlen;
134
135// Perform the action
136//
137// Perform the action. Since we said padding is zero and the input must be
138// 8 bytes, and we are using blowfish ECB when we encrypt the result will
139// not be buffered but placed in the output buffer upon return.
140//
141 evpMutex.Lock();
142 EVP_EncryptUpdate(encCTX, out8, &dlen, in8, 8);
143 evpMutex.UnLock();
144}

◆ Instance()

XrdCryptoLite_BFecb * XrdCryptoLite_BFecb::Instance ( const unsigned char * key = 0,
unsigned int klen = 0 )
static

Return an instance of an ECB encryption/decryption object upon success.

Parameters
keyPointer to the encryption key which should be 128 bits. When null, a random 128 bit key is generated for use.
keylenThe length of the key in bytes if key is specified.
Returns
A pointer to the crypto object or a null pointer upon failure.

Definition at line 150 of file XrdCryptoLite_BFecb.cc.

152{
154 bool isOK;
155
156// Get an instance or return a nil pointer
157//
158 obj = new XrdCryptoLite_BFecb(isOK, key, klen);
159 if (!isOK) {delete obj; obj = 0;}
160 return obj;
161}
XrdCryptoLite_BFecb(bool &aOK, const unsigned char *key=0, unsigned int keylen=0)

References XrdCryptoLite_BFecb().

Here is the call graph for this function:

The documentation for this class was generated from the following files: