Exemplo n.º 1
0
 def __init__(self, bArray, bOffset, bLength):
     self.rsa_KeyPair = KeyPair( KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024 )
     self.rsa_KeyPair.genKeyPair();
     self.rsa_PublicKey = self.rsa_KeyPair.getPublic();
     self.rsa_PrivateKey = self.rsa_KeyPair.getPrivate();
     self.cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, False);
     self.register(bArray, bOffset + 1, bArray[bOffset]);
Exemplo n.º 2
0
    def testRSASignVerify(self):
        kp = KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024)
        kp.genKeyPair()
        pubk = kp.getPublic()
        self.assertEquals(1024, pubk.getSize())
        privk = kp.getPrivate()
        self.assertEquals(1024, privk.getSize())

        c = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, False)
        c.init(privk, Cipher.MODE_ENCRYPT)
        res = [0]*1024
        l = c.doFinal([0,1,2,3,4,5], 0, 6, res, 0)

        c.init(pubk, Cipher.MODE_DECRYPT)
        res2 = [0]*1024
        l = c.doFinal(res, 0, l, res2, 0)

        self.assertEquals([0,1,2,3,4,5], res2[:l])
Exemplo n.º 3
0
class HandsonRSA2048EncryptDecrypt (Applet):
    """ actually 1024 ... we are testing not waiting ... """
    rsa_PrivateKey = None
    rsa_PublicKey = None
    rsa_KeyPair = None
    cipherRSA = None

    dataOffset = ISO7816.OFFSET_CDATA

    def __init__(self, bArray, bOffset, bLength):
        self.rsa_KeyPair = KeyPair( KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024 )
        self.rsa_KeyPair.genKeyPair();
        self.rsa_PublicKey = self.rsa_KeyPair.getPublic();
        self.rsa_PrivateKey = self.rsa_KeyPair.getPrivate();
        self.cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, False);
        self.register(bArray, bOffset + 1, bArray[bOffset]);

    @staticmethod
    def install(bArray, bOffset, bLength):
        HandsonRSA2048EncryptDecrypt(bArray, bOffset, bLength);

    def process(self, apdu):
        if self.selectingApplet():
            return

        buf = apdu.getBuffer();

        if buf[ISO7816.OFFSET_CLA] != 0:
            ISOException.throwIt(framework.ISO7816.SW_CLA_NOT_SUPPORTED)

        if buf[ISO7816.OFFSET_INS] != 0xAA:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED)

        try:
            action = {0x01: self.encryptRSA,
                      0x02: self.decryptRSA}
            action[buf[ISO7816.OFFSET_P1]](apdu)
            return
        except KeyError:
            ISOException.throwIt(ISO7816.SW_WRONG_P1P2)

    def encryptRSA(self, apdu):
        a = apdu.getBuffer()
        byteRead = apdu.setIncomingAndReceive()
        assert(self.rsa_PrivateKey.isInitialized())
        self.cipherRSA.init(self.rsa_PublicKey, Cipher.MODE_ENCRYPT)
        cyphertext = self.cipherRSA.doFinal(a, self.dataOffset, byteRead, a, self.dataOffset)

        apdu.setOutgoing();
        apdu.setOutgoingLength(cyphertext);
        apdu.sendBytesLong(a, self.dataOffset, cyphertext);

    def decryptRSA(self, apdu):
        a = apdu.getBuffer()
        byteRead = apdu.setIncomingAndReceive()
        self.cipherRSA.init(self.rsa_PrivateKey, Cipher.MODE_DECRYPT)
        textlength = self.cipherRSA.doFinal(a, self.dataOffset, byteRead, a, self.dataOffset)

        apdu.setOutgoing()
        apdu.setOutgoingLength(textlength )
        apdu.sendBytesLong(a, self.dataOffset, textlength )