123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- '
- ' Copyright (C) 2019-2022 Bruce A Henderson
- '
- ' This software is provided 'as-is', without any express or implied
- ' warranty. In no event will the authors be held liable for any damages
- ' arising from the use of this software.
- '
- ' Permission is granted to anyone to use this software for any purpose,
- ' including commercial applications, and to alter it and redistribute it
- ' freely, subject to the following restrictions:
- '
- ' 1. The origin of this software must not be misrepresented; you must not
- ' claim that you wrote the original software. If you use this software
- ' in a product, an acknowledgment in the product documentation would be
- ' appreciated but is not required.
- ' 2. Altered source versions must be plainly marked as such, and must not be
- ' misrepresented as being the original software.
- ' 3. This notice may not be removed or altered from any source distribution.
- '
- SuperStrict
- Rem
- bbdoc: DES/3DES Symmetric Key Block Cipher.
- 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.
- See <https://en.wikipedia.org/wiki/Data_Encryption_Standard>
- End Rem
- Module Crypto.DESCipher
- ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
- ModuleInfo "CC_OPTS: -DLTC_DES"
- Import "common.bmx"
- New TDESCipherFactory(bmx_crypto_des_register())
- New T3DESCipherFactory(bmx_crypto_3des_register())
- Type TDESCipher Extends TBlockCipher
- Method KeySize:Int(key:Int) Override
- Return bmx_crypto_des_keysize(key)
- End Method
-
- Method Name:String() Override
- Return "DES"
- End Method
- Method BlockSize:Int() Override
- Return 8
- End Method
- Method Setup:Int(key:String, rounds:Int = 0) Override
- Local s:Byte Ptr = key.ToUTF8String()
- Local ret:Int = des_setup(s, Int(strlen_(s)), rounds, keyPtr)
- MemFree(s)
- Return ret
- End Method
- Method Setup:Int(key:Byte[], rounds:Int = 0) Override
- Return des_setup(key, key.length, rounds, keyPtr)
- End Method
- Method Setup:Int(key:Byte Ptr, keylen:Int, rounds:Int = 0) Override
- Return des_setup(key, keylen, rounds, keyPtr)
- End Method
- Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr) Override
- Return des_ecb_encrypt(pt, ct, keyPtr)
- End Method
- Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr) Override
- Return des_ecb_decrypt(ct, pt, keyPtr)
- End Method
- Method Done() Override
- des_done(keyPtr)
- End Method
- End Type
- Type T3DESCipher Extends TBlockCipher
- Method KeySize:Int(key:Int) Override
- Return bmx_crypto_3des_keysize(key)
- End Method
-
- Method Name:String() Override
- Return "3DES"
- End Method
- Method BlockSize:Int() Override
- Return 8
- End Method
- Method Setup:Int(key:String, rounds:Int = 0) Override
- Local s:Byte Ptr = key.ToUTF8String()
- Local ret:Int = des3_setup(s, Int(strlen_(s)), rounds, keyPtr)
- MemFree(s)
- Return ret
- End Method
- Method Setup:Int(key:Byte[], rounds:Int = 0) Override
- Return des3_setup(key, key.length, rounds, keyPtr)
- End Method
- Method Setup:Int(key:Byte Ptr, keylen:Int, rounds:Int = 0) Override
- Return des3_setup(key, keylen, rounds, keyPtr)
- End Method
- Method Encrypt:Int(pt:Byte Ptr, ct:Byte Ptr) Override
- Return des3_ecb_encrypt(pt, ct, keyPtr)
- End Method
- Method Decrypt:Int(ct:Byte Ptr, pt:Byte Ptr) Override
- Return des3_ecb_decrypt(ct, pt, keyPtr)
- End Method
- Method Done() Override
- des3_done(keyPtr)
- End Method
- End Type
- Type TDESCipherFactory Extends TCipherFactory
- Method Find:TCipher(index:Int) Override
- If index = Self.index Then
- Return New TDESCipher(index)
- End If
- End Method
- End Type
- Type T3DESCipherFactory Extends TCipherFactory
- Method Find:TCipher(index:Int) Override
- If index = Self.index Then
- Return New T3DESCipher(index)
- End If
- End Method
- End Type
|