2021-01-23 13:46:36 +08:00
|
|
|
/*
|
|
|
|
* StreamCipher.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2020 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-26 05:11:21 +08:00
|
|
|
#pragma once
|
2021-01-23 13:46:36 +08:00
|
|
|
|
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "flow/FastRef.h"
|
|
|
|
#include "flow/flow.h"
|
|
|
|
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <string>
|
2021-02-04 13:30:20 +08:00
|
|
|
#include <unordered_set>
|
2021-01-23 13:46:36 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2021-03-29 13:14:37 +08:00
|
|
|
// Wrapper class for openssl implementation of AES-128-GCM
|
|
|
|
// encryption/decryption
|
2021-02-04 13:30:20 +08:00
|
|
|
class StreamCipher final : NonCopyable {
|
|
|
|
static std::unordered_set<EVP_CIPHER_CTX*> ctxs;
|
|
|
|
EVP_CIPHER_CTX* ctx;
|
|
|
|
|
2021-02-04 10:13:39 +08:00
|
|
|
public:
|
2021-02-04 13:30:20 +08:00
|
|
|
StreamCipher();
|
|
|
|
~StreamCipher();
|
|
|
|
EVP_CIPHER_CTX* getCtx();
|
|
|
|
class Key : NonCopyable {
|
|
|
|
std::array<unsigned char, 16> arr;
|
|
|
|
static std::unique_ptr<Key> globalKey;
|
|
|
|
struct ConstructorTag {};
|
|
|
|
|
|
|
|
public:
|
2021-06-27 02:15:12 +08:00
|
|
|
using RawKeyType = decltype(arr);
|
2021-02-04 13:30:20 +08:00
|
|
|
Key(ConstructorTag) {}
|
2021-06-26 05:11:21 +08:00
|
|
|
Key(Key&&);
|
|
|
|
Key& operator=(Key&&);
|
2021-02-04 13:30:20 +08:00
|
|
|
~Key();
|
|
|
|
unsigned char const* data() const { return arr.data(); }
|
2021-06-27 02:15:12 +08:00
|
|
|
static void initializeKey(RawKeyType&&);
|
2021-06-26 05:11:21 +08:00
|
|
|
static void initializeRandomTestKey();
|
2021-02-04 13:30:20 +08:00
|
|
|
static const Key& getKey();
|
|
|
|
static void cleanup() noexcept;
|
|
|
|
};
|
|
|
|
static void cleanup() noexcept;
|
|
|
|
using IV = std::array<unsigned char, 16>;
|
2021-02-04 10:13:39 +08:00
|
|
|
};
|
2021-01-23 13:47:36 +08:00
|
|
|
|
2021-01-23 13:46:36 +08:00
|
|
|
class EncryptionStreamCipher final : NonCopyable, public ReferenceCounted<EncryptionStreamCipher> {
|
2021-02-04 13:30:20 +08:00
|
|
|
StreamCipher cipher;
|
|
|
|
|
2021-01-23 13:46:36 +08:00
|
|
|
public:
|
2021-01-23 13:47:36 +08:00
|
|
|
EncryptionStreamCipher(const StreamCipher::Key& key, const StreamCipher::IV& iv);
|
2021-01-23 13:46:36 +08:00
|
|
|
StringRef encrypt(unsigned char const* plaintext, int len, Arena&);
|
|
|
|
StringRef finish(Arena&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DecryptionStreamCipher final : NonCopyable, public ReferenceCounted<DecryptionStreamCipher> {
|
2021-02-04 13:30:20 +08:00
|
|
|
StreamCipher cipher;
|
|
|
|
|
2021-01-23 13:46:36 +08:00
|
|
|
public:
|
2021-01-23 13:47:36 +08:00
|
|
|
DecryptionStreamCipher(const StreamCipher::Key& key, const StreamCipher::IV& iv);
|
2021-01-23 13:46:36 +08:00
|
|
|
StringRef decrypt(unsigned char const* ciphertext, int len, Arena&);
|
|
|
|
StringRef finish(Arena&);
|
|
|
|
};
|