You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

  1. --- a/lib/Crypto/PublicKey/ElGamal.py
  2. +++ b/lib/Crypto/PublicKey/ElGamal.py
  3. @@ -153,33 +153,33 @@ def generate(bits, randfunc, progress_fu
  4. if number.isPrime(obj.p, randfunc=randfunc):
  5. break
  6. # Generate generator g
  7. - # See Algorithm 4.80 in Handbook of Applied Cryptography
  8. - # Note that the order of the group is n=p-1=2q, where q is prime
  9. if progress_func:
  10. progress_func('g\n')
  11. while 1:
  12. + # Choose a square residue; it will generate a cyclic group of order q.
  13. + obj.g = pow(number.getRandomRange(2, obj.p, randfunc), 2, obj.p)
  14. +
  15. # We must avoid g=2 because of Bleichenbacher's attack described
  16. # in "Generating ElGamal signatures without knowning the secret key",
  17. # 1996
  18. - #
  19. - obj.g = number.getRandomRange(3, obj.p, randfunc)
  20. - safe = 1
  21. - if pow(obj.g, 2, obj.p)==1:
  22. - safe=0
  23. - if safe and pow(obj.g, q, obj.p)==1:
  24. - safe=0
  25. + if obj.g in (1, 2):
  26. + continue
  27. +
  28. # Discard g if it divides p-1 because of the attack described
  29. # in Note 11.67 (iii) in HAC
  30. - if safe and divmod(obj.p-1, obj.g)[1]==0:
  31. - safe=0
  32. + if (obj.p - 1) % obj.g == 0:
  33. + continue
  34. +
  35. # g^{-1} must not divide p-1 because of Khadir's attack
  36. # described in "Conditions of the generator for forging ElGamal
  37. # signature", 2011
  38. ginv = number.inverse(obj.g, obj.p)
  39. - if safe and divmod(obj.p-1, ginv)[1]==0:
  40. - safe=0
  41. - if safe:
  42. - break
  43. + if (obj.p - 1) % ginv == 0:
  44. + continue
  45. +
  46. + # Found
  47. + break
  48. +
  49. # Generate private key x
  50. if progress_func:
  51. progress_func('x\n')