File: Cipher_test.cpp

package info (click to toggle)
encfs 1.9.5-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 13,560 kB
  • sloc: cpp: 77,790; python: 9,665; xml: 3,888; sh: 995; perl: 866; makefile: 513; ansic: 213; exp: 16; sed: 16
file content (195 lines) | stat: -rw-r--r-- 5,665 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "gtest/gtest.h"

#include "encfs/BlockNameIO.h"
#include "encfs/Cipher.h"
#include "encfs/CipherKey.h"
#include "encfs/DirNode.h"
#include "encfs/FSConfig.h"
#include "encfs/FileUtils.h"
#include "encfs/StreamNameIO.h"

using namespace encfs;
using namespace testing;
using std::string;

const int FSBlockSize = 256;
const char TEST_ROOTDIR[] = "/foo";

static void testNameCoding(DirNode &dirNode) {
  // encrypt a name
  const char *name[] = {
      "1234567",         "12345678",         "123456789",
      "123456789ABCDEF", "123456789ABCDEF0", "123456789ABCDEF01",
      "test-name",       "test-name2",       "test",
      "../test",         "/foo/bar/blah",    "test-name.21",
      "test-name.22",    "test-name.o",      "1.test",
      "2.test",          "a/b/c/d",          "a/c/d/e",
      "b/c/d/e",         "b/a/c/d",          NULL};

  const char **orig = name;
  while (*orig) {
    string encName = dirNode.relativeCipherPath(*orig);

    // decrypt name
    string decName = dirNode.plainPath(encName.c_str());

    ASSERT_EQ(decName, *orig);
    orig++;
  }
}

class CipherTest : public TestWithParam<Cipher::CipherAlgorithm> {
 protected:
  virtual void SetUp() {
    Cipher::CipherAlgorithm alg = GetParam();
    cipher = Cipher::New(alg.name, alg.keyLength.closest(256));
  }
  virtual void TearDown() {}
  std::shared_ptr<Cipher> cipher;
};

TEST_P(CipherTest, SaveRestoreKey) {
  auto key = cipher->newRandomKey();

  auto encodingKey = cipher->newRandomKey();
  int encodedKeySize = cipher->encodedKeySize();
  unsigned char keyBuf[encodedKeySize];

  cipher->writeKey(key, keyBuf, encodingKey);
  auto restored = cipher->readKey(keyBuf, encodingKey);
  EXPECT_TRUE(restored);
  EXPECT_TRUE(cipher->compareKey(key, restored));
}

TEST_P(CipherTest, NameStreamEncoding) {
  auto key = cipher->newRandomKey();

  FSConfigPtr fsCfg = FSConfigPtr(new FSConfig);
  fsCfg->cipher = cipher;
  fsCfg->key = key;
  fsCfg->config.reset(new EncFSConfig);
  fsCfg->config->blockSize = FSBlockSize;
  fsCfg->opts.reset(new EncFS_Opts);
  fsCfg->opts->idleTracking = false;
  fsCfg->config->uniqueIV = false;

  fsCfg->nameCoding.reset(
      new StreamNameIO(StreamNameIO::CurrentInterface(), cipher, key));

  {
    fsCfg->nameCoding->setChainedNameIV(true);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }
  {
    fsCfg->nameCoding->setChainedNameIV(false);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }
}

TEST_P(CipherTest, NameBlockEncoding) {
  auto key = cipher->newRandomKey();

  FSConfigPtr fsCfg = FSConfigPtr(new FSConfig);
  fsCfg->cipher = cipher;
  fsCfg->key = key;
  fsCfg->config.reset(new EncFSConfig);
  fsCfg->config->blockSize = FSBlockSize;
  fsCfg->opts.reset(new EncFS_Opts);
  fsCfg->opts->idleTracking = false;
  fsCfg->config->uniqueIV = false;
  fsCfg->nameCoding.reset(new BlockNameIO(
      BlockNameIO::CurrentInterface(), cipher, key, cipher->cipherBlockSize()));

  {
    fsCfg->nameCoding->setChainedNameIV(true);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }
  {
    fsCfg->nameCoding->setChainedNameIV(false);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }
}

TEST_P(CipherTest, NameBlockBase32Encoding) {
  auto key = cipher->newRandomKey();

  FSConfigPtr fsCfg = FSConfigPtr(new FSConfig);
  fsCfg->cipher = cipher;
  fsCfg->key = key;
  fsCfg->config.reset(new EncFSConfig);
  fsCfg->config->blockSize = FSBlockSize;
  fsCfg->opts.reset(new EncFS_Opts);
  fsCfg->opts->idleTracking = false;
  fsCfg->config->uniqueIV = false;
  fsCfg->nameCoding.reset(new BlockNameIO(BlockNameIO::CurrentInterface(),
                                          cipher, key,
                                          cipher->cipherBlockSize(), true));

  {
    fsCfg->nameCoding->setChainedNameIV(true);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }

  {
    fsCfg->nameCoding->setChainedNameIV(false);
    DirNode dirNode(NULL, TEST_ROOTDIR, fsCfg);
    testNameCoding(dirNode);
  }
}

TEST_P(CipherTest, ConfigLoadStore) {
  auto key = cipher->newRandomKey();
  CipherKey encodingKey = cipher->newRandomKey();
  int encodedKeySize = cipher->encodedKeySize();
  unsigned char keyBuf[encodedKeySize];

  cipher->writeKey(key, keyBuf, encodingKey);

  // store in config struct..
  EncFSConfig cfg;
  cfg.cipherIface = cipher->interface();
  cfg.keySize = 8 * cipher->keySize();
  cfg.blockSize = FSBlockSize;
  cfg.assignKeyData(keyBuf, encodedKeySize);

  // save config
  // Creation of a temporary file should be more platform independent. On
  // c++17 we could use std::filesystem.
  std::string name = "/tmp/encfstestXXXXXX";
  int tmpFd = mkstemp(&name[0]);
  EXPECT_GE(tmpFd, 0);
  // mkstemp opens the temporary file, but we only need its name -> close it
  EXPECT_EQ(close(tmpFd), 0);
  {
    auto ok = writeV6Config(name.c_str(), &cfg);
    EXPECT_TRUE(ok);
  }

  // read back in and check everything..
  EncFSConfig cfg2;
  {
    auto ok = readV6Config(name.c_str(), &cfg2, nullptr);
    EXPECT_TRUE(ok);
  }
  // delete the temporary file where we stored the config
  EXPECT_EQ(unlink(name.c_str()), 0);

  // check..
  EXPECT_TRUE(cfg.cipherIface.implements(cfg2.cipherIface));
  EXPECT_EQ(cfg.keySize, cfg2.keySize);
  EXPECT_EQ(cfg.blockSize, cfg2.blockSize);

  // try decoding key..

  CipherKey key2 = cipher->readKey(cfg2.getKeyData(), encodingKey);
  EXPECT_TRUE(key2);
  EXPECT_TRUE(cipher->compareKey(key, key2));
}

INSTANTIATE_TEST_CASE_P(CipherKey, CipherTest,
                        ValuesIn(Cipher::GetAlgorithmList()));