Esempio n. 1
0
def test_double():
    a = b"hello"
    b = enpad(a,16)
    c = enpad(b,16)
    d = unpad(c,16)
    assert b==d
    e = unpad(d,16)
    assert a==e
Esempio n. 2
0
    def decrypt(self,ciphertext,nonce=None):
        if nonce is None:
            nonce = ciphertext[:self.NONCE_SIZE]
            ciphertext = ciphertext[self.NONCE_SIZE:]

        if len(nonce)!=self.NONCE_SIZE:
            raise ValueError("nonce must be exactly {0:d} bytes long".format(self.NONCE_SIZE))

        l = len(ciphertext) - self._hash.digest_size
        cdigest = ciphertext[l:]
        ciphertext = ciphertext[:l]

        cipher = self._cipher.new(self.__key,self._cipher.MODE_CBC,nonce)
        plaintext = unpad(cipher.decrypt(ciphertext),cipher.block_size)

        # we check the mac after decrypting to mitigate timing attacks
        if not consteq(cdigest,self._getmac(nonce,ciphertext)):
            raise ValueError("decryption failed")

        return plaintext
Esempio n. 3
0
    def decrypt(self, ciphertext, nonce=None):
        if nonce is None:
            nonce = ciphertext[:self.NONCE_SIZE]
            ciphertext = ciphertext[self.NONCE_SIZE:]

        if len(nonce) != self.NONCE_SIZE:
            raise ValueError("nonce must be exactly {0:d} bytes long".format(
                self.NONCE_SIZE))

        l = len(ciphertext) - self._hash.digest_size
        cdigest = ciphertext[l:]
        ciphertext = ciphertext[:l]

        cipher = self._cipher.new(self.__key, self._cipher.MODE_CBC, nonce)
        plaintext = unpad(cipher.decrypt(ciphertext), cipher.block_size)

        # we check the mac after decrypting to mitigate timing attacks
        if not consteq(cdigest, self._getmac(nonce, ciphertext)):
            raise ValueError("decryption failed")

        return plaintext
Esempio n. 4
0
def check_pad(text,bs):
    p = enpad(text,bs)
    u = unpad(p,bs)
    assert (len(p)%bs)==0
    assert u == text
Esempio n. 5
0
def test_evil():
    a = b'aaaaaaaaaaaaaa\x80a'
    b = unpad(a,16)
    assert b is None