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
|
#!/usr/bin/python
#
# Python bindings to libcryptsetup test
#
# Copyright (C) 2011, Red Hat, Inc. All rights reserved.
#
# This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
import os
sys.path.insert(0, ".libs")
import pycryptsetup
IMG = "test.img"
PASSWORD = "password"
PASSWORD2 = "password2"
DEVICE = "pycryptsetup_test_dev"
def log(level, txt):
if level == pycryptsetup.CRYPT_LOG_ERROR:
print txt,
return
def askyes(txt):
print "Question:", txt
return 1
def askpassword(txt):
return PASSWORD
def print_status(c):
r = c.status()
print "status :",
if r == pycryptsetup.CRYPT_ACTIVE:
print "ACTIVE"
elif r == pycryptsetup.CRYPT_INACTIVE:
print "INACTIVE"
else:
print "ERROR"
return
if os.geteuid() != 0:
print "WARNING: You must be root to run this test, test skipped."
sys.exit(0)
os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")
c = pycryptsetup.CryptSetup(
device = IMG,
name = DEVICE,
yesDialog = askyes,
logFunc = log,
passwordDialog = askpassword)
#c.debugLevel(pycryptsetup.CRYPT_DEBUG_ALL);
c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
c.iterationTime(1)
r = c.isLuks()
print "isLuks :", r
c.askyes(message = "Is there anybody out there?")
c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512)
print "isLuks :", c.isLuks()
print "luksUUID:", c.luksUUID()
print "addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2)
print "addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
newPassphrase = PASSWORD2, slot = 3)
print "removeP :", c.removePassphrase(passphrase = PASSWORD2)
print "addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2)
# original api required wrong passphrase parameter here
# print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
print "killSlot:", c.killSlot(slot = 0)
print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
print "suspend :", c.suspend()
# os.system("dmsetup info -c " + DEVICE)
print "resume :", c.resume(passphrase = PASSWORD)
print_status(c)
info = c.info()
print "cipher :", info["cipher"]
print "cmode :", info["cipher_mode"]
print "keysize :", info["keysize"]
print "dir :", info["dir"]
print "device :", info["device"]
print "offset :", info["offset"]
print "name :", info["name"]
print "uuid :", info["uuid"]
# os.system("cryptsetup luksDump " + info["device"])
print "deact. :", c.deactivate()
del c
c = pycryptsetup.CryptSetup(
device = IMG,
name = DEVICE,
yesDialog = askyes,
logFunc = log,
passwordDialog = askpassword)
print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
c2 = pycryptsetup.CryptSetup(
name = DEVICE,
yesDialog = askyes,
logFunc = log,
passwordDialog = askpassword)
info = c2.info()
print "cipher :", info["cipher"]
print "cmode :", info["cipher_mode"]
print "keysize :", info["keysize"]
print "deact. :", c.deactivate()
r = c2.deactivate()
print "deact. :", r
del c
del c2
os.remove(IMG)
|