Advertisement
Python253

CAST-128_Doc

Feb 26th, 2019
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. CAST-128
  2. Warning
  3.  
  4. Use AES. This module is provided only for legacy purposes.
  5.  
  6. CAST-128 (or CAST5) is a symmetric block cipher specified in RFC2144.
  7.  
  8. It has a fixed data block size of 8 bytes. Its key can vary in length from 40 to 128 bits.
  9.  
  10. CAST is deemed to be cryptographically secure, but its usage is not widespread. Keys of sufficient length should be used to prevent brute force attacks (128 bits are recommended).
  11.  
  12. As an example, encryption can be done as follows:
  13.  
  14. from Crypto.Cipher import CAST
  15.  
  16. key = b'Sixteen byte key'
  17. cipher = CAST.new(key, CAST.MODE_OPENPGP)
  18. plaintext = b'??? '
  19. msg = cipher.encrypt(plaintext)
  20.  
  21. ...
  22. eiv = msg[:CAST.block_size+8]
  23. ciphertext = msg[CAST.block_size+8:]
  24. cipher = CAST.new(key, CAST.MODE_OPENPGP, eiv)
  25. print cipher.decrypt(ciphertext)
  26.  
  27. Module’s constants for the modes of operation supported with CAST:
  28.  
  29. var MODE_ECB:   Electronic Code Book (ECB)
  30. var MODE_CBC:   Cipher-Block Chaining (CBC)
  31. var MODE_CFB:   Cipher FeedBack (CFB)
  32. var MODE_OFB:   Output FeedBack (OFB)
  33. var MODE_CTR:   CounTer Mode (CTR)
  34. var MODE_OPENPGP:
  35.     OpenPGP Mode
  36. var MODE_EAX:   EAX Mode
  37. Crypto.Cipher.CAST.new(key, mode, *args, **kwargs)
  38. Create a new CAST cipher
  39.  
  40. Parameters:
  41. key (bytes, bytearray, memoryview) – The secret key to use in the symmetric cipher. Its length can vary from 5 to 16 bytes.
  42. mode (One of the supported MODE_* constants) – The chaining mode to use for encryption or decryption.
  43. Keyword Arguments:
  44.    
  45. ---> iv (bytes, bytearray, memoryview)(Only applicable for MODE_CBC, MODE_CFB, MODE_OFB, and MODE_OPENPGP modes).
  46.  
  47. The initialization vector to use for encryption or decryption.
  48.  
  49. For MODE_CBC, MODE_CFB, and MODE_OFB it must be 8 bytes long.
  50.  
  51. For MODE_OPENPGP mode only, it must be 8 bytes long for encryption and 10 bytes for decryption (in the latter case, it is actually the encrypted IV which was prefixed to the ciphertext).
  52.  
  53. If not provided, a random byte string is generated (you must then read its value with the iv attribute).
  54.  
  55. nonce (bytes, bytearray, memoryview)(Only applicable for MODE_EAX and MODE_CTR).
  56.  
  57. A value that must never be reused for any other encryption done with this key.
  58.  
  59. For MODE_EAX there are no restrictions on its length (recommended: 16 bytes).
  60.  
  61. For MODE_CTR, its length must be in the range [0..7].
  62.  
  63. If not provided for MODE_EAX, a random byte string is generated (you can read it back via the nonce attribute).
  64.  
  65. segment_size (integer)(Only MODE_CFB).The number of bits the plaintext and ciphertext are segmented in. It must be a multiple of 8. If not specified, it will be assumed to be 8.
  66.  
  67. mac_len : (integer)(Only MODE_EAX) Length of the authentication tag, in bytes. It must be no longer than 8 (default).
  68.  
  69. initial_value : (integer)(Only MODE_CTR). The initial value for the counter within the counter block. By default it is 0.
  70.  
  71. Return:
  72. a CAST object, of the applicable mode.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement