@@ -45,32 +45,50 @@ namespace duckdb
4545 }
4646 return result;
4747 }
48+ }
49+
50+ const EVP_MD *LookupAlgorithm (const std::string &algorithm)
51+ {
52+ std::string algo_lower = StringUtil::Lower (algorithm);
4853
49- const EVP_MD * getDigestByName ( const std::string &algorithm )
54+ if (algo_lower == " blake3 " )
5055 {
51- std::string algo_lower = StringUtil::Lower (algorithm);
56+ return nullptr ;
57+ }
5258
53- if (algo_lower == " blake3" )
54- {
55- return nullptr ;
56- }
59+ auto it = GetDigestMap ().find (algo_lower);
60+ if (it != GetDigestMap ().end ())
61+ {
62+ return it->second ();
63+ }
5764
58- auto it = GetDigestMap ().find (algo_lower);
59- if (it != GetDigestMap ().end ())
60- {
61- return it->second ();
62- }
65+ throw InvalidInputException (
66+ " Invalid hash algorithm '" + algorithm + " '. " +
67+ " Available algorithms are: " + getAvailableAlgorithms ());
68+ }
6369
64- return nullptr ;
70+ void ValidateRandomBytesLength (int64_t length)
71+ {
72+ if (length <= 0 )
73+ {
74+ throw InvalidInputException (" Random bytes length must be greater than 0" );
75+ }
76+
77+ if (length > CRYPTO_MAX_RANDOM_BYTES)
78+ {
79+ throw InvalidInputException (
80+ " Random bytes length must be less than or equal to " +
81+ std::to_string (CRYPTO_MAX_RANDOM_BYTES) + " bytes (4GB)" );
6582 }
6683 }
6784
6885 void CryptoHash (const std::string &algorithm, const char *data, size_t data_len, unsigned char *result, unsigned int &result_len)
6986 {
70- std::string algo_lower = StringUtil::Lower (algorithm);
87+ // LookupAlgorithm returns nullptr for blake3, throws on invalid algorithm
88+ const EVP_MD *md = LookupAlgorithm (algorithm);
7189
7290 // Handle Blake3 separately since it doesn't use OpenSSL
73- if (algo_lower == " blake3 " )
91+ if (md == nullptr )
7492 {
7593 blake3_hasher hasher;
7694 blake3_hasher_init (&hasher);
@@ -80,15 +98,6 @@ namespace duckdb
8098 return ;
8199 }
82100
83- const EVP_MD *md = getDigestByName (algo_lower);
84-
85- if (md == nullptr )
86- {
87- throw InvalidInputException (
88- " Invalid hash algorithm '" + algorithm + " '. " +
89- " Available algorithms are: " + getAvailableAlgorithms ());
90- }
91-
92101 EVP_MD_CTX *ctx = EVP_MD_CTX_new ();
93102 if (ctx == nullptr )
94103 {
@@ -113,10 +122,11 @@ namespace duckdb
113122
114123 void CryptoHmac (const std::string &algorithm, const std::string &key, const std::string &data, unsigned char *result, unsigned int &result_len)
115124 {
116- std::string algo_lower = StringUtil::Lower (algorithm);
125+ // LookupAlgorithm returns nullptr for blake3, throws on invalid algorithm
126+ const EVP_MD *md = LookupAlgorithm (algorithm);
117127
118128 // Handle Blake3 HMAC separately
119- if (algo_lower == " blake3 " )
129+ if (md == nullptr )
120130 {
121131 // Blake3 keyed mode requires exactly 32 bytes for the key
122132 if (key.size () != BLAKE3_KEY_LEN)
@@ -132,15 +142,6 @@ namespace duckdb
132142 return ;
133143 }
134144
135- const EVP_MD *md = getDigestByName (algo_lower);
136-
137- if (md == nullptr )
138- {
139- throw InvalidInputException (
140- " Invalid hash algorithm '" + algorithm + " '. " +
141- " Available algorithms are: " + getAvailableAlgorithms ());
142- }
143-
144145 unsigned char *hmac_result = HMAC (
145146 md,
146147 key.data (), key.size (),
@@ -155,20 +156,7 @@ namespace duckdb
155156
156157 void CryptoRandomBytes (int64_t length, unsigned char *result)
157158 {
158- // Validate input length
159- if (length <= 0 )
160- {
161- throw InvalidInputException (" Random bytes length must be greater than 0" );
162- }
163-
164- // DuckDB BLOB maximum size is 4GB (2^32 - 1 bytes)
165- constexpr int64_t MAX_BLOB_SIZE = 4294967295LL ; // 4GB - 1
166- if (length > MAX_BLOB_SIZE)
167- {
168- throw InvalidInputException (
169- " Random bytes length must be less than or equal to " +
170- std::to_string (MAX_BLOB_SIZE) + " bytes (4GB)" );
171- }
159+ ValidateRandomBytesLength (length);
172160
173161 // Generate random bytes using OpenSSL's RAND_bytes
174162 // RAND_bytes is cryptographically secure and automatically seeds itself
0 commit comments