
In addition to the previously announced signature functions (see Signieren mit CONZEPT 16), the procedures will be expanded to include encryption and decryption commands, which primarily enable the exchange of encrypted data with other applications. The symmetric cryptographic method AES (Advanced Encryption Standard) is used for this purpose.
In symmetric procedures, the same key is used for both encryption and decryption. AES is the successor to DES (Data Encryption Standard), whereby the data is processed in data blocks. Each data block is always 128 bits long; larger amounts of data are divided into a corresponding number of blocks. If the length of the data is not exactly 128 bits or a multiple thereof, it is padded using PKCS#7 padding.
A block is a two-dimensional byte array, which is defined with 4 columns and 4 rows in AES. Accordingly, the block size is 16 bytes = 128 bits. Several rounds (repetitions) of certain operations are then carried out on the data of a block using the key, resulting in the encrypted block at the end.
The length of the key is independent of the block length and can be 128, 192 or 256 bits. The designations AES-128, AES-192 and AES-256 therefore refer to the length of the key used. A longer key length means greater data security, but also a slightly higher computing effort.
With block encryption, it is important that the ciphertext cannot be used to draw any conclusions about the key (confusion) and that even changing a tiny part of the message to be encrypted changes the entire ciphertext in an unpredictable way (diffusion). For this purpose, AES is used in conjunction with a so-called operating mode, which defines how larger data volumes with multiple blocks are to be handled.
The following operating modes are currently available:
- Cipher-block chaining (CBC)
- Output feedback (OFB)
- Galois/Counter Mode (GCM)
Another security factor is the initialization vector (IV). The IV is intended to prevent the ciphertext from always being the same when using the same message and the same key. Otherwise, it would be possible to use cryptanalysis to draw conclusions about the key used (for example, to recognize certain groups of words). The IV is generated randomly and should not be used more than once. The length of the IV is identical to the block length used, i.e. 128 bits. Since the IV itself is not sufficient for decryption, it does not have to be secret.
Encrypting
The MemEncrypt()
function is used for encryption and is structured as follows:
MemEncrypt(
MemMsg : handle; // Plain text / Ciphertext
Options : int; // Options
Key : alpha; // Key
IV : alpha; // Initialization vector
opt Pos : int; // Plain text position
opt Len : int; // Plain text length
opt MemDst : handle; // Cipher
opt PosDst : int; // Cipher position
opt MemKey : handle; // Binary key
opt MemIV : handle; // Binary IV
opt MemTag : handle; // GCM authentication tag
)
: int // Error value
Arguments
- MemMsg
A memory object is passed in MemMsg, which contains the message that is to be encrypted. If no target object is specified in MemDst, MemMsg is overwritten with the ciphertext after successful encryption. - Options
The Options parameter is made up of the following 4 areas: Encryption algorithm, Operating mode, Key coding and IV coding. The encryption algorithms AES-128 (_MemCipherAES128
), AES-192 (_MemCipherAES192
) and AES-256 (_MemCipherAES256
) are provided at the beginning. The constants_MemCipherModeCBC
,_MemCipherModeOFB
and_MemCipherModeGCM
are available for the modes already mentioned above.The Key encoding area specifies whether the key in Key is hex (_MemKeyHex
) or base64 encoded (_MemKeyBase64
). The key can also be binary (_MemKeyMem
). If this constant is specified, a memory object containing the key must also be transferred in MemKey.The same applies for the IV coding area as for the Key coding. The coding of the initialization vector is specified here and the constants
_MemIVHex
,_MemIVBase64
and_MemIVMem
are available. For_MemIVMem
, a memory object with the initialization vector in MemIV must be transferred. - Key / IV
The key and the initialization vector are transferred in Key and IV. When using_MemKeyMem
/_MemIVMem
, Key / IV is not evaluated. - Pos / Len
The position and length of the plain text in the memory object can be specified using Pos and Len. - MemDst / PosDst
If a memory object is passed in MemDst, it contains the ciphertext after successful encryption. MPosDst can be used to specify the position at which the ciphertext is written in MemDst. - MemKey / MemIV
contains the key / initialization vector and is only evaluated if the option_MemKeyMem
/_MemIVMem
is set. - MemTag
A memory object must be specified here if GCM mode is used. After encryption, it contains a tag that is used for authentication.
Decryption
The function for decrypting is MemDecrypt()
and has the following structure:
MemDecrypt(
MemChf : handle; // Plain text / Ciphertext
Options : int; // Options
Key : alpha; // Key
IV : alpha; // Initialization vector
opt Pos : int; // Plain text position
opt Len : int; // Plain text length
opt MemDst : handle; // Cipher
opt PosDst : int; // Cipher position
opt MemKey : handle; // Binary key
opt MemIV : handle; // Binary IV
opt MemTag : handle; // GCM authentication tag
)
: int // Error value
The heads of the functions MemEncrypt()
and MemDecrypt()
are almost identical. The only difference between the functions is that MemDecrypt()
expects a memory object in the first argument (MemChf) that contains the ciphertext to be decrypted. Furthermore, the authentication tag returned by MemEncrypt()
is specified in the last argument.