1 /*****************************************************************************
2  * Author:   Valient Gough <vgough@pobox.com>
3  *
4  *****************************************************************************
5  * Copyright (c) 2004, Valient Gough
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _CipherFileIO_incl_
22 #define _CipherFileIO_incl_
23 
24 #include <inttypes.h>
25 #include <memory>
26 #include <stdint.h>
27 #include <sys/types.h>
28 
29 #include "BlockFileIO.h"
30 #include "CipherKey.h"
31 #include "FSConfig.h"
32 #include "FileUtils.h"
33 #include "Interface.h"
34 
35 namespace encfs {
36 
37 class Cipher;
38 class FileIO;
39 struct IORequest;
40 
41 /*
42     Implement the FileIO interface encrypting data in blocks.
43 
44     Uses BlockFileIO to handle the block scatter / gather issues.
45 */
46 class CipherFileIO : public BlockFileIO {
47  public:
48   CipherFileIO(std::shared_ptr<FileIO> base, const FSConfigPtr &cfg);
49   virtual ~CipherFileIO();
50 
51   virtual Interface interface() const;
52 
53   virtual void setFileName(const char *fileName);
54   virtual const char *getFileName() const;
55   virtual bool setIV(uint64_t iv);
56 
57   virtual int open(int flags);
58 
59   virtual int getAttr(struct stat *stbuf) const;
60   virtual off_t getSize() const;
61 
62   virtual int truncate(off_t size);
63 
64   virtual bool isWritable() const;
65 
66  private:
67   virtual ssize_t readOneBlock(const IORequest &req) const;
68   virtual ssize_t writeOneBlock(const IORequest &req);
69   virtual int generateReverseHeader(unsigned char *data);
70 
71   int initHeader();
72   bool writeHeader();
73   bool blockRead(unsigned char *buf, int size, uint64_t iv64) const;
74   bool streamRead(unsigned char *buf, int size, uint64_t iv64) const;
75   bool blockWrite(unsigned char *buf, int size, uint64_t iv64) const;
76   bool streamWrite(unsigned char *buf, int size, uint64_t iv64) const;
77 
78   ssize_t read(const IORequest &req) const;
79 
80   std::shared_ptr<FileIO> base;
81 
82   FSConfigPtr fsConfig;
83 
84   // if haveHeader is true, then we have a transparent file header which
85   // contains a 64 bit initialization vector.
86   bool haveHeader;
87   uint64_t externalIV;
88   uint64_t fileIV;
89   int lastFlags;
90 
91   std::shared_ptr<Cipher> cipher;
92   CipherKey key;
93 };
94 
95 }  // namespace encfs
96 
97 #endif
98