Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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
Ejemplo n.º 6
0
 def getModulus(self, buffer, offset):
     Util.arrayCopy(self.modulus, 0, buffer, offset, len(self.modulus))
     return len(self.modulus)
Ejemplo n.º 7
0
 def getExponent(self, buffer, offset):
     Util.arrayCopy(self.exponent, 0, buffer, offset, len(self.exponent))
     return len(self.exponent)
Ejemplo n.º 8
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
Ejemplo n.º 9
0
 def getValue(self, tlvValue, tOff):
     Util.arrayCopy(self._value, 0, tlvValue, tOff, self._length)
     return self._length
Ejemplo n.º 10
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])