Chris_M_Thomasson

Python HMAC Cipher Test...

Jul 18th, 2019
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. # Chris M. Thomasson Copyright 2018 (c)
  2. # Experimental HMAC Cipher
  3. # Test Vector For C program at
  4. # https://pastebin.com/raw/feUnA3kP
  5. #____________________________________________________________
  6.  
  7.  
  8. # Our external libs
  9. #____________________________________________________________
  10. import random;
  11. import hashlib;
  12. import hmac;
  13.  
  14.  
  15. # Some Utilities
  16. #____________________________________________________________
  17. def ct_bytes_to_hex(origin, offset):
  18.     hex = "";
  19.     n = len(origin);
  20.     t = "0123456789ABCDEF";
  21.     for i in range(offset, n):
  22.         c = ord(origin[i]);
  23.         nibl = c & 0x0F;
  24.         nibh = (c & 0xF0) >> 4;
  25.         hex = hex + t[nibh];
  26.         hex = hex + t[nibl];
  27.         hex = hex + " ";
  28.         if (not ((i + 1) % 16) and i != n - 1):
  29.             hex = hex + "\r\n";
  30.     return hex;
  31.  
  32.  
  33. # Generate n random bytes
  34. # These need should ideally be from a truly random, non-repeatable
  35. # source. TRNG!
  36. def ct_rand_bytes(n):
  37.     rb = "";
  38.     for i in range(n):
  39.         rb = rb + chr(random.randint(0, 255));
  40.         # HACK to get the same numbers
  41.         #rb = rb + chr(i);
  42.     return rb;
  43.  
  44.  
  45. # The Secret Key
  46. # Contains all the parts of the secret key
  47. #____________________________________________________________
  48. class ct_secret_key:
  49.     def __init__(self, hmac_key, hash_algo, rand_n):
  50.         self.hmac_key = hmac_key;
  51.         self.hash_algo = hash_algo;
  52.         self.rand_n = rand_n;
  53.  
  54.     def __repr__(self):
  55.         return "hmac_key:%s\nhash_algo:%s\nrand_n:%s" % (ct_bytes_to_hex(self.hmac_key, 0), self.hash_algo, self.rand_n);
  56.  
  57.     def __str__(self): return self.__repr__();
  58.  
  59.  
  60. # The Ciphertext or Plaintext
  61. # It holds the bytes of a ciphertext or a plaintext
  62. #____________________________________________________________
  63. class ct_bin:
  64.     def __init__(self, ctxt):
  65.         self.bytes = ctxt;
  66.     def __repr__(self):
  67.         return "%s" % (ct_bytes_to_hex(self.bytes, 0));
  68.  
  69.     def __str__(self): return self.__repr__();
  70.  
  71.  
  72. # The Crypt Round Function
  73. #____________________________________________________________
  74. def ct_crypt_round(SK, P, M):
  75.     H = hmac.new(SK.hmac_key.encode(), None, SK.hash_algo);
  76.     H.update(SK.hmac_key[::-1].encode());
  77.     C = "";
  78.     I_P = 0;
  79.     I_P_N = len(P.bytes);
  80.  
  81.     while (I_P < I_P_N):
  82.         D = H.digest();
  83.         print("D:%s" % (H.hexdigest()));
  84.         I_D = 0;
  85.         I_D_N = len(D);
  86.         while (I_P < I_P_N and I_D < len(D)):
  87.             P_byte = ord(P.bytes[I_P]);
  88.             C_I_P = P_byte ^ D[I_D];
  89.             C = C + chr(C_I_P);
  90.             if (M == False):
  91.                 U = bytearray([P_byte, C_I_P]);
  92.                 H.update(U);
  93.             else:
  94.                 U = bytearray([C_I_P, P_byte]);
  95.                 H.update(U);
  96.             I_P = I_P + 1;
  97.             I_D = I_D + 1;
  98.     return ct_bin(C);
  99.  
  100.  
  101. # The Crypt Function
  102. #____________________________________________________________
  103. def ct_crypt(SK, P, M):
  104.     if (M == False):
  105.         R = ct_rand_bytes(SK.rand_n);
  106.         P.bytes = R + P.bytes;
  107.     C = ct_crypt_round(SK, P, M);
  108.     C_1 = ct_bin(C.bytes[::-1]);
  109.     C = ct_crypt_round(SK, C_1, M);
  110.     if (M == True):
  111.         size = len(C.bytes) - SK.rand_n;
  112.         C.bytes = C.bytes[SK.rand_n : SK.rand_n + size];
  113.     return C;
  114.  
  115.  
  116. # The Main Program
  117. #____________________________________________________________
  118.  
  119. # Alice and Bob's Secret Key
  120. #____________________
  121. SK = ct_secret_key(
  122.     "Password",
  123.     hashlib.sha256, # The hash function. It should be a crypto secure hash.
  124.     32 # The number of bytes. The should be generated by a TRNG
  125. );
  126. print("%s" % (SK));
  127.  
  128. # Alice's Plaintext
  129. #____________________
  130. Original_Plaintext = "PlaintextAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  131. A_P = ct_bin(Original_Plaintext);
  132. print(
  133.     "\n\nAlice's Plaintext Bytes:"
  134.     "\n____________________\n%s\n" % (A_P)
  135. );
  136.  
  137. # Encrypt
  138. #____________________
  139. C = ct_crypt(SK, A_P, False);
  140. print(
  141.     "\n\nCiphertext Bytes:"
  142.     "\n____________________\n%s\n" % (C)
  143. );
  144.  
  145. # Decrypt
  146. #____________________
  147. B_P = ct_crypt(SK, C, True);
  148. print(
  149.     "\n\nBob's Ciphertext Bytes:"
  150.     "\n____________________\n%s\n" % (B_P)
  151. );
  152.  
  153.  
  154. if (B_P.bytes != Original_Plaintext):
  155.   print("DATA CORRUPTED!");
Advertisement
Add Comment
Please, Sign In to add comment