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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# -*- coding: utf-8 -*-
# test_opengpg_speed.py
# Copyright (C) 2016 LEAP Encryption Acess Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Benchmarking for the leap.bitmask.keymanager.openpgp module
"""
import commands
import pytest
from functools import partial
from gnupg import GPG
from leap.bitmask.keymanager.wrapper import TempGPGWrapper
from common import CIPHERTEXT
from common import SIGNEDTEXT
GROUP_INIT = 'initialization only'
GROUP_CRYPTO = 'crypto only'
GROUP_INIT_AND_CRYPTO = 'initialization and crypto'
# the gnupg module gets the binary version each time the GPG object is
# initialized. In some platforms this takes hundreds of milliseconds, and
# sometimes more than a second. This is currently a known bug. For making it
# evident, we provide a way to bypass the version check by monkeypatching the
# actual function that does the job.
def get_gpg_version():
output = commands.getoutput(
'gpg --list-config --with-colons | grep version')
version = output.split(':').pop()
return version
GPG_VERSION = get_gpg_version()
def mock_gpg_get_version(monkeypatch):
def _setver(self):
self.binary_version = GPG_VERSION
monkeypatch.setattr(
GPG, '_check_sane_and_get_gpg_version', _setver)
#
# generic speed test creator
#
def create_test(fun, num_keys=0, mock_get_version=True, init=None, group=None):
@pytest.mark.benchmark(group=group)
def test(tmpdir, benchmark, openpgp_keys, monkeypatch):
if mock_get_version:
mock_gpg_get_version(monkeypatch)
if init:
res = init(tmpdir, benchmark, openpgp_keys, monkeypatch, num_keys)
benchmark(fun, res)
else:
benchmark(
fun, tmpdir, benchmark, openpgp_keys, monkeypatch, num_keys)
return test
#
# gpg initializarion: 0, 1 and 2 keys
#
def gpg_init_only(tmpdir, benchmark, openpgp_keys, monkeypatch, num_keys):
keys = openpgp_keys[0:num_keys]
gpg = GPG(homedir=tmpdir.dirname)
for key in keys:
gpg.import_keys(key.key_data)
test_gpg_init_nokey_slow = create_test(
gpg_init_only, num_keys=0,
mock_get_version=False,
group=GROUP_INIT)
test_gpg_init_1key_slow = create_test(
gpg_init_only, num_keys=1,
mock_get_version=False,
group=GROUP_INIT)
test_gpg_init_2keys_slow = create_test(
gpg_init_only, num_keys=2,
mock_get_version=False,
group=GROUP_INIT)
test_gpg_init_nokey = create_test(
gpg_init_only, num_keys=0,
group=GROUP_INIT)
test_gpg_init_1key = create_test(
gpg_init_only, num_keys=1,
group=GROUP_INIT)
test_gpg_init_2keys = create_test(
gpg_init_only, num_keys=2,
group=GROUP_INIT)
#
# wrapper initialization: 0, 1 and 2 keys
#
def wrapper_init_only(tmpdir, benchmark, openpgp_keys, monkeypatch, num_keys):
keys = openpgp_keys[0:num_keys]
wrapper = TempGPGWrapper(keys=keys)
with wrapper as gpg:
assert GPG == type(gpg)
test_wrapper_init_nokey_slow = create_test(
wrapper_init_only, num_keys=0,
mock_get_version=False,
group=GROUP_INIT)
test_wrapper_init_1key_slow = create_test(
wrapper_init_only, num_keys=1,
mock_get_version=False,
group=GROUP_INIT)
test_wrapper_init_2keys_slow = create_test(
wrapper_init_only, num_keys=2,
mock_get_version=False,
group=GROUP_INIT)
test_wrapper_init_nokey = create_test(
wrapper_init_only, num_keys=0,
group=GROUP_INIT)
test_wrapper_init_1key = create_test(
wrapper_init_only, num_keys=1,
group=GROUP_INIT)
test_wrapper_init_2keys = create_test(
wrapper_init_only, num_keys=2,
group=GROUP_INIT)
#
# initialization + encryption
#
PLAINTEXT = ' ' * 10000 # 10 KB
def gpg_init_exec(fun, tmpdir, benchmark, openpgp_keys, monkeypatch, _):
pubkey = openpgp_keys[0]
privkey = openpgp_keys[2] # this is PRIVATE_KEY
gpg = GPG(homedir=tmpdir.dirname)
gpg.import_keys(pubkey.key_data)
gpg.import_keys(privkey.key_data)
fun((gpg, pubkey, privkey))
def wrapper_init_exec(fun, tmpdir, benchmark, openpgp_keys, monkeypatch, _):
pubkey = openpgp_keys[0]
privkey = openpgp_keys[2]
wrapper = TempGPGWrapper(keys=[pubkey, privkey])
wrapper._build_keyring()
fun((wrapper._gpg, pubkey, privkey))
def gpg_enc(res):
gpg, pubkey, _ = res
ciphertext = gpg.encrypt(PLAINTEXT, pubkey.fingerprint)
assert ciphertext.ok
assert len(ciphertext.data)
test_gpg_init_enc = create_test(
partial(gpg_init_exec, gpg_enc),
group=GROUP_INIT_AND_CRYPTO)
test_wrapper_init_enc = create_test(
partial(wrapper_init_exec, gpg_enc),
group=GROUP_INIT_AND_CRYPTO)
#
# initialization + decryption
#
def gpg_dec(res):
gpg, _, _ = res
plaintext = gpg.decrypt(CIPHERTEXT)
assert plaintext.ok
assert len(plaintext.data)
test_gpg_init_dec = create_test(
partial(gpg_init_exec, gpg_dec),
group=GROUP_INIT_AND_CRYPTO)
test_wrapper_init_dec = create_test(
partial(wrapper_init_exec, gpg_dec),
group=GROUP_INIT_AND_CRYPTO)
#
# initialization + sign
#
def gpg_sign(res):
gpg, _, privkey = res
gpg.import_keys(privkey.key_data)
signed = gpg.sign(PLAINTEXT, default_key=privkey.fingerprint)
assert signed.status == 'begin signing'
assert len(signed.data) > len(PLAINTEXT)
assert '-----BEGIN PGP SIGNATURE-----' in signed.data
assert '-----END PGP SIGNATURE-----' in signed.data
test_gpg_init_sign = create_test(
partial(gpg_init_exec, gpg_sign),
group=GROUP_INIT_AND_CRYPTO)
test_wrapper_init_sign = create_test(
partial(wrapper_init_exec, gpg_sign),
group=GROUP_INIT_AND_CRYPTO)
#
# initialization + verify
#
def gpg_verify(res):
gpg, _, privkey = res
signed = gpg.verify(SIGNEDTEXT)
assert signed.valid
test_gpg_init_verify = create_test(
partial(gpg_init_exec, gpg_verify),
group=GROUP_INIT_AND_CRYPTO)
test_wrapper_init_verify = create_test(
partial(wrapper_init_exec, gpg_verify),
group=GROUP_INIT_AND_CRYPTO)
#
# encryption only
#
def gpg_init(tmpdir, benchmark, openpgp_keys, monkeypatch, _):
pubkey = openpgp_keys[0]
privkey = openpgp_keys[2] # this is PRIVATE_KEY
gpg = GPG(homedir=tmpdir.dirname)
gpg.import_keys(pubkey.key_data)
gpg.import_keys(privkey.key_data)
return gpg, pubkey, privkey
def wrapper_init(tmpdir, benchmark, openpgp_keys, monkeypatch, _):
pubkey = openpgp_keys[0]
privkey = openpgp_keys[2]
wrapper = TempGPGWrapper(keys=[pubkey, privkey])
wrapper._build_keyring()
return wrapper._gpg, pubkey, privkey
test_gpg_enc = create_test(
gpg_enc, init=gpg_init, group=GROUP_CRYPTO)
test_wrapper_enc = create_test(
gpg_enc, init=wrapper_init, group=GROUP_CRYPTO)
#
# decryption only
#
test_gpg_dec = create_test(
gpg_dec,
init=gpg_init, group=GROUP_CRYPTO)
test_wrapper_dec = create_test(
gpg_dec,
init=wrapper_init, group=GROUP_CRYPTO)
#
# sign only
#
test_gpg_sign = create_test(
gpg_sign, init=gpg_init, group=GROUP_CRYPTO)
test_wrapper_sign = create_test(
gpg_sign, init=wrapper_init, group=GROUP_CRYPTO)
#
# verify only
#
test_gpg_verify = create_test(
gpg_verify, init=gpg_init, group=GROUP_CRYPTO)
test_wrapper_verify = create_test(
gpg_verify, init=wrapper_init, group=GROUP_CRYPTO)
|