From e7b732c3ea5dd1849bb7711da04f569f82aea372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Stro=CC=88mbergson?= Date: Thu, 5 Apr 2018 16:38:25 +0200 Subject: (1) Added dumping of T1 inputs, calculated values and result needed to pipeline the design. (2) Automagically removed trailing whitespace (thanks Emacs). --- src/model/python/sha512.py | 77 +++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 31 deletions(-) (limited to 'src/model/python') diff --git a/src/model/python/sha512.py b/src/model/python/sha512.py index b4d47e6..8116c20 100755 --- a/src/model/python/sha512.py +++ b/src/model/python/sha512.py @@ -12,7 +12,7 @@ # Author: Joachim Strömbergson # Copyright (c) 2014, NORDUnet A/S # All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: @@ -62,7 +62,7 @@ class SHA512(): 'MODE_SHA_384', 'MODE_SHA_512'] self.mode = mode self.verbose = verbose - self.mode + self.mode self.NUM_ROUNDS = 80 self.H = [0] * 8 self.t1 = 0 @@ -78,7 +78,7 @@ class SHA512(): self.w = 0 self.W = [0] * 16 self.k = 0 - self.K = [0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, + self.K = [0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, @@ -106,7 +106,7 @@ class SHA512(): 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817] - + def init(self): if self.mode == 'MODE_SHA_512_224': self.H = [0x8c3d37c819544da2, 0x73e1996689dcd4d6, @@ -115,23 +115,23 @@ class SHA512(): 0x3f9d85a86a1d36c8, 0x1112e6ad91d692a1] elif self.mode == 'MODE_SHA_512_256': - self.H = [0x22312194fc2bf72c, 0x9f555fa3c84c64c2, - 0x2393b86b6f53b151, 0x963877195940eabd, - 0x96283ee2a88effe3, 0xbe5e1e2553863992, + self.H = [0x22312194fc2bf72c, 0x9f555fa3c84c64c2, + 0x2393b86b6f53b151, 0x963877195940eabd, + 0x96283ee2a88effe3, 0xbe5e1e2553863992, 0x2b0199fc2c85b8aa, 0x0eb72ddc81c52ca2] - + elif self.mode == 'MODE_SHA_384': self.H = [0xcbbb9d5dc1059ed8, 0x629a292a367cd507, - 0x9159015a3070dd17, 0x152fecd8f70e5939, - 0x67332667ffc00b31, 0x8eb44a8768581511, + 0x9159015a3070dd17, 0x152fecd8f70e5939, + 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4] elif self.mode == 'MODE_SHA_512': self.H = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, - 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, - 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, + 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179] - + def next(self, block): self._W_schedule(block) @@ -163,16 +163,16 @@ class SHA512(): def _copy_digest(self): - self.a = self.H[0] - self.b = self.H[1] - self.c = self.H[2] - self.d = self.H[3] - self.e = self.H[4] - self.f = self.H[5] - self.g = self.H[6] + self.a = self.H[0] + self.b = self.H[1] + self.c = self.H[2] + self.d = self.H[3] + self.e = self.H[4] + self.f = self.H[5] + self.g = self.H[6] self.h = self.H[7] - - + + def _update_digest(self): self.H[0] = (self.H[0] + self.a) & MAX_64BIT self.H[1] = (self.H[1] + self.b) & MAX_64BIT @@ -216,7 +216,7 @@ class SHA512(): else: tmp_w = (self._delta1(self.W[14]) + - self.W[9] + + self.W[9] + self._delta0(self.W[1]) + self.W[0]) & MAX_64BIT for i in range(15): @@ -251,10 +251,20 @@ class SHA512(): def _delta1(self, x): return (self._rotr64(x, 19) ^ self._rotr64(x, 61) ^ self._shr64(x, 6)) - + def _T1(self, e, f, g, h, k, w): - return (h + self._sigma1(e) + self._Ch(e, f, g) + k + w) & MAX_64BIT + T1 = (h + self._sigma1(e) + self._Ch(e, f, g) + k + w) & MAX_64BIT + + if self.verbose: + print("Inputs, calculations and result for T1:") + print("e = 0x%016x, f = 0x%016x, g = 0x%016x, h = 0x%016x" % (e, f, g, h)) + print("k = 0x%016x, w = 0x%016x" % (k, w)) + print("Ch = 0x%016x, sigma1 = 0x%016x" % (self._Ch(e, f, g), self._sigma1(e))) + print("T1 = 0x%016x" % (T1)) + print("") + + return T1 def _T2(self, a, b, c): @@ -264,11 +274,16 @@ class SHA512(): def _rotr64(self, n, r): return ((n >> r) | (n << (64 - r))) & MAX_64BIT - + def _shr64(self, n, r): return (n >> r) +#------------------------------------------------------------------- +# compare_digests() +# +# Compare if two given digests are equal or not. +#------------------------------------------------------------------- def compare_digests(digest, expected): if (digest != expected): print("Error:") @@ -278,8 +293,8 @@ def compare_digests(digest, expected): print(expected) else: print("Test case ok.") - - + + #------------------------------------------------------------------- # main() # @@ -296,7 +311,7 @@ def main(): 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000018] - + my_sha512 = SHA512(mode = 'MODE_SHA_512', verbose=1); TC1_expected = [0xDDAF35A193617ABA, 0xCC417349AE204131, 0x12E6FA4E89A97EA2, 0x0A9EEEE64B55D39A, 0x2192992A274FC1A8, 0x36BA3C23A3FEEBBD, 0x454D4423643CE80E, 0x2A9AC94FA54CA49F] @@ -330,14 +345,14 @@ def main(): my_digest = my_sha512.get_digest() compare_digests(my_digest, TC4_expected) - + #------------------------------------------------------------------- # __name__ # Python thingy which allows the file to be run standalone as # well as parsed from within a Python interpreter. #------------------------------------------------------------------- -if __name__=="__main__": +if __name__=="__main__": # Run the main function. sys.exit(main()) -- cgit v1.2.3