_RSA.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # RSA.py : RSA encryption/decryption
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # Written by Andrew Kuchling, Paul Swartz, and others
  7. #
  8. # ===================================================================
  9. # The contents of this file are dedicated to the public domain. To
  10. # the extent that dedication to the public domain is not available,
  11. # everyone is granted a worldwide, perpetual, royalty-free,
  12. # non-exclusive license to exercise all rights associated with the
  13. # contents of this file for any purpose whatsoever.
  14. # No rights are reserved.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # ===================================================================
  25. #
  26. __revision__ = "$Id$"
  27. from Crypto.PublicKey import pubkey
  28. from Crypto.Util import number
  29. def generate_py(bits, randfunc, progress_func=None, e=65537):
  30. """generate(bits:int, randfunc:callable, progress_func:callable, e:int)
  31. Generate an RSA key of length 'bits', public exponent 'e'(which must be
  32. odd), using 'randfunc' to get random data and 'progress_func',
  33. if present, to display the progress of the key generation.
  34. """
  35. obj=RSAobj()
  36. obj.e = long(e)
  37. # Generate the prime factors of n
  38. if progress_func:
  39. progress_func('p,q\n')
  40. p = q = 1L
  41. while number.size(p*q) < bits:
  42. # Note that q might be one bit longer than p if somebody specifies an odd
  43. # number of bits for the key. (Why would anyone do that? You don't get
  44. # more security.)
  45. p = pubkey.getStrongPrime(bits>>1, obj.e, 1e-12, randfunc)
  46. q = pubkey.getStrongPrime(bits - (bits>>1), obj.e, 1e-12, randfunc)
  47. # It's OK for p to be larger than q, but let's be
  48. # kind to the function that will invert it for
  49. # th calculation of u.
  50. if p > q:
  51. (p, q)=(q, p)
  52. obj.p = p
  53. obj.q = q
  54. if progress_func:
  55. progress_func('u\n')
  56. obj.u = pubkey.inverse(obj.p, obj.q)
  57. obj.n = obj.p*obj.q
  58. if progress_func:
  59. progress_func('d\n')
  60. obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
  61. assert bits <= 1+obj.size(), "Generated key is too small"
  62. return obj
  63. class RSAobj(pubkey.pubkey):
  64. def size(self):
  65. """size() : int
  66. Return the maximum number of bits that can be handled by this key.
  67. """
  68. return number.size(self.n) - 1