# This file is dual licensed under the terms of the Apache License, Version # 2.0, and the BSD License. See the LICENSE file in the root of this repository # for complete details. from __future__ import absolute_import, division, print_function import binascii from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import algorithms, base, modes def encrypt(mode, key, iv, plaintext): cipher = base.Cipher( algorithms.CAST5(binascii.unhexlify(key)), mode(binascii.unhexlify(iv)), default_backend() ) encryptor = cipher.encryptor() ct = encryptor.update(binascii.unhexlify(plaintext)) ct += encryptor.finalize() return binascii.hexlify(ct) def build_vectors(mode, filename): vector_file = open(filename, "r") count = 0 output = [] key = None iv = None plaintext = None for line in vector_file: line = line.strip() if line.startswith("KEY"): if count != 0: output.append("CIPHERTEXT = {}".format( encrypt(mode, key, iv, plaintext)) ) output.append("\nCOUNT = {}".format(count)) count += 1 name, key = line.split(" = ") output.append("KEY = {}".format(key)) elif line.startswith("IV"): name, iv = line.split(" = ") iv = iv[0:16] output.append("IV = {}".format(iv)) elif line.startswith("PLAINTEXT"): name, plaintext = line.split(" = ") output.append("PLAINTEXT = {}".format(plaintext)) output.append("CIPHERTEXT = {}".format(encrypt(mode, key, iv, plaintext))) return "\n".join(output) def write_file(data, filename): with open(filename, "w") as f: f.write(data) cbc_path = "tests/hazmat/primitives/vectors/ciphers/AES/CBC/CBCMMT128.rsp" write_file(build_vectors(modes.CBC, cbc_path), "cast5-cbc.txt") ofb_path = "tests/hazmat/primitives/vectors/ciphers/AES/OFB/OFBMMT128.rsp" write_file(build_vectors(modes.OFB, ofb_path), "cast5-ofb.txt") cfb_path = "tests/hazmat/primitives/vectors/ciphers/AES/CFB/CFB128MMT128.rsp" write_file(build_vectors(modes.CFB, cfb_path), "cast5-cfb.txt") ctr_path = "tests/hazmat/primitives/vectors/ciphers/AES/CTR/aes-128-ctr.txt" write_file(build_vectors(modes.CTR, ctr_path), "cast5-ctr.txt") ion value='author'>author
path: root/src/_cffi_src/openssl/crypto.py
blob: 03672d55ed4997e22694abaef453570d79cdcbc6 (plain)
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