Guest User

Untitled

a guest
Nov 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. from salsa20 import Salsa20_keystream, Salsa20_xor
  2.  
  3.  
  4. def hexdump(data):
  5. print(' '.join([
  6. "{0:08x}".format(int.from_bytes(data[4*i:4*i+4], byteorder='little'))
  7. for i in range(len(data) // 4)
  8. ]))
  9.  
  10.  
  11. def to_bytes(arr):
  12. return b''.join(map(lambda x: x.to_bytes(4, byteorder='big'), arr))
  13.  
  14.  
  15. def print_keystream(nonce, key, block):
  16. stream = \
  17. Salsa20_keystream(
  18. length=64 * (block + 1),
  19. nonce=to_bytes(nonce),
  20. key=to_bytes(key),
  21. )
  22.  
  23. plaintext = "cryptography is awesome"
  24. ciphertext = \
  25. Salsa20_xor(
  26. b'\x00'*(block * 64) + plaintext.encode(),
  27. nonce=to_bytes(nonce),
  28. key=to_bytes(key),
  29. )
  30.  
  31. hexdump(stream[-64:])
  32. hexdump(ciphertext[-len(plaintext.encode()):])
  33.  
  34.  
  35. print('reference implementation: libsodium (NaCl)')
  36. print('generating keystream for: ')
  37. print()
  38. print('1) example from salsa20 specification: nonce: (3,1,4,1,5,9,2,6), key: (1,2,3,...), block: 7')
  39. print_keystream(
  40. nonce=[0x03010401, 0x05090206],
  41. key=[0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10, 0x11121314, 0x15161718, 0x191a1b1c, 0x1d1e1f20],
  42. block=7
  43. )
  44.  
  45. print()
  46. print('2) homework from crypto class: nonce: (0x00c0ffee, 0x00c0ffee), key: (0xffffff...ffff), block: 0')
  47. print_keystream(
  48. nonce=[0x00c0ffee, 0x00c0ffee],
  49. key=[0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff],
  50. block=0
  51. )
Add Comment
Please, Sign In to add comment