Exemple #1
0
def hmac(ipBytes, key, hashFunc, blockSize, outputSize):
    opad = bytes(('\x5c' * blockSize).encode('latin1'))
    ipad = bytes(('\x36' * blockSize).encode('latin1'))

    if len(key) > blockSize:
        h = hashFunc.copy()
        h.update(key)
        key = h.digest()

    keyPad = '\x00' * (blockSize - len(key))
    keyPad = bytes(keyPad.encode('latin1'))
    key = key + keyPad

    o_key_pad = ENC.fixedXor(key, opad)
    i_key_pad = ENC.fixedXor(key, ipad)

    h = hashFunc.copy()
    h.update(i_key_pad + ipBytes)
    tempHash = h.digest()

    h = hashFunc.copy()
    h.update(o_key_pad + tempHash)
    finalHash = h.digest()

    return finalHash
Exemple #2
0
def AES_CTR(ipBytes, key, nonce):
    blockSize = 16
    nonce = get64BitLittleEndianBytes(nonce)
    noOfBlocks = math.ceil(len(ipBytes) / blockSize)

    opBytes = bytes(''.encode('utf-8'))
    for i in range(noOfBlocks):
        ctr = get64BitLittleEndianBytes(i)
        ipToAes = nonce + ctr
        encryptedCtr = AES_ECB.AESencrypt(ipToAes, key)
        if i == noOfBlocks - 1:
            ipBytesForXor = ipBytes[i * blockSize:]
            op = ENC.fixedXor(encryptedCtr[:len(ipBytesForXor)], ipBytesForXor)
            opBytes += op
        else:
            op = ENC.fixedXor(encryptedCtr,
                              ipBytes[i * blockSize:(i + 1) * blockSize])
            opBytes += op
    return opBytes
def AES_CBCencrypt(ptByteArray, key, IV):
    prevCt = IV
    ct = []
    for i in range(len(ptByteArray) // 16):
        ptInput = xorEnc.fixedXor(ptByteArray[i * 16:(i + 1) * 16], prevCt)
        newCtBlock = AES_ECB.AESencrypt(ptInput, key)
        for j in range(len(newCtBlock)):
            ct.append(newCtBlock[j])
        prevCt = newCtBlock

    ct = bytes(ct)
    return ct
def AES_CBCdecrypt(ctByteArray, key, IV):
    pt = []
    for i in range(len(ctByteArray) // 16):
        ctInputBlock = ctByteArray[len(ctByteArray) -
                                   (i + 1) * 16:len(ctByteArray) - i * 16]
        temp = AES_ECB.AESdecrypt(ctInputBlock, key)

        newPtBlock = 0
        if i == (len(ctByteArray) // 16 - 1):
            newPtBlock = xorEnc.fixedXor(temp, IV)
        else:
            prevCtBlock = ctByteArray[len(ctByteArray) -
                                      (i + 2) * 16:len(ctByteArray) -
                                      (i + 1) * 16]
            newPtBlock = xorEnc.fixedXor(temp, prevCtBlock)

        for j in range(len(newPtBlock)):
            pt.append(newPtBlock[len(newPtBlock) - j - 1])

    pt = pt[::-1]

    pt = bytes(pt)
    return pt
Exemple #5
0
def recoverKey():
    payload = 'a' * 64
    payload = bytes(payload.encode('latin1'))
    ct = encrypt(payload)

    modifiedCt = ct[:16]
    modifiedCt += bytes(('\x00' * 16).encode('latin1'))
    modifiedCt += ct[:16]
    modifiedCt += ct[48:]

    modifiedPt = decrypt(modifiedCt)
    if modifiedPt != None:
        key = ENC.fixedXor(modifiedPt[:16], modifiedPt[32:48])
        return key
    else:
        print('Something went wrong :/')
        return None