cast5cipher.bmx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: CAST5 Symmetric Key Block Cipher.
  23. about: Also known as CAST-128, it has a 64-bit block size and a key size of between 40 and 128 bits (in 8-bit increments).
  24. See <https://en.wikipedia.org/wiki/CAST-128>
  25. End Rem
  26. Module Crypto.Cast5Cipher
  27. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
  28. ModuleInfo "CC_OPTS: -DLTC_CAST5"
  29. Import "common.bmx"
  30. New TCast5CipherFactory(bmx_crypto_cast5_register())
  31. Type TCast5Cipher Extends TBlockCipher
  32. Method KeySize:Int(key:Int) Override
  33. Return bmx_crypto_cast5_keysize(key)
  34. End Method
  35. Method Name:String() Override
  36. Return "cast5"
  37. End Method
  38. Method BlockSize:Int() Override
  39. Return 8
  40. End Method
  41. Method Setup:Int(key:String, rounds:Int = 0) Override
  42. Local s:Byte Ptr = key.ToUTF8String()
  43. Local ret:Int = cast5_setup(s, Int(strlen_(s)), rounds, keyPtr)
  44. MemFree(s)
  45. Return ret
  46. End Method
  47. Method Setup:Int(key:Byte[], rounds:Int = 0) Override
  48. Return cast5_setup(key, key.length, rounds, keyPtr)
  49. End Method
  50. Method Setup:Int(key:Byte Ptr, keylen:Int, rounds:Int = 0) Override
  51. Return cast5_setup(key, keylen, rounds, keyPtr)
  52. End Method
  53. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr) Override
  54. Return cast5_ecb_encrypt(pt, ct, keyPtr)
  55. End Method
  56. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr) Override
  57. Return cast5_ecb_decrypt(ct, pt, keyPtr)
  58. End Method
  59. Method Done() Override
  60. cast5_done(keyPtr)
  61. End Method
  62. End Type
  63. Type TCast5CipherFactory Extends TCipherFactory
  64. Method Find:TCipher(index:Int) Override
  65. If index = Self.index Then
  66. Return New TCast5Cipher(index)
  67. End If
  68. End Method
  69. End Type