File: test_cipher.py

package info (click to toggle)
python-mbedtls 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 960 kB
  • sloc: python: 4,595; sh: 170; makefile: 18
file content (432 lines) | stat: -rw-r--r-- 12,864 bytes parent folder | download
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# SPDX-License-Identifier: MIT

"""Unit tests for mbedtls.cipher."""

from __future__ import annotations

import pickle
import sys
from collections import defaultdict
from typing import (
    Any,
    Callable,
    Iterable,
    Iterator,
    Mapping,
    NamedTuple,
    Sequence,
    Tuple,
    TypeVar,
    Union,
    cast,
)

import pytest

from mbedtls import has_feature
from mbedtls.cipher import (
    AES,
    ARC4,
    ARIA,
    CHACHA20,
    DES,
    DES3,
    AEADCipherType,
    Blowfish,
    Camellia,
    CipherType,
    DES3dbl,
    Mode,
    get_supported_ciphers,
)
from mbedtls.exceptions import TLSError


class Size(NamedTuple):
    key_size: Sequence[int]
    iv_size: int


T = TypeVar("T")


def constant(value: T) -> Callable[[], T]:
    return lambda: value


_CipherModule = TypeVar(
    "_CipherModule", bound=Union[CipherType, AEADCipherType]
)


SUPPORTED_SIZES: Mapping[object, Mapping[Mode, Size]] = {
    AES: defaultdict(
        constant(Size(key_size=(16, 24, 32), iv_size=16)),
        {
            Mode.CCM: Size((16, 24, 32), 12),
            Mode.ECB: Size((16, 24, 32), 0),
            Mode.GCM: Size((16, 24, 32), 12),
            Mode.XTS: Size((32, 64), 16),
        },
    ),
    ARC4: defaultdict(
        constant(Size(key_size=(ARC4.key_size,), iv_size=0)),
        {Mode.CFB: Size((ARC4.key_size,), iv_size=ARC4.block_size)},
    ),
    ARIA: defaultdict(
        constant(Size(key_size=(16, 24, 32), iv_size=16)),
        {
            Mode.CBC: Size((16, 24, 32), 16),
            Mode.ECB: Size((16, 24, 32), 0),
            Mode.GCM: Size((16, 24, 32), 12),
        },
    ),
    Blowfish: defaultdict(
        constant(Size(key_size=(16,), iv_size=8)),
        {Mode.ECB: Size((16,), 0)},
    ),
    Camellia: defaultdict(
        constant(Size(key_size=(16, 24, 32), iv_size=16)),
        {
            Mode.ECB: Size((16, 24, 32), 0),
            Mode.GCM: Size((16, 24, 32), 12),
        },
    ),
    CHACHA20: defaultdict(
        constant(Size(key_size=(CHACHA20.key_size,), iv_size=12)),
        {Mode.ECB: Size((CHACHA20.key_size,), 0)},
    ),
    DES: defaultdict(
        constant(Size(key_size=(DES.key_size,), iv_size=8)),
        {Mode.ECB: Size((DES.key_size,), 0)},
    ),
    DES3: defaultdict(
        constant(Size(key_size=(DES3.key_size,), iv_size=8)),
        {Mode.ECB: Size((DES3.key_size,), 0)},
    ),
    DES3dbl: defaultdict(
        constant(Size(key_size=(DES3dbl.key_size,), iv_size=8)),
        {Mode.ECB: Size((DES3dbl.key_size,), 0)},
    ),
}


SUPPORTED_MODES: Mapping[object, Sequence[Mode]] = {
    AES: (
        Mode.ECB,
        Mode.CBC,
        Mode.CFB,
        Mode.CTR,
        Mode.OFB,
        Mode.XTS,
    ),
    ARC4: (Mode.STREAM,),
    ARIA: (Mode.ECB, Mode.CBC, Mode.CTR, Mode.GCM),
    Blowfish: (Mode.ECB, Mode.CBC, Mode.CFB, Mode.CTR),
    Camellia: (
        Mode.ECB,
        Mode.CBC,
        Mode.CFB,
        Mode.CTR,
        Mode.GCM,
    ),
    CHACHA20: (Mode.STREAM,),
    DES: (Mode.ECB, Mode.CBC),
    DES3: (Mode.ECB, Mode.CBC),
    DES3dbl: (Mode.ECB, Mode.CBC),
}

SUPPORTED_CIPHERS: Sequence[object] = tuple(SUPPORTED_MODES.keys())

SUPPORTED_AEAD_MODES: Mapping[object, Sequence[Mode]] = {
    AES: (Mode.GCM, Mode.CCM),
    CHACHA20: (Mode.CHACHAPOLY,),
}

SUPPORTED_AEAD_CIPHERS: Sequence[object] = tuple(SUPPORTED_AEAD_MODES)


def modname(mod: _CipherModule) -> str:
    return mod.__name__.rsplit(".", 1)[-1]


def gen_cipher_data(
    module: object,
    *,
    modes: Mapping[object, Sequence[Mode]],
) -> Iterator[Tuple[int, Mode, int]]:
    for mode in modes[module]:
        sizes = SUPPORTED_SIZES[module][mode]
        for key_size in sizes.key_size:
            yield key_size, mode, sizes.iv_size


def gen_cipher(
    modules: Iterable[object],
    *,
    modes: Mapping[object, Sequence[Mode]],
) -> Iterator[Tuple[object, int, Mode, int]]:
    for module in modules:
        for key_size, mode, iv_size in gen_cipher_data(module, modes=modes):
            yield module, key_size, mode, iv_size


def paramids(params: Tuple[_CipherModule, int, Mode, int]) -> str:
    module, key_size, mode, iv_size = params
    return (
        f"{module.__name__}, "
        f"key_size={key_size}, "
        f"mode={mode!s}, "
        f"iv_size={iv_size}"
    )


def test_get_supported_ciphers() -> None:
    # Check a few basic ciphers that really have to be present.
    assert b"AES-128-ECB" in get_supported_ciphers()
    assert b"AES-192-ECB" in get_supported_ciphers()
    assert b"AES-256-ECB" in get_supported_ciphers()


class TestCipher:
    @pytest.fixture(
        params=gen_cipher(SUPPORTED_CIPHERS, modes=SUPPORTED_MODES),
        ids=paramids,
    )
    def params(self, request: Any) -> Tuple[CipherType, int, Mode, int]:
        module: CipherType = request.param[0]
        name = modname(module)
        if not has_feature({"DES3": "DES", "DES3dbl": "DES"}.get(name, name)):
            return pytest.skip(f"{name} unavailable")
        assert isinstance(ARIA, CipherType)
        if module is ARIA and sys.platform.startswith("win"):
            return pytest.skip("unsupported")

        return cast(Tuple[CipherType, int, Mode, int], request.param)

    def test_pickle(
        self,
        params: Tuple[CipherType, int, Mode, int],
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(randbytes(key_size), mode, randbytes(iv_size))
        with pytest.raises(TypeError) as excinfo:
            pickle.dumps(cipher)

        assert str(excinfo.value).startswith("cannot pickle")

    def test_accessors(
        self,
        params: Tuple[CipherType, int, Mode, int],
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(randbytes(key_size), mode, randbytes(iv_size))
        assert cipher.key_size == key_size
        assert cipher.mode == mode
        assert cipher.iv_size == iv_size
        assert module.block_size == cipher.block_size
        assert module.key_size in {module.key_size, None}

    def test_cipher_name(
        self,
        params: Tuple[CipherType, int, Mode, int],
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(randbytes(key_size), mode, randbytes(iv_size))
        assert str(cipher) == cipher.name.decode("ascii")

    def test_encrypt_decrypt(
        self,
        params: Tuple[CipherType, int, Mode, int],
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(randbytes(key_size), mode, randbytes(iv_size))
        data = randbytes(
            cipher.block_size
            if cipher.mode is Mode.ECB
            else cipher.block_size * 128
        )
        assert cipher.decrypt(cipher.encrypt(data)) == data


class TestAEADCipher:
    @pytest.fixture(
        params=gen_cipher(SUPPORTED_AEAD_CIPHERS, modes=SUPPORTED_AEAD_MODES),
        ids=paramids,
    )
    def params(self, request: Any) -> Tuple[AEADCipherType, int, Mode, int]:
        module: AEADCipherType = request.param[0]
        name = modname(module)
        if not has_feature({"DES3": "DES", "DES3dbl": "DES"}.get(name, name)):
            return pytest.skip(f"{name} unavailable")

        return cast(Tuple[AEADCipherType, int, Mode, int], request.param)

    @pytest.mark.parametrize("ad_size", [0, 1, 16, 256])
    def test_pickle(
        self,
        params: Tuple[AEADCipherType, int, Mode, int],
        ad_size: int,
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(
            randbytes(key_size),
            mode,
            iv=randbytes(iv_size),
            ad=randbytes(ad_size),
        )
        with pytest.raises(TypeError) as excinfo:
            pickle.dumps(cipher)

        assert str(excinfo.value).startswith("cannot pickle")

    @pytest.mark.parametrize("ad_size", [0, 1, 16, 256])
    def test_accessors(
        self,
        params: Tuple[AEADCipherType, int, Mode, int],
        ad_size: int,
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(
            randbytes(key_size),
            mode,
            iv=randbytes(iv_size),
            ad=randbytes(ad_size),
        )
        assert cipher.key_size == key_size
        assert cipher.mode == mode
        assert cipher.iv_size == iv_size
        assert module.block_size == cipher.block_size
        assert module.key_size in {module.key_size, None}

    @pytest.mark.parametrize("ad_size", [0, 1, 16, 256])
    def test_cipher_name(
        self,
        params: Tuple[AEADCipherType, int, Mode, int],
        ad_size: int,
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(
            randbytes(key_size),
            mode,
            iv=randbytes(iv_size),
            ad=randbytes(ad_size),
        )
        assert str(cipher) == cipher.name.decode("ascii")

    @pytest.mark.parametrize("ad_size", [0, 1, 16, 256])
    def test_encrypt_decrypt(
        self,
        params: Tuple[AEADCipherType, int, Mode, int],
        ad_size: int,
        randbytes: Callable[[int], bytes],
    ) -> None:
        module, key_size, mode, iv_size = params
        cipher = module.new(
            randbytes(key_size),
            mode,
            iv=randbytes(iv_size),
            ad=randbytes(ad_size),
        )
        data = randbytes(
            cipher.block_size
            if cipher.mode is Mode.ECB
            else cipher.block_size * 128
        )
        msg, tag = cipher.encrypt(data)
        assert cipher.decrypt(msg, tag) == data


class TestVector:
    def test_aes_128_gcm(self) -> None:
        key = bytes.fromhex(32 * "0")
        nonce = bytes.fromhex(24 * "0")
        plaintext = b""
        ciphertext = b""
        adata = b""
        mac = bytes.fromhex("58e2fccefa7e3061367f1d57a4e7455a")

        cipher = AES.new(key, Mode.GCM, nonce, adata)
        assert cipher.encrypt(plaintext) == (ciphertext, mac)
        assert cipher.decrypt(*cipher.encrypt(plaintext)) == plaintext

    def test_aes_256_gcm(self) -> None:
        key = bytes.fromhex(64 * "0")
        nonce = bytes.fromhex(24 * "0")
        plaintext = b""
        ciphertext = b""
        adata = b""
        mac = bytes.fromhex("530f8afbc74536b9a963b4f1c4cb738b")

        cipher = AES.new(key, Mode.GCM, nonce, adata)
        assert cipher.encrypt(plaintext) == (ciphertext, mac)
        assert cipher.decrypt(*cipher.encrypt(plaintext)) == plaintext


class TestGenericCipher:
    @pytest.fixture(
        params=[
            AES,
            ARC4,
            ARIA,
            Blowfish,
            Camellia,
            CHACHA20,
            DES,
            DES3,
            DES3dbl,
        ]
    )
    def module(self, request: Any) -> CipherType:
        module: CipherType = request.param
        name = modname(module)
        if not has_feature({"DES3": "DES", "DES3dbl": "DES"}.get(name, name)):
            return pytest.skip(f"{name} unavailable")
        assert isinstance(ARIA, CipherType)
        if module is ARIA and sys.platform.startswith("win"):
            return pytest.skip("unsupported")
        return module

    def test_unsupported_mode(
        self, module: CipherType, randbytes: Callable[[int], bytes]
    ) -> None:
        supported_modes = frozenset(SUPPORTED_MODES[module]) | frozenset(
            SUPPORTED_AEAD_MODES.get(module, ())
        )
        for key_size, mode, iv_size in gen_cipher_data(
            module,
            modes={module: tuple(frozenset(Mode) - supported_modes)},
        ):
            with pytest.raises(TLSError) as excinfo:
                module.new(randbytes(key_size), mode, randbytes(iv_size))

            assert excinfo.value.msg.startswith("unsupported mode")

    def test_cbc_requires_padding(
        self, module: CipherType, randbytes: Callable[[int], bytes]
    ) -> None:
        mode = Mode.CBC
        if mode not in SUPPORTED_MODES[module]:
            pytest.skip(f"unsupported mode for {module!r}: {mode!s}")

        sizes = SUPPORTED_SIZES[module][mode]
        for key_size in sizes.key_size:
            cipher = module.new(
                randbytes(key_size), mode, iv=randbytes(sizes.iv_size)
            )
            data = randbytes(
                cipher.block_size
                if cipher.mode is Mode.ECB
                else cipher.block_size * 128
            )
            data += b"\0"
            if cipher.mode is Mode.CBC:
                with pytest.raises(ValueError):
                    cipher.encrypt(data)