How to Add a New Cipher
Contents
- Introduction to the Library
- Basic support package (com.kryptel.bslx)
- Kryptel API Commons package (com.kryptel)
- Cipher package (com.kryptel.cipher)
- Compressor package (com.kryptel.compressor)
- Exceptions package (com.kryptel.exceptions)
- Hash function package (com.kryptel.hash_function)
- MAC function package (com.kryptel.mac)
- Key-related functions (com.kryptel.key)
- Silver Key engine (com.kryptel.silver_key)
- Kryptel encrypted storage (com.kryptel.storage)
Adding a New Cipher
Class com.kryptel.cipher.BlockCipherBase lets you easily add a new block cipher to the library. The first step you need to do is to create a new CID (component ID). Add a new record to the com.kryptel.Guids class. Please remember that CIDs 11056249-400A-4461-BD5E-xxxxxxxxxxxx are reserved.
static public final UUID CID_MY_COOL_CIPHER = UUID.fromString("44987434-A3B7-43f1-8D47-379C21FEF638");
Now declare your cipher class by extending the com.kryptel.cipher.BlockCipherBase class.
final class MyCoolCipher extends BlockCipherBase {
Declare a static constant componentID containing the CID of your cipher:
static final UUID componentID = CID_MY_COOL_CIPHER;
Declare a class constructor to initialize the CipherInfo structure describing your cipher, and default cipher parameters:
MyCoolCipher(long capabilities) { super(capabilities); DEFAULT_KEY_SIZE = 32; DEFAULT_BLOCK_SIZE = 16; DEFAULT_ROUNDS = 1; DEFAULT_SCHEME = 1; private static final CipherInfo info = new CipherInfo( new int[] { 16, 32 }, // Key sizes (128 and 256 bits) new int[] { 16, 32 }, // Block sizes (128 and 256 bits) new int[] { 1 }, // Rounds new String[] { "Standard" } ); cipherKeySize = DEFAULT_KEY_SIZE; cipherBlockSize = DEFAULT_BLOCK_SIZE; cipherRounds = DEFAULT_ROUNDS; cipherScheme = DEFAULT_SCHEME; // Other construction code }
Implement IKryptelComponent methods, which BlockCipherBase does not implement.
public UUID ComponentID() { return componentID; } public String ComponentName() { return "My Cool Cipher"; }
Now it is time to add the actual cipher implementation. Override three abstract functions – ExpandKey, EncryptBasicBlock, and DecryptBasicBlock. ExpandKey is called by the ICipherParams.SetKey method to prepare (expand) key for using by encryption and decryption methods. The key to be expanded is in the cipherKey byte array. The size of the array is the current key size, which may be found in the variable cipherKeySize.
EncryptBasicBlock and DecryptBasicBlock correspondingly encrypt or decrypt single block of data residing in src at from position, and store the resulting block of data to dst at to position.
protected void ExpandKey() protected void EncryptBasicBlock(byte[] dst, int to, byte[] src, int from) protected void DecryptBasicBlock(byte[] dst, int to, byte[] src, int from)
EncryptBasicBlock and DecryptBasicBlock must allow inplace processing, that is, they must correctly work in the situation when src/from and dst/to point to the same or overlapping memory area.
Now the cipher class is completed and the last step is to add it to com.kryptel.cipher.ComponentLoader class. Add this code
if (cid.equals(CID_MY_COOL_CIPHER)) return new MyCoolCipher(capabilities);
to the CreateComponent function and the following line
if ((MyCoolCipher.componentType & mask) != 0) uidList.add(MyCoolCipher.componentID);
to the GetComponentList function.
That's all! Your new cipher is ready to be used.