Exemplo n.º 1
0
    def testInit(self):
        pubk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
        self.assertEquals(False, pubk.isInitialized())
        pubk.setExponent([0,1,2,3,4,5,6,7,8,9], 5, 5)
        self.assertEquals(False, pubk.isInitialized())
        pubk.setModulus([0,0,0,0,0]+[0,1,2,3,4,5,6,7]*16, 5, 128)
        self.assertEquals(True, pubk.isInitialized())

        pubk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
        self.assertEquals(False, pubk.isInitialized())
        pubk.setModulus([0,0,0,0,0]+[0,1,2,3,4,5,6,7]*16, 5, 128)
        self.assertEquals(False, pubk.isInitialized())
        pubk.setExponent([0,1,2,3,4,5,6,7,8,9], 5, 5)
        self.assertEquals(True, pubk.isInitialized())
Exemplo n.º 2
0
 def _pyCryptogenKeyPair(self):
     if pyCryptoRSA is None:
         raise CryptoException(CryptoException.NO_SUCH_ALGORITHM)
     if self._algorithm not in [self.ALG_RSA, self.ALG_RSA_CRT]:
         raise CryptoException(CryptoException.ILLEGAL_VALUE)
     keypair = pyCryptoRSA.generate(self._keylength)
     # fill in the public key components
     self._publicKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, self._keylength, False)
     self._publicKey.setTheKey(keypair)
     # private key
     if self._algorithm == self.ALG_RSA:
         self._privateKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE, self._keylength, False)
         self._privateKey.setTheKey(keypair)
     elif self._algorithm == self.ALG_RSA_CRT:
         self._privateKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_CRT_PRIVATE, self._keylength, False)
         self._privateKey.setTheKey(keypair)
Exemplo n.º 3
0
 def testGetters(self):
     pubk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
     try:
         pubk.getExponent([], 0)
         self.fail("Got exponent")
     except CryptoException, ce:
         self.assertEquals(CryptoException.UNINITIALIZED_KEY,
                          ce.getReason())
Exemplo n.º 4
0
 def testValue(self):
     pubk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
     pubk.setExponent([0,1,2,3,4,5,6,7,8,9], 5, 5)
     pubk.setModulus([65]*128, 0, 128)
     buf = []
     try:
         self.assertEquals(5, pubk.getExponent([], 7))
         self.fail()
     except ArrayIndexOutOfBoundsException:
         pass
     buf = [0 for i in range(20)]
     self.assertEquals(5, pubk.getExponent(buf, 7))
     self.assertEquals([5,6,7,8,9], buf[7:7+5])
Exemplo n.º 5
0
    def testDES(self):

        KeyArray = [1,2,3,4,5,6,7,8]
        bytBuffer = [0 for i in xrange(8)]
        MyBuffer = [7,5,6,8]

        MyDesKey = KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, False)
        crypt_des = Cipher.getInstance(Cipher.ALG_DES_ECB_PKCS5, False)

        MyDesKey.setKey(KeyArray, 0);
        crypt_des.init(MyDesKey, Cipher.MODE_ENCRYPT);
        length = crypt_des.doFinal(MyBuffer, 0, len(MyBuffer), bytBuffer, 0)
        crypt_des.init(MyDesKey, Cipher.MODE_DECRYPT);
        crypt_des.doFinal(bytBuffer, 0, length, MyBuffer, 0)

        self.assertEquals([7,5,6,8], MyBuffer)
Exemplo n.º 6
0
 def testinheritance(self):
     pubk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
     self.assertTrue(isinstance(pubk, RSAPublicKey))
     self.assertTrue(isinstance(pubk, PublicKey))
     self.assertTrue(isinstance(pubk, Key))
Exemplo n.º 7
0
    def testInit(self):
        c = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, False)
        self.assertEquals(Cipher.ALG_RSA_NOPAD, c.getAlgorithm())
        try:
            c.update([], 0, 0, [], 0)
            self.fail()
        except CryptoException, ce:
            self.assertEquals(CryptoException.INVALID_INIT, ce.getReason())
        try:
            c.init("abcd", Cipher.MODE_ENCRYPT)
            self.fail()
        except CryptoException, ce:
            self.assertEquals(CryptoException.ILLEGAL_VALUE, ce.getReason())

        pbk = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, False)
        try:
            c.init(pbk, Cipher.MODE_ENCRYPT)
            self.fail()
        except CryptoException, ce:
            self.assertEquals(CryptoException.UNINITIALIZED_KEY, ce.getReason())

        pbk.setExponent([0,1,2,3,4,5,6,7,8,9], 5, 5)
        pbk.setModulus([7]*128, 0, 128) # 1024 // 8

        c.init(pbk, Cipher.MODE_ENCRYPT)

    def testRSAEncryptDecrypt(self):
        kp = KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024)
        kp.genKeyPair()
        pubk = kp.getPublic()
Exemplo n.º 8
0
 def testLength(self):
     key = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_768, False)
     self.assertEquals(768, key.getSize())