Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

695 linhas
24 KiB

  1. from __future__ import print_function
  2. import sys
  3. import hypothesis.strategies as st
  4. from hypothesis import given, settings, note, example
  5. try:
  6. import unittest2 as unittest
  7. except ImportError:
  8. import unittest
  9. import pytest
  10. from .ecdsa import (
  11. Private_key,
  12. Public_key,
  13. Signature,
  14. generator_192,
  15. digest_integer,
  16. ellipticcurve,
  17. point_is_valid,
  18. generator_224,
  19. generator_256,
  20. generator_384,
  21. generator_521,
  22. generator_secp256k1,
  23. curve_192,
  24. InvalidPointError,
  25. curve_112r2,
  26. generator_112r2,
  27. int_to_string,
  28. )
  29. from .ellipticcurve import Point
  30. HYP_SETTINGS = {}
  31. # old hypothesis doesn't have the "deadline" setting
  32. if sys.version_info > (2, 7): # pragma: no branch
  33. # SEC521p is slow, allow long execution for it
  34. HYP_SETTINGS["deadline"] = 5000
  35. class TestP192FromX9_62(unittest.TestCase):
  36. """Check test vectors from X9.62"""
  37. @classmethod
  38. def setUpClass(cls):
  39. cls.d = 651056770906015076056810763456358567190100156695615665659
  40. cls.Q = cls.d * generator_192
  41. cls.k = 6140507067065001063065065565667405560006161556565665656654
  42. cls.R = cls.k * generator_192
  43. cls.msg = 968236873715988614170569073515315707566766479517
  44. cls.pubk = Public_key(generator_192, generator_192 * cls.d)
  45. cls.privk = Private_key(cls.pubk, cls.d)
  46. cls.sig = cls.privk.sign(cls.msg, cls.k)
  47. def test_point_multiplication(self):
  48. assert self.Q.x() == 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5
  49. def test_point_multiplication_2(self):
  50. assert self.R.x() == 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD
  51. assert self.R.y() == 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835
  52. def test_mult_and_addition(self):
  53. u1 = 2563697409189434185194736134579731015366492496392189760599
  54. u2 = 6266643813348617967186477710235785849136406323338782220568
  55. temp = u1 * generator_192 + u2 * self.Q
  56. assert temp.x() == 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD
  57. assert temp.y() == 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835
  58. def test_signature(self):
  59. r, s = self.sig.r, self.sig.s
  60. assert r == 3342403536405981729393488334694600415596881826869351677613
  61. assert s == 5735822328888155254683894997897571951568553642892029982342
  62. def test_verification(self):
  63. assert self.pubk.verifies(self.msg, self.sig)
  64. def test_rejection(self):
  65. assert not self.pubk.verifies(self.msg - 1, self.sig)
  66. def test_verification_with_regular_point(self):
  67. pubk = Public_key(
  68. Point(
  69. generator_192.curve(),
  70. generator_192.x(),
  71. generator_192.y(),
  72. generator_192.order(),
  73. ),
  74. self.pubk.point,
  75. )
  76. assert pubk.verifies(self.msg, self.sig)
  77. class TestPublicKey(unittest.TestCase):
  78. def test_equality_public_keys(self):
  79. gen = generator_192
  80. x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  81. y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  82. point = ellipticcurve.Point(gen.curve(), x, y)
  83. pub_key1 = Public_key(gen, point)
  84. pub_key2 = Public_key(gen, point)
  85. self.assertEqual(pub_key1, pub_key2)
  86. def test_inequality_public_key(self):
  87. gen = generator_192
  88. x1 = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  89. y1 = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  90. point1 = ellipticcurve.Point(gen.curve(), x1, y1)
  91. x2 = 0x6A223D00BD22C52833409A163E057E5B5DA1DEF2A197DD15
  92. y2 = 0x7B482604199367F1F303F9EF627F922F97023E90EAE08ABF
  93. point2 = ellipticcurve.Point(gen.curve(), x2, y2)
  94. pub_key1 = Public_key(gen, point1)
  95. pub_key2 = Public_key(gen, point2)
  96. self.assertNotEqual(pub_key1, pub_key2)
  97. def test_inequality_different_curves(self):
  98. gen = generator_192
  99. x1 = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  100. y1 = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  101. point1 = ellipticcurve.Point(gen.curve(), x1, y1)
  102. x2 = 0x722BA0FB6B8FC8898A4C6AB49E66
  103. y2 = 0x2B7344BB57A7ABC8CA0F1A398C7D
  104. point2 = ellipticcurve.Point(generator_112r2.curve(), x2, y2)
  105. pub_key1 = Public_key(gen, point1)
  106. pub_key2 = Public_key(generator_112r2, point2)
  107. self.assertNotEqual(pub_key1, pub_key2)
  108. def test_inequality_public_key_not_implemented(self):
  109. gen = generator_192
  110. x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  111. y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  112. point = ellipticcurve.Point(gen.curve(), x, y)
  113. pub_key = Public_key(gen, point)
  114. self.assertNotEqual(pub_key, None)
  115. def test_public_key_with_generator_without_order(self):
  116. gen = ellipticcurve.PointJacobi(
  117. generator_192.curve(), generator_192.x(), generator_192.y(), 1
  118. )
  119. x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  120. y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  121. point = ellipticcurve.Point(gen.curve(), x, y)
  122. with self.assertRaises(InvalidPointError) as e:
  123. Public_key(gen, point)
  124. self.assertIn("Generator point must have order", str(e.exception))
  125. def test_public_point_on_curve_not_scalar_multiple_of_base_point(self):
  126. x = 2
  127. y = 0xBE6AA4938EF7CFE6FE29595B6B00
  128. # we need a curve with cofactor != 1
  129. point = ellipticcurve.PointJacobi(curve_112r2, x, y, 1)
  130. self.assertTrue(curve_112r2.contains_point(x, y))
  131. with self.assertRaises(InvalidPointError) as e:
  132. Public_key(generator_112r2, point)
  133. self.assertIn("Generator point order", str(e.exception))
  134. def test_point_is_valid_with_not_scalar_multiple_of_base_point(self):
  135. x = 2
  136. y = 0xBE6AA4938EF7CFE6FE29595B6B00
  137. self.assertFalse(point_is_valid(generator_112r2, x, y))
  138. # the tests to verify the extensiveness of tests in ecdsa.ecdsa
  139. # if PointJacobi gets modified to calculate the x and y mod p the tests
  140. # below will need to use a fake/mock object
  141. def test_invalid_point_x_negative(self):
  142. pt = ellipticcurve.PointJacobi(curve_192, -1, 0, 1)
  143. with self.assertRaises(InvalidPointError) as e:
  144. Public_key(generator_192, pt)
  145. self.assertIn("The public point has x or y", str(e.exception))
  146. def test_invalid_point_x_equal_p(self):
  147. pt = ellipticcurve.PointJacobi(curve_192, curve_192.p(), 0, 1)
  148. with self.assertRaises(InvalidPointError) as e:
  149. Public_key(generator_192, pt)
  150. self.assertIn("The public point has x or y", str(e.exception))
  151. def test_invalid_point_y_negative(self):
  152. pt = ellipticcurve.PointJacobi(curve_192, 0, -1, 1)
  153. with self.assertRaises(InvalidPointError) as e:
  154. Public_key(generator_192, pt)
  155. self.assertIn("The public point has x or y", str(e.exception))
  156. def test_invalid_point_y_equal_p(self):
  157. pt = ellipticcurve.PointJacobi(curve_192, 0, curve_192.p(), 1)
  158. with self.assertRaises(InvalidPointError) as e:
  159. Public_key(generator_192, pt)
  160. self.assertIn("The public point has x or y", str(e.exception))
  161. class TestPublicKeyVerifies(unittest.TestCase):
  162. # test all the different ways that a signature can be publicly invalid
  163. @classmethod
  164. def setUpClass(cls):
  165. gen = generator_192
  166. x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  167. y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  168. point = ellipticcurve.Point(gen.curve(), x, y)
  169. cls.pub_key = Public_key(gen, point)
  170. def test_sig_with_r_zero(self):
  171. sig = Signature(0, 1)
  172. self.assertFalse(self.pub_key.verifies(1, sig))
  173. def test_sig_with_r_order(self):
  174. sig = Signature(generator_192.order(), 1)
  175. self.assertFalse(self.pub_key.verifies(1, sig))
  176. def test_sig_with_s_zero(self):
  177. sig = Signature(1, 0)
  178. self.assertFalse(self.pub_key.verifies(1, sig))
  179. def test_sig_with_s_order(self):
  180. sig = Signature(1, generator_192.order())
  181. self.assertFalse(self.pub_key.verifies(1, sig))
  182. class TestPrivateKey(unittest.TestCase):
  183. @classmethod
  184. def setUpClass(cls):
  185. gen = generator_192
  186. x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
  187. y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
  188. point = ellipticcurve.Point(gen.curve(), x, y)
  189. cls.pub_key = Public_key(gen, point)
  190. def test_equality_private_keys(self):
  191. pr_key1 = Private_key(self.pub_key, 100)
  192. pr_key2 = Private_key(self.pub_key, 100)
  193. self.assertEqual(pr_key1, pr_key2)
  194. def test_inequality_private_keys(self):
  195. pr_key1 = Private_key(self.pub_key, 100)
  196. pr_key2 = Private_key(self.pub_key, 200)
  197. self.assertNotEqual(pr_key1, pr_key2)
  198. def test_inequality_private_keys_not_implemented(self):
  199. pr_key = Private_key(self.pub_key, 100)
  200. self.assertNotEqual(pr_key, None)
  201. # Testing point validity, as per ECDSAVS.pdf B.2.2:
  202. P192_POINTS = [
  203. (
  204. generator_192,
  205. 0xCD6D0F029A023E9AACA429615B8F577ABEE685D8257CC83A,
  206. 0x00019C410987680E9FB6C0B6ECC01D9A2647C8BAE27721BACDFC,
  207. False,
  208. ),
  209. (
  210. generator_192,
  211. 0x00017F2FCE203639E9EAF9FB50B81FC32776B30E3B02AF16C73B,
  212. 0x95DA95C5E72DD48E229D4748D4EEE658A9A54111B23B2ADB,
  213. False,
  214. ),
  215. (
  216. generator_192,
  217. 0x4F77F8BC7FCCBADD5760F4938746D5F253EE2168C1CF2792,
  218. 0x000147156FF824D131629739817EDB197717C41AAB5C2A70F0F6,
  219. False,
  220. ),
  221. (
  222. generator_192,
  223. 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6,
  224. 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F,
  225. True,
  226. ),
  227. (
  228. generator_192,
  229. 0xCDF56C1AA3D8AFC53C521ADF3FFB96734A6A630A4A5B5A70,
  230. 0x97C1C44A5FB229007B5EC5D25F7413D170068FFD023CAA4E,
  231. True,
  232. ),
  233. (
  234. generator_192,
  235. 0x89009C0DC361C81E99280C8E91DF578DF88CDF4B0CDEDCED,
  236. 0x27BE44A529B7513E727251F128B34262A0FD4D8EC82377B9,
  237. True,
  238. ),
  239. (
  240. generator_192,
  241. 0x6A223D00BD22C52833409A163E057E5B5DA1DEF2A197DD15,
  242. 0x7B482604199367F1F303F9EF627F922F97023E90EAE08ABF,
  243. True,
  244. ),
  245. (
  246. generator_192,
  247. 0x6DCCBDE75C0948C98DAB32EA0BC59FE125CF0FB1A3798EDA,
  248. 0x0001171A3E0FA60CF3096F4E116B556198DE430E1FBD330C8835,
  249. False,
  250. ),
  251. (
  252. generator_192,
  253. 0xD266B39E1F491FC4ACBBBC7D098430931CFA66D55015AF12,
  254. 0x193782EB909E391A3148B7764E6B234AA94E48D30A16DBB2,
  255. False,
  256. ),
  257. (
  258. generator_192,
  259. 0x9D6DDBCD439BAA0C6B80A654091680E462A7D1D3F1FFEB43,
  260. 0x6AD8EFC4D133CCF167C44EB4691C80ABFFB9F82B932B8CAA,
  261. False,
  262. ),
  263. (
  264. generator_192,
  265. 0x146479D944E6BDA87E5B35818AA666A4C998A71F4E95EDBC,
  266. 0xA86D6FE62BC8FBD88139693F842635F687F132255858E7F6,
  267. False,
  268. ),
  269. (
  270. generator_192,
  271. 0xE594D4A598046F3598243F50FD2C7BD7D380EDB055802253,
  272. 0x509014C0C4D6B536E3CA750EC09066AF39B4C8616A53A923,
  273. False,
  274. ),
  275. ]
  276. @pytest.mark.parametrize("generator,x,y,expected", P192_POINTS)
  277. def test_point_validity(generator, x, y, expected):
  278. """
  279. `generator` defines the curve; is `(x, y)` a point on
  280. this curve? `expected` is True if the right answer is Yes.
  281. """
  282. assert point_is_valid(generator, x, y) == expected
  283. # Trying signature-verification tests from ECDSAVS.pdf B.2.4:
  284. CURVE_192_KATS = [
  285. (
  286. generator_192,
  287. int(
  288. "0x84ce72aa8699df436059f052ac51b6398d2511e49631bcb7e71f89c499b9ee"
  289. "425dfbc13a5f6d408471b054f2655617cbbaf7937b7c80cd8865cf02c8487d30"
  290. "d2b0fbd8b2c4e102e16d828374bbc47b93852f212d5043c3ea720f086178ff79"
  291. "8cc4f63f787b9c2e419efa033e7644ea7936f54462dc21a6c4580725f7f0e7d1"
  292. "58",
  293. 16,
  294. ),
  295. 0xD9DBFB332AA8E5FF091E8CE535857C37C73F6250FFB2E7AC,
  296. 0x282102E364FEDED3AD15DDF968F88D8321AA268DD483EBC4,
  297. 0x64DCA58A20787C488D11D6DD96313F1B766F2D8EFE122916,
  298. 0x1ECBA28141E84AB4ECAD92F56720E2CC83EB3D22DEC72479,
  299. True,
  300. ),
  301. (
  302. generator_192,
  303. int(
  304. "0x94bb5bacd5f8ea765810024db87f4224ad71362a3c28284b2b9f39fab86db1"
  305. "2e8beb94aae899768229be8fdb6c4f12f28912bb604703a79ccff769c1607f5a"
  306. "91450f30ba0460d359d9126cbd6296be6d9c4bb96c0ee74cbb44197c207f6db3"
  307. "26ab6f5a659113a9034e54be7b041ced9dcf6458d7fb9cbfb2744d999f7dfd63"
  308. "f4",
  309. 16,
  310. ),
  311. 0x3E53EF8D3112AF3285C0E74842090712CD324832D4277AE7,
  312. 0xCC75F8952D30AEC2CBB719FC6AA9934590B5D0FF5A83ADB7,
  313. 0x8285261607283BA18F335026130BAB31840DCFD9C3E555AF,
  314. 0x356D89E1B04541AFC9704A45E9C535CE4A50929E33D7E06C,
  315. True,
  316. ),
  317. (
  318. generator_192,
  319. int(
  320. "0xf6227a8eeb34afed1621dcc89a91d72ea212cb2f476839d9b4243c66877911"
  321. "b37b4ad6f4448792a7bbba76c63bdd63414b6facab7dc71c3396a73bd7ee14cd"
  322. "d41a659c61c99b779cecf07bc51ab391aa3252386242b9853ea7da67fd768d30"
  323. "3f1b9b513d401565b6f1eb722dfdb96b519fe4f9bd5de67ae131e64b40e78c42"
  324. "dd",
  325. 16,
  326. ),
  327. 0x16335DBE95F8E8254A4E04575D736BEFB258B8657F773CB7,
  328. 0x421B13379C59BC9DCE38A1099CA79BBD06D647C7F6242336,
  329. 0x4141BD5D64EA36C5B0BD21EF28C02DA216ED9D04522B1E91,
  330. 0x159A6AA852BCC579E821B7BB0994C0861FB08280C38DAA09,
  331. False,
  332. ),
  333. (
  334. generator_192,
  335. int(
  336. "0x16b5f93afd0d02246f662761ed8e0dd9504681ed02a253006eb36736b56309"
  337. "7ba39f81c8e1bce7a16c1339e345efabbc6baa3efb0612948ae51103382a8ee8"
  338. "bc448e3ef71e9f6f7a9676694831d7f5dd0db5446f179bcb737d4a526367a447"
  339. "bfe2c857521c7f40b6d7d7e01a180d92431fb0bbd29c04a0c420a57b3ed26ccd"
  340. "8a",
  341. 16,
  342. ),
  343. 0xFD14CDF1607F5EFB7B1793037B15BDF4BAA6F7C16341AB0B,
  344. 0x83FA0795CC6C4795B9016DAC928FD6BAC32F3229A96312C4,
  345. 0x8DFDB832951E0167C5D762A473C0416C5C15BC1195667DC1,
  346. 0x1720288A2DC13FA1EC78F763F8FE2FF7354A7E6FDDE44520,
  347. False,
  348. ),
  349. (
  350. generator_192,
  351. int(
  352. "0x08a2024b61b79d260e3bb43ef15659aec89e5b560199bc82cf7c65c77d3919"
  353. "2e03b9a895d766655105edd9188242b91fbde4167f7862d4ddd61e5d4ab55196"
  354. "683d4f13ceb90d87aea6e07eb50a874e33086c4a7cb0273a8e1c4408f4b846bc"
  355. "eae1ebaac1b2b2ea851a9b09de322efe34cebe601653efd6ddc876ce8c2f2072"
  356. "fb",
  357. 16,
  358. ),
  359. 0x674F941DC1A1F8B763C9334D726172D527B90CA324DB8828,
  360. 0x65ADFA32E8B236CB33A3E84CF59BFB9417AE7E8EDE57A7FF,
  361. 0x9508B9FDD7DAF0D8126F9E2BC5A35E4C6D800B5B804D7796,
  362. 0x36F2BF6B21B987C77B53BB801B3435A577E3D493744BFAB0,
  363. False,
  364. ),
  365. (
  366. generator_192,
  367. int(
  368. "0x1843aba74b0789d4ac6b0b8923848023a644a7b70afa23b1191829bbe4397c"
  369. "e15b629bf21a8838298653ed0c19222b95fa4f7390d1b4c844d96e645537e0aa"
  370. "e98afb5c0ac3bd0e4c37f8daaff25556c64e98c319c52687c904c4de7240a1cc"
  371. "55cd9756b7edaef184e6e23b385726e9ffcba8001b8f574987c1a3fedaaa83ca"
  372. "6d",
  373. 16,
  374. ),
  375. 0x10ECCA1AAD7220B56A62008B35170BFD5E35885C4014A19F,
  376. 0x04EB61984C6C12ADE3BC47F3C629ECE7AA0A033B9948D686,
  377. 0x82BFA4E82C0DFE9274169B86694E76CE993FD83B5C60F325,
  378. 0xA97685676C59A65DBDE002FE9D613431FB183E8006D05633,
  379. False,
  380. ),
  381. (
  382. generator_192,
  383. int(
  384. "0x5a478f4084ddd1a7fea038aa9732a822106385797d02311aeef4d0264f824f"
  385. "698df7a48cfb6b578cf3da416bc0799425bb491be5b5ecc37995b85b03420a98"
  386. "f2c4dc5c31a69a379e9e322fbe706bbcaf0f77175e05cbb4fa162e0da82010a2"
  387. "78461e3e974d137bc746d1880d6eb02aa95216014b37480d84b87f717bb13f76"
  388. "e1",
  389. 16,
  390. ),
  391. 0x6636653CB5B894CA65C448277B29DA3AD101C4C2300F7C04,
  392. 0xFDF1CBB3FC3FD6A4F890B59E554544175FA77DBDBEB656C1,
  393. 0xEAC2DDECDDFB79931A9C3D49C08DE0645C783A24CB365E1C,
  394. 0x3549FEE3CFA7E5F93BC47D92D8BA100E881A2A93C22F8D50,
  395. False,
  396. ),
  397. (
  398. generator_192,
  399. int(
  400. "0xc598774259a058fa65212ac57eaa4f52240e629ef4c310722088292d1d4af6"
  401. "c39b49ce06ba77e4247b20637174d0bd67c9723feb57b5ead232b47ea452d5d7"
  402. "a089f17c00b8b6767e434a5e16c231ba0efa718a340bf41d67ea2d295812ff1b"
  403. "9277daacb8bc27b50ea5e6443bcf95ef4e9f5468fe78485236313d53d1c68f6b"
  404. "a2",
  405. 16,
  406. ),
  407. 0xA82BD718D01D354001148CD5F69B9EBF38FF6F21898F8AAA,
  408. 0xE67CEEDE07FC2EBFAFD62462A51E4B6C6B3D5B537B7CAF3E,
  409. 0x4D292486C620C3DE20856E57D3BB72FCDE4A73AD26376955,
  410. 0xA85289591A6081D5728825520E62FF1C64F94235C04C7F95,
  411. False,
  412. ),
  413. (
  414. generator_192,
  415. int(
  416. "0xca98ed9db081a07b7557f24ced6c7b9891269a95d2026747add9e9eb80638a"
  417. "961cf9c71a1b9f2c29744180bd4c3d3db60f2243c5c0b7cc8a8d40a3f9a7fc91"
  418. "0250f2187136ee6413ffc67f1a25e1c4c204fa9635312252ac0e0481d89b6d53"
  419. "808f0c496ba87631803f6c572c1f61fa049737fdacce4adff757afed4f05beb6"
  420. "58",
  421. 16,
  422. ),
  423. 0x7D3B016B57758B160C4FCA73D48DF07AE3B6B30225126C2F,
  424. 0x4AF3790D9775742BDE46F8DA876711BE1B65244B2B39E7EC,
  425. 0x95F778F5F656511A5AB49A5D69DDD0929563C29CBC3A9E62,
  426. 0x75C87FC358C251B4C83D2DD979FAAD496B539F9F2EE7A289,
  427. False,
  428. ),
  429. (
  430. generator_192,
  431. int(
  432. "0x31dd9a54c8338bea06b87eca813d555ad1850fac9742ef0bbe40dad400e102"
  433. "88acc9c11ea7dac79eb16378ebea9490e09536099f1b993e2653cd50240014c9"
  434. "0a9c987f64545abc6a536b9bd2435eb5e911fdfde2f13be96ea36ad38df4ae9e"
  435. "a387b29cced599af777338af2794820c9cce43b51d2112380a35802ab7e396c9"
  436. "7a",
  437. 16,
  438. ),
  439. 0x9362F28C4EF96453D8A2F849F21E881CD7566887DA8BEB4A,
  440. 0xE64D26D8D74C48A024AE85D982EE74CD16046F4EE5333905,
  441. 0xF3923476A296C88287E8DE914B0B324AD5A963319A4FE73B,
  442. 0xF0BAEED7624ED00D15244D8BA2AEDE085517DBDEC8AC65F5,
  443. True,
  444. ),
  445. (
  446. generator_192,
  447. int(
  448. "0xb2b94e4432267c92f9fdb9dc6040c95ffa477652761290d3c7de312283f645"
  449. "0d89cc4aabe748554dfb6056b2d8e99c7aeaad9cdddebdee9dbc099839562d90"
  450. "64e68e7bb5f3a6bba0749ca9a538181fc785553a4000785d73cc207922f63e8c"
  451. "e1112768cb1de7b673aed83a1e4a74592f1268d8e2a4e9e63d414b5d442bd045"
  452. "6d",
  453. 16,
  454. ),
  455. 0xCC6FC032A846AAAC25533EB033522824F94E670FA997ECEF,
  456. 0xE25463EF77A029ECCDA8B294FD63DD694E38D223D30862F1,
  457. 0x066B1D07F3A40E679B620EDA7F550842A35C18B80C5EBE06,
  458. 0xA0B0FB201E8F2DF65E2C4508EF303BDC90D934016F16B2DC,
  459. False,
  460. ),
  461. (
  462. generator_192,
  463. int(
  464. "0x4366fcadf10d30d086911de30143da6f579527036937007b337f7282460eae"
  465. "5678b15cccda853193ea5fc4bc0a6b9d7a31128f27e1214988592827520b214e"
  466. "ed5052f7775b750b0c6b15f145453ba3fee24a085d65287e10509eb5d5f602c4"
  467. "40341376b95c24e5c4727d4b859bfe1483d20538acdd92c7997fa9c614f0f839"
  468. "d7",
  469. 16,
  470. ),
  471. 0x955C908FE900A996F7E2089BEE2F6376830F76A19135E753,
  472. 0xBA0C42A91D3847DE4A592A46DC3FDAF45A7CC709B90DE520,
  473. 0x1F58AD77FC04C782815A1405B0925E72095D906CBF52A668,
  474. 0xF2E93758B3AF75EDF784F05A6761C9B9A6043C66B845B599,
  475. False,
  476. ),
  477. (
  478. generator_192,
  479. int(
  480. "0x543f8af57d750e33aa8565e0cae92bfa7a1ff78833093421c2942cadf99866"
  481. "70a5ff3244c02a8225e790fbf30ea84c74720abf99cfd10d02d34377c3d3b412"
  482. "69bea763384f372bb786b5846f58932defa68023136cd571863b304886e95e52"
  483. "e7877f445b9364b3f06f3c28da12707673fecb4b8071de06b6e0a3c87da160ce"
  484. "f3",
  485. 16,
  486. ),
  487. 0x31F7FA05576D78A949B24812D4383107A9A45BB5FCCDD835,
  488. 0x8DC0EB65994A90F02B5E19BD18B32D61150746C09107E76B,
  489. 0xBE26D59E4E883DDE7C286614A767B31E49AD88789D3A78FF,
  490. 0x8762CA831C1CE42DF77893C9B03119428E7A9B819B619068,
  491. False,
  492. ),
  493. (
  494. generator_192,
  495. int(
  496. "0xd2e8454143ce281e609a9d748014dcebb9d0bc53adb02443a6aac2ffe6cb009f"
  497. "387c346ecb051791404f79e902ee333ad65e5c8cb38dc0d1d39a8dc90add502357"
  498. "2720e5b94b190d43dd0d7873397504c0c7aef2727e628eb6a74411f2e400c65670"
  499. "716cb4a815dc91cbbfeb7cfe8c929e93184c938af2c078584da045e8f8d1",
  500. 16,
  501. ),
  502. 0x66AA8EDBBDB5CF8E28CEB51B5BDA891CAE2DF84819FE25C0,
  503. 0x0C6BC2F69030A7CE58D4A00E3B3349844784A13B8936F8DA,
  504. 0xA4661E69B1734F4A71B788410A464B71E7FFE42334484F23,
  505. 0x738421CF5E049159D69C57A915143E226CAC8355E149AFE9,
  506. False,
  507. ),
  508. (
  509. generator_192,
  510. int(
  511. "0x6660717144040f3e2f95a4e25b08a7079c702a8b29babad5a19a87654bc5c5af"
  512. "a261512a11b998a4fb36b5d8fe8bd942792ff0324b108120de86d63f65855e5461"
  513. "184fc96a0a8ffd2ce6d5dfb0230cbbdd98f8543e361b3205f5da3d500fdc8bac6d"
  514. "b377d75ebef3cb8f4d1ff738071ad0938917889250b41dd1d98896ca06fb",
  515. 16,
  516. ),
  517. 0xBCFACF45139B6F5F690A4C35A5FFFA498794136A2353FC77,
  518. 0x6F4A6C906316A6AFC6D98FE1F0399D056F128FE0270B0F22,
  519. 0x9DB679A3DAFE48F7CCAD122933ACFE9DA0970B71C94C21C1,
  520. 0x984C2DB99827576C0A41A5DA41E07D8CC768BC82F18C9DA9,
  521. False,
  522. ),
  523. ]
  524. @pytest.mark.parametrize("gen,msg,qx,qy,r,s,expected", CURVE_192_KATS)
  525. def test_signature_validity(gen, msg, qx, qy, r, s, expected):
  526. """
  527. `msg` = message, `qx` and `qy` represent the base point on
  528. elliptic curve of `gen`, `r` and `s` are the signature, and
  529. `expected` is True iff the signature is expected to be valid."""
  530. pubk = Public_key(gen, ellipticcurve.Point(gen.curve(), qx, qy))
  531. with pytest.warns(DeprecationWarning) as warns:
  532. msg_dgst = digest_integer(msg)
  533. assert len(warns) == 3
  534. assert "unused" in warns[0].message.args[0]
  535. assert "unused" in warns[1].message.args[0]
  536. assert "unused" in warns[2].message.args[0]
  537. assert expected == pubk.verifies(msg_dgst, Signature(r, s))
  538. @pytest.mark.parametrize(
  539. "gen,msg,qx,qy,r,s,expected", [x for x in CURVE_192_KATS if x[6]]
  540. )
  541. def test_pk_recovery(gen, msg, r, s, qx, qy, expected):
  542. del expected
  543. sign = Signature(r, s)
  544. with pytest.warns(DeprecationWarning) as warns:
  545. msg_dgst = digest_integer(msg)
  546. assert len(warns) == 3
  547. assert "unused" in warns[0].message.args[0]
  548. assert "unused" in warns[1].message.args[0]
  549. assert "unused" in warns[2].message.args[0]
  550. pks = sign.recover_public_keys(msg_dgst, gen)
  551. assert pks
  552. # Test if the signature is valid for all found public keys
  553. for pk in pks:
  554. q = pk.point
  555. test_signature_validity(gen, msg, q.x(), q.y(), r, s, True)
  556. # Test if the original public key is in the set of found keys
  557. original_q = ellipticcurve.Point(gen.curve(), qx, qy)
  558. points = [pk.point for pk in pks]
  559. assert original_q in points
  560. @st.composite
  561. def st_random_gen_key_msg_nonce(draw):
  562. """Hypothesis strategy for test_sig_verify()."""
  563. name_gen = {
  564. "generator_192": generator_192,
  565. "generator_224": generator_224,
  566. "generator_256": generator_256,
  567. "generator_secp256k1": generator_secp256k1,
  568. "generator_384": generator_384,
  569. "generator_521": generator_521,
  570. }
  571. name = draw(st.sampled_from(sorted(name_gen.keys())))
  572. note("Generator used: {0}".format(name))
  573. generator = name_gen[name]
  574. order = int(generator.order()) - 1
  575. key = draw(st.integers(min_value=1, max_value=order))
  576. msg = draw(st.integers(min_value=1, max_value=order))
  577. nonce = draw(
  578. st.integers(min_value=1, max_value=order)
  579. | st.integers(min_value=order >> 1, max_value=order)
  580. )
  581. return generator, key, msg, nonce
  582. SIG_VER_SETTINGS = dict(HYP_SETTINGS)
  583. if "--fast" in sys.argv: # pragma: no cover
  584. SIG_VER_SETTINGS["max_examples"] = 1
  585. else:
  586. SIG_VER_SETTINGS["max_examples"] = 10
  587. @settings(**SIG_VER_SETTINGS)
  588. @example((generator_224, 4, 1, 1))
  589. @given(st_random_gen_key_msg_nonce())
  590. def test_sig_verify(args):
  591. """
  592. Check if signing and verification works for arbitrary messages and
  593. that signatures for other messages are rejected.
  594. """
  595. generator, sec_mult, msg, nonce = args
  596. pubkey = Public_key(generator, generator * sec_mult)
  597. privkey = Private_key(pubkey, sec_mult)
  598. signature = privkey.sign(msg, nonce)
  599. assert pubkey.verifies(msg, signature)
  600. assert not pubkey.verifies(msg - 1, signature)
  601. def test_int_to_string_with_zero():
  602. with pytest.warns(DeprecationWarning) as warns:
  603. assert int_to_string(0) == b"\x00"
  604. assert len(warns) == 1
  605. assert "unused" in warns[0].message.args[0]