Ejemplo n.º 1
0
def test_chacha20_stream():
    """Test that we can generate a correct stream for encryption/decryption
    """
    tests = [(
        'ce496ec94def95aadd4bec15cdb41a740c9f2b62347c4917325fcc6fb0453986',
        'e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a'
    ), (
        '450ffcabc6449094918ebe13d4f03e433d20a3d28a768203337bc40b6e4b2c59',
        '03455084337a8dbe5d5bfa27f825f3a9ae4f431f6f7a16ad786704887cbd85bd'
    ), (
        '11bf5c4f960239cb37833936aa3d02cea82c0f39fd35f566109c41f9eac8deea',
        'e22ea443b8a275174533abc584fae578e80ed4c1851d0554235171e45e1e2a18'
    ), (
        'cbe784ab745c13ff5cffc2fbe3e84424aa0fd669b8ead4ee562901a4a4e89e9e',
        '35de88a5f7e63d2c0072992046827fc997c3312b54591844fc713c0cca433626'
    )]

    for a, b in tests:
        stream = bytearray(32)
        onion.chacha20_stream(bytes.fromhex(a), stream)
        assert(bytes.hex(bytes(stream)) == b)

        # And since we're at it make sure we can actually encrypt inplace on a
        # memoryview.
        stream = memoryview(bytearray(64))
        onion.chacha20_stream(bytes.fromhex(a), memoryview(stream[16:-16]))
        assert(bytes.hex(bytes(stream)) == '00' * 16 + b + '00' * 16)
Ejemplo n.º 2
0
def wrap_error(keys, err):
    b = unhexlify(err)
    l = len(b)
    padlen = 256 - l
    pad = b"\x00" * padlen
    b = struct.pack("!H", l) + b + struct.pack("!H", padlen) + pad
    assert len(b) == 256 + 2 + 2
    h = hmac.HMAC(keys.um, hashes.SHA256(), backend=default_backend())
    h.update(b)
    # h.update(unhexlify(PAYMENT_HASH))
    hh = h.finalize()
    b = bytearray(hh + b)
    chacha20_stream(keys.ammag, b)
    return hexlify(bytes(b)).decode("ASCII")