z/OS Cryptographic Services ICSF Application Programmer's Guide
Previous topic | Next topic | Contents | Index | Contact z/OS | Library | PDF


PKCS Padding Method

z/OS Cryptographic Services ICSF Application Programmer's Guide
SA22-7522-16

This section describes the algorithm used to pad clear text when the PKCS-PAD method is specified. Padding is applied before encryption when this keyword is specified with the Symmetric Algorithm Encipher callable service, and it is removed from decrypted data when the keyword is specified with the Symmetric Algorithm Decipher callable service.

The rules for PKCS padding are very simple:

  • Padding bytes are always added to the clear text before it is encrypted.
  • Each padding byte has a value equal to the total number of padding bytes that are added. For example, if 6 padding bytes must be added, each of those bytes will have the value 0x06.
  • The total number of padding bytes is at least one, and is the number that is required in order to bring the data length up to a multiple of the cipher algorithm block size.

The callable services described in this document use AES, which has a cipher block size of 16 bytes. The total number of padding bytes added to the clear text will always be between 1 and 16. The table below indicates exactly how many padding bytes are added according to the data length, and also shows the value of the padding bytes that are applied.

Value of clear text length (mod 16)Number of padding bytes addedValue of each padding byte
0160x10
1150x0F
2140x0E
3130x0D
4120x0C
5110x0B
6100x0A
790x09
880x08
970x07
1060x06
1150x05
1240x04
1330x03
1420x02
1510x01

Note that the PKCS standards that define this padding method describe it in a way that limits the maximum padding length to 8 bytes. This is a consequence of the fact that the algorithms at that time used 8-byte blocks. We extend the definition to apply to 16-byte AES cipher blocks.

PKCS Padding Method (Example 1)

Clear text consists of the following18 bytes:

F14ADBDA019D6DB7 EFD91546E3FF8444 9BCB

In order to make this a multiple of 16 bytes (the AES block size), we must add 14 bytes. Each byte will contain the value 0x0E, which is 14, the total number of padding bytes added. The result is that the padded clear text is as follows:

F14ADBDA019D6DB7 EFD91546E3FF8444 9BCB0E0E0E0E0E0E 
0E0E0E0E0E0E0E0E

The padded value is 32 bytes in length, which is two AES blocks. This padded string is encrypted in CBC mode, and the resulting ciphertext will also be 32 bytes in length.

PKCS Padding Method (Example 2)

Clear text consists of the following16 bytes:

971ACD01C9C7ADEA CC83257926F490FF

This is already a multiple of the AES block size, but PKCS padding rules say that padding is always applied. Thus, we add 16 bytes of padding to bring the total length to 32, the next multiple of the AES block size. Each pad byte has the value 0x10, which is 16, the total number of padding bytes added. The result is that the padded clear text is as follows:

971ACD01C9C7ADEA CC83257926F490FF 1010101010101010 
1010101010101010

The padded value is 32 bytes in length, which is two AES blocks. This padded string is encrypted in CBC mode, and the resulting cipher text will also be 32 bytes in length.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014