cipher.bmx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: Ciphers
  23. End Rem
  24. Module Crypto.Cipher
  25. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
  26. Import "common.bmx"
  27. Private
  28. Global _cipher_factories:TCipherFactory
  29. Public
  30. Rem
  31. bbdoc: Cipher base type.
  32. End Rem
  33. Type TCipher
  34. Field index:Int
  35. Method New(index:Int)
  36. Self.index = index
  37. End Method
  38. Rem
  39. bbdoc: Returns a list of all registered ciphers.
  40. End Rem
  41. Function ListCiphers:String[]()
  42. Return bmx_crypto_listCiphers()
  43. End Function
  44. Rem
  45. bbdoc: Gets the cipher index of the given @name.
  46. returns: the index or -1 if not found.
  47. End Rem
  48. Function Find:TCipher(name:String)
  49. Local index:Int = bmx_crypto_findCipher(name)
  50. If index >= 0 Then
  51. Local factory:TCipherFactory=_cipher_factories
  52. Local cipher:TCipher
  53. While factory
  54. cipher = factory.Find(index)
  55. If cipher Exit
  56. factory = factory._succ
  57. Wend
  58. Return cipher
  59. End If
  60. End Function
  61. Rem
  62. bbdoc: Returns the appropriate key size for @size.
  63. about: Rounds the input keysize @size down to the next appropriate key size for use with the cipher.
  64. End Rem
  65. Method KeySize:Int(size:Int) Abstract
  66. Rem
  67. bbdoc: Returns the name of the cipher.
  68. End Rem
  69. Method Name:String() Abstract
  70. End Type
  71. Rem
  72. bbdoc: Base type for cipher chaining modes.
  73. End Rem
  74. Type TCipherMode
  75. Field modePtr:Byte Ptr
  76. Rem
  77. bbdoc:
  78. End Rem
  79. Method Done:Int() Abstract
  80. End Type
  81. Type TCipherFactory
  82. Field _succ:TCipherFactory
  83. Field index:Int
  84. Method New(index:Int)
  85. Self.index = index
  86. _succ=_cipher_factories
  87. _cipher_factories=Self
  88. End Method
  89. Method Find:TCipher( index:Int ) Abstract
  90. End Type