f8mode.bmx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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: F8 Chaining Mode
  23. about: It behaves much like CTR mode in that it XORs a keystream against the plaintext to encrypt.
  24. F8 mode comes with the additional twist that the counter value is secret, encrypted by a salt key.
  25. End Rem
  26. Module Crypto.F8Mode
  27. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE -DLTC_F8_MODE"
  28. Import "common.bmx"
  29. Rem
  30. bbdoc: F8 Cipher Mode
  31. about: A chaining mode for block ciphers.
  32. End Rem
  33. Type TF8CipherMode Extends TCipherMode
  34. Rem
  35. bbdoc: Starts the cipher mode state using @key as the secret key and @iv as the counter.
  36. returns: CRYPT_OK if the cipher initialized correctly, otherwise, returns an error code.
  37. about: It uses @saltKey as the IV encryption. The salt key can be shorter than the secret key, but not longer.
  38. End Rem
  39. Method Start:Int(cipher:TCipher, iv:Byte Ptr, key:Byte Ptr, keylen:Int, saltKey:Byte Ptr, saltkeylen:Int, numRounds:Int)
  40. Local res:Int
  41. modePtr = bmx_crypto_f8_start(cipher.index, iv, key, keylen, saltKey, saltkeylen, numRounds, res)
  42. Return res
  43. End Method
  44. Rem
  45. bbdoc: Encrypts the plaintext @pt of @length to @ct.
  46. returns: CRYPT_OK on success.
  47. about: The @length is specified in bytes and does not have to be a multiple of the ciphers block size.
  48. End Rem
  49. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr, length:UInt)
  50. Return bmx_crypto_f8_encrypt(modePtr, pt, ct, length)
  51. End Method
  52. Rem
  53. bbdoc: Decrypts the ciphertext @ct of @length to @pt.
  54. returns: CRYPT_OK on success.
  55. about: The @length is specified in bytes and does not have to be a multiple of the ciphers block size.
  56. End Rem
  57. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr, length:UInt)
  58. Return bmx_crypto_f8_decrypt(modePtr, ct, pt, length)
  59. End Method
  60. Rem
  61. bbdoc: Reads the IV out of the chaining mode, and stores it in @IV along with the @length of the IV.
  62. about: This works with the current IV value only and not the encrypted IV value specified during the call to #Start.
  63. The purpose of this method is to be able to seek within a current session only. If you want to change the session IV you will have to call #Done and
  64. then start a new state with #Start.
  65. End Rem
  66. Method GetIV:Int(IV:Byte Ptr, length:UInt Var)
  67. Return bmx_crypto_f8_getiv(modePtr, IV, length)
  68. End Method
  69. Rem
  70. bbdoc: Initializes the chaining mode state as if the original IV were the new IV specified.
  71. about: The @length of @IV must be the size of the cipher block size.
  72. This method is handy if you wish to change the IV without re–keying the cipher.
  73. The IV is encrypted as if it were the prior encrypted pad.
  74. End Rem
  75. Method SetIV:Int(IV:Byte Ptr, length:UInt)
  76. Return bmx_crypto_f8_setiv(modePtr, IV, length)
  77. End Method
  78. Rem
  79. bbdoc: Terminates the cipher stream.
  80. End Rem
  81. Method Done:Int()
  82. Return bmx_crypto_f8_done(modePtr)
  83. End Method
  84. End Type