def runCoverage(): """ Simple coverage test. Currently executing at 37% line coverage. """ A = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] # cover normal and verbose on all nominal b-values for b in [25, 50, 100, 200, 400, 800, 1600]: myKeccak = Keccak.Keccak(b) myKeccak.KeccakF(A, False) for b in [25, 50, 100, 200, 400, 800, 1600]: myKeccak = Keccak.Keccak(b) myKeccak.KeccakF(A, False) #assert coverage on only permitted setB myKeccak = Keccak.Keccak(25) try: myKeccak.setB(1) except: pass myKeccak.setB(1600) myKeccak.KeccakF(A, False) # # myKeccak = Keccak.Keccak(r=1024,c=576,n=1024,verbose=False) myKeccak = Keccak.Keccak(1600) print myKeccak.Keccak('1')
def test_null_string(self): k244 = Keccak(448)("", 224) self.assertEqual( k244, "0xf71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd") k256 = Keccak(512)("", 256) self.assertEqual( k256, "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad804" + "5d85a470") k384 = Keccak(768)("", 384) self.assertEqual( k384, "0x2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa" + "8fe2479b2dd2b21362337441ac12b515911957ff") k512 = Keccak(1024)("", 512) self.assertEqual( k512, "0x0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466" + "f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160c" + "dab33d3670680e") sha3foobar = SHA3_384("foobar") self.assertEqual( sha3foobar, "0x0fa8abfbdaf924ad307b74dd2ed183b9a4a398891a2f6bac8fd2db7041b77f068580f9c6c66f699b496c2da1cbcc7ed8" )
def runDemo(b): A = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] myKeccak = Keccak.Keccak(b) myKeccak.KeccakF(A, True) myKeccak.printState(A, 'Final result')
def main(): my_keccak = Keccak.Keccak() message_1 = get_random_message(SIZE_OF_MESSAGE_BYTE) message_2 = change_one_bit(message_1) message_1 = message_1.hex().upper() message_2 = message_2.hex().upper() assert len(message_1) == len(message_2) differences = [] for num_round in range(1, MAX_NUM_OF_ROUNDS + 1): encrypted_message_1 = my_keccak.Keccak((len(message_1) * 4, message_1), num_of_rounds=num_round, n=SIZE_OF_ENCRYPTED_MESSAGE_BIT) encrypted_message_2 = my_keccak.Keccak((len(message_1) * 4, message_2), num_of_rounds=num_round, n=SIZE_OF_ENCRYPTED_MESSAGE_BIT) differences.append( calculate_difference(encrypted_message_1, encrypted_message_2)) graph(differences)
def cn_fast_hash(s): k = Keccak.Keccak() return k.Keccak((len(s) * 4, s), 1088, 512, 32 * 8, False).lower( ) #r = bitrate = 1088, c = capacity, n = output length in bits
## In case of difference, it stops the processing and print a message dirTestVector = os.path.abspath(os.path.join('../Test_Files')) verbose = False instances = [ ['SHAKE128', 336, 64, 0x1F, 0], # 1344, 256 ['SHAKE256', 272, 128, 0x1F, 0], # 1088, 512 ['SHA3-224', 288, 112, 0x06, 224], # 1152, 448 ['SHA3-256', 272, 128, 0x06, 256], # 1088, 512 ['SHA3-384', 208, 192, 0x06, 384], # 832, 768 ['SHA3-512', 144, 256, 0x06, 512], # change 576 1024 ] fileTypes = ['Short'] #Create an instance for reference myKeccak = Keccak.Keccak(400) def delimitedSuffixInBinary(delimitedSuffix): binary = '' while (delimitedSuffix != 1): binary = binary + ('%d' % (delimitedSuffix % 2)) delimitedSuffix = delimitedSuffix // 2 return binary with Timer() as t: for i in range(1): for instance in instances: [fileNameSuffix, r, c, delimitedSuffix, n] = instance
#The parts of code from Bernstein(?)'s library possibly has it's own license # which you can dig up from http://cr.yp.to/djb.html ######################################################################## import hashlib import struct import base64 import binascii import sys from Crypto.Util import number import Crypto.Random.random as rand import Keccak from collections import namedtuple import copy KEK = Keccak.Keccak(1600) CURVE_P = (2**255 - 19) b = 256 q = 2**255 - 19 l = 2**252 + 27742317777372353535851937790883648493 BASEPOINT = "0900000000000000000000000000000000000000000000000000000000000000" ##################################### #Bernstein(?) Eddie Library in python ##################################### def H(m): return hashlib.sha512(m).digest()
#! /usr/bin/pythonw # -- encoding: utf-8 -- # The Keccak sponge function, designed by Guido Bertoni, Joan Daemen, # Michaël Peeters and Gilles Van Assche. For more information, feedback or # questions, please refer to our website: http://keccak.noekeon.org/ # # Implementation by Renaud Bauvin, # hereby denoted as "the implementer". # # To the extent possible under law, the implementer has waived all copyright # and related or neighboring rights to the source code in this file. # http://creativecommons.org/publicdomain/zero/1.0/ import Keccak A = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] myKeccak = Keccak.Keccak(1600) myKeccak.KeccakF(A, True) myKeccak.printState(A, 'Final result')
import Keccak as Keccak myKeccak = Keccak.Keccak() def KeyGen(string): #KeyGen macht aus einem Satz oder Passwort s = list(string) einString = '' for i in range(0, len(s)): s[i] = ord(s[i]) dump = format(s[i], '#04x') dump = dump.split('x')[1] einString += dump sha3 = str(myKeccak.Keccak( (128, einString), 1152, 448, 224, True)) #dieser String wird mit Keccak (sha3) gehashed return sha3
def cn_fast_hash(s):#Keccak-256 hashing k = Keccak.Keccak() return k.Keccak((len(s) * 4, s), 1088, 512, 32 * 8, False).lower()
def runTestVectors(verbose=False): dirTestVector=os.path.abspath(os.path.join('KeccakKAT')) instances=[ # ['r40c160', 40, 160, 0], # ['r144c256', 144, 256, 0], # ['r544c256', 544, 256, 0], # ['r1344c256', 1344, 256, 0], ['0', 1024, 576, 0], ['224', 1152, 448, 224], ['256', 1088, 512, 256], ['384', 832, 768, 384], ['512', 576, 1024, 512] ] fileTypes=['Short'] #fileTypes=['Short', 'Long'] #String comparison function (useful later to compare test vector and computation def sameString(string1, string2): """Compare 2 strings""" if len(string1)!=len(string2): return False for i in range(len(string1)): if string1[i]!=string2[i]: return False return True #Create an instance myKeccak=Keccak.Keccak() for instance in instances: [suffix, r, c, n] = instance for fileType in fileTypes: print('Processing file: %sMsgKAT_%s.txt...' % (fileType, suffix)) #Open the corresponding file try: reference=open(os.path.join(dirTestVector,fileType+('MsgKAT_%s.txt' % suffix)), 'r') except IOError: print("Error: test vector files must be stored in %s" % (dirTestVector)) exit() #Parse the document line by line (works only for Short and Long files) for line in reference: if line.startswith('Len'): Len=int(line.split(' = ')[1].strip('\n\r')) if line.startswith('Msg'): Msg=line.split(' = ')[1].strip('\n\r') if (line.startswith('MD') or line.startswith('Squeezed')): MD_ref=line.split(' = ')[1].strip('\n\r') # If line starts with 'Squeezed', use the output length from the test vector if line.startswith('Squeezed'): n = (len(MD_ref)//2)*8 elif n == 0: print("Error: the output length should be specified") exit() # Perform our own computation MD_comp=myKeccak.Keccak((Len,Msg), r, c, n, verbose) #Compare the results if not sameString(MD_comp,MD_ref): print('ERROR: \n\t type=%s\n\t length=%d\n\t message=%s\n\t reference=%s\n\t computed=%s' % (suffix, Len, Msg, MD_ref, MD_comp)) exit() print("OK\n") reference.close()
def runSimple(v): myKeccak = Keccak.Keccak(25) myKeccak.KeccakF(v, False)