descipher.bmx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: DES/3DES Symmetric Key Block Cipher.
  23. about: Incorprates the legacy Data Encryption Standard 56-bit key cipher and its more scure successor 3DES with key sizes of 168, 112 or 56 bits.
  24. See <https://en.wikipedia.org/wiki/Data_Encryption_Standard>
  25. End Rem
  26. Module Crypto.DESCipher
  27. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
  28. ModuleInfo "CC_OPTS: -DLTC_DES"
  29. Import "common.bmx"
  30. New TDESCipherFactory(bmx_crypto_des_register())
  31. New T3DESCipherFactory(bmx_crypto_3des_register())
  32. Type TDESCipher Extends TBlockCipher
  33. Method KeySize:Int(key:Int) Override
  34. Return bmx_crypto_des_keysize(key)
  35. End Method
  36. Method Name:String() Override
  37. Return "DES"
  38. End Method
  39. Method BlockSize:Int() Override
  40. Return 8
  41. End Method
  42. Method Setup:Int(key:String, rounds:Int = 0) Override
  43. Local s:Byte Ptr = key.ToUTF8String()
  44. Local ret:Int = des_setup(s, Int(strlen_(s)), rounds, keyPtr)
  45. MemFree(s)
  46. Return ret
  47. End Method
  48. Method Setup:Int(key:Byte[], rounds:Int = 0) Override
  49. Return des_setup(key, key.length, rounds, keyPtr)
  50. End Method
  51. Method Setup:Int(key:Byte Ptr, keylen:Int, rounds:Int = 0) Override
  52. Return des_setup(key, keylen, rounds, keyPtr)
  53. End Method
  54. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr) Override
  55. Return des_ecb_encrypt(pt, ct, keyPtr)
  56. End Method
  57. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr) Override
  58. Return des_ecb_decrypt(ct, pt, keyPtr)
  59. End Method
  60. Method Done() Override
  61. des_done(keyPtr)
  62. End Method
  63. End Type
  64. Type T3DESCipher Extends TBlockCipher
  65. Method KeySize:Int(key:Int) Override
  66. Return bmx_crypto_3des_keysize(key)
  67. End Method
  68. Method Name:String() Override
  69. Return "3DES"
  70. End Method
  71. Method BlockSize:Int() Override
  72. Return 8
  73. End Method
  74. Method Setup:Int(key:String, rounds:Int = 0) Override
  75. Local s:Byte Ptr = key.ToUTF8String()
  76. Local ret:Int = des3_setup(s, Int(strlen_(s)), rounds, keyPtr)
  77. MemFree(s)
  78. Return ret
  79. End Method
  80. Method Setup:Int(key:Byte[], rounds:Int = 0) Override
  81. Return des3_setup(key, key.length, rounds, keyPtr)
  82. End Method
  83. Method Setup:Int(key:Byte Ptr, keylen:Int, rounds:Int = 0) Override
  84. Return des3_setup(key, keylen, rounds, keyPtr)
  85. End Method
  86. Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr) Override
  87. Return des3_ecb_encrypt(pt, ct, keyPtr)
  88. End Method
  89. Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr) Override
  90. Return des3_ecb_decrypt(ct, pt, keyPtr)
  91. End Method
  92. Method Done() Override
  93. des3_done(keyPtr)
  94. End Method
  95. End Type
  96. Type TDESCipherFactory Extends TCipherFactory
  97. Method Find:TCipher(index:Int) Override
  98. If index = Self.index Then
  99. Return New TDESCipher(index)
  100. End If
  101. End Method
  102. End Type
  103. Type T3DESCipherFactory Extends TCipherFactory
  104. Method Find:TCipher(index:Int) Override
  105. If index = Self.index Then
  106. Return New T3DESCipher(index)
  107. End If
  108. End Method
  109. End Type