Пример #1
0
    def sign(self, message):
        # calculate the tree path, a bit-string (represented as an integer)
        # declaring whether to go right or left at each node
        #
        # N.B. for our security guarantees to hold, the path must be
        # unpredictable by an attacker and deterministically produced from the
        # message. A path can be reused once before our security guarantees
        # are invalidated
        k = keccak.Keccak(**_keccak_args)
        k.update(k.pad10star1(hash(self, tree_height//8), k.r))
        k.update(hash(message, tree_height//8))
        tree_path = k.squeeze(tree_height//8)
        tree_path = bytes2int(tree_path)
        assert tree_path >> tree_height == 0
        
        keys = [None] * tree_height
        signatures = [None] * tree_height
    
        keys[0] = LamportTreePrivKey.from_seed_index(self, tree_height, tree_path)
        signatures[0] = keys[0].sign(message)
        for i in xrange(1, len(keys)-1):
            keys[i] = LamportTreePrivKey.from_seed_index(self, tree_height - i, tree_path >> i)
            signatures[i] = keys[i].sign(LamportTreePubKey.from_privkey(keys[i-1]).serialize())

        keys[-1] = self
        signatures[-1] = self._sign(LamportTreePubKey.from_privkey(keys[-2]).serialize())
        return LamportSignature(signatures)
Пример #2
0
    def runTest(self):
        k = keccak.Keccak(r=self.keccak_args['r'],
                          c=self.keccak_args['c'],
                          fixed_out=True)
        k.absorb(self.input_vector)
        self.assertEqual(k.squeeze(self.keccak_args['n']), self.output_vector,
                         'input: %s\nparameters r=%d, c=%d, n=%d, fixed_out=True' \
                           % (repr(hexlify(self.input_vector)),
                              self.keccak_args['r'],
                              self.keccak_args['c'],
                              self.keccak_args['n']))

        k = keccak.Keccak(r=self.keccak_args['r'],
                          c=self.keccak_args['c'],
                          fixed_out=False)
        k.absorb(self.input_vector)
        self.assertEqual(k.squeeze(self.keccak_args['n']), self.output_vector,
                         'input: %s\nparameters r=%d, c=%d, n=%d, fixed_out=False' \
                           % (repr(hexlify(self.input_vector)),
                              self.keccak_args['r'],
                              self.keccak_args['c'],
                              self.keccak_args['n']))
Пример #3
0
    def runTest(self):
        k = keccak.Keccak(r=self.keccak_args['r'],
                          c=self.keccak_args['c'],
                          fixed_out=True)
        for _ in xrange(self.repeats):
            k.absorb(self.input_vector)
        self.assertEqual(k.squeeze(self.keccak_args['n']), unhexlify(self.output_vector),
                         'parameters r=%d, c=%d, n=%d, fixed_out=True' \
                           % (self.keccak_args['r'],
                              self.keccak_args['c'],
                              self.keccak_args['n']))

        k = keccak.Keccak(r=self.keccak_args['r'],
                          c=self.keccak_args['c'],
                          fixed_out=False)
        for _ in xrange(self.repeats):
            k.absorb(self.input_vector)
        self.assertEqual(k.squeeze(self.keccak_args['n']), unhexlify(self.output_vector),
                         'parameters r=%d, c=%d, n=%d, fixed_out=False' \
                           % (self.keccak_args['r'],
                              self.keccak_args['c'],
                              self.keccak_args['n']))
Пример #4
0
    def from_seed_index(cls, seed, depth, index):
        if not isinstance(seed, bytes):
            raise TypeError('seed must be a bytes')
        if not isinstance(depth, Integral):
            raise TypeError('depth must be an Integral')
        if not isinstance(index, Integral):
            raise TypeError('index must be an Integral')

        k = keccak.Keccak(**_keccak_args)
        seed = k.pad10star1(seed, k.r)
        k.update(seed)
        depth = encode_varint(depth,endian='big')
        depth = k.pad10star1(depth, k.r)
        k.update(depth)
        index = encode_varint(index,endian='big')
        # k will automatically pad index upon squeezing
        k.update(index)
        raw_key_material = k.squeeze(key_size * num_subkey_pairs * 2)
        subkey_type = globals()[cls.subkey_type]
        temp = ( subkey_type(''.join(subkey))
                 for subkey in _grouper(raw_key_material,
                                        key_size) )
        temp = tuple(temp)
        return cls(temp)
Пример #5
0
def hash(m, out_len):
    if not isinstance(m, bytes):
        raise TypeError('argument must be a bytes')
    k = keccak.Keccak(**_keccak_args)
    k.update(m)
    return k.squeeze(out_len)
Пример #6
0
                msg = bytearray(binascii.unhexlify(Msg))
                msg = msg[:Len // 8]
            if (line.startswith('MD')):
                MD_ref = line.split(' = ')[1].strip('\n\r')
                reference = bytearray(binascii.unhexlify(MD_ref))
                # If line starts with 'Squeezed', use the output length from the test vector
                if line.startswith('Squeezed'):
                    n = len(reference) * 8
                elif n == 0:
                    print("Error: the output length should be specified")
                    exit()

                if ((Len % 8) == 0):
                    # Perform our own computation
                    t1 = time.time()
                    computed = keccak.Keccak(r, c, msg, d, n // 8)
                    t2 = time.time()
                    # print("TIME of %s: %f" % (suffix, (t2-t1)))
                    total += (t2 - t1)
                    of.write('%f\n' % (t2 - t1))

        testfile.close()
print("OK -- %f\n" % (total))
of.write('Keccak total: %f\n' % total)

# Testing Skein
# print("Skein Testing")
# total = 0.0
# for file in files:
#     suffix = file[0]
#     r = file[1]
Пример #7
0
def shake_128(msg, d):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 1344, "1111")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 1344, 256, d, False)
    return digest
Пример #8
0
def shake_256(msg, d):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 1088, "1111")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 1088, 512, d, False)
    return digest
Пример #9
0
def sha3_512(msg):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 576, "01")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 576, 1024, 512, False)
    return digest
Пример #10
0
def sha3_384(msg):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 832, "01")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 832, 768, 384, False)
    return digest
Пример #11
0
def sha3_256(msg):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 1088, "01")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 1088, 512, 256, False)
    return digest
Пример #12
0
def sha3_224(msg):
    sha3_keccak = keccak.Keccak(1600)
    msg = sha3_pad(msg, 1152, "01")
    digest = sha3_keccak.Keccak((4 * len(msg), msg), 1152, 448, 224, False)
    return digest
Пример #13
0
import blakeC
import grostlC

import os
from binascii import hexlify, unhexlify
import binascii
import time
import sys

Msg = raw_input("Input a string to be hashed: ")
msg = bytearray(binascii.hexlify(Msg))
msg = msg[:len(msg) // 8]
Msg = Msg[:len(msg) // 8]

t1 = time.time()
keccakOut = keccak.Keccak(576, 1024, msg, 0x06, 512 // 8)
t2 = time.time()
print("-----Keccak Output------ total time: %f " % (t2 - t1))
print(binascii.hexlify(keccakOut))
print("------------------------------------------------")

t1 = time.time()
grostlOut = grostlC.groestl(512).digest(Msg)
t2 = time.time()
print("-----Grostl Output------ total time: %f " % (t2 - t1))
print(binascii.hexlify(grostlOut))
print("------------------------------------------------")

t1 = time.time()
blakeOut = blakeC.BLAKE(512).digest(Msg)
t2 = time.time()