Exemple #1
0
    def doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset):
        Cipher.doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset)

        data = [0 for i in xrange(inLength)]
        Util.arrayCopy(inBuff, inOffset, data, 0, inLength)
        if ((self.algorithm == self.ALG_RSA_PKCS1) and
            (self.mode == self.MODE_ENCRYPT)):
            data = self.EME_PKCS1_v1_5_enc(data)

        if len(data) != (self._theKey.getSize() // 8):
            raise CryptoException(CryptoException.ILLEGAL_VALUE)

        if self.mode == self.MODE_ENCRYPT:
            (res, ) = self._theKey._theKey.encrypt(_arrayTolong(data), None)
        else:
            res = self._theKey._theKey.decrypt(_arrayTolong(data))

        buf = _longToArray(res)

        # remove padding
        if ((self.algorithm == self.ALG_RSA_PKCS1) and
            (self.mode == self.MODE_DECRYPT)):
            buf = self.EME_PKCS1_v1_5_dec(buf)

        Util.arrayCopy(buf, 0, outBuff, outOffset, len(buf))

        return len(buf)
Exemple #2
0
 def testShort(self):
     a = [0,0,0,0]
     for i in range(30):
         s1 = random.randint(-128, 127)
         s2 = random.randint(-128, 127)
         s = Util.makeShort(s1, s2)
         Util.setShort(a, 1, s)
         self.assertEquals(s1, a[1])
         self.assertEquals(s2, a[2])
         self.assertEquals(s, Util.getShort(a, 1))
Exemple #3
0
    def testArrayCompare(self):
        a1 = [1,2,3,4]
        a2 = [0,1,2,3,4]

        self.assertEqual(0, Util.arrayCompare(a1, 0, a2, 1, 4))

        self.assertEqual(1, Util.arrayCompare(a1, 2, a2, 0, 2))

        self.assertEqual(-1, Util.arrayCompare(a2, 0, a1, 2, 2))

        try:
            # overflow near the end
            a1[3] = a2[0]
            Util.arrayCompare(a1, 3, a2, 0, 5)
            self.fail()
        except ArrayIndexOutOfBoundsException:
            pass

        try:
            # immediate first array overflow
            Util.arrayCompare(a1, 8, a2, 0, 5)
            self.fail()
        except ArrayIndexOutOfBoundsException:
            pass

        try:
            # immediate second array overflow
            Util.arrayCompare(a1, 0, a2, 8, 5)
            self.fail()
        except ArrayIndexOutOfBoundsException:
            pass
Exemple #4
0
    def  doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset):
        Cipher.doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset)

        data = [0 for i in xrange(inLength)]
        Util.arrayCopy(inBuff, inOffset, data, 0, inLength)
        data = _arrayTobinary(data)

        if self.mode == Cipher.MODE_ENCRYPT:
            result = self._cipher.encrypt(data)
        else:
            result = self._cipher.decrypt(data)

        result = _binaryToarray(result)
        Util.arrayCopy(result, 0, outBuff, outOffset, len(result))

        return len(result)
Exemple #5
0
    def init(self, theKey, theMode, bArray = [0,0,0,0,0,0,0,0], bOff = 0, bLen = 8):
        Cipher.init(self, theKey, theMode, bArray, bOff, bLen)

        if not isinstance(theKey, pyDesDESKey):
            raise CryptoException(CryptoException.ILLEGAL_VALUE)

        if bLen != 8:
            raise CryptoException(CryptoException.ILLEGAL_VALUE)

        iv = [0 for i in xrange(8)]
        Util.arrayCopy(bArray, bOff, iv, 0, bLen)

        iv = _arrayTobinary(iv)

        if 64 == theKey.getSize():
            # DES
            self._cipher = pyDes.des(theKey._key, self.desmode, iv,
                                     padmode = self.padmode)
Exemple #6
0
 def toBytes(self, outBuf, bOff, numBytes, arrayFormat):
     array = []
     if arrayFormat == self.FORMAT_BCD:
         value = self._value
         while value > 0:
             array.append(to_bcd(value % 100))
             value = value // 100
     elif arrayFormat == self.FORMAT_HEX:
         value = self._value
         while value > 0:
             array.append(to_hex(value & 0xff))
             value = value >> 8
     else:
         raise ArithmeticException
     if numBytes < len(array):
         raise ArithmeticException
     array.extend([0 for i in range(numBytes - len(array))])
     array.reverse()
     Util.arrayCopy(array, 0, outBuf, bOff, numBytes)
Exemple #7
0
 def _toBytesBound(self, outBuf, bOff):
     bLen = self._tag.toBytes(outBuf, bOff)
     if self._length > 127:
         length = self._length
         chunks = []
         while length > 127:
             chunks.append(length & 0xff)
             length = length >> 8
         chunk.reverse()
         lLen =  len(chunk)
         outBuf[bOff+bLen] = 0x80 | lLen
         bLen += 1
         Util.arrayCopy(chunk, 0, outBuf, bOff+bLen, lLen)
         bLen += lLen
     else:
         outBuf[bOff+bLen] = self._length
         bLen += 1
     Util.arrayCopy(self._value, 0, outBuf, bOff+bLen, self._length)
     return bLen + self._length
Exemple #8
0
 def getModulus(self, buffer, offset):
     Util.arrayCopy(self.modulus, 0, buffer, offset, len(self.modulus))
     return len(self.modulus)
Exemple #9
0
 def getExponent(self, buffer, offset):
     Util.arrayCopy(self.exponent, 0, buffer, offset, len(self.exponent))
     return len(self.exponent)
Exemple #10
0
 def replaceValue(self, vArray, vOff, vLen):
     self._value = [0 for i in range(vLen)]
     Util.arrayCopy(vArray, vOff, self._value, 0, vLen)
     self._length = vLen
Exemple #11
0
 def getValue(self, tlvValue, tOff):
     Util.arrayCopy(self._value, 0, tlvValue, tOff, self._length)
     return self._length
Exemple #12
0
 def testArrayCopy(self):
     a1 = [1,2,3,4,5]
     a2 = [0,0,0,0,0,0,0,0,0]
     
     Util.arrayCopy(a1, 2, a2, 0, 3)
     self.assertEquals(a1[2:2+3], a2[0:0+3])