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 <alibabacloud/oss/encryption/Cipher.h>
19 #include "../utils/StreamBuf.h"
20 
21 namespace AlibabaCloud
22 {
23 namespace OSS
24 {
25     class CryptoStreamBuf : public StreamBufProxy
26     {
27     public:
28         static const std::streamsize BLK_SIZE = 16;
29 
30         CryptoStreamBuf(std::iostream& stream,
31             const std::shared_ptr<SymmetricCipher>& cipher,
32             const ByteBuffer& key, const ByteBuffer& iv,
33             const int skipCnt = 0);
34         ~CryptoStreamBuf();
35     protected:
36         std::streamsize xsgetn(char * _Ptr, std::streamsize _Count);
37         std::streamsize xsputn(const char *_Ptr, std::streamsize _Count);
38         std::streampos seekoff(off_type _Off, std::ios_base::seekdir _Way, std::ios_base::openmode _Mode = std::ios_base::in | std::ios_base::out);
39         std::streampos seekpos(pos_type _Pos, std::ios_base::openmode _Mode = std::ios_base::in | std::ios_base::out);
40     private:
41         std::streamsize read_from_encrypted_buffer(char * _Ptr, std::streamsize _Count);
42         std::streamsize xsputn_with_skip(const char *_Ptr, std::streamsize _Count);
43         std::shared_ptr<SymmetricCipher> cipher_;
44         unsigned char encBuffer_[BLK_SIZE * 2];
45         std::streamsize encBufferCnt_;
46         std::streamsize encBufferOff_;
47         unsigned char decBuffer_[BLK_SIZE * 2];
48         std::streamsize decBufferCnt_;
49         std::streamsize decBufferOff_;
50         ByteBuffer key_;
51         ByteBuffer iv_;
52         bool initEncrypt;
53         bool initDecrypt;
54         std::streamsize skipCnt_; // for decrypt, must be less BLK_SIZE
55         std::streampos StartPosForIV_;
56     };
57 }
58 }
59