CryptoSys PKI Pro Manual

Specifying the algorithm, mode and padding for generic block cipher functions

The generic block cipher functions CIPHER_Byte, CIPHER_Hex and CIPHER_File allow the block cipher algorithm and mode to specified either by a szAlgAndMode string or by using the nOptions flags, but not both. The algorithm-and-mode parameter string combines the name of the block cipher algorithm and the mode, e.g. "tdea/ecb". The output from the CIPHER_Byte and CIPHER_Hex functions is always the same length as the input, and any padding required for ECB and CBC modes must be dealt with separately using the PAD_* functions.

The functions CIPHER_EncryptBytes and CIPHER_DecryptBytes use the string parameter szAlgModePad to specify the method of padding as well, e.g. "aes128/cbc/pkcs5padding". With CIPHER_EncryptBytes the output will be longer than the input if padding is to be added. The user must query the function first to find the correct padded length and then allocate a buffer before encrypting.

Valid algorithm names are:

ValueAlgorithmOption
tdeaTriple DES, a.k.a. 3DES, des-ede3PKI_BC_TDEA
3desAlternative for Triple DESPKI_BC_3DES
desede3Another alternative for Triple DES (desede is OK)PKI_BC_DESEDE3
aes128AES-128PKI_BC_AES128
aes192AES-192PKI_BC_AES192
aes256AES-256PKI_BC_AES256

We have used "TDEA" consistently in CryptoSys products to refer to the Triple DES algorithm (as in its official name "Triple Data Encryption Algorithm"). In this case, you can use any one of "tdea", "3des" or "desede3" (or "desede"). These are all equivalent and all yield identical results.

Valid mode names are:

ValueModeOption
ecbElectronic Code Book mode (default)PKI_MODE_ECB
cbcCipher Block Chaining modePKI_MODE_CBC
ofbOutput Feedback mode PKI_MODE_OFB
cfb64/128-bit† Cipher Feedback mode PKI_MODE_CFB
ctrCounter mode PKI_MODE_CTR
gcmGalois/Counter mode‡PKI_MODE_GCM

† Only 64-bit CFB mode is provided for Triple DES and only 128-bit CFB mode is provided for AES.

‡ GCM is only available for AES, and only for some functions.

Some examples of valid string values for the szAlgAndMode parameter are:

strAlgAndModeDescriptionAlternative Option value
tdea-cbcTriple DES in CBC modePKI_BC_TDEA+PKI_MODE_CBC
3des-cbcditto PKI_BC_3DES+PKI_MODE_CBC
des-ede3-cbcditto PKI_BC_DESEDE3+PKI_MODE_CBC
tdea-ecbTriple DES in ECB modePKI_BC_TDEA+PKI_MODE_ECB
tdeaditto (ECB is default mode)PKI_BC_TDEA
aes128-cbcAES-128 in CBC modePKI_BC_AES128+PKI_MODE_CBC
aes256-ctrAES-256 in Counter modePKI_BC_AES256+PKI_MODE_CTR
aes192-gcmAES-192 in Galois/Counter modePKI_BC_AES192+PKI_MODE_GCM

Punctuation and space characters and upper- and lower-case are ignored in the szAlgAndMode string, so "tdea-cbc", "TDeA---cBc", "tdea cbc", and "TDEACBC" are equivalent (as indeed is "t*D$e^A c@b!C!!")

It is an error to use both the szAlgAndMode and nOptions parameters to specify the algorithm and mode. The algorithm must be explicitly specified. There is no default algorithm. The default cipher mode is ECB mode, which is not recommended because of security issues. It is recommended to use either CBC or CTR mode with a IV value that is unique each time it is used with a given key.

Padding

For the functions CIPHER_EncryptBytes, CIPHER_DecryptBytes, CIPHER_EncryptHex, CIPHER_DecryptHex, CIPHER_FileEncrypt and CIPHER_FileDecrypt, you can specify padding as well as the algorithm and mode. The given padding will be added before encryption and removed after decryption according to the relevant rules. If the padding is not given, default padding will be assumed which depends on the cipher mode.

Valid padding names are:

ValuePaddingOption
nopad[ding]No padding is addedPKI_PAD_NOPAD
pkcs5[padding]The padding scheme described in PKCS#5/#7 [PKCS5]PKI_PAD_PKCS5
oneandzeroes[padding]Pads with 0x80 followed by as many zero bytes necessary to fill the blockPKI_PAD_1ZERO
ansix923[padding]The padding scheme described in ANSI X9.23 [X9-23]PKI_PAD_AX923
w3c[padding]The padding scheme described in W3C Recommendation for XML encryption [XMLENC]PKI_PAD_W3C

So, for example, both nopad and nopadding are accepted, and so are pkcs5 and pkcs5padding. Remember that punctuation characters and case in the szAlgModePad string are ignored. The keywords must be in the correct order algorithm-mode-padding. Some valid examples:

szAlgModePadAlternative Option value
tdea/cbc/pkcs5PKI_BC_TDEA+PKI_MODE_CBC+PKI_PAD_PKCS5
tdea/cbc/pkcs5paddingPKI_BC_TDEA+PKI_MODE_CBC+PKI_PAD_PKCS5
tdeacbcpkcs5PKI_BC_TDEA+PKI_MODE_CBC+PKI_PAD_PKCS5
aes128-ecb-oneandzeroesPKI_BC_AES128+PKI_MODE_ECB+PKI_PAD_1ZERO

[Contents] [Index]

[PREV: Base64 and PEM string alternatives for CMS objects...]   [Contents]   [Index]   
   [NEXT: Padding schemes for block ciphers...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.