aboutsummaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorJoachim Strömbergson <joachim@secworks.se>2014-05-16 16:36:37 +0200
committerJoachim Strömbergson <joachim@secworks.se>2014-05-16 16:36:37 +0200
commit3c7c7c3b829c857e72d75d5054e39f6e0417c58f (patch)
treed522d3c48180222efa6f06e2b9173b08d3207259 /src/model
parentf69f454a5e3f56a96f6d28bb48c661630e31e300 (diff)
Adding helper functions for printing digest. Adding testcase for message with 1000 blocks.
Diffstat (limited to 'src/model')
-rwxr-xr-xsrc/model/sha256.py42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/model/sha256.py b/src/model/sha256.py
index ca0a77c..3b16e1f 100755
--- a/src/model/sha256.py
+++ b/src/model/sha256.py
@@ -10,7 +10,8 @@
#
#
# Author: Joachim Strömbergson
-# Copyright (c) 2014, SUNET
+# Copyright (c) 2013, SUNET
+# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
@@ -46,6 +47,12 @@ import sys
#-------------------------------------------------------------------
+# Constants.
+#-------------------------------------------------------------------
+VERBOSE = True
+
+
+#-------------------------------------------------------------------
# ChaCha()
#-------------------------------------------------------------------
class SHA256():
@@ -214,13 +221,31 @@ class SHA256():
return (n >> r)
+#-------------------------------------------------------------------
+# print_digest()
+#
+# Print the given digest.
+#-------------------------------------------------------------------
+def print_digest(digest):
+ print("0x%08x, 0x%08x, 0x%08x, 0x%08x" %\
+ (digest[0], digest[1], digest[2], digest[3]))
+ print("0x%08x, 0x%08x, 0x%08x, 0x%08x" %\
+ (digest[4], digest[5], digest[6], digest[7]))
+ print("")
+
+
+#-------------------------------------------------------------------
+# compare_digests()
+#
+# Check that the given digest matches the expected digest.
+#-------------------------------------------------------------------
def compare_digests(digest, expected):
if (digest != expected):
print("Error:")
print("Got:")
- print(digest)
+ print_digest(digest)
print("Expected:")
- print(expected)
+ print_digest(expected)
else:
print("Test case ok.")
@@ -285,20 +310,23 @@ def main():
# TC3: Huge message with n blocks
- n = 10000
+ n = 1000
print("TC3: Huge message with %d blocks test case." % n)
TC3_block = [0xaa55aa55, 0xdeadbeef, 0x55aa55aa, 0xf00ff00f,
0xaa55aa55, 0xdeadbeef, 0x55aa55aa, 0xf00ff00f,
0xaa55aa55, 0xdeadbeef, 0x55aa55aa, 0xf00ff00f,
0xaa55aa55, 0xdeadbeef, 0x55aa55aa, 0xf00ff00f]
- TC3_expected = [0xf407ff0d, 0xb9dce2f6, 0x9b9759a9, 0xd3cdc805,
- 0xf250086d, 0x73bbefd5, 0xa972e0f7, 0x61a9c13e]
+ TC3_expected = [0x7638f3bc, 0x500dd1a6, 0x586dd4d0, 0x1a1551af,
+ 0xd821d235, 0x2f919e28, 0xd5842fab, 0x03a40f2a]
my_sha256.init()
for i in range(n):
my_sha256.next(TC3_block)
- my_digest = my_sha256.get_digest()
+ my_digest = my_sha256.get_digest()
+ if (VERBOSE):
+ print("Digest for block %d:" % i)
+ print_digest(my_digest)
compare_digests(my_digest, TC3_expected)