test_pkcs1_oaep.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Cipher/test_pkcs1_oaep.py: Self-test for PKCS#1 OAEP encryption
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. import json
  23. import unittest
  24. from binascii import unhexlify
  25. from tls.Crypto.SelfTest.st_common import list_test_cases, a2b_hex, b2a_hex
  26. from tls.Crypto.PublicKey import RSA
  27. from tls.Crypto.Cipher import PKCS1_OAEP as PKCS
  28. from tls.Crypto.Hash import MD2, MD5, SHA1, SHA256, RIPEMD160, SHA224, SHA384, SHA512
  29. from tls.Crypto import Random
  30. from tls.Crypto.Signature.pss import MGF1
  31. from tls.Crypto.Util.py3compat import b, bchr
  32. from tls.Crypto.Util._file_system import pycryptodome_filename
  33. def rws(t):
  34. """Remove white spaces, tabs, and new lines from a string"""
  35. for c in ['\n', '\t', ' ']:
  36. t = t.replace(c,'')
  37. return t
  38. def t2b(t):
  39. """Convert a text string with bytes in hex form to a byte string"""
  40. clean = rws(t)
  41. if len(clean)%2 == 1:
  42. raise ValueError("Even number of characters expected")
  43. return a2b_hex(clean)
  44. class PKCS1_OAEP_Tests(unittest.TestCase):
  45. def setUp(self):
  46. self.rng = Random.new().read
  47. self.key1024 = RSA.generate(1024, self.rng)
  48. # List of tuples with test data for PKCS#1 OAEP
  49. # Each tuple is made up by:
  50. # Item #0: dictionary with RSA key component
  51. # Item #1: plaintext
  52. # Item #2: ciphertext
  53. # Item #3: random data (=seed)
  54. # Item #4: hash object
  55. _testData = (
  56. #
  57. # From in oaep-int.txt to be found in
  58. # ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  59. #
  60. (
  61. # Private key
  62. {
  63. 'n':'''bb f8 2f 09 06 82 ce 9c 23 38 ac 2b 9d a8 71 f7
  64. 36 8d 07 ee d4 10 43 a4 40 d6 b6 f0 74 54 f5 1f
  65. b8 df ba af 03 5c 02 ab 61 ea 48 ce eb 6f cd 48
  66. 76 ed 52 0d 60 e1 ec 46 19 71 9d 8a 5b 8b 80 7f
  67. af b8 e0 a3 df c7 37 72 3e e6 b4 b7 d9 3a 25 84
  68. ee 6a 64 9d 06 09 53 74 88 34 b2 45 45 98 39 4e
  69. e0 aa b1 2d 7b 61 a5 1f 52 7a 9a 41 f6 c1 68 7f
  70. e2 53 72 98 ca 2a 8f 59 46 f8 e5 fd 09 1d bd cb''',
  71. # Public key
  72. 'e':'11',
  73. # In the test vector, only p and q were given...
  74. # d is computed offline as e^{-1} mod (p-1)(q-1)
  75. 'd':'''a5dafc5341faf289c4b988db30c1cdf83f31251e0
  76. 668b42784813801579641b29410b3c7998d6bc465745e5c3
  77. 92669d6870da2c082a939e37fdcb82ec93edac97ff3ad595
  78. 0accfbc111c76f1a9529444e56aaf68c56c092cd38dc3bef
  79. 5d20a939926ed4f74a13eddfbe1a1cecc4894af9428c2b7b
  80. 8883fe4463a4bc85b1cb3c1'''
  81. }
  82. ,
  83. # Plaintext
  84. '''d4 36 e9 95 69 fd 32 a7 c8 a0 5b bc 90 d3 2c 49''',
  85. # Ciphertext
  86. '''12 53 e0 4d c0 a5 39 7b b4 4a 7a b8 7e 9b f2 a0
  87. 39 a3 3d 1e 99 6f c8 2a 94 cc d3 00 74 c9 5d f7
  88. 63 72 20 17 06 9e 52 68 da 5d 1c 0b 4f 87 2c f6
  89. 53 c1 1d f8 23 14 a6 79 68 df ea e2 8d ef 04 bb
  90. 6d 84 b1 c3 1d 65 4a 19 70 e5 78 3b d6 eb 96 a0
  91. 24 c2 ca 2f 4a 90 fe 9f 2e f5 c9 c1 40 e5 bb 48
  92. da 95 36 ad 87 00 c8 4f c9 13 0a de a7 4e 55 8d
  93. 51 a7 4d df 85 d8 b5 0d e9 68 38 d6 06 3e 09 55''',
  94. # Random
  95. '''aa fd 12 f6 59 ca e6 34 89 b4 79 e5 07 6d de c2
  96. f0 6c b5 8f''',
  97. # Hash
  98. SHA1,
  99. ),
  100. #
  101. # From in oaep-vect.txt to be found in Example 1.1
  102. # ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  103. #
  104. (
  105. # Private key
  106. {
  107. 'n':'''a8 b3 b2 84 af 8e b5 0b 38 70 34 a8 60 f1 46 c4
  108. 91 9f 31 87 63 cd 6c 55 98 c8 ae 48 11 a1 e0 ab
  109. c4 c7 e0 b0 82 d6 93 a5 e7 fc ed 67 5c f4 66 85
  110. 12 77 2c 0c bc 64 a7 42 c6 c6 30 f5 33 c8 cc 72
  111. f6 2a e8 33 c4 0b f2 58 42 e9 84 bb 78 bd bf 97
  112. c0 10 7d 55 bd b6 62 f5 c4 e0 fa b9 84 5c b5 14
  113. 8e f7 39 2d d3 aa ff 93 ae 1e 6b 66 7b b3 d4 24
  114. 76 16 d4 f5 ba 10 d4 cf d2 26 de 88 d3 9f 16 fb''',
  115. 'e':'''01 00 01''',
  116. 'd':'''53 33 9c fd b7 9f c8 46 6a 65 5c 73 16 ac a8 5c
  117. 55 fd 8f 6d d8 98 fd af 11 95 17 ef 4f 52 e8 fd
  118. 8e 25 8d f9 3f ee 18 0f a0 e4 ab 29 69 3c d8 3b
  119. 15 2a 55 3d 4a c4 d1 81 2b 8b 9f a5 af 0e 7f 55
  120. fe 73 04 df 41 57 09 26 f3 31 1f 15 c4 d6 5a 73
  121. 2c 48 31 16 ee 3d 3d 2d 0a f3 54 9a d9 bf 7c bf
  122. b7 8a d8 84 f8 4d 5b eb 04 72 4d c7 36 9b 31 de
  123. f3 7d 0c f5 39 e9 cf cd d3 de 65 37 29 ea d5 d1 '''
  124. }
  125. ,
  126. # Plaintext
  127. '''66 28 19 4e 12 07 3d b0 3b a9 4c da 9e f9 53 23
  128. 97 d5 0d ba 79 b9 87 00 4a fe fe 34''',
  129. # Ciphertext
  130. '''35 4f e6 7b 4a 12 6d 5d 35 fe 36 c7 77 79 1a 3f
  131. 7b a1 3d ef 48 4e 2d 39 08 af f7 22 fa d4 68 fb
  132. 21 69 6d e9 5d 0b e9 11 c2 d3 17 4f 8a fc c2 01
  133. 03 5f 7b 6d 8e 69 40 2d e5 45 16 18 c2 1a 53 5f
  134. a9 d7 bf c5 b8 dd 9f c2 43 f8 cf 92 7d b3 13 22
  135. d6 e8 81 ea a9 1a 99 61 70 e6 57 a0 5a 26 64 26
  136. d9 8c 88 00 3f 84 77 c1 22 70 94 a0 d9 fa 1e 8c
  137. 40 24 30 9c e1 ec cc b5 21 00 35 d4 7a c7 2e 8a''',
  138. # Random
  139. '''18 b7 76 ea 21 06 9d 69 77 6a 33 e9 6b ad 48 e1
  140. dd a0 a5 ef''',
  141. SHA1
  142. ),
  143. #
  144. # From in oaep-vect.txt to be found in Example 2.1
  145. # ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  146. #
  147. (
  148. # Private key
  149. {
  150. 'n':'''01 94 7c 7f ce 90 42 5f 47 27 9e 70 85 1f 25 d5
  151. e6 23 16 fe 8a 1d f1 93 71 e3 e6 28 e2 60 54 3e
  152. 49 01 ef 60 81 f6 8c 0b 81 41 19 0d 2a e8 da ba
  153. 7d 12 50 ec 6d b6 36 e9 44 ec 37 22 87 7c 7c 1d
  154. 0a 67 f1 4b 16 94 c5 f0 37 94 51 a4 3e 49 a3 2d
  155. de 83 67 0b 73 da 91 a1 c9 9b c2 3b 43 6a 60 05
  156. 5c 61 0f 0b af 99 c1 a0 79 56 5b 95 a3 f1 52 66
  157. 32 d1 d4 da 60 f2 0e da 25 e6 53 c4 f0 02 76 6f
  158. 45''',
  159. 'e':'''01 00 01''',
  160. 'd':'''08 23 f2 0f ad b5 da 89 08 8a 9d 00 89 3e 21 fa
  161. 4a 1b 11 fb c9 3c 64 a3 be 0b aa ea 97 fb 3b 93
  162. c3 ff 71 37 04 c1 9c 96 3c 1d 10 7a ae 99 05 47
  163. 39 f7 9e 02 e1 86 de 86 f8 7a 6d de fe a6 d8 cc
  164. d1 d3 c8 1a 47 bf a7 25 5b e2 06 01 a4 a4 b2 f0
  165. 8a 16 7b 5e 27 9d 71 5b 1b 45 5b dd 7e ab 24 59
  166. 41 d9 76 8b 9a ce fb 3c cd a5 95 2d a3 ce e7 25
  167. 25 b4 50 16 63 a8 ee 15 c9 e9 92 d9 24 62 fe 39'''
  168. },
  169. # Plaintext
  170. '''8f f0 0c aa 60 5c 70 28 30 63 4d 9a 6c 3d 42 c6
  171. 52 b5 8c f1 d9 2f ec 57 0b ee e7''',
  172. # Ciphertext
  173. '''01 81 af 89 22 b9 fc b4 d7 9d 92 eb e1 98 15 99
  174. 2f c0 c1 43 9d 8b cd 49 13 98 a0 f4 ad 3a 32 9a
  175. 5b d9 38 55 60 db 53 26 83 c8 b7 da 04 e4 b1 2a
  176. ed 6a ac df 47 1c 34 c9 cd a8 91 ad dc c2 df 34
  177. 56 65 3a a6 38 2e 9a e5 9b 54 45 52 57 eb 09 9d
  178. 56 2b be 10 45 3f 2b 6d 13 c5 9c 02 e1 0f 1f 8a
  179. bb 5d a0 d0 57 09 32 da cf 2d 09 01 db 72 9d 0f
  180. ef cc 05 4e 70 96 8e a5 40 c8 1b 04 bc ae fe 72
  181. 0e''',
  182. # Random
  183. '''8c 40 7b 5e c2 89 9e 50 99 c5 3e 8c e7 93 bf 94
  184. e7 1b 17 82''',
  185. SHA1
  186. ),
  187. #
  188. # From in oaep-vect.txt to be found in Example 10.1
  189. # ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
  190. #
  191. (
  192. # Private key
  193. {
  194. 'n':'''ae 45 ed 56 01 ce c6 b8 cc 05 f8 03 93 5c 67 4d
  195. db e0 d7 5c 4c 09 fd 79 51 fc 6b 0c ae c3 13 a8
  196. df 39 97 0c 51 8b ff ba 5e d6 8f 3f 0d 7f 22 a4
  197. 02 9d 41 3f 1a e0 7e 4e be 9e 41 77 ce 23 e7 f5
  198. 40 4b 56 9e 4e e1 bd cf 3c 1f b0 3e f1 13 80 2d
  199. 4f 85 5e b9 b5 13 4b 5a 7c 80 85 ad ca e6 fa 2f
  200. a1 41 7e c3 76 3b e1 71 b0 c6 2b 76 0e de 23 c1
  201. 2a d9 2b 98 08 84 c6 41 f5 a8 fa c2 6b da d4 a0
  202. 33 81 a2 2f e1 b7 54 88 50 94 c8 25 06 d4 01 9a
  203. 53 5a 28 6a fe b2 71 bb 9b a5 92 de 18 dc f6 00
  204. c2 ae ea e5 6e 02 f7 cf 79 fc 14 cf 3b dc 7c d8
  205. 4f eb bb f9 50 ca 90 30 4b 22 19 a7 aa 06 3a ef
  206. a2 c3 c1 98 0e 56 0c d6 4a fe 77 95 85 b6 10 76
  207. 57 b9 57 85 7e fd e6 01 09 88 ab 7d e4 17 fc 88
  208. d8 f3 84 c4 e6 e7 2c 3f 94 3e 0c 31 c0 c4 a5 cc
  209. 36 f8 79 d8 a3 ac 9d 7d 59 86 0e aa da 6b 83 bb''',
  210. 'e':'''01 00 01''',
  211. 'd':'''05 6b 04 21 6f e5 f3 54 ac 77 25 0a 4b 6b 0c 85
  212. 25 a8 5c 59 b0 bd 80 c5 64 50 a2 2d 5f 43 8e 59
  213. 6a 33 3a a8 75 e2 91 dd 43 f4 8c b8 8b 9d 5f c0
  214. d4 99 f9 fc d1 c3 97 f9 af c0 70 cd 9e 39 8c 8d
  215. 19 e6 1d b7 c7 41 0a 6b 26 75 df bf 5d 34 5b 80
  216. 4d 20 1a dd 50 2d 5c e2 df cb 09 1c e9 99 7b be
  217. be 57 30 6f 38 3e 4d 58 81 03 f0 36 f7 e8 5d 19
  218. 34 d1 52 a3 23 e4 a8 db 45 1d 6f 4a 5b 1b 0f 10
  219. 2c c1 50 e0 2f ee e2 b8 8d ea 4a d4 c1 ba cc b2
  220. 4d 84 07 2d 14 e1 d2 4a 67 71 f7 40 8e e3 05 64
  221. fb 86 d4 39 3a 34 bc f0 b7 88 50 1d 19 33 03 f1
  222. 3a 22 84 b0 01 f0 f6 49 ea f7 93 28 d4 ac 5c 43
  223. 0a b4 41 49 20 a9 46 0e d1 b7 bc 40 ec 65 3e 87
  224. 6d 09 ab c5 09 ae 45 b5 25 19 01 16 a0 c2 61 01
  225. 84 82 98 50 9c 1c 3b f3 a4 83 e7 27 40 54 e1 5e
  226. 97 07 50 36 e9 89 f6 09 32 80 7b 52 57 75 1e 79'''
  227. },
  228. # Plaintext
  229. '''8b ba 6b f8 2a 6c 0f 86 d5 f1 75 6e 97 95 68 70
  230. b0 89 53 b0 6b 4e b2 05 bc 16 94 ee''',
  231. # Ciphertext
  232. '''53 ea 5d c0 8c d2 60 fb 3b 85 85 67 28 7f a9 15
  233. 52 c3 0b 2f eb fb a2 13 f0 ae 87 70 2d 06 8d 19
  234. ba b0 7f e5 74 52 3d fb 42 13 9d 68 c3 c5 af ee
  235. e0 bf e4 cb 79 69 cb f3 82 b8 04 d6 e6 13 96 14
  236. 4e 2d 0e 60 74 1f 89 93 c3 01 4b 58 b9 b1 95 7a
  237. 8b ab cd 23 af 85 4f 4c 35 6f b1 66 2a a7 2b fc
  238. c7 e5 86 55 9d c4 28 0d 16 0c 12 67 85 a7 23 eb
  239. ee be ff 71 f1 15 94 44 0a ae f8 7d 10 79 3a 87
  240. 74 a2 39 d4 a0 4c 87 fe 14 67 b9 da f8 52 08 ec
  241. 6c 72 55 79 4a 96 cc 29 14 2f 9a 8b d4 18 e3 c1
  242. fd 67 34 4b 0c d0 82 9d f3 b2 be c6 02 53 19 62
  243. 93 c6 b3 4d 3f 75 d3 2f 21 3d d4 5c 62 73 d5 05
  244. ad f4 cc ed 10 57 cb 75 8f c2 6a ee fa 44 12 55
  245. ed 4e 64 c1 99 ee 07 5e 7f 16 64 61 82 fd b4 64
  246. 73 9b 68 ab 5d af f0 e6 3e 95 52 01 68 24 f0 54
  247. bf 4d 3c 8c 90 a9 7b b6 b6 55 32 84 eb 42 9f cc''',
  248. # Random
  249. '''47 e1 ab 71 19 fe e5 6c 95 ee 5e aa d8 6f 40 d0
  250. aa 63 bd 33''',
  251. SHA1
  252. ),
  253. )
  254. def testEncrypt1(self):
  255. # Verify encryption using all test vectors
  256. for test in self._testData:
  257. # Build the key
  258. comps = [ int(rws(test[0][x]),16) for x in ('n','e') ]
  259. key = RSA.construct(comps)
  260. # RNG that takes its random numbers from a pool given
  261. # at initialization
  262. class randGen:
  263. def __init__(self, data):
  264. self.data = data
  265. self.idx = 0
  266. def __call__(self, N):
  267. r = self.data[self.idx:N]
  268. self.idx += N
  269. return r
  270. # The real test
  271. cipher = PKCS.new(key, test[4], randfunc=randGen(t2b(test[3])))
  272. ct = cipher.encrypt(t2b(test[1]))
  273. self.assertEqual(ct, t2b(test[2]))
  274. def testEncrypt2(self):
  275. # Verify that encryption fails if plaintext is too long
  276. pt = '\x00'*(128-2*20-2+1)
  277. cipher = PKCS.new(self.key1024)
  278. self.assertRaises(ValueError, cipher.encrypt, pt)
  279. def testDecrypt1(self):
  280. # Verify decryption using all test vectors
  281. for test in self._testData:
  282. # Build the key
  283. comps = [ int(rws(test[0][x]),16) for x in ('n','e','d') ]
  284. key = RSA.construct(comps)
  285. # The real test
  286. cipher = PKCS.new(key, test[4])
  287. pt = cipher.decrypt(t2b(test[2]))
  288. self.assertEqual(pt, t2b(test[1]))
  289. def testDecrypt2(self):
  290. # Simplest possible negative tests
  291. for ct_size in (127,128,129):
  292. cipher = PKCS.new(self.key1024)
  293. self.assertRaises(ValueError, cipher.decrypt, bchr(0x00)*ct_size)
  294. def testEncryptDecrypt1(self):
  295. # Encrypt/Decrypt messages of length [0..128-2*20-2]
  296. for pt_len in range(0,128-2*20-2):
  297. pt = self.rng(pt_len)
  298. cipher = PKCS.new(self.key1024)
  299. ct = cipher.encrypt(pt)
  300. pt2 = cipher.decrypt(ct)
  301. self.assertEqual(pt,pt2)
  302. def testEncryptDecrypt2(self):
  303. # Helper function to monitor what's requested from RNG
  304. global asked
  305. def localRng(N):
  306. global asked
  307. asked += N
  308. return self.rng(N)
  309. # Verify that OAEP is friendly to all hashes
  310. for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD160):
  311. # Verify that encrypt() asks for as many random bytes
  312. # as the hash output size
  313. asked = 0
  314. pt = self.rng(40)
  315. cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
  316. ct = cipher.encrypt(pt)
  317. self.assertEqual(cipher.decrypt(ct), pt)
  318. self.assertEqual(asked, hashmod.digest_size)
  319. def testEncryptDecrypt3(self):
  320. # Verify that OAEP supports labels
  321. pt = self.rng(35)
  322. xlabel = self.rng(22)
  323. cipher = PKCS.new(self.key1024, label=xlabel)
  324. ct = cipher.encrypt(pt)
  325. self.assertEqual(cipher.decrypt(ct), pt)
  326. def testEncryptDecrypt4(self):
  327. # Verify that encrypt() uses the custom MGF
  328. global mgfcalls
  329. # Helper function to monitor what's requested from MGF
  330. def newMGF(seed,maskLen):
  331. global mgfcalls
  332. mgfcalls += 1
  333. return bchr(0x00)*maskLen
  334. mgfcalls = 0
  335. pt = self.rng(32)
  336. cipher = PKCS.new(self.key1024, mgfunc=newMGF)
  337. ct = cipher.encrypt(pt)
  338. self.assertEqual(mgfcalls, 2)
  339. self.assertEqual(cipher.decrypt(ct), pt)
  340. def testByteArray(self):
  341. pt = b("XER")
  342. cipher = PKCS.new(self.key1024)
  343. ct = cipher.encrypt(bytearray(pt))
  344. pt2 = cipher.decrypt(bytearray(ct))
  345. self.assertEqual(pt, pt2)
  346. def testMemoryview(self):
  347. pt = b("XER")
  348. cipher = PKCS.new(self.key1024)
  349. ct = cipher.encrypt(memoryview(bytearray(pt)))
  350. pt2 = cipher.decrypt(memoryview(bytearray(ct)))
  351. self.assertEqual(pt, pt2)
  352. import sys
  353. if sys.version[:3] == "2.6":
  354. del testMemoryview
  355. class TestVectorsWycheproof(unittest.TestCase):
  356. def __init__(self, wycheproof_warnings, skip_slow_tests):
  357. unittest.TestCase.__init__(self)
  358. self._wycheproof_warnings = wycheproof_warnings
  359. self._skip_slow_tests = skip_slow_tests
  360. self._id = "None"
  361. def load_tests(self, filename):
  362. comps = "Crypto.SelfTest.Cipher.test_vectors.wycheproof".split(".")
  363. with open(pycryptodome_filename(comps, filename), "rt") as file_in:
  364. tv_tree = json.load(file_in)
  365. class TestVector(object):
  366. pass
  367. result = []
  368. for group in tv_tree['testGroups']:
  369. rsa_key = RSA.import_key(group['privateKeyPem'])
  370. if group['sha'] == "SHA-1":
  371. hash_mod = SHA1
  372. elif group['sha'] == "SHA-224":
  373. hash_mod = SHA224
  374. elif group['sha'] == "SHA-256":
  375. hash_mod = SHA256
  376. elif group['sha'] == "SHA-384":
  377. hash_mod = SHA384
  378. elif group['sha'] == "SHA-512":
  379. hash_mod = SHA512
  380. else:
  381. raise ValueError("Unknown sha " + group['sha'])
  382. if group['mgfSha'] == "SHA-1":
  383. mgf = lambda x,y: MGF1(x, y, SHA1)
  384. elif group['mgfSha'] == "SHA-224":
  385. mgf = lambda x,y: MGF1(x, y, SHA224)
  386. elif group['mgfSha'] == "SHA-256":
  387. mgf = lambda x,y: MGF1(x, y, SHA256)
  388. elif group['mgfSha'] == "SHA-384":
  389. mgf = lambda x,y: MGF1(x, y, SHA384)
  390. elif group['mgfSha'] == "SHA-512":
  391. mgf = lambda x,y: MGF1(x, y, SHA512)
  392. else:
  393. raise ValueError("Unknown mgf/sha " + group['mgfSha'])
  394. for test in group['tests']:
  395. tv = TestVector()
  396. tv.rsa_key = rsa_key
  397. tv.hash_mod = hash_mod
  398. tv.mgf = mgf
  399. tv.algo = "%s with MGF1/%s" % (group['sha'], group['mgfSha'])
  400. tv.id = test['tcId']
  401. tv.comment = test['comment']
  402. for attr in 'msg', 'ct', 'label':
  403. setattr(tv, attr, unhexlify(test[attr]))
  404. tv.valid = test['result'] != "invalid"
  405. tv.warning = test['result'] == "acceptable"
  406. result.append(tv)
  407. return result
  408. def setUp(self):
  409. self.tv = []
  410. self.tv.extend(self.load_tests("rsa_oaep_2048_sha1_mgf1sha1_test.json"))
  411. self.tv.extend(self.load_tests("rsa_oaep_2048_sha224_mgf1sha1_test.json"))
  412. self.tv.extend(self.load_tests("rsa_oaep_2048_sha224_mgf1sha224_test.json"))
  413. self.tv.extend(self.load_tests("rsa_oaep_2048_sha256_mgf1sha1_test.json"))
  414. self.tv.extend(self.load_tests("rsa_oaep_2048_sha256_mgf1sha256_test.json"))
  415. self.tv.extend(self.load_tests("rsa_oaep_2048_sha384_mgf1sha1_test.json"))
  416. self.tv.extend(self.load_tests("rsa_oaep_2048_sha384_mgf1sha384_test.json"))
  417. self.tv.extend(self.load_tests("rsa_oaep_2048_sha512_mgf1sha1_test.json"))
  418. self.tv.extend(self.load_tests("rsa_oaep_2048_sha512_mgf1sha512_test.json"))
  419. if not self._skip_slow_tests:
  420. self.tv.extend(self.load_tests("rsa_oaep_3072_sha256_mgf1sha1_test.json"))
  421. self.tv.extend(self.load_tests("rsa_oaep_3072_sha256_mgf1sha256_test.json"))
  422. self.tv.extend(self.load_tests("rsa_oaep_3072_sha512_mgf1sha1_test.json"))
  423. self.tv.extend(self.load_tests("rsa_oaep_3072_sha512_mgf1sha512_test.json"))
  424. self.tv.extend(self.load_tests("rsa_oaep_4096_sha256_mgf1sha1_test.json"))
  425. self.tv.extend(self.load_tests("rsa_oaep_4096_sha256_mgf1sha256_test.json"))
  426. self.tv.extend(self.load_tests("rsa_oaep_4096_sha512_mgf1sha1_test.json"))
  427. self.tv.extend(self.load_tests("rsa_oaep_4096_sha512_mgf1sha512_test.json"))
  428. self.tv.extend(self.load_tests("rsa_oaep_4096_sha512_mgf1sha512_test.json"))
  429. self.tv.extend(self.load_tests("rsa_oaep_misc_test.json"))
  430. def shortDescription(self):
  431. return self._id
  432. def warn(self, tv):
  433. if tv.warning and self._wycheproof_warnings:
  434. import warnings
  435. warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment))
  436. def test_decrypt(self, tv):
  437. self._id = "Wycheproof Decrypt %s Test #%s" % (tv.algo, tv.id)
  438. cipher = PKCS.new(tv.rsa_key, hashAlgo=tv.hash_mod, mgfunc=tv.mgf, label=tv.label)
  439. try:
  440. pt = cipher.decrypt(tv.ct)
  441. except ValueError:
  442. assert not tv.valid
  443. else:
  444. assert tv.valid
  445. self.assertEqual(pt, tv.msg)
  446. self.warn(tv)
  447. def runTest(self):
  448. for tv in self.tv:
  449. self.test_decrypt(tv)
  450. def get_tests(config={}):
  451. skip_slow_tests = not config.get('slow_tests')
  452. wycheproof_warnings = config.get('wycheproof_warnings')
  453. tests = []
  454. tests += list_test_cases(PKCS1_OAEP_Tests)
  455. tests += [TestVectorsWycheproof(wycheproof_warnings, skip_slow_tests)]
  456. return tests
  457. if __name__ == '__main__':
  458. suite = lambda: unittest.TestSuite(get_tests())
  459. unittest.main(defaultTest='suite')
  460. # vim:set ts=4 sw=4 sts=4 expandtab: