MCPcopy Create free account
hub / github.com/apache/impala / InitializeEncryptionKey

Function InitializeEncryptionKey

be/src/exprs/string-functions.cc:411–468  ·  view source on GitHub ↗

Initializes the EncryptionKey for AES Encryption/ Decryption by validating arguments.

Source from the content-addressed store, hash-verified

409
410// Initializes the EncryptionKey for AES Encryption/ Decryption by validating arguments.
411Status InitializeEncryptionKey(FunctionContext* ctx, const StringVal& expr,
412 const StringVal& key, const StringVal& mode, const StringVal& iv, bool is_encrypt,
413 EncryptionKey* encryption_key) {
414 if (key.is_null) {
415 return Status(Substitute("Key cannot be NULL."));
416 }
417 if (key.len != 16 && key.len != 32) {
418 return Status(Substitute("AES only supports 128 and 256 bit key lengths."));
419 }
420 AES_CIPHER_MODE cipher_mode;
421
422 // If user passed a "NULL" field in mode, default AES encryption mode is chosen.
423 if (mode.is_null) {
424 cipher_mode = EncryptionKey::GetSupportedDefaultMode();
425 bool* state = reinterpret_cast<bool*>(
426 ctx->GetFunctionState(FunctionContext::THREAD_LOCAL));
427 if (state == nullptr || *state == false) {
428 VLOG_QUERY << "No AES mode was specified by user. Using " <<
429 EncryptionKey::ModeToString(cipher_mode) << " mode as default.";
430 if (state != nullptr) {
431 *state = true;
432 }
433 }
434 } else {
435 cipher_mode = EncryptionKey::StringToMode
436 (std::string_view(reinterpret_cast<const char*>(mode.ptr), mode.len));
437 }
438
439 if (cipher_mode == AES_CIPHER_MODE::INVALID) {
440 return Status(Substitute("Invalid AES 'mode': $0", StringPiece
441 (reinterpret_cast<const char*>(mode.ptr), mode.len)).c_str());
442 }
443
444 bool is_ecb = (cipher_mode == AES_CIPHER_MODE::AES_256_ECB
445 || cipher_mode == AES_CIPHER_MODE::AES_128_ECB);
446
447 // Check if iv is null in case of non ECB modes.
448 if (!is_ecb && iv.is_null) {
449 return Status(Substitute("IV vector required for $0 mode",
450 EncryptionKey::ModeToString(cipher_mode)).c_str());
451 }
452
453 // Check if IV vector size is valid (<= AES_BLOCK_SIZE) in case of non ECB modes.
454 if (!is_ecb && iv.len > AES_BLOCK_SIZE) {
455 return Status(Substitute("IV vector size is greater than 16 bytes."));
456 }
457
458 // ECB mode is not supported for Encryption.
459 if (is_encrypt && is_ecb) {
460 return Status(Substitute("ECB mode is not supported for encryption."));
461 }
462
463 // Initialize key and IV.
464 Status status = encryption_key->InitializeFields(key.ptr, key.len,
465 iv.ptr, iv.len, cipher_mode);
466
467 return status;
468}

Callers 2

AesDecryptImplMethod · 0.85
AesEncryptImplMethod · 0.85

Calls 5

SubstituteFunction · 0.85
GetFunctionStateMethod · 0.80
InitializeFieldsMethod · 0.80
StatusClass · 0.70
StringPieceClass · 0.50

Tested by

no test coverage detected