aes_cipher_test.cc 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */

#include "paddle/fluid/framework/io/crypto/aes_cipher.h"

17
#include <cryptopp/cryptlib.h>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <glog/logging.h>
#include <gtest/gtest.h>

#include <string>
#include <vector>

#include "paddle/fluid/framework/io/crypto/cipher.h"
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"

namespace paddle {
namespace framework {

class AESTest : public ::testing::Test {
 public:
  std::string key;

  void SetUp() override { key = CipherUtils::GenKey(256); }
  static void GenConfigFile(const std::string& cipher_name);
};

void AESTest::GenConfigFile(const std::string& cipher_name) {
  std::ofstream fout("aes_test.conf");
  fout << "cipher_name : " << cipher_name << std::endl;
  fout.close();
}

TEST_F(AESTest, security_string) {
  std::vector<std::string> name_list(
      {"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
       "AES_GCM_NoPadding"});
  const std::string plaintext("hello world.");
Y
yangqingyou 已提交
49
  bool is_throw = false;
50 51 52 53 54 55 56 57 58
  for (auto& i : name_list) {
    AESTest::GenConfigFile(i);
    try {
      auto cipher = CipherFactory::CreateCipher("aes_test.conf");
      std::string ciphertext = cipher->Encrypt(plaintext, AESTest::key);

      std::string plaintext1 = cipher->Decrypt(ciphertext, AESTest::key);
      EXPECT_EQ(plaintext, plaintext1);
    } catch (CryptoPP::Exception& e) {
Y
yangqingyou 已提交
59
      is_throw = true;
60 61
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
62
    EXPECT_FALSE(is_throw);
63 64 65 66 67 68 69 70
  }
}

TEST_F(AESTest, security_vector) {
  std::vector<std::string> name_list(
      {"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
       "AES_GCM_NoPadding"});
  std::vector<int> input{1, 2, 3, 4};
Y
yangqingyou 已提交
71
  bool is_throw = false;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  for (auto& i : name_list) {
    AESTest::GenConfigFile(i);
    try {
      auto cipher = CipherFactory::CreateCipher("aes_test.conf");
      for (auto& i : input) {
        std::string ciphertext =
            cipher->Encrypt(std::to_string(i), AESTest::key);

        std::string plaintext = cipher->Decrypt(ciphertext, AESTest::key);

        int output = std::stoi(plaintext);

        EXPECT_EQ(i, output);
      }
    } catch (CryptoPP::Exception& e) {
Y
yangqingyou 已提交
87
      is_throw = true;
88 89
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
90
    EXPECT_FALSE(is_throw);
91 92 93 94 95 96 97 98 99
  }
}

TEST_F(AESTest, encrypt_to_file) {
  std::vector<std::string> name_list(
      {"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
       "AES_GCM_NoPadding"});
  const std::string plaintext("hello world.");
  std::string filename("aes_test.ciphertext");
Y
yangqingyou 已提交
100
  bool is_throw = false;
101 102 103 104 105 106 107 108
  for (auto& i : name_list) {
    AESTest::GenConfigFile(i);
    try {
      auto cipher = CipherFactory::CreateCipher("aes_test.conf");
      cipher->EncryptToFile(plaintext, AESTest::key, filename);
      std::string plaintext1 = cipher->DecryptFromFile(AESTest::key, filename);
      EXPECT_EQ(plaintext, plaintext1);
    } catch (CryptoPP::Exception& e) {
Y
yangqingyou 已提交
109
      is_throw = true;
110 111
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
112
    EXPECT_FALSE(is_throw);
113 114 115 116 117
  }
}

}  // namespace framework
}  // namespace paddle