cbcmode.bmx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. '
  2. ' Copyright (C) 2019-2020 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: A simple mode designed to prevent trivial forms of replay and swap attacks on ciphers.
  24. It is important that the initialization vector be unique and preferably random for each message encrypted under the same key.
  25. End Rem
  26. Module Crypto.CBCMode
  27. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE -DLTC_CBC_MODE"
  28. Import "common.bmx"
  29. Rem
  30. bbdoc:
  31. End Rem
  32. Type TCBCCipherMode Extends TCipherMode
  33. Rem
  34. bbdoc:
  35. End Rem
  36. Method Start:Int(cipher:TCipher, iv:Byte Ptr, key:Byte Ptr, keylen:Int, numRounds:Int)
  37. Local res:Int
  38. modePtr = bmx_crypto_cbc_start(cipher.index, iv, key, keylen, numRounds, res)
  39. Return res
  40. End Method
  41. Rem
  42. bbdoc: Encrypts the plaintext @pt of @length to @ct.
  43. returns: CRYPT_OK on success.
  44. about: @length must be a multiple of the cipher block size, otherwise you must manually pad the end of your
  45. message (either with zeroes or with whatever your protocol requires).
  46. End Rem
  47. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr, length:UInt)
  48. Return bmx_crypto_cbc_encrypt(modePtr, pt, ct, length)
  49. End Method
  50. Rem
  51. bbdoc:
  52. End Rem
  53. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr, length:UInt)
  54. Return bmx_crypto_cbc_decrypt(modePtr, ct, pt, length)
  55. End Method
  56. Rem
  57. bbdoc: Reads the IV out of the chaining mode, and stores it in @IV along with the @length of the IV.
  58. End Rem
  59. Method GetIV:Int(IV:Byte Ptr, length:UInt Var)
  60. Return bmx_crypto_cbc_getiv(modePtr, IV, length)
  61. End Method
  62. Rem
  63. bbdoc: Initializes the chaining mode state as if the original IV were the new IV specified.
  64. about: The @length of @IV must be the size of the cipher block size.
  65. This method is handy if you wish to change the IV without re–keying the cipher.
  66. The new IV replaces the existing IV as if it were the last ciphertext block.
  67. End Rem
  68. Method SetIV:Int(IV:Byte Ptr, length:UInt)
  69. Return bmx_crypto_cbc_setiv(modePtr, IV, length)
  70. End Method
  71. Rem
  72. bbdoc: Terminates the cipher stream.
  73. End Rem
  74. Method Done:Int()
  75. Return bmx_crypto_cbc_done(modePtr)
  76. End Method
  77. End Type