Exemplo n.º 1
0
    def test_lib_crypto_tnfl_break(self):
        ekey = s_tinfoil.newkey()
        tinh = s_tinfoil.TinFoilHat(ekey)

        goodbyts = tinh.enc(b'foobar', b'hehe')
        edict = s_msgpack.un(goodbyts)

        # Empty values will fail to decrypt
        for key in ('iv', 'data', 'asscd'):
            bdict = {k: v for k, v in edict.items() if k != key}
            byts = s_msgpack.en(bdict)
            self.none(tinh.dec(byts))

        # Tampered values will fail
        bdict = {k: v for k, v in edict.items()}
        bdict['iv'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))

        bdict = {k: v for k, v in edict.items()}
        bdict['data'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))

        bdict = {k: v for k, v in edict.items()}
        bdict['asscd'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))
Exemplo n.º 2
0
    def test_lib_crypto_tnfl_break(self):
        ekey = s_tinfoil.newkey()
        tinh = s_tinfoil.TinFoilHat(ekey)

        goodbyts = tinh.enc(b'foobar', b'hehe')
        edict = s_msgpack.un(goodbyts)

        # Empty values will fail to decrypt
        for key in ('iv', 'data', 'asscd'):
            bdict = {k: v for k, v in edict.items() if k != key}
            byts = s_msgpack.en(bdict)
            self.none(tinh.dec(byts))

        # Tampered values will fail
        bdict = {k: v for k, v in edict.items()}
        bdict['iv'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))

        bdict = {k: v for k, v in edict.items()}
        bdict['data'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))

        bdict = {k: v for k, v in edict.items()}
        bdict['asscd'] = os.urandom(16)
        byts = s_msgpack.en(bdict)
        self.none(tinh.dec(byts))
Exemplo n.º 3
0
    def __init__(self, chan, boss, lisn=False):

        s_net.Link.__init__(self, chan)

        self._sess_boss = boss

        self.lisn = lisn  # True if we are the listener.

        self.txkey = s_tinfoil.newkey()
        self.txtinh = s_tinfoil.TinFoilHat(self.txkey)

        self.rxkey = None
        self.rxtinh = None
Exemplo n.º 4
0
    def test_lib_crypto_tnfl_base(self):

        ekey = s_tinfoil.newkey()
        self.len(32, ekey)
        self.isinstance(ekey, bytes)

        # Keys are random from s_tinfoil.newkey
        self.ne(ekey, s_tinfoil.newkey())
        self.ne(ekey, s_tinfoil.newkey())

        tinh = s_tinfoil.TinFoilHat(ekey)
        self.true(tinh.bend is default_backend())

        byts = tinh.enc(b'foobar')

        # Ensure the envelope is shaped as we expect it too be
        edict = s_msgpack.un(byts)
        self.isinstance(edict, dict)
        self.len(3, edict)

        data = edict.get('data')
        self.isinstance(data, bytes)
        self.len(6 + 16, data)

        iv = edict.get('iv')
        self.isinstance(iv, bytes)
        self.len(16, iv)

        asscd = edict.get('asscd')
        self.eq(asscd, None)

        # We can decrypt and get our original message back
        self.eq(tinh.dec(byts), b'foobar')

        # There isn't anythign special about the tinfoilhat object
        # We can make a new one to decrypt our existing message with
        # the known key
        self.eq(s_tinfoil.TinFoilHat(ekey).dec(byts), b'foobar')

        # We can encrypt/decrypt null messages
        byts = tinh.enc(b'')
        self.eq(tinh.dec(byts), b'')

        # Attempting to decrypt with the wrong key fails
        self.none(s_tinfoil.TinFoilHat(s_tinfoil.newkey()).dec(byts))

        # Messages are stream encoded so the length is 1 to 1
        for msize in [0, 1, 2, 15, 16, 17, 31, 32, 33, 63, 65]:
            mesg = msize * b'!'
            byts = tinh.enc(mesg)
            edict = s_msgpack.un(byts)

            self.len(16, edict.get('iv'))
            data = edict.get('data')
            self.len(len(mesg) + 16, data)
            self.eq(tinh.dec(byts), mesg)

        # We can pass in additional data that we want authed too
        byts = tinh.enc(b'robert grey', b'pennywise')
        edict = s_msgpack.un(byts)
        self.eq(edict.get('asscd'), b'pennywise')
        self.eq(tinh.dec(byts), b'robert grey')
        # A malformed edict with a bad asscd won't decrypt
        edict['asscd'] = b'georgey'
        self.none(tinh.dec(s_msgpack.en(edict)))
    def test_lib_crypto_tnfl_base(self):

        ekey = s_tinfoil.newkey()
        self.len(32, ekey)
        self.isinstance(ekey, bytes)

        # Keys are random from s_tinfoil.newkey
        self.ne(ekey, s_tinfoil.newkey())
        self.ne(ekey, s_tinfoil.newkey())

        tinh = s_tinfoil.TinFoilHat(ekey)
        self.true(tinh.bend is default_backend())

        byts = tinh.enc(b'foobar')

        # Ensure the envelope is shaped as we expect it too be
        edict = s_msgpack.un(byts)
        self.isinstance(edict, dict)
        self.len(3, edict)

        data = edict.get('data')
        self.isinstance(data, bytes)
        self.len(16, data)  # The output is padded

        iv = edict.get('iv')
        self.isinstance(iv, bytes)
        self.len(16, iv)

        hmac = edict.get('hmac')
        self.isinstance(hmac, bytes)
        self.len(32, hmac)

        # We can decrypt and get our original message back
        self.eq(tinh.dec(byts), b'foobar')

        # There isn't anythign special about the tinfoilhat object
        # We can make a new one to decrypt our existing message with
        # the known key
        self.eq(s_tinfoil.TinFoilHat(ekey).dec(byts), b'foobar')

        # We can encrypt/decrypt null messages
        byts = tinh.enc(b'')
        self.eq(tinh.dec(byts), b'')

        # Attempting to decrypt with the wrong key fails
        self.none(s_tinfoil.TinFoilHat(s_tinfoil.newkey()).dec(byts))

        # Messages are padded to expected lengths
        for msize in [0, 1, 2, 15, 16, 17, 31, 32, 33, 63, 65]:
            mesg = msize * b'!'
            byts = tinh.enc(mesg)
            edict = s_msgpack.un(byts)

            self.len(16, edict.get('iv'))
            self.len(32, edict.get('hmac'))

            mul = msize // 16
            elen = (mul + 1) * 16

            data = edict.get('data')
            self.len(elen, data)  # The output is padded
            self.eq(tinh.dec(byts), mesg)
Exemplo n.º 6
0
    def test_lib_crypto_tnfl_base(self):

        ekey = s_tinfoil.newkey()
        self.len(32, ekey)
        self.isinstance(ekey, bytes)

        # Keys are random from s_tinfoil.newkey
        self.ne(ekey, s_tinfoil.newkey())
        self.ne(ekey, s_tinfoil.newkey())

        tinh = s_tinfoil.TinFoilHat(ekey)
        self.true(tinh.bend is default_backend())

        byts = tinh.enc(b'foobar')

        # Ensure the envelope is shaped as we expect it too be
        edict = s_msgpack.un(byts)
        self.isinstance(edict, dict)
        self.len(3, edict)

        data = edict.get('data')
        self.isinstance(data, bytes)
        self.len(6 + 16, data)

        iv = edict.get('iv')
        self.isinstance(iv, bytes)
        self.len(16, iv)

        asscd = edict.get('asscd')
        self.eq(asscd, None)

        # We can decrypt and get our original message back
        self.eq(tinh.dec(byts), b'foobar')

        # There isn't anythign special about the tinfoilhat object
        # We can make a new one to decrypt our existing message with
        # the known key
        self.eq(s_tinfoil.TinFoilHat(ekey).dec(byts), b'foobar')

        # We can encrypt/decrypt null messages
        byts = tinh.enc(b'')
        self.eq(tinh.dec(byts), b'')

        # Attempting to decrypt with the wrong key fails
        self.none(s_tinfoil.TinFoilHat(s_tinfoil.newkey()).dec(byts))

        # Messages are stream encoded so the length is 1 to 1
        for msize in [0, 1, 2, 15, 16, 17, 31, 32, 33, 63, 65]:
            mesg = msize * b'!'
            byts = tinh.enc(mesg)
            edict = s_msgpack.un(byts)

            self.len(16, edict.get('iv'))
            data = edict.get('data')
            self.len(len(mesg) + 16, data)
            self.eq(tinh.dec(byts), mesg)

        # We can pass in additional data that we want authed too
        byts = tinh.enc(b'robert grey', b'pennywise')
        edict = s_msgpack.un(byts)
        self.eq(edict.get('asscd'), b'pennywise')
        self.eq(tinh.dec(byts), b'robert grey')
        # A malformed edict with a bad asscd won't decrypt
        edict['asscd'] = b'georgey'
        self.none(tinh.dec(s_msgpack.en(edict)))