cbcmode.bmx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. '
  2. ' Copyright (C) 2019-2022 Bruce A Henderson
  3. '
  4. ' This software is provided 'as-is', without any express or implied
  5. ' warranty. In no event will the authors be held liable for any damages
  6. ' arising from the use of this software.
  7. '
  8. ' Permission is granted to anyone to use this software for any purpose,
  9. ' including commercial applications, and to alter it and redistribute it
  10. ' freely, subject to the following restrictions:
  11. '
  12. ' 1. The origin of this software must not be misrepresented; you must not
  13. ' claim that you wrote the original software. If you use this software
  14. ' in a product, an acknowledgment in the product documentation would be
  15. ' appreciated but is not required.
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. ' 3. This notice may not be removed or altered from any source distribution.
  19. '
  20. SuperStrict
  21. Rem
  22. bbdoc: Cipher Block Chaining Mode
  23. about: Cipher Block Chaining mode is a simple mode designed to prevent trivial forms of replay and swap attacks on ciphers.
  24. It is given as:
  25. $$$
  26. C_i = E_k(P_i \oplus C_{i - 1})
  27. $$$
  28. It is important that the initialization vector be unique and preferably random for each message encrypted under the same key.
  29. End Rem
  30. Module Crypto.CBCMode
  31. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE -DLTC_CBC_MODE"
  32. Import "common.bmx"
  33. Rem
  34. bbdoc: CBC Cipher Mode.
  35. about: A symmetric mode block cipher.
  36. End Rem
  37. Type TCBCCipherMode Extends TCipherMode
  38. Rem
  39. bbdoc: Initializes the cipher mode.
  40. returns: CRYPT_OK if the cipher initialized correctly, otherwise, returns an error code.
  41. about: The @iv value is the initialization vector to be used with the cipher.
  42. You must fill the IV yourself and it is assumed they are the same length as the block size of the cipher you choose.
  43. It is important that the IV be random for each unique message you want to encrypt.
  44. End Rem
  45. Method Start:Int(cipher:TCipher, iv:Byte Ptr, key:Byte Ptr, keylen:Int, numRounds:Int)
  46. Local res:Int
  47. modePtr = bmx_crypto_cbc_start(cipher.index, iv, key, keylen, numRounds, res)
  48. Return res
  49. End Method
  50. Rem
  51. bbdoc: Encrypts the plaintext @pt of @length to @ct.
  52. returns: CRYPT_OK on success.
  53. about: @length must be a multiple of the cipher block size, otherwise you must manually pad the end of your
  54. message (either with zeroes or with whatever your protocol requires).
  55. End Rem
  56. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr, length:UInt)
  57. Return bmx_crypto_cbc_encrypt(modePtr, pt, ct, length)
  58. End Method
  59. Rem
  60. bbdoc: Decrypts the ciphertext @ct of @length to @pt.
  61. returns: CRYPT_OK on success.
  62. End Rem
  63. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr, length:UInt)
  64. Return bmx_crypto_cbc_decrypt(modePtr, ct, pt, length)
  65. End Method
  66. Rem
  67. bbdoc: Reads the IV out of the chaining mode, and stores it in @IV along with the @length of the IV.
  68. End Rem
  69. Method GetIV:Int(IV:Byte Ptr, length:UInt Var)
  70. Return bmx_crypto_cbc_getiv(modePtr, IV, length)
  71. End Method
  72. Rem
  73. bbdoc: Initializes the chaining mode state as if the original IV were the new IV specified.
  74. about: The @length of @IV must be the size of the cipher block size.
  75. This method is handy if you wish to change the IV without re–keying the cipher.
  76. The new IV replaces the existing IV as if it were the last ciphertext block.
  77. End Rem
  78. Method SetIV:Int(IV:Byte Ptr, length:UInt)
  79. Return bmx_crypto_cbc_setiv(modePtr, IV, length)
  80. End Method
  81. Rem
  82. bbdoc: Terminates the cipher stream.
  83. End Rem
  84. Method Done:Int()
  85. Return bmx_crypto_cbc_done(modePtr)
  86. End Method
  87. End Type