| 425 | } |
| 426 | |
| 427 | Status EncryptionKey::InitializeRandom(int iv_len, AES_CIPHER_MODE m) { |
| 428 | mode_ = m; |
| 429 | if (!IsModeSupported(m, true)) { |
| 430 | mode_ = GetSupportedDefaultMode(); |
| 431 | LOG(WARNING) << Substitute("$0 is not supported, fall back to $1.", |
| 432 | ModeToString(m), ModeToString(mode_)); |
| 433 | } |
| 434 | |
| 435 | if (m == AES_CIPHER_MODE::INVALID) { |
| 436 | return Status("Invalid AES mode specified."); |
| 437 | } |
| 438 | |
| 439 | uint64_t next_key_num = keys_generated.Add(1); |
| 440 | if (next_key_num % RNG_RESEED_INTERVAL == 0) { |
| 441 | SeedOpenSSLRNG(); |
| 442 | } |
| 443 | RAND_bytes(key_, sizeof(key_)); |
| 444 | RAND_bytes(iv_, sizeof(iv_)); |
| 445 | memset(gcm_tag_, 0, sizeof(gcm_tag_)); |
| 446 | initialized_ = true; |
| 447 | key_length_ = GetKeyLenForMode(mode_); |
| 448 | iv_length_ = iv_len; |
| 449 | return Status::OK(); |
| 450 | } |
| 451 | |
| 452 | Status EncryptionKey::Encrypt(const uint8_t* data, int64_t len, uint8_t* out, |
| 453 | int64_t* out_len) { |