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 17 #pragma once 18 #include <memory> 19 #include <alibabacloud/oss/Export.h> 20 #include <alibabacloud/oss/Types.h> 21 22 namespace AlibabaCloud 23 { 24 namespace OSS 25 { 26 enum class CipherAlgorithm { 27 AES, 28 RSA, 29 }; 30 31 enum class CipherMode { 32 NONE, 33 ECB, 34 CBC, 35 CTR, 36 }; 37 38 enum class CipherPadding { 39 NoPadding, 40 PKCS1Padding, 41 PKCS5Padding, 42 PKCS7Padding, 43 ZeroPadding, 44 }; 45 46 class ALIBABACLOUD_OSS_EXPORT SymmetricCipher 47 { 48 public: ~SymmetricCipher()49 virtual ~SymmetricCipher() {}; 50 51 //algorithm/mode/padding format. ex. AES/CBC/NoPadding Name()52 const std::string& Name() const { return name_; } Algorithm()53 CipherAlgorithm Algorithm() { return algorithm_; } Mode()54 CipherMode Mode() { return mode_; } Padding()55 CipherPadding Padding() { return padding_; } 56 BlockSize()57 int BlockSize() { return blockSize_; } 58 59 virtual void EncryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0; 60 virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0; 61 virtual int Encrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0; 62 virtual ByteBuffer EncryptFinish() = 0; 63 64 virtual void DecryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0; 65 virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0; 66 virtual int Decrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0; 67 virtual ByteBuffer DecryptFinish() = 0; 68 69 public: 70 static ByteBuffer GenerateIV(size_t length); 71 static ByteBuffer GenerateKey(size_t length); 72 static ByteBuffer IncCTRCounter(const ByteBuffer& counter, uint64_t numberOfBlocks); 73 74 static std::shared_ptr<SymmetricCipher> CreateAES128_CTRImpl(); 75 static std::shared_ptr<SymmetricCipher> CreateAES128_CBCImpl(); 76 static std::shared_ptr<SymmetricCipher> CreateAES256_CTRImpl(); 77 protected: 78 SymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad); 79 private: 80 std::string impl_; 81 std::string name_; 82 CipherAlgorithm algorithm_; 83 CipherMode mode_; 84 CipherPadding padding_; 85 int blockSize_; 86 }; 87 88 class ALIBABACLOUD_OSS_EXPORT AsymmetricCipher 89 { 90 public: ~AsymmetricCipher()91 virtual ~AsymmetricCipher() {}; Name()92 const std::string& Name() const { return name_; } Algorithm()93 CipherAlgorithm Algorithm() { return algorithm_; } Mode()94 CipherMode Mode() { return mode_; } Padding()95 CipherPadding Padding() { return padding_; } 96 setPublicKey(const std::string & key)97 void setPublicKey(const std::string& key) { publicKey_ = key; } setPrivateKey(const std::string & key)98 void setPrivateKey(const std::string& key) { privateKey_ = key; } 99 PublicKey()100 const std::string& PublicKey() const { return publicKey_; } PrivateKey()101 const std::string& PrivateKey() const { return privateKey_; } 102 103 virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0; 104 virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0; 105 106 public: 107 static std::shared_ptr<AsymmetricCipher> CreateRSA_NONEImpl(); 108 109 protected: 110 AsymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad); 111 private: 112 std::string impl_; 113 std::string name_; 114 CipherAlgorithm algorithm_; 115 CipherMode mode_; 116 CipherPadding padding_; 117 std::string publicKey_; 118 std::string privateKey_; 119 }; 120 } 121 } 122