Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(937)

Side by Side Diff: third_party/google-endpoints/Crypto/SelfTest/Cipher/test_DES3.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/Cipher/DES3.py: Self-test for the Triple-DES cipher
4 #
5 # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
6 #
7 # ===================================================================
8 # The contents of this file are dedicated to the public domain. To
9 # the extent that dedication to the public domain is not available,
10 # everyone is granted a worldwide, perpetual, royalty-free,
11 # non-exclusive license to exercise all rights associated with the
12 # contents of this file for any purpose whatsoever.
13 # No rights are reserved.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
23 # ===================================================================
24
25 """Self-test suite for Crypto.Cipher.DES3"""
26
27 __revision__ = "$Id$"
28
29 from common import dict # For compatibility with Python 2.1 and 2.2
30 from Crypto.Util.py3compat import *
31 from binascii import hexlify
32
33 # This is a list of (plaintext, ciphertext, key, description) tuples.
34 SP800_20_A1_KEY = '01' * 24
35 SP800_20_A2_PT = '00' * 8
36 test_data = [
37 # Test vector from Appendix B of NIST SP 800-67
38 # "Recommendation for the Triple Data Encryption Algorithm (TDEA) Block
39 # Cipher"
40 # http://csrc.nist.gov/publications/nistpubs/800-67/SP800-67.pdf
41 ('54686520717566636b2062726f776e20666f78206a756d70',
42 'a826fd8ce53b855fcce21c8112256fe668d5c05dd9b6b900',
43 '0123456789abcdef23456789abcdef01456789abcdef0123',
44 'NIST SP800-67 B.1'),
45
46 # Test vectors "The Multi-block Message Test (MMT) for DES and TDES"
47 # http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf
48 ('326a494cd33fe756', 'b22b8d66de970692',
49 '627f460e08104a1043cd265d5840eaf1313edf97df2a8a8c',
50 'DESMMT #1', dict(mode='CBC', iv='8e29f75ea77e5475')),
51
52 ('84401f78fe6c10876d8ea23094ea5309', '7b1f7c7e3b1c948ebd04a75ffba7d2f5',
53 '37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae',
54 'DESMMT #2', dict(mode='CBC', iv='3d1de3cc132e3b65')),
55
56 # Test vectors from Appendix A of NIST SP 800-20
57 # "Modes of Operation Validation System for the Triple Data Encryption
58 # Algorithm (TMOVS): Requirements and Procedures"
59 # http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf
60
61 # Table A.1 - Variable Plaintext Known Answer Test
62 ('8000000000000000', '95f8a5e5dd31d900', SP800_20_A1_KEY,
63 'NIST SP800-20 A.1 #0'),
64 ('4000000000000000', 'dd7f121ca5015619', SP800_20_A1_KEY,
65 'NIST SP800-20 A.1 #1'),
66 ('2000000000000000', '2e8653104f3834ea', SP800_20_A1_KEY,
67 'NIST SP800-20 A.1 #2'),
68 ('1000000000000000', '4bd388ff6cd81d4f', SP800_20_A1_KEY,
69 'NIST SP800-20 A.1 #3'),
70 ('0800000000000000', '20b9e767b2fb1456', SP800_20_A1_KEY,
71 'NIST SP800-20 A.1 #4'),
72 ('0400000000000000', '55579380d77138ef', SP800_20_A1_KEY,
73 'NIST SP800-20 A.1 #5'),
74 ('0200000000000000', '6cc5defaaf04512f', SP800_20_A1_KEY,
75 'NIST SP800-20 A.1 #6'),
76 ('0100000000000000', '0d9f279ba5d87260', SP800_20_A1_KEY,
77 'NIST SP800-20 A.1 #7'),
78 ('0080000000000000', 'd9031b0271bd5a0a', SP800_20_A1_KEY,
79 'NIST SP800-20 A.1 #8'),
80 ('0040000000000000', '424250b37c3dd951', SP800_20_A1_KEY,
81 'NIST SP800-20 A.1 #9'),
82 ('0020000000000000', 'b8061b7ecd9a21e5', SP800_20_A1_KEY,
83 'NIST SP800-20 A.1 #10'),
84 ('0010000000000000', 'f15d0f286b65bd28', SP800_20_A1_KEY,
85 'NIST SP800-20 A.1 #11'),
86 ('0008000000000000', 'add0cc8d6e5deba1', SP800_20_A1_KEY,
87 'NIST SP800-20 A.1 #12'),
88 ('0004000000000000', 'e6d5f82752ad63d1', SP800_20_A1_KEY,
89 'NIST SP800-20 A.1 #13'),
90 ('0002000000000000', 'ecbfe3bd3f591a5e', SP800_20_A1_KEY,
91 'NIST SP800-20 A.1 #14'),
92 ('0001000000000000', 'f356834379d165cd', SP800_20_A1_KEY,
93 'NIST SP800-20 A.1 #15'),
94 ('0000800000000000', '2b9f982f20037fa9', SP800_20_A1_KEY,
95 'NIST SP800-20 A.1 #16'),
96 ('0000400000000000', '889de068a16f0be6', SP800_20_A1_KEY,
97 'NIST SP800-20 A.1 #17'),
98 ('0000200000000000', 'e19e275d846a1298', SP800_20_A1_KEY,
99 'NIST SP800-20 A.1 #18'),
100 ('0000100000000000', '329a8ed523d71aec', SP800_20_A1_KEY,
101 'NIST SP800-20 A.1 #19'),
102 ('0000080000000000', 'e7fce22557d23c97', SP800_20_A1_KEY,
103 'NIST SP800-20 A.1 #20'),
104 ('0000040000000000', '12a9f5817ff2d65d', SP800_20_A1_KEY,
105 'NIST SP800-20 A.1 #21'),
106 ('0000020000000000', 'a484c3ad38dc9c19', SP800_20_A1_KEY,
107 'NIST SP800-20 A.1 #22'),
108 ('0000010000000000', 'fbe00a8a1ef8ad72', SP800_20_A1_KEY,
109 'NIST SP800-20 A.1 #23'),
110 ('0000008000000000', '750d079407521363', SP800_20_A1_KEY,
111 'NIST SP800-20 A.1 #24'),
112 ('0000004000000000', '64feed9c724c2faf', SP800_20_A1_KEY,
113 'NIST SP800-20 A.1 #25'),
114 ('0000002000000000', 'f02b263b328e2b60', SP800_20_A1_KEY,
115 'NIST SP800-20 A.1 #26'),
116 ('0000001000000000', '9d64555a9a10b852', SP800_20_A1_KEY,
117 'NIST SP800-20 A.1 #27'),
118 ('0000000800000000', 'd106ff0bed5255d7', SP800_20_A1_KEY,
119 'NIST SP800-20 A.1 #28'),
120 ('0000000400000000', 'e1652c6b138c64a5', SP800_20_A1_KEY,
121 'NIST SP800-20 A.1 #29'),
122 ('0000000200000000', 'e428581186ec8f46', SP800_20_A1_KEY,
123 'NIST SP800-20 A.1 #30'),
124 ('0000000100000000', 'aeb5f5ede22d1a36', SP800_20_A1_KEY,
125 'NIST SP800-20 A.1 #31'),
126 ('0000000080000000', 'e943d7568aec0c5c', SP800_20_A1_KEY,
127 'NIST SP800-20 A.1 #32'),
128 ('0000000040000000', 'df98c8276f54b04b', SP800_20_A1_KEY,
129 'NIST SP800-20 A.1 #33'),
130 ('0000000020000000', 'b160e4680f6c696f', SP800_20_A1_KEY,
131 'NIST SP800-20 A.1 #34'),
132 ('0000000010000000', 'fa0752b07d9c4ab8', SP800_20_A1_KEY,
133 'NIST SP800-20 A.1 #35'),
134 ('0000000008000000', 'ca3a2b036dbc8502', SP800_20_A1_KEY,
135 'NIST SP800-20 A.1 #36'),
136 ('0000000004000000', '5e0905517bb59bcf', SP800_20_A1_KEY,
137 'NIST SP800-20 A.1 #37'),
138 ('0000000002000000', '814eeb3b91d90726', SP800_20_A1_KEY,
139 'NIST SP800-20 A.1 #38'),
140 ('0000000001000000', '4d49db1532919c9f', SP800_20_A1_KEY,
141 'NIST SP800-20 A.1 #39'),
142 ('0000000000800000', '25eb5fc3f8cf0621', SP800_20_A1_KEY,
143 'NIST SP800-20 A.1 #40'),
144 ('0000000000400000', 'ab6a20c0620d1c6f', SP800_20_A1_KEY,
145 'NIST SP800-20 A.1 #41'),
146 ('0000000000200000', '79e90dbc98f92cca', SP800_20_A1_KEY,
147 'NIST SP800-20 A.1 #42'),
148 ('0000000000100000', '866ecedd8072bb0e', SP800_20_A1_KEY,
149 'NIST SP800-20 A.1 #43'),
150 ('0000000000080000', '8b54536f2f3e64a8', SP800_20_A1_KEY,
151 'NIST SP800-20 A.1 #44'),
152 ('0000000000040000', 'ea51d3975595b86b', SP800_20_A1_KEY,
153 'NIST SP800-20 A.1 #45'),
154 ('0000000000020000', 'caffc6ac4542de31', SP800_20_A1_KEY,
155 'NIST SP800-20 A.1 #46'),
156 ('0000000000010000', '8dd45a2ddf90796c', SP800_20_A1_KEY,
157 'NIST SP800-20 A.1 #47'),
158 ('0000000000008000', '1029d55e880ec2d0', SP800_20_A1_KEY,
159 'NIST SP800-20 A.1 #48'),
160 ('0000000000004000', '5d86cb23639dbea9', SP800_20_A1_KEY,
161 'NIST SP800-20 A.1 #49'),
162 ('0000000000002000', '1d1ca853ae7c0c5f', SP800_20_A1_KEY,
163 'NIST SP800-20 A.1 #50'),
164 ('0000000000001000', 'ce332329248f3228', SP800_20_A1_KEY,
165 'NIST SP800-20 A.1 #51'),
166 ('0000000000000800', '8405d1abe24fb942', SP800_20_A1_KEY,
167 'NIST SP800-20 A.1 #52'),
168 ('0000000000000400', 'e643d78090ca4207', SP800_20_A1_KEY,
169 'NIST SP800-20 A.1 #53'),
170 ('0000000000000200', '48221b9937748a23', SP800_20_A1_KEY,
171 'NIST SP800-20 A.1 #54'),
172 ('0000000000000100', 'dd7c0bbd61fafd54', SP800_20_A1_KEY,
173 'NIST SP800-20 A.1 #55'),
174 ('0000000000000080', '2fbc291a570db5c4', SP800_20_A1_KEY,
175 'NIST SP800-20 A.1 #56'),
176 ('0000000000000040', 'e07c30d7e4e26e12', SP800_20_A1_KEY,
177 'NIST SP800-20 A.1 #57'),
178 ('0000000000000020', '0953e2258e8e90a1', SP800_20_A1_KEY,
179 'NIST SP800-20 A.1 #58'),
180 ('0000000000000010', '5b711bc4ceebf2ee', SP800_20_A1_KEY,
181 'NIST SP800-20 A.1 #59'),
182 ('0000000000000008', 'cc083f1e6d9e85f6', SP800_20_A1_KEY,
183 'NIST SP800-20 A.1 #60'),
184 ('0000000000000004', 'd2fd8867d50d2dfe', SP800_20_A1_KEY,
185 'NIST SP800-20 A.1 #61'),
186 ('0000000000000002', '06e7ea22ce92708f', SP800_20_A1_KEY,
187 'NIST SP800-20 A.1 #62'),
188 ('0000000000000001', '166b40b44aba4bd6', SP800_20_A1_KEY,
189 'NIST SP800-20 A.1 #63'),
190
191 # Table A.2 - Variable Key Known Answer Test
192 (SP800_20_A2_PT, '95a8d72813daa94d', '8001010101010101'*3,
193 'NIST SP800-20 A.2 #0'),
194 (SP800_20_A2_PT, '0eec1487dd8c26d5', '4001010101010101'*3,
195 'NIST SP800-20 A.2 #1'),
196 (SP800_20_A2_PT, '7ad16ffb79c45926', '2001010101010101'*3,
197 'NIST SP800-20 A.2 #2'),
198 (SP800_20_A2_PT, 'd3746294ca6a6cf3', '1001010101010101'*3,
199 'NIST SP800-20 A.2 #3'),
200 (SP800_20_A2_PT, '809f5f873c1fd761', '0801010101010101'*3,
201 'NIST SP800-20 A.2 #4'),
202 (SP800_20_A2_PT, 'c02faffec989d1fc', '0401010101010101'*3,
203 'NIST SP800-20 A.2 #5'),
204 (SP800_20_A2_PT, '4615aa1d33e72f10', '0201010101010101'*3,
205 'NIST SP800-20 A.2 #6'),
206 (SP800_20_A2_PT, '2055123350c00858', '0180010101010101'*3,
207 'NIST SP800-20 A.2 #7'),
208 (SP800_20_A2_PT, 'df3b99d6577397c8', '0140010101010101'*3,
209 'NIST SP800-20 A.2 #8'),
210 (SP800_20_A2_PT, '31fe17369b5288c9', '0120010101010101'*3,
211 'NIST SP800-20 A.2 #9'),
212 (SP800_20_A2_PT, 'dfdd3cc64dae1642', '0110010101010101'*3,
213 'NIST SP800-20 A.2 #10'),
214 (SP800_20_A2_PT, '178c83ce2b399d94', '0108010101010101'*3,
215 'NIST SP800-20 A.2 #11'),
216 (SP800_20_A2_PT, '50f636324a9b7f80', '0104010101010101'*3,
217 'NIST SP800-20 A.2 #12'),
218 (SP800_20_A2_PT, 'a8468ee3bc18f06d', '0102010101010101'*3,
219 'NIST SP800-20 A.2 #13'),
220 (SP800_20_A2_PT, 'a2dc9e92fd3cde92', '0101800101010101'*3,
221 'NIST SP800-20 A.2 #14'),
222 (SP800_20_A2_PT, 'cac09f797d031287', '0101400101010101'*3,
223 'NIST SP800-20 A.2 #15'),
224 (SP800_20_A2_PT, '90ba680b22aeb525', '0101200101010101'*3,
225 'NIST SP800-20 A.2 #16'),
226 (SP800_20_A2_PT, 'ce7a24f350e280b6', '0101100101010101'*3,
227 'NIST SP800-20 A.2 #17'),
228 (SP800_20_A2_PT, '882bff0aa01a0b87', '0101080101010101'*3,
229 'NIST SP800-20 A.2 #18'),
230 (SP800_20_A2_PT, '25610288924511c2', '0101040101010101'*3,
231 'NIST SP800-20 A.2 #19'),
232 (SP800_20_A2_PT, 'c71516c29c75d170', '0101020101010101'*3,
233 'NIST SP800-20 A.2 #20'),
234 (SP800_20_A2_PT, '5199c29a52c9f059', '0101018001010101'*3,
235 'NIST SP800-20 A.2 #21'),
236 (SP800_20_A2_PT, 'c22f0a294a71f29f', '0101014001010101'*3,
237 'NIST SP800-20 A.2 #22'),
238 (SP800_20_A2_PT, 'ee371483714c02ea', '0101012001010101'*3,
239 'NIST SP800-20 A.2 #23'),
240 (SP800_20_A2_PT, 'a81fbd448f9e522f', '0101011001010101'*3,
241 'NIST SP800-20 A.2 #24'),
242 (SP800_20_A2_PT, '4f644c92e192dfed', '0101010801010101'*3,
243 'NIST SP800-20 A.2 #25'),
244 (SP800_20_A2_PT, '1afa9a66a6df92ae', '0101010401010101'*3,
245 'NIST SP800-20 A.2 #26'),
246 (SP800_20_A2_PT, 'b3c1cc715cb879d8', '0101010201010101'*3,
247 'NIST SP800-20 A.2 #27'),
248 (SP800_20_A2_PT, '19d032e64ab0bd8b', '0101010180010101'*3,
249 'NIST SP800-20 A.2 #28'),
250 (SP800_20_A2_PT, '3cfaa7a7dc8720dc', '0101010140010101'*3,
251 'NIST SP800-20 A.2 #29'),
252 (SP800_20_A2_PT, 'b7265f7f447ac6f3', '0101010120010101'*3,
253 'NIST SP800-20 A.2 #30'),
254 (SP800_20_A2_PT, '9db73b3c0d163f54', '0101010110010101'*3,
255 'NIST SP800-20 A.2 #31'),
256 (SP800_20_A2_PT, '8181b65babf4a975', '0101010108010101'*3,
257 'NIST SP800-20 A.2 #32'),
258 (SP800_20_A2_PT, '93c9b64042eaa240', '0101010104010101'*3,
259 'NIST SP800-20 A.2 #33'),
260 (SP800_20_A2_PT, '5570530829705592', '0101010102010101'*3,
261 'NIST SP800-20 A.2 #34'),
262 (SP800_20_A2_PT, '8638809e878787a0', '0101010101800101'*3,
263 'NIST SP800-20 A.2 #35'),
264 (SP800_20_A2_PT, '41b9a79af79ac208', '0101010101400101'*3,
265 'NIST SP800-20 A.2 #36'),
266 (SP800_20_A2_PT, '7a9be42f2009a892', '0101010101200101'*3,
267 'NIST SP800-20 A.2 #37'),
268 (SP800_20_A2_PT, '29038d56ba6d2745', '0101010101100101'*3,
269 'NIST SP800-20 A.2 #38'),
270 (SP800_20_A2_PT, '5495c6abf1e5df51', '0101010101080101'*3,
271 'NIST SP800-20 A.2 #39'),
272 (SP800_20_A2_PT, 'ae13dbd561488933', '0101010101040101'*3,
273 'NIST SP800-20 A.2 #40'),
274 (SP800_20_A2_PT, '024d1ffa8904e389', '0101010101020101'*3,
275 'NIST SP800-20 A.2 #41'),
276 (SP800_20_A2_PT, 'd1399712f99bf02e', '0101010101018001'*3,
277 'NIST SP800-20 A.2 #42'),
278 (SP800_20_A2_PT, '14c1d7c1cffec79e', '0101010101014001'*3,
279 'NIST SP800-20 A.2 #43'),
280 (SP800_20_A2_PT, '1de5279dae3bed6f', '0101010101012001'*3,
281 'NIST SP800-20 A.2 #44'),
282 (SP800_20_A2_PT, 'e941a33f85501303', '0101010101011001'*3,
283 'NIST SP800-20 A.2 #45'),
284 (SP800_20_A2_PT, 'da99dbbc9a03f379', '0101010101010801'*3,
285 'NIST SP800-20 A.2 #46'),
286 (SP800_20_A2_PT, 'b7fc92f91d8e92e9', '0101010101010401'*3,
287 'NIST SP800-20 A.2 #47'),
288 (SP800_20_A2_PT, 'ae8e5caa3ca04e85', '0101010101010201'*3,
289 'NIST SP800-20 A.2 #48'),
290 (SP800_20_A2_PT, '9cc62df43b6eed74', '0101010101010180'*3,
291 'NIST SP800-20 A.2 #49'),
292 (SP800_20_A2_PT, 'd863dbb5c59a91a0', '0101010101010140'*3,
293 'NIST SP800-20 A.2 #50'),
294 (SP800_20_A2_PT, 'a1ab2190545b91d7', '0101010101010120'*3,
295 'NIST SP800-20 A.2 #51'),
296 (SP800_20_A2_PT, '0875041e64c570f7', '0101010101010110'*3,
297 'NIST SP800-20 A.2 #52'),
298 (SP800_20_A2_PT, '5a594528bebef1cc', '0101010101010108'*3,
299 'NIST SP800-20 A.2 #53'),
300 (SP800_20_A2_PT, 'fcdb3291de21f0c0', '0101010101010104'*3,
301 'NIST SP800-20 A.2 #54'),
302 (SP800_20_A2_PT, '869efd7f9f265a09', '0101010101010102'*3,
303 'NIST SP800-20 A.2 #55'),
304
305 # "Two-key 3DES". Test vector generated using PyCrypto 2.0.1.
306 # This test is designed to test the DES3 API, not the correctness of the
307 # output.
308 ('21e81b7ade88a259', '5c577d4d9b20c0f8',
309 '9b397ebf81b1181e282f4bb8adbadc6b', 'Two-key 3DES'),
310
311 # The following test vectors have been generated with gpg v1.4.0.
312 # The command line used was:
313 # gpg -c -z 0 --cipher-algo 3DES --passphrase secret_passphrase \
314 # --disable-mdc --s2k-mode 0 --output ct pt
315 # For an explanation, see test_AES.py .
316 ( 'ac1762037074324fb53ba3596f73656d69746556616c6c6579', # Plaintext, 'Yo semiteValley'
317 '9979238528357b90e2e0be549cb0b2d5999b9a4a447e5c5c7d', # Ciphertext
318 '7ade65b460f5ea9be35f9e14aa883a2048e3824aa616c0b2', # Key (hash of ' BearsAhead')
319 'GPG Test Vector #1',
320 dict(mode='OPENPGP', iv='cd47e2afb8b7e4b0', encrypted_iv='6a7eef0b58050e8b 904a' ) ),
321 ]
322
323 def get_tests(config={}):
324 from Crypto.Cipher import DES3
325 from common import make_block_tests
326 return make_block_tests(DES3, "DES3", test_data)
327
328 if __name__ == '__main__':
329 import unittest
330 suite = lambda: unittest.TestSuite(get_tests())
331 unittest.main(defaultTest='suite')
332
333 # vim:set ts=4 sw=4 sts=4 expandtab:
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698