bool AESDecryptor::start()

in util/EncryptionUtils.cpp [400:449]


bool AESDecryptor::start(const EncryptionParams& encryptionData,
                         const std::string& iv) {
  WDT_CHECK(!started_);

  // reset the enc ctx
  evpCtx_.reset(createAndInitCtx());

  type_ = encryptionData.getType();

  const std::string& key = encryptionData.getSecret();
  if (key.length() != kAESBlockSize) {
    WLOG(ERROR) << "Encryption key size must be " << kAESBlockSize
                << ", but input size length " << key.length();
    return false;
  }
  if (iv.length() != kAESBlockSize) {
    WLOG(ERROR) << "Initialization size must be " << kAESBlockSize
                << ", but input size length " << iv.length();
    return false;
  }

  uint8_t* ivPtr = (uint8_t*)(&iv.front());
  uint8_t* keyPtr = (uint8_t*)(&key.front());

  const EVP_CIPHER* cipher = getCipher(type_);
  if (cipher == nullptr) {
    return false;
  }
  int cipherBlockSize = EVP_CIPHER_block_size(cipher);
  // block size for ctr mode should be 1
  WDT_CHECK_EQ(1, cipherBlockSize);

  if (type_ == ENC_AES128_GCM) {
    if (EVP_EncryptInit_ex(evpCtx_.get(), cipher, nullptr, nullptr, nullptr) !=
        1) {
      WLOG(ERROR) << "GCM Decryptor First init error";
    }
    if (EVP_CIPHER_CTX_ctrl(evpCtx_.get(), EVP_CTRL_GCM_SET_IVLEN, iv.size(),
                            nullptr) != 1) {
      WLOG(ERROR) << "Encrypt Init ivlen set failed";
    }
  }

  if (EVP_DecryptInit_ex(evpCtx_.get(), cipher, nullptr, keyPtr, ivPtr) != 1) {
    WLOG(ERROR) << "Decrypt Init failed";
    return false;
  }
  started_ = true;
  return true;
}