home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / utils / RSAKey.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.8 KB  |  304 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Abstract class for RSA.'''
  5. from cryptomath import *
  6.  
  7. class RSAKey:
  8.     """This is an abstract base class for RSA keys.
  9.  
  10.     Particular implementations of RSA keys, such as
  11.     L{OpenSSL_RSAKey.OpenSSL_RSAKey},
  12.     L{Python_RSAKey.Python_RSAKey}, and
  13.     L{PyCrypto_RSAKey.PyCrypto_RSAKey},
  14.     inherit from this.
  15.  
  16.     To create or parse an RSA key, don't use one of these classes
  17.     directly.  Instead, use the factory functions in
  18.     L{tlslite.utils.keyfactory}.
  19.     """
  20.     
  21.     def __init__(self, n = 0, e = 0):
  22.         '''Create a new RSA key.
  23.  
  24.         If n and e are passed in, the new key will be initialized.
  25.  
  26.         @type n: int
  27.         @param n: RSA modulus.
  28.  
  29.         @type e: int
  30.         @param e: RSA public exponent.
  31.         '''
  32.         raise NotImplementedError()
  33.  
  34.     
  35.     def __len__(self):
  36.         '''Return the length of this key in bits.
  37.  
  38.         @rtype: int
  39.         '''
  40.         return numBits(self.n)
  41.  
  42.     
  43.     def hasPrivateKey(self):
  44.         '''Return whether or not this key has a private component.
  45.  
  46.         @rtype: bool
  47.         '''
  48.         raise NotImplementedError()
  49.  
  50.     
  51.     def hash(self):
  52.         '''Return the cryptoID <keyHash> value corresponding to this
  53.         key.
  54.  
  55.         @rtype: str
  56.         '''
  57.         raise NotImplementedError()
  58.  
  59.     
  60.     def getSigningAlgorithm(self):
  61.         '''Return the cryptoID sigAlgo value corresponding to this key.
  62.  
  63.         @rtype: str
  64.         '''
  65.         return 'pkcs1-sha1'
  66.  
  67.     
  68.     def hashAndSign(self, bytes):
  69.         '''Hash and sign the passed-in bytes.
  70.  
  71.         This requires the key to have a private component.  It performs
  72.         a PKCS1-SHA1 signature on the passed-in data.
  73.  
  74.         @type bytes: str or L{array.array} of unsigned bytes
  75.         @param bytes: The value which will be hashed and signed.
  76.  
  77.         @rtype: L{array.array} of unsigned bytes.
  78.         @return: A PKCS1-SHA1 signature on the passed-in data.
  79.         '''
  80.         if not isinstance(bytes, type('')):
  81.             bytes = bytesToString(bytes)
  82.         
  83.         hashBytes = stringToBytes(sha.sha(bytes).digest())
  84.         prefixedHashBytes = self._addPKCS1SHA1Prefix(hashBytes)
  85.         sigBytes = self.sign(prefixedHashBytes)
  86.         return sigBytes
  87.  
  88.     
  89.     def hashAndVerify(self, sigBytes, bytes):
  90.         '''Hash and verify the passed-in bytes with the signature.
  91.  
  92.         This verifies a PKCS1-SHA1 signature on the passed-in data.
  93.  
  94.         @type sigBytes: L{array.array} of unsigned bytes
  95.         @param sigBytes: A PKCS1-SHA1 signature.
  96.  
  97.         @type bytes: str or L{array.array} of unsigned bytes
  98.         @param bytes: The value which will be hashed and verified.
  99.  
  100.         @rtype: bool
  101.         @return: Whether the signature matches the passed-in data.
  102.         '''
  103.         if not isinstance(bytes, type('')):
  104.             bytes = bytesToString(bytes)
  105.         
  106.         hashBytes = stringToBytes(sha.sha(bytes).digest())
  107.         prefixedHashBytes = self._addPKCS1SHA1Prefix(hashBytes)
  108.         return self.verify(sigBytes, prefixedHashBytes)
  109.  
  110.     
  111.     def sign(self, bytes):
  112.         '''Sign the passed-in bytes.
  113.  
  114.         This requires the key to have a private component.  It performs
  115.         a PKCS1 signature on the passed-in data.
  116.  
  117.         @type bytes: L{array.array} of unsigned bytes
  118.         @param bytes: The value which will be signed.
  119.  
  120.         @rtype: L{array.array} of unsigned bytes.
  121.         @return: A PKCS1 signature on the passed-in data.
  122.         '''
  123.         if not self.hasPrivateKey():
  124.             raise AssertionError()
  125.         self.hasPrivateKey()
  126.         paddedBytes = self._addPKCS1Padding(bytes, 1)
  127.         m = bytesToNumber(paddedBytes)
  128.         if m >= self.n:
  129.             raise ValueError()
  130.         m >= self.n
  131.         c = self._rawPrivateKeyOp(m)
  132.         sigBytes = numberToBytes(c)
  133.         return sigBytes
  134.  
  135.     
  136.     def verify(self, sigBytes, bytes):
  137.         '''Verify the passed-in bytes with the signature.
  138.  
  139.         This verifies a PKCS1 signature on the passed-in data.
  140.  
  141.         @type sigBytes: L{array.array} of unsigned bytes
  142.         @param sigBytes: A PKCS1 signature.
  143.  
  144.         @type bytes: L{array.array} of unsigned bytes
  145.         @param bytes: The value which will be verified.
  146.  
  147.         @rtype: bool
  148.         @return: Whether the signature matches the passed-in data.
  149.         '''
  150.         paddedBytes = self._addPKCS1Padding(bytes, 1)
  151.         c = bytesToNumber(sigBytes)
  152.         if c >= self.n:
  153.             return False
  154.         m = self._rawPublicKeyOp(c)
  155.         checkBytes = numberToBytes(m)
  156.         return checkBytes == paddedBytes
  157.  
  158.     
  159.     def encrypt(self, bytes):
  160.         '''Encrypt the passed-in bytes.
  161.  
  162.         This performs PKCS1 encryption of the passed-in data.
  163.  
  164.         @type bytes: L{array.array} of unsigned bytes
  165.         @param bytes: The value which will be encrypted.
  166.  
  167.         @rtype: L{array.array} of unsigned bytes.
  168.         @return: A PKCS1 encryption of the passed-in data.
  169.         '''
  170.         paddedBytes = self._addPKCS1Padding(bytes, 2)
  171.         m = bytesToNumber(paddedBytes)
  172.         if m >= self.n:
  173.             raise ValueError()
  174.         m >= self.n
  175.         c = self._rawPublicKeyOp(m)
  176.         encBytes = numberToBytes(c)
  177.         return encBytes
  178.  
  179.     
  180.     def decrypt(self, encBytes):
  181.         '''Decrypt the passed-in bytes.
  182.  
  183.         This requires the key to have a private component.  It performs
  184.         PKCS1 decryption of the passed-in data.
  185.  
  186.         @type encBytes: L{array.array} of unsigned bytes
  187.         @param encBytes: The value which will be decrypted.
  188.  
  189.         @rtype: L{array.array} of unsigned bytes or None.
  190.         @return: A PKCS1 decryption of the passed-in data or None if
  191.         the data is not properly formatted.
  192.         '''
  193.         if not self.hasPrivateKey():
  194.             raise AssertionError()
  195.         self.hasPrivateKey()
  196.         c = bytesToNumber(encBytes)
  197.         if c >= self.n:
  198.             return None
  199.         m = self._rawPrivateKeyOp(c)
  200.         decBytes = numberToBytes(m)
  201.         if len(decBytes) != numBytes(self.n) - 1:
  202.             return None
  203.         if decBytes[0] != 2:
  204.             return None
  205.         for x in range(len(decBytes) - 1):
  206.             if decBytes[x] == 0:
  207.                 break
  208.                 continue
  209.             decBytes[0] != 2
  210.         else:
  211.             return None
  212.         return len(decBytes) != numBytes(self.n) - 1[x + 1:]
  213.  
  214.     
  215.     def _rawPrivateKeyOp(self, m):
  216.         raise NotImplementedError()
  217.  
  218.     
  219.     def _rawPublicKeyOp(self, c):
  220.         raise NotImplementedError()
  221.  
  222.     
  223.     def acceptsPassword(self):
  224.         '''Return True if the write() method accepts a password for use
  225.         in encrypting the private key.
  226.  
  227.         @rtype: bool
  228.         '''
  229.         raise NotImplementedError()
  230.  
  231.     
  232.     def write(self, password = None):
  233.         '''Return a string containing the key.
  234.  
  235.         @rtype: str
  236.         @return: A string describing the key, in whichever format (PEM
  237.         or XML) is native to the implementation.
  238.         '''
  239.         raise NotImplementedError()
  240.  
  241.     
  242.     def writeXMLPublicKey(self, indent = ''):
  243.         '''Return a string containing the key.
  244.  
  245.         @rtype: str
  246.         @return: A string describing the public key, in XML format.
  247.         '''
  248.         return Python_RSAKey(self.n, self.e).write(indent)
  249.  
  250.     
  251.     def generate(bits):
  252.         '''Generate a new key with the specified bit length.
  253.  
  254.         @rtype: L{tlslite.utils.RSAKey.RSAKey}
  255.         '''
  256.         raise NotImplementedError()
  257.  
  258.     generate = staticmethod(generate)
  259.     
  260.     def _addPKCS1SHA1Prefix(self, bytes):
  261.         prefixBytes = createByteArraySequence([
  262.             48,
  263.             33,
  264.             48,
  265.             9,
  266.             6,
  267.             5,
  268.             43,
  269.             14,
  270.             3,
  271.             2,
  272.             26,
  273.             5,
  274.             0,
  275.             4,
  276.             20])
  277.         prefixedBytes = prefixBytes + bytes
  278.         return prefixedBytes
  279.  
  280.     
  281.     def _addPKCS1Padding(self, bytes, blockType):
  282.         padLength = numBytes(self.n) - (len(bytes) + 3)
  283.         if blockType == 1:
  284.             pad = [
  285.                 255] * padLength
  286.         elif blockType == 2:
  287.             pad = createByteArraySequence([])
  288.             for b in padBytes:
  289.                 if b != 0:
  290.                     continue
  291.                 _[1][b]
  292.                 pad = _[1]
  293.                 pad = pad[:padLength]
  294.                 []
  295.         else:
  296.             raise AssertionError()
  297.         padding = []([
  298.             blockType] + pad + [
  299.             0])
  300.         paddedBytes = padding + bytes
  301.         return paddedBytes
  302.  
  303.  
  304.