Cipher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <memory>
  18. #include <alibabacloud/oss/Export.h>
  19. #include <alibabacloud/oss/Types.h>
  20. namespace AlibabaCloud
  21. {
  22. namespace OSS
  23. {
  24. enum class CipherAlgorithm {
  25. AES,
  26. RSA,
  27. };
  28. enum class CipherMode {
  29. NONE,
  30. ECB,
  31. CBC,
  32. CTR,
  33. };
  34. enum class CipherPadding {
  35. NoPadding,
  36. PKCS1Padding,
  37. PKCS5Padding,
  38. PKCS7Padding,
  39. ZeroPadding,
  40. };
  41. class ALIBABACLOUD_OSS_EXPORT SymmetricCipher
  42. {
  43. public:
  44. virtual ~SymmetricCipher() {};
  45. //algorithm/mode/padding format. ex. AES/CBC/NoPadding
  46. const std::string& Name() const { return name_; }
  47. CipherAlgorithm Algorithm() { return algorithm_; }
  48. CipherMode Mode() { return mode_; }
  49. CipherPadding Padding() { return padding_; }
  50. int BlockSize() { return blockSize_; }
  51. virtual void EncryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0;
  52. virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0;
  53. virtual int Encrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0;
  54. virtual ByteBuffer EncryptFinish() = 0;
  55. virtual void DecryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0;
  56. virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0;
  57. virtual int Decrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0;
  58. virtual ByteBuffer DecryptFinish() = 0;
  59. public:
  60. static ByteBuffer GenerateIV(size_t length);
  61. static ByteBuffer GenerateKey(size_t length);
  62. static ByteBuffer IncCTRCounter(const ByteBuffer& counter, uint64_t numberOfBlocks);
  63. static std::shared_ptr<SymmetricCipher> CreateAES128_CTRImpl();
  64. static std::shared_ptr<SymmetricCipher> CreateAES128_CBCImpl();
  65. static std::shared_ptr<SymmetricCipher> CreateAES256_CTRImpl();
  66. protected:
  67. SymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad);
  68. private:
  69. std::string impl_;
  70. std::string name_;
  71. CipherAlgorithm algorithm_;
  72. CipherMode mode_;
  73. CipherPadding padding_;
  74. int blockSize_;
  75. };
  76. class ALIBABACLOUD_OSS_EXPORT AsymmetricCipher
  77. {
  78. public:
  79. virtual ~AsymmetricCipher() {};
  80. const std::string& Name() const { return name_; }
  81. CipherAlgorithm Algorithm() { return algorithm_; }
  82. CipherMode Mode() { return mode_; }
  83. CipherPadding Padding() { return padding_; }
  84. void setPublicKey(const std::string& key) { publicKey_ = key; }
  85. void setPrivateKey(const std::string& key) { privateKey_ = key; }
  86. const std::string& PublicKey() const { return publicKey_; }
  87. const std::string& PrivateKey() const { return privateKey_; }
  88. virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0;
  89. virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0;
  90. public:
  91. static std::shared_ptr<AsymmetricCipher> CreateRSA_NONEImpl();
  92. protected:
  93. AsymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad);
  94. private:
  95. std::string impl_;
  96. std::string name_;
  97. CipherAlgorithm algorithm_;
  98. CipherMode mode_;
  99. CipherPadding padding_;
  100. std::string publicKey_;
  101. std::string privateKey_;
  102. };
  103. }
  104. }