Dubinsky I Cryptography For Payment Professionals
Dubinsky I Cryptography For Payment Professionals
Professionals
Although cryptography plays an essential part in most modern solutions, especially in pay-
ments, cryptographic algorithms remain a black box for most users of these tools. Just as
a sane backend developer will not drill down into low-level disk access details of a server
filesystem, payments professionals have enough things to worry about before they ever need
to bother themselves with debugging an encrypted value or a message digest. However, at a
certain point, an engineer faces the need to identify a problem with a particular algorithm or,
perhaps, to create a testing tool that would simulate a counterpart in a protocol that involves
encryption.
The world of cryptography has moved on with giant leaps. Available technical standards men-
tion acronyms and link to more standards, some of which are very large while others are not
available for free. After finding the standards for the algorithm, the specific mode of opera-
tion must also be identified. Most implementations use several cryptographic primitives—for
example, key derivation with a block cipher, which produces a secret that is used together
with a hash function and a double padding scheme to produce a digital signature of a base64-
encoded value. Understanding this requires more sifting through online sources, more read-
ing of platform and library documents, and finally, when some code can be written, there are
very few test cases to validate it.
Cryptography for Payment Professionals is intended for technical people, preferably with
some background in software engineering, who may need to deal with a cryptographic algo-
rithm in the payments realm. It does not cover the payment technology in-depth, nor does it pro-
vide more than a brief overview of some regulations and security standards. Instead, it focuses
on the cryptographic aspects of each field it mentions. Highlights include:
• Major cryptographic algorithms and the principles of their operation.
• Cryptographic aspects of card-present (e.g., magnetic stripe, EMV) and online (e.g.,
e-Commerce and 3DS 2.0) transactions.
• A detailed description of TDES DUKPT and AES DUKPT protocols, as well as an example
implementation and test cases for both.
It is best if the reader understands programming, number and string representations in
machine memory, and bit operations. Knowledge of C, Python, or Java may make the examples
easier to read but this is not mandatory
Cryptography for Payment
Professionals
Ilya Dubinsky
First edition published 2023
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
Reasonable efforts have been made to publish reliable data and information, but the author and pub-
lisher cannot assume responsibility for the validity of all materials or the consequences of their use.
The authors and publishers have attempted to trace the copyright holders of all material reproduced
in this publication and apologize to copyright holders if permission to publish in this form has not
been obtained. If any copyright material has not been acknowledged please write and let us know so
we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information
storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.
com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA
01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermis-
[email protected]
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are
used only for identification and explanation without intent to infringe.
DOI: 10.1201/9781003371366
Publisher’s note: This book has been prepared from camera-ready copy provided by the authors.
Code related to the book is available at the author’s GitHub site: https://github.com/ilya-dubinsky/cfpp
To Rabbeinu, Rajoub, Oren Guru,
and the dearest Mr. Carasso.
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Author Biography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1 Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
vii
viii Contents
2 Understanding Payments . . . . . . . . . . . . . . . . . . . . . . . 37
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.2 Payment Methods . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2.1 Card payments . . . . . . . . . . . . . . . . . . . . . . 38
2.2.2 Bank transfers . . . . . . . . . . . . . . . . . . . . . . 40
2.2.3 Digital wallets . . . . . . . . . . . . . . . . . . . . . . 41
2.2.4 Cryptocurrencies . . . . . . . . . . . . . . . . . . . . . 41
2.2.4.1 Blockchain . . . . . . . . . . . . . . . . . . 42
2.2.4.2 Payment over blockchain . . . . . . . . . . . 43
2.3 Security of Card Payments . . . . . . . . . . . . . . . . . . . . 44
2.3.1 Card authentication . . . . . . . . . . . . . . . . . . . 45
2.3.2 Cardholder verification . . . . . . . . . . . . . . . . . 45
2.3.3 Data security . . . . . . . . . . . . . . . . . . . . . . . 46
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.2 Definitions and Legacy Mechanisms . . . . . . . . . . . . . . 48
3.2.1 CVV/CVC calculation and CVKs . . . . . . . . . . . . 49
3.2.2 PVV, PVKs, and PVKI . . . . . . . . . . . . . . . . . 50
3.3 EMV Transaction Flow . . . . . . . . . . . . . . . . . . . . . 51
3.4 Card Authentication . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.1 Static data authentication . . . . . . . . . . . . . . . . 53
Contents ix
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.2 Zoning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5.2.1 PIN translation . . . . . . . . . . . . . . . . . . . . . . 89
5.2.2 Key exchange and management . . . . . . . . . . . . . 89
5.3 Key Encryption and Representation . . . . . . . . . . . . . . . 92
5.3.1 X9.17 and variant formats . . . . . . . . . . . . . . . . 92
5.3.2 Key block . . . . . . . . . . . . . . . . . . . . . . . . 93
5.3.2.1 Key block header . . . . . . . . . . . . . . . 93
5.3.2.2 Encryption and authentication (Binding) . . 94
5.3.2.3 Key block encryption key and key block
MAC key generation . . . . . . . . . . . . . 96
5.4 Derived Unique Key Per Transaction (DUKPT) . . . . . . . . . 97
5.4.1 Design considerations . . . . . . . . . . . . . . . . . . 97
5.4.2 Key serial number (KSN) . . . . . . . . . . . . . . . . 99
5.4.3 Initial key derivation . . . . . . . . . . . . . . . . . . . 101
5.4.4 Intermediate key derivation . . . . . . . . . . . . . . . 103
5.4.4.1 TDES DUKPT intermediate key derivation . 103
5.4.4.2 AES DUKPT intermediate key derivation . . 104
5.4.4.3 Acquiring host intermediate key derivation . 105
5.4.4.4 Terminal intermediate key derivation . . . . 106
5.4.5 Working key derivation . . . . . . . . . . . . . . . . . 109
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Preface
It is hard to feel thrilled at the site of a new mandate letter from a card scheme
or exhilarated by the fact that a new key representation method may become
mandatory as soon as 2025. And the host-to-host PIN translation with a static
key stays the same after you figure it out once.
Years spent working as the keeper of payments knowledge at a fast-growing,
primarily e-Commerce acquirer can really dull one’s sense of excitement.
Then the changes came. First, we had to handle the PSD2 regulation, and the
EMV 3DS authentication protocol quickly became mandatory.
We grumbled about the copious extra work that landed on our lap. Little did
we know how lucky we were.
EMV 3DS is a repeatable, transparent protocol with a clear message struc-
ture. An EMV 3DS message could be written into a log in case of an error,
analyzed, and the error corrected. A transaction could be resent as many times
as were needed in the testing environment. These traits are rarely treasured in a
protocol, being taken for granted.
One day, after a strategic deal got signed, we had to get our hands dirty with
some POS devices.
Suddenly, very repeatable, static, and simple things became much harder.
If, before that, we could prepare fixed PIN blocks for each test card number,
encipher them with a static key and keep reusing the values, with the new devices,
there was no way around a dynamically generated PIN key derived from a value
based on the device number.
Now, instead of simply saying “we will use DUKPT; we know it works,” the
algorithm’s internals had to be explained to a developer so that a testing tool
could be built. Once the tool was built, it had to be tested itself since, for some
reason, it did not work correctly right away.
DUKPT generates some keys and encrypts some values with them. The
cryptographic algorithms it uses are of the sound, strong kind, and one of the
xiii
xiv Preface
xv
List of Figures
Building Blocks
DOI: 10.1201/9781003371366-1 1
2 Cryptography for Payment Professionals
more sifting through online sources, more reading of platform and library docu-
ments, and finally, when some code can be written, there are very few test cases
to validate it.
The book is intended for technical people, preferably with some background
in software engineering, who may need to deal with a cryptographic algorithm
in the payments realm in the future or present. It does not cover the payment
technology in-depth, nor does it provide more than a brief overview of some reg-
ulations and security standards. Instead, it focuses on the cryptographic aspects
of each field it mentions. It is best if the reader understands programming, num-
ber and string representations in machine memory, and bit operations. Knowl-
edge of C, Python, or Java may make the examples easier to read but this is not
mandatory.
The overview chapter on cryptography (Section 1.3 Cryptography) provides
an introduction to the field and covers all significant algorithms used today in the
industry. It is followed by an overview of the payment methods and the industry
in Chapter 2 Understanding Payments.
Several chapters cover cryptography in various technological areas of the
payment field: first, the security of a payment card (Chapter 3 Securing the Plas-
tic: Magnetic Stripe and EMV), then the network, the basis of all online payment
methods (Chapter 4 Securing the Network).
Methods to protect the PIN are cryptography-heavy and merit a separate
chapter, Chapter 5 Protecting the PIN.
Finally, regulatory aspects of cryptographic algorithms and requirements for
key strength are addressed in Chapter 6 Regulation and Compliance.
Besides the theoretical descriptions, the book contains implementation ex-
amples of some necessary bitwise operations (see Appendix A Bits and Digits),
basic and advanced cryptographic algorithms (Appendix B Cryptographic Exam-
ples), and step-by-step examples of some of the protocols mentioned in the book
(Appendix C EMV Examples, Appendix D PIN Examples, and Appendix E JOSE
Examples).
Source code for these examples, some test implementations, and test cases are
available at https://github.com/ilya-dubinsky/cfpp. The repository has
subfolders for the respective supported languages. References for each language
are relative to the parent subfolder, so “a C example is found in src/bits.c”
means that the file resides at c/src/bits.c.
single digit, as 0xN, and an 8-bit value (byte) is represented with two digits,
as 0xNN.
’AA’ where AA are alphanumeric characters, is a string value in the ASCII en-
coding. For example, ‘M8’ corresponds to bytes 0x4D, 0x37.
~A bitwise NOT applied to the value A.
A n bitwise shift left of the value A by n bits.
A ≡ B mod N integers A and B are congruent modulo the integer n. In other
words, there exists an integer d such that A − B = dN.
A mod N the reduction of the integer A modulo N. In other words, the unique d
for which there exists an integer r, 0 <= r < N, such that A = dn + r.
A k B concatenation of the values A and B, seen as a sequence of bits.
A ⊕ B the XOR operation on values A and B.
A=B the A is assigned the value of B.
base64(A) Base64 encoding of the binary value A. Unless specified otherwise,
the URL flavor of the Base64 encoding is used (see A.8).
BCD Binary-coded decimal. Refers to the representation of a decimal value in
memory where each digit of the number is mapped to a nibble. For in-
stance, the decimal value of 48is represented as 0x48 (while as a binary
number, it is equal to 0x30).
byte 8-bit unit of information. The value for the byte is often shown as two
hexadecimal digits.
D(C) = M decryption of the ciphertext value C with the algorithm D yields the
cleartext M.
DK (C) = M decryption of the ciphertext value C with the algorithm D yields the
cleartext M.
E(M) = C encryption of the cleartext value M with the algorithm E yields the
ciphertext C.
EK (M) = C encryption of the cleartext value M with the algorithm E and the
key K yields the ciphertext C.
leftmost most significant, if applied to bits.
nibble A 4-bit value corresponding to a single decimal digit. A byte consists of
two nibbles, corresponding to 4 most significant and 4 less significant bits.
rightmost least significant, if applied to bits.
4 Cryptography for Payment Professionals
1.3 Cryptography
Cryptography is the art and science of secure communication in the presence of
adversarial behavior.
The security of communication is typically divided into several separate func-
tions, and, as we see later, all of those can be supported by a cryptographic algo-
rithm or algorithms:
At this point, it is intuitively clear that all of these functions can be very useful
in payments.
For example, it is essential not to let an eavesdropper intercept sensitive pay-
ment credentials such as a credit card number, which means maintaining the con-
fidentiality of payment communications. It is crucial to ensure that the account
owner is indeed the one making the payment (authentication) and that the owner
cannot deny making the payment later (non-repudiation). Finally, it is essential
to ensure the attacker did not modify the message in transit (integrity).
Surprisingly enough, practical cryptography can achieve all these goals with
a relatively small number of building block algorithms combined skilfully into
higher-level protocols. While a complete understanding of cryptography un-
doubtedly requires in-depth knowledge of the internals of each of those algo-
rithms, their detailed description is beyond the scope of this book.
Many of these “primitive” individual functions are implemented as part of
mature libraries, available on all major platforms and for all major technology
stacks. Others are easy to develop as a straightforward combination of several
standard functions.
For a practitioner, it is more important to understand which method to use,
when, and how to invoke it on a particular technology platform than it is to know
how exactly the implementation works.
EK (P) = C
DK (C) = P
For cryptography to work, these functions necessarily have the properties:
DK (EK (P)) ≡ P
EK (DK (C)) ≡ C
However, this notation implicitly assumes that the same key is used for both
encryption and decryption. While true for the class of algorithms called symmet-
ric algorithms, it is also possible to have a different key for encryption (K1 ) and
Building Blocks 7
EK1 (P) = C,
DK2 (C) = P
and
DK2 (EK1 (P)) = P.
Cryptographic algorithms are also grouped into block and stream algorithms
or block ciphers and stream ciphers, correspondingly.
A block cipher operates on a block of data of fixed size. For example, DES
and Triple-DES algorithms operate on 64-bit data blocks, while the AES algo-
rithm works on 128-bit data blocks. A stream cipher operates on a stream of
bits. While this family of algorithms has its applications, it is not present in the
payments space.
It is worth noting that, given a stream cipher, it is easy to construct a block
cipher by applying the stream cipher to the block of data bit by bit. The opposite
is also possible: for example, a block cipher can be invoked iteratively by feeding
the output of a previous iteration as input into the next one. The result will be a
sequence of bits which can then be XORed with the bitstream of the actual data
(see also Appendix A Bits and Digits for more details on bit manipulations and
Section 1.8.1 Cipher block chaining for details on iterative invocation of block
ciphers).
and must be carefully shared with specific parties. Hence one of the terms used
for the key of a symmetric algorithm is secret, and establishing a shared secret
between multiple parties is not a simple task (see, for example, Key Trust and
Key Certificates Key Trust and Key Certificates or Section 5.2.2 Key exchange
and management).
It is possible to create an efficient hybrid system using both symmetric and
asymmetric algorithms, keeping the benefits of both algorithm families with-
out most of the drawbacks. For instance, the two communicating parties can
use an asymmetric algorithm to establish a mutually shared session key, which
will be symmetric. The TLS protocol (see Section 4.1 Transport Layer Security
(TLS)) is the most widespread example of a hybrid system. Another example
is the Elliptic-Curve Integrated Encryption Scheme (ECIES) (see Section 1.6.3
Elliptic curve integrated encryption scheme (ECIES)).
Public key cryptography is discussed in Section 1.5 Public Key Cryptogra-
phy, and symmetric algorithms are further discussed in Section 1.4 Symmetric
Algorithms.
While the overall length of the key is 8 bytes, the algorithm only uses 56 bits,
and the remaining one bit per byte is used for parity.
The algorithm generates 48-bit subkeys by using a combination of bit shifts
and a bit permutation table.
The input data passes a bit permutation first. The data is then split into half-
blocks of 32 bits each.
On each iteration, the algorithm takes one-half of the input, expands it to 48
bits using a pre-defined table (mixing and duplicating input bits), then XORs it
with the current subkey.
After the XOR operation, the bits are fed into a substitution box (S-box), a
predefined table that maps 6-bit values into 4-bit outputs, thus reducing the 48-
bit expanded value back to 32 bits.
Then, the value passes through a permutation box (P-box) which permutates
the bits.
After this step, the two halves of the input are swapped, and in the next
round, the former right half of the input data undergoes the steps above. See
Section 1.1 for an illustration of this method, also called Feistel’s process (see
Figure 1.1).
Finally, when the entire cycle of 16 rounds is completed, the output undergoes
another bit permutation, and the encrypted value is ready.
The algorithm has four known “weak” and 12 “semi-weak” keys. For in-
stance, a sequence of 8 0xFE bytes is a weak key. If the DES key is generated
using a random number generator, the probability of obtaining such a key is very
low. However, some implementations, such as the OpenSSL library (see Ap-
pendix B.5 DES and Triple DES), check their input values for the known weak
and semi-weak keys.
The algorithm was already considered weak in the 1990s and today serves
only as a building block together with various flavors of the Triple-DES mode of
operation (see Section 1.8.1 Padding and cipher block chaining). For example,
the first step of the CVV calculation still uses DES as the encryption algorithm
(see Section 3.2.1 CVV/CVC calculation and CVKs ).
As DES stands for “Data Encryption Standard,” in some cases, a distinction
is made between the standard and the algorithm it describes. In this case, the
algorithm is referred to as the “Data Encryption Algorithm” or DEA.
1.4.2 AES
The Advanced Encryption Standard [4, 5] was established in 2001 after an open
selection process that took into account input from independent researchers. The
algorithm was selected from a shortlist of several candidates and later adopted
by the US government as the replacement for DES.
The algorithm can work with 128-, 192- and 256-bit keys to encrypt a 128-
bit input. Like DES, AES derives “round keys” from the input key, with the
number of rounds depending on the key size. For brevity, the AES algorithms
with different key lengths are sometimes referred to as AES-128, AES-192, and
AES-256, correspondingly.
Each round key is 128 bits. At the beginning of the algorithm, a round key is
first XORed with the input data. This step is called AddRoundKey.
Then, iterating through rounds, the algorithm performs the SubBytes step,
where an S-box mechanism with carefully designed substitution tables is applied
to the data bits.
After the substitution, the algorithm treats the data as a matrix and performs
a ShiftRows operation (reshuffling rows so that elements of each column in the
matrix intermingle) and a MixColumns step which applies a linear transformation
to each column of the matrix. Finally, the result is XORed with the round key in
another AddRoundKey invocation.
After the substitution, the algorithm treats the data as a matrix and performs
a ShiftRows operation (reshuffling rows so that elements of each column in the
matrix intermingle) and a MixColumns step which applies a linear transformation
to each column of the matrix. Finally, the result is XORed with the round key in
another AddRoundKey invocation.
As with DES, AES performs well on different hardware. It allows further
optimization for speed with lookup tables for some of the steps. Built-in AES
instructions are also available in many processors (e.g., x86 architecture).
Building Blocks 11
1.5.1 RSA
Perhaps the oldest encryption algorithm in this field is RSA [6], which relies on
the computational difficulty of number factorization. According to the algorithm,
a part of each key is a large product of two large prime numbers. Breaking the en-
cryption requires finding a root of the ciphertext over the ring of integers modulo
the large number, which is computationally easy only if the two primes become
known.
The RSA algorithm supports two modes: encryption and signature.
Under the encryption scenario, the receiver publishes the encryption key and
keeps the decryption key secret. The sender can then use the public key to encrypt
the message, and only the receiver can decrypt it.
Under the signature scenario, the role of the keys is converse. The signing
party calculates the message hash and then encrypts it using their private key.
Any party with the public key can use it to decrypt the signature and compare it
to the independently calculated message hash value.
Both the public and the private key are a pair of numbers. The algorithm uses
arithmetics for encryption, which assumes a certain message length (size of the
number), and is, therefore, a block cipher.
The public key for the algorithm is a pair of two numbers, a product of two
primes p and q, called the modulus and denoted as n, and a number e that is
coprime with the least common multiple of p − 1 and q − 1, λ . In practice, to
4
simplify computations, e is usually chosen to be either 3 or 22 + 1 = 65537
12 Cryptography for Payment Professionals
(both prime numbers) and pre-defined for the entire protocol. The number e is
also called the public exponent.
The private key is the pair of two numbers, n and d, where the latter is a
modular multiplicative inverse of e modulo λ . The actual calculation is more
straightforward than it looks; see Appendix B.2 RSA for an example with small
numbers. The number d is also called the private exponent.
It turns out2 that M to the power of ed is always equal to M modulo n.
Hence, the algorithm does the following to encrypt and decrypt the data.
To encrypt the message M, the sender raises it to the power of e modulo n.
To decrypt the message, the recipient raises the encrypted message to the power
of d modulo n.
C ≡ M e mod n
M ≡ Cd mod n
Note that the length of the output of the encryption is equal to the length of
the modulus, and this algorithm is a form of a block cipher.
Unfortunately, the computational complexity on which this elegant algorithm
relies is only valid for deterministic computers. There are ways to break the en-
cryption using a quantum computer quickly. However, no quantum computer
today has enough qubits to get anywhere close to the key complexity used in the
field.
The RSA cryptography standard is covered by PKCS #1 with the latest ver-
sion published as RFC 8017 [8].
An example of the RSA algorithm can be found at Appendix B.2 RSA.
through them, find a third point where it intersects the curve and take its opposite
relative to the X-axis, R, to be R = P · Q (see Figure 1.3)3 .
With the addition of the infinity point, this operation follows the group law
over the curve. Furthermore, this algebraic structure remains valid over real num-
bers and a finite field. The latter can either be a field of remainders modulo a
prime number (Z p ) or a field of polynomials with binary coefficients (GF(2n )).
In other words, given an elliptic curve, a particular point on the curve, G, and
an integer number M, we can define multiplication by an integer scalar: using
point addition, add G to itself M times, and obtain the value of C = M · G.
It turns out that if the elliptic curve has certain properties, it is computation-
ally easy to calculate C given M and G but computationally hard to find M given
C and G. This issue is also called the problem of a discrete logarithm on an el-
liptic curve and this problem is computationally much harder than the simple
discrete logarithm.
To give an estimate [11], the computational power required to break a 2048-
bit RSA key is roughly equivalent to the power required to break a 168-bit ECC
key. Besides requiring shorter keys, the ECC-based operations of encryption, de-
cryption, and key generation all outperform RSA operations of equivalent cryp-
tographic strength. Unfortunately, the ECC is also more vulnerable to quantum
algorithms.
Not every elliptic curve, field, or point G yield a computationally hard dis-
crete logarithm. In a manner somewhat similar to how public exponents for RSA
are usually chosen from only a few numbers, standard lists of cryptographically
3 For special cases such as point doubling a tangent line is used, and the infinity point is also a member
of the group.
16 Cryptography for Payment Professionals
strong elliptic curves are available, for example, from the National Institute of
Standards and Technology [12].
The complete set of parameters of an elliptic curve, including the base point
G, is called domain parameters.
Message authentication codes are described in more detail in Section 1.9 Mes-
sage Authentication Codes. It is worth noting that the mentioned approach (ap-
pending a secret to the hash and then using it to ensure message authenticity) with
some hash functions is vulnerable to attacks where the attacker cannot change the
original message but can extend it.
Some of the widely used hash functions are MD5 [14] and SHA-1 [15]. The
former, “Message Digest 5,” generates 16-byte hash values, while the latter, “Se-
cure Hash Algorithm 1,” generates 20-byte values. Both are cryptographically
broken, and effective attacks were demonstrated against them. However, they
remain widely used.
The next generation of hash functions is the Secure Hash Algorithm 2 [15]
family, consisting of six hash functions with different hash sizes. The hashes are
SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256. The
SHA-224 and SHA-384 are both a version of SHA-256 and SHA-512 with a
slight difference in initialization and the output value truncated. SHA-512/224
and SHA 512/256 are truncated versions of SHA-512 with minor initialization
differences.
A newer standard, SHA-3 or Secure Hash Algorithm 3 [16], also exists. How-
ever, since SHA-2 is considered to have adequate security, SHA-3 hashes have a
much smaller footprint in the field.
The MD5, SHA-1, and SHA-2 hash functions share a common architecture,
the Merkle-Damgård construction. With it, the input value is padded in a manner
that also encodes its length. Then, internal state variables are initialized, and for
each block of input data, a variety of bitwise and arithmetical operations are
applied to the state variables. At the end of the input, the state is output as the
hash value.
values which rarely are as long as 6 digits, whereas even the smaller DES/TDES
block is 8 bytes and can contain at least 16 decimal digits (see also Section 3.4.3.1
PIN block format 2 and offline plaintext validation, Section 3.4.3.4 PIN block
formats 0, 1, 3, and 4).
Multiple approaches for padding exist [17]. The value can be padded to the
block size multiple with zero bits (ISO 9797 padding method 1) or a single 1 bit
and then zeros until full block size (ISO 9797 padding method 2). Data length can
be put at the beginning of the padded data, followed by the data itself, followed
by enough zero bits to fill the block size multiple (ISO 9797 padding method 3).
the recipient does not know the seed S, they have no way to recover the
data unless we find a way to transmit the seed securely. The seed is trans-
mitted in the following steps.
6. The result of step 5, D0 , is used as a seed for another mask of length hLen,
XS .
7. The mask, XS , is XORed with the seed S to obtain S0 = S ⊕ XS .
8. The full padded message consists of a fixed value of 0x00, then S0 , then
D0 .
To recover the message, the recipient needs to perform the following steps:
1. Split the S0 and the D0 from the input.
2. Use D0 as the seed for a mask XS .
3. Recover S via S = XS ⊕ S0 .
4. Use S as the seed for a mask XD .
5. Recover D via D = XD ⊕ D0 .
6. Validate LH and process the message by skipping the zero bytes after LH
and the 0x01 sentinel byte before the payload.
An example of OAEP can be found in Appendix B.7 OAEP.
Building Blocks 21
1.8.1.3 Electronic code book (ECB) and cipher block chaining (CBC)
When the data to be encrypted is longer than the block size, the evident approach
is to pad the data to a length that divides by block size, then encrypt each block
separately.
This method is called the electronic code book (ECB) mode. It is simple to
comprehend and implement, but it also has a flaw: identical data blocks will be
encrypted into identical ciphertext blocks, no matter where they are or when they
occurred over the communication session. Since real-life communication proto-
cols tend to have similar beginnings and endings for messages, and data often
comes with repeating patterns, an attacker can collect a code book of known or
repeating encrypted segments and then use it to analyze the encrypted messages
or simplify the task of breaking the encryption.
Furthermore, the ECB approach does not preserve integrity. While it is im-
possible to modify a single bit or a single byte in a single block, given a code
book, it is easy to replace a block with another block, tampering with the en-
crypted message.
These vulnerabilities can be mitigated by changing the keys frequently and
adding a MAC, but a more elegant solution exists.
This solution is chaining, and its underlying principle is to feed the encrypted
result of the previous block into the next one. This mode of operation of block
ciphers is called cipher block chaining (CBC), and its flow for encryption is as
follows (see also Figure 1.5):
For decryption, the flow is similar, except that the XOR happens after the
decryption (see also Figure 1.6):
1. The enciphered data already has the correct padding, so it is only split into
blocks, Ci .
2. Block Ci is decrypted using the key K.
3. The outcome is XORed with the result of the previous iteration: Pi =
DK (Ci−1 ) ⊕ Pi−1 .
4. The outcome is sent to the output and stored for the next iteration.
The two algorithms above describe all steps except for the first one. There
is no result of the previous iteration for the first data block with which it can be
XORed prior to encryption and, conversely, after the decryption. It is possible
to use zero values as that initial XOR mask since the bitwise operation will not
affect the value. However, with this approach, two identical plaintext messages
will result in two identical ciphertexts, and two similar plaintext messages will
yield identical ciphertexts until the first difference.
The first data block is XORed with a random data block to address that. Such
a block is called the initialization vector (IV), and it can be sent in the clear
alongside the ciphertext without weakening the protocol.
hash to the receiver. The receiver, knowing the secret, calculates the hash inde-
pendently and validates it.
With the block cipher approach, the sender uses the secret key for a cipher
block chaining message encryption with a zero initialization vector. The sender
then transmits the message and the result of the last iteration as the MAC. The
overall is unsurprisingly called cipher block chaining message authentication
code (CBC-MAC).
This approach is valid for messages of fixed length or such where the length
is sent in the message header (or in another location at the beginning of the
transmission). If the length varies and is not sent at the beginning of the message,
an attack called length-extension can be used to add more data to the message4 .
Among the well-known hash algorithms, MD5, SHA-1, and SHA-2 are sus-
ceptible to this type of attack, while SHA-3, SHA-384, and SHA-512/256 are
not [21].
The ISO/IEC 9797 standard specifies MAC types and various algorithms used
to calculate them [17, 22, 23].
Further developments of the idea exist which fix that vulnerability. For CBC-
based MACs, there is, for example, the ECBC-MAC algorithm, also known as
encrypt-last-block, where after the final iteration of the CBC with the first secret
key, it is encrypted once with a second secret key.
A method to calculate message authentication codes using polynomials over
a finite field is called Galois message authentication code (GMAC) and is briefly
described in 1.9.6 Galois/Counter Mode.
1.9.1 CMAC
Another option for block cipher-based MACs suitable for variable-length mes-
sages is an algorithm called One-Key MAC (OMAC1), also referred to as CMAC:
with this algorithm, the last (padded) chunk of the plaintext is XORed with a key
derived from the pre-shared secret (see Figure 1.7).
Assume that K is the original key and that M is the message. The algorithm
begins by deriving keys K1 and K2 :
1. A block of zeroes is encrypted with K to obtain K0 : K0 = E(0).
2. If the most significant bit of K0 is 0, K1 = K0 1. Otherwise, K1 = K0
1 ⊕ C, where C is a well-known constant that only depends on the block
size.
3. If the most significant bit of K1 is 0, K2 = K1 1. Otherwise, K2 = K1
1 ⊕C, where C is the same constant as the previous step.
4 For example, in 2009 researchers Thai Duong and Juliano Rizzo used the attack to compromise
Flickr [20].
Building Blocks 25
The algorithm then uses one of the keys to adjust the input value. If M is
divided into chunks M1 . . . Mn , the last chunk may or may not be of the full size
of the encryption algorithm input block.
If the size of Mn is equal to that of the input block, it is XOR’ed with K1 .
Otherwise, it is padded with a single set bit and then with zero bits up to the full
length of the input block (ISO 9797 padding method 2). The result is XOR’ed
with K2 to obtain Mn0 , the new trailing chunk of the message M.
The algorithm continues with CBC encryption of the updated M with an IV
of 0. The output of the last iteration is the CMAC value for M.
An example calculation of a CMAC can be found in B.9 HMAC and CMAC.
1.9.2 HMAC
For hash-based MACs, there is an algorithm called hash message authentication
code (HMAC) [24]. The HMAC works with any hash function and any block size
of the hash function.
The HMAC takes a secret key, K, and the message M as the input.
To calculate the HMAC, the sender first makes sure the secret has the same
size as the hash function internal block. If it is longer than the block size, the
secret is hashed with the hash function; if it is shorter than the block size, it is
right-padded with zero.
In other words, if the length of K is greater than the hash function’s H internal
block size, K 0 = H(K) is used instead of K.
26 Cryptography for Payment Professionals
Two padding values are used with the HMAC algorithm, the inner (ipad) and
the outer (opad) paddings. The inner padding is the value of 0x36, repeated to
the size of K 0 , and the outer padding is the value of 0x5c, repeated to the same
length as the inner padding.
The output is calculated according to the formula: HMAC(K, M) = H((K 0 ⊕
opad) k H(K 0 ⊕ ipad) k M)):
1. The secret K is adjusted to the block size by either padding or hashing,
yielding K 0 .
2. K 0 is XORed with the inner padding (0x36), and the message M is ap-
pended to it.
3. The result is hashed using H to obtain the intermediate value V1 .
4. K 0 is XORed with the outer padding (0x5c), and the intermediate value of
V1 is appended to it.
5. The result is hashed to obtain the HMAC.
An example calculation of an HMAC can be found in Appendix B.9 HMAC
and CMAC.
In a simplified manner, the DSS uses that fact to sign a message as follows:
If m is a number that we want to sign using two secrets, x and k, we can prove
that we have access to these secrets by crafting a linear combination of them,
so that m = ax + bk, and then the recipient can calculate gm with g and m, and
gax+bk with gx and gk , without knowing the secrets, if the sender provides some
information on a and b.
Since the signature algorithm must work on messages of an arbitrary length,
the standard prescribes first applying a hash function to the message. In the initial
version, the hash function was SHA-1. In the later revisions of the standard,
Building Blocks 27
stronger hash functions are allowed, and only several leading bytes of their output
are taken as m.
The DSS works as follows. To prepare the keys:
1. Prime p is chosen, and q is a prime divisor of p − 1.
p−1 p−1
2. g = h q , where 1 < h < p − 1 such that h q mod p > 1. The values p,
q, and g are the domain parameters of the algorithm and can be shared
among many parties.
3. x is a randomly chosen number, the private key, 0 < x < q.
4. Compute y ≡ gx mod p. y is the public key.
To sign the message M:
1. Compute m = H(M) where H is the SHA-1 hash function.
2. Choose a random number k such that 0 < k < q. k is kept secret and is
different for each signature.
3. Set r = (gk mod p) mod q.
4. Set s = k−1 (m + xr) mod q.
The pair of values r and s is the signature. Note that m ≡ ks − xr (mod q), a
linear combination of k and x. k−1 is such that k−1 · k ≡ 1 mod q.
To verify the signature, the recipient has g, p, q, y, r, and s, as well as the
message M.
1. Compute m = H(M) using the SHA-1 algorithm.
2. Compute w = s−1 mod q. Note that w = (m + xr)−1 k mod q, but the recip-
ient does not know x and k.
3. Compute u1 = mw mod q. Here, u1 = (m + xr)−1 km mod q.
4. Compute u2 = rw mod q. Here, u2 = (m + xr)−1 kr mod q.
5. Compute v = ((gu1 )(yu2 ) mod p) mod q. If v = r, the signature is valid.
To understand why it works consider the expression of
u1 + xu2 = (m + xr)−1 km + (m + xr)−1 krx = k(m + xr)−1 (m + rx) = k
Due to the choice of p and q and some properties of finite fields, we can
actually substitute the values above and obtain:
gu1 yu2 mod p ≡ gu1 gxu2 mod p ≡ gu1 +xu2 mod p
which, if the values of s and r correspond to m, yield gk ≡ r mod p.
An example calculation of DSS can be found in Appendix B.8 DSS (DSA).
28 Cryptography for Payment Professionals
1.9.4 ECDSA
The elliptic-curve digital signature algorithm (ECDSA) uses very similar princi-
ples as the DSS (see Section 1.9.3 DSS (DSA)) but relies on scalar multiplication
of points on an elliptic curve instead of raising to power over a finite field. Re-
fer to Section 1.6 Elliptic-Curve Cryptography (ECC) to better understand the
underlying principles of point addition on a curve.
For the ECDSA, the idea is the same as with DSS: the first leading bytes of
the message hash, m, are represented as a linear combination of two points on
the curve. The sender knows the generating scalars for these points and can ef-
ficiently compute them. The recipient can validate that the computation matches
m without knowing the secret values.
The key generation for ECDSA works as follows:
1. Pick an elliptic curve.
2. Find a point G on the curve such that p·G = O, where p is a prime number.
3. Randomly select the private key, dA , such that 0 < dA < p, and compute
the public key, QA = dA · G.
To sign the message M:
1. Compute H(M), message hash value, and take a pre-defined number of
leading bits as m.
2. Choose a random number k such that 0 < k < p. The number must be
unique for each message.
3. Calculate hx1 , y1 i = k · G.
4. Let r = x1 mod p.
5. Let s = k−1 (m + rdA ) mod p. Note that m ≡ ks − rdA mod p.
6. If r or s is zero, repeat from step 2.
7. The signature is the pair (r, s).
To verify the signature:
1. Calculate m based on the message.
2. Let u1 = ms−1 mod p. Note that u1 equals mk(m + rdA )−1 mod p.
3. Let u2 = rs−1 mod p. Note that u2 equals x1 k(m + rdA )−1 mod p.
4. Calculate the curve point u − 1 · G + u2 · QA . Its first coordinate must be
equal to r mod p.
Building Blocks 29
u1 · G + u2 · QA = u1 · G + u2 dA · G = (u1 + u2 dA ) · G =
= (mk(m + rdA )−1 + x1 kdA (m + rdA )−1 ) · G =
= k(m + x1 dA )(m + rdA )−1 · G
Since the IV counter values are predictable, the encryption can commence in
parallel for multiple input blocks.
The encryption function gives this mode the “counter” part of its name. The
“Galois” part comes from its authentication function, GHASH. The exact com-
putation relies on polynomial arithmetics over the GF(2128 ) final field and is
beyond the scope of this book. However, at a high level, the idea is as follows:
1. A point H is calculated by encrypting a zero input block with the key K.
2. The ciphertext and the additional authentication data are concatenated
with their lengths.
3. The concatenated blocks are seen as coefficients of a polynomial.
4. The value of the polynomial at the point H is calculated and used as the
authentication value of the message.
The GCM algorithm is sometimes used for authentication only. In that case,
it is referred to as GMAC.
uniformly (i.e., each value has an equal probability of being output by the gener-
ator) and independently (i.e., knowing a previous value does not help predict the
next value).
However, computers are by design deterministic and are built to produce out-
puts that are as predictable as possible. Therefore, without special hardware, a
computer cannot produce a truly random sequence of bits. Luckily, this is not
critical for cryptography: a computer cannot practically work with a truly un-
breakable cipher, too, and the defining characteristic is just the computational
difficulty of a possible attack.
Therefore, a deterministic pseudo-random function (PRF) is used in com-
puting. A pseudo-random function can be computed effectively, and no efficient
algorithm can distinguish between the PRF output and that of a truly random
sequence.
The practice of applying cryptography often requires a reproducible genera-
tion of one or more keys out of a particular input which on its own may not be
suitable as an encryption key for a particular algorithm. For instance, an elliptic
curve Diffie-Helman scheme may use 168-bit values while the encryption algo-
rithm for the data is a 192-bit AES. Alternatively, derivation of several keys from
a single secret value may be required for a particular solution, as with AEAD
(see Section 1.9.5 Authenticated encryption).
Initially, the recommended action in such circumstances was to pad the
shared secret value to the necessary length, then XOR it with different values to
derive specific keys from it. This approach is often called the variant approach,
with the pre-defined XOR masks being the variants, and it was used, for instance,
to represent encrypted keys (see Section 5.3.1 X9.17 and variant formats), to de-
rive encryption and authentication keys for a key block (see Section 5.3.2 Key
block), and to derive keys as part of the DUKPT protocol (see Section 5.4 De-
rived Unique Key Per Transaction (DUKPT)).
Later, the key derivation process was augmented and standardized in the form
of key derivation functions (KDF) [27]. The KDF relies on a pseudo-random
function as its main working component.
In the counter mode, the input of the PRF function includes a counter value
which increases by one with each call. In the feedback mode, the output of the
previous call to the PRF function becomes part of the input of the following in-
vocation of the PRF function in addition to the counter. Finally, with the double-
pipeline mode, the PRF function is invoked twice: first to generate a sequence of
secret values that becomes part of the input for the second invocation of the PRF
function, which finally outputs the key.
Note that the counter and the PRF outputs form only part of the input in all
cases.
In the domain of payments, only the counter mode of the KDF is widely used.
Furthermore, in some instances, the requirement for the pseudorandom function
is slightly relaxed for the sake of speed of implementation.
In the counter mode, the input to the PRF function contains the following
elements:
The counter itself.
Label – a string that identifies the purpose for the derived material. For
example, data encryption or data authentication.
A zero separator byte.
Context – a string that defines information regarding the derived key. For
example, it may contain the details of the algorithm that will be using the
derived key.
The length, in bits, of the derived key.
The data is concatenated and fed into the pseudo-random function, using the
secret as the key. The process is repeated until the PRF produces enough bits.
This method is implemented with TR-31 key block representations of en-
crypted keys (see Section 5.3.2 Key Block).
just needs to validate the signature using a known public key to confirm that the
certificate is valid.
An arrangement that binds public keys and their signatures to entities such as
individuals or companies is called a public key infrastructure (PKI).
It is possible to create a hierarchy with arbitrary many levels of trust, or at
least as many as is practically useable. Such a hierarchy is best presented in the
form of a tree (see Figure 1.8).
The authority that needs to be explicitly trusted and does not derive the trust
elsewhere is called the root or certification authority (CA). While keys from
lower levels of the key trust hierarchy can be validated automatically, the public
keys of the root authorities must be deployed as part of the solution that relies on
them.
For example, root certificates are deployed as part of operating systems and
internet browsers. With these trusted certificates deployed, individual TLS cer-
tificates can be recognized and validated automatically. It is worth noting that
typically, CAs operate via intermediate certificates, and for a clean deployment
to validate certificates successfully, the intermediaries have to be present as well.
The standard that governs TLS certificates is ITU-T X.509 [30], it is identical
to ISO/IEC 9594-8 [31]. The X.509 format has many details regarding possible
certificate fields.
For each certificate, there is the issuer and the subject. The subject is the
entity that requests the certificate, such as a website or an API endpoint. The
issuer is the entity that signs the certificate.
Building Blocks 35
To obtain a certificate, the applicant (the future subject of the certificate) cre-
ates a certificate request (CSR) and sends it to the certificate authority, which
signs it and returns the certificate. The most common format for certificate re-
quests is defined in PKCS #10 [32], and its filename typically has extensions of
.pem, .cer, .crt, or .der.
Before generating the CSR file, the applicant must generate a public/private
key pair and then sign the certificate with the private key. The CA will sign the
certificate with theirs, and during the TLS handshake (see Section 4.1 Transport
Layer Security (TLS)), the private key of the applicant will be required to confirm
the ownership of the certificate.
The certificate request contains details such as the domain and the organiza-
tion name, the organizational unit, address, and email. Its filename extension is
often .csr.
The certificate authority can then use these details to perform several types
of validation, from the more superficial domain validation (confirming own-
ership of the domain) to the extended validation, which covers legal business
verification.
Since the private key must be retained and used alongside the certificate to
confirm its validity as part of the TLS protocol, it is usually kept in a secure
keystore on the relevant server. The PKCS #12 standard defines a secure file
format that can store multiple cryptographic objects in a single file [33]. This
format is often used for such a keystore. Its filename extension is .p12 or .pfx.
The certificate authorities manage certificate revocation lists (CRL). Such
lists contain certificates that had been revoked or put on hold prior to their ex-
piration date. The PKCS #7 standard defines a format that is frequently used to
contain CRLs [34]. While an online protocol, OCSP (see below), is gradually
replacing CRLs, specific solutions may have to continue relying on this method.
The Online Certificate Status Protocol (OCSP) is used to obtain a particular
certificate’s status [35]. A CA usually sets up an OCSP server, and its address
is available as part of the certificate. The OCSP responses from CAs are signed
by the CA private key, and the requesting entity can validate the signature using
CA’s public key. Modern browsers have the OCSP support built-in and use it by
default.
The file formats mentioned above and the OCSP protocol rely on ASN.1
[36–46]. The OpenSSL library provides a comprehensive set of tools to generate,
manipulate, and sign certificates, certificate requests, and certificate files and read
and write ASN.1 encodings.
Chapter 2
Understanding Payments
2.1 Introduction
Payment is the economically essential process of making a due return for goods
or services rendered. As people sold goods and provided services way before the
invention of the alphabet, this is one of the eldest processes known to man.
Initially, the payment was by barter; a measure of grain or livestock1 ad-
dressed the need for a unit of account. As the range of the trade increased, metals
came into use, and minted coins followed.
At a certain point, notes and cheques (which, in the form of IOUs, existed at
least since the clay tablets) became a significant payment instrument alongside
the coin and the bullion. Then, with time, paper displaced metal until, at some
point, computers and electronic communication means became affordable. This
way, money – essentially just an idea – finally broke free from any material
embodiment to which it formerly had to be tied.
Throughout history, payment has been a prime target for fraud and coun-
terfeiting. And with fraud and counterfeits came the counter measures. One of
the most famous is probably the anecdotal story of Archimedes inventing the
eponymous law: tasked with identifying whether a metal object was pure gold,
Archimedes made the reasonable choice of taking a bath instead. Being a keen
1 The interest as we know it is a future contract on the increase of a flock: an owner would lend out their
sheep to a herder, who would commit to return the original headcount plus a few extras, keeping whatever
extra animal yield his skill would provide.
DOI: 10.1201/9781003371366-2 37
38 Cryptography for Payment Professionals
closed schemes, the scheme itself provides service to cardholders and to busi-
nesses that accept its cards. With open schemes, multiple institutions can service
cardholders and merchants. Institutions (usually banks) that service cardholders
are called issuers since they issue payment instruments. Institutions that service
merchants are called acquirers since they acquire card payments. Of course, an
institution can be, and usually is, both2 .
In addition to the major participants such as issuers and acquirers, there are
many other players in the card payments ecosystem. Some simply provide ser-
vices to other entities. For example, organizations that resell payment processing
services and provide customer support are called independent sales organiza-
tions or ISOs.
Others have a technology platform that participates in the payment processing
flow. The most generic name for entities of that sort is payment service providers
(PSPs). In some cases, when the PSP is also handling payouts to merchants, the
term used is payment facilitators (PFs or PayFacs).
Payment cards are used in two major scenarios, depending on whether the
card is physically present at the point of sale. Accordingly, these are card present
and card not present.
Today, a smartphone or a wearable device can replace a plastic card. For
simplicity, we will use the term card to denote all such objects or devices.
The devices used to capture card payments are often called point-of-sale de-
vices or POS. This term is imprecise since a cashier supports many additional
functions in addition to capturing card payments. An alternative term for such a
device is pin entry device (PED), card reader, point of interaction (POI), or PIN
pad. All, too, err on the side of simplicity.
The flow of a payment transaction is shown in Figure 2.1. The POS devices
interact with a terminal management system (TMS), which connects to the ac-
quirer host and through it to the scheme network. The transactions flow through
2 Prima facie, open schemes introduce extra competing players and have a more complicated ecosystem
than closed schemes. However, in reality open schemes completely dominate the card payments market,
leaving only single-digit market shares to the closed schemes.
40 Cryptography for Payment Professionals
the network to the issuer host. The interfaces between the TMS, the hosts, and
the scheme network are usually called host-to-host interfaces.
In most cases, for a card payment, card details are captured on the
eCommerce website, in the mobile app, or at a payment terminal and forwarded
through several intermediaries to the card scheme network and from there to the
issuer. The issuer analyzes the details and decides whether to approve or decline
the request.
In some cases, the issuer can delegate this decision. For instance, major
schemes support a version of stand-in processing. In case of issuer unavailability
or according to a rule, the scheme can approve or decline the request and only
notify the issuer about the decision made.
The eCommerce website will not connect directly to a card scheme network;
in most cases, the websites do not connect directly to acquiring hosts. A software
component, called a payment gateway, usually captures the online transaction
and then forwards it to a back-end processor.
With chip-based cards, the issuer can configure (personalize) the chip on the
card to authorize certain transactions without communication to the issuer, sim-
ply based on the exchange of messages between the terminal and the card.
Depending on the scheme protocol, the request might have to be followed
with a clearing message or a clearing file to perform the actual financial opera-
tion. The clearing message may contain additional details regarding the transac-
tion that were unavailable during authorization. There might be just the clearing
record in case of offline authorization (when the terminal authorized the transac-
tion based on its exchange with the chip on the card).
During an online transaction performed via the browser, it is enough to sim-
ply pass the card details to the payment gateway (the specialized software for
processing payments) to perform the transaction. However, based on the secu-
rity needs of the transaction, the website may decide or even have to redirect the
user to their issuer’s specialized system for some extra authentication steps, upon
completion of which the user will be redirected back.
Examples of card schemes include Visa, Mastercard, UnionPay (four-party
schemes), American Express, Discover/Diners, and JCB (three-party schemes).
2.2.4 Cryptocurrencies
Other payment methods mentioned here require cryptography in security pro-
tocols but can perfectly function without any. If an algorithm used by card
payments becomes obsolete because of a vulnerability or quantum supremacy,
schemes can overcome this with a slight change in a specific protocol.
Cryptocurrencies differ in that, by design, they rely on cryptographic algo-
rithms to work. So a discovered vulnerability will mandate a significant reengi-
neering of a cryptocurrency, which is not always possible due to the distributed
and anarchical nature of the crypto coins.
Cryptocurrencies are a fascinating topic with many solutions and many de-
velopments, but most of them are beyond the scope of this book. To process a
payment with a cryptocurrency, one need not understand every possible detail of
42 Cryptography for Payment Professionals
its consensus mechanism nor be able to develop smart contracts. Hence, we will
illustrate the principles of blockchain technology and cryptocurrencies, follow-
ing the structure of bitcoin but not in every detail.
There are two mechanisms that power cryptocurrencies: hash functions and
asymmetric cryptography. The former ensures that the underlying blockchain can
work; the latter allows authentication and authorization of payments over it.
2.2.4.1 Blockchain
Blockchain is a method to chain blocks of transactions. It is unrelated to the
CBC (cipher block chaining) except in name. It is also called distributed ledger
technology (DLT).
Imagine a ledger to which users write transactions between some accounts.
Once we have accumulated a certain number of transactions or a predefined time
has elapsed, we sign off the block by computing a hash of its contents. We then
start a new block by writing this hash value first and adding the transactions to
the block after that (see Figure 2.2).
Since our ledger is distributed by nature, it is public, and many entities (or
nodes) hold its copies. Due to the hashing and chaining of the hashes, an en-
tity cannot falsify a historical transaction without a trace: the block’s hash will
necessarily change, the next block will have to be modified, and its hash will
change too, and so on. Hence, to modify a historical transaction, it is necessary
to recalculate the entire chain of blocks after the one where it belonged.
Besides malicious intent, such a disparity in the blockchain structure can oc-
cur for technical reasons, such as a network split, which is not uncommon in
distributed systems.
As the classic blockchain implementations were designed to be distributed
and open to anyone, there was a need for a mechanism to resolve such disparities
and protect the system from bad players. Such a mechanism is called a consensus
mechanism.
Several alternatives to the consensus mechanism design exist today, but the
proof-of-work is the oldest and the most widespread. Its goal is to artificially
increase the computational complexity of hashing when signing the block while
leaving the validation of the hash computationally easy.
With this method, in case of a dispute, the combined computational power
of each side of the argument will resolve it. If we assume that the combined
computational power of all participating nodes is significant and the power is
distributed more or less evenly among the nodes, this is not a bad solution.
If a part of the network splits off, the majority of the nodes will force their
version of the ledger on the minority; if there is malicious intent by a portion of
the network, the majority that is not part of the plot will neutralize it.
One way to increase the complexity of computing a hash while leaving its
validation unaffected is to add a salt value to it and enforce a constraint on the
resulting value of the hash (see also Section 1.7 Hash Functions). Participating
nodes have the liberty of adding a nonce (a number that is passed along openly as
part of each block in the blockchain). However, to successfully sign off a block,
a node needs to make sure the hash, if viewed as a large number, complies with
a demand of being smaller than a particular threshold (or starts with a particular
number of zero bytes).
As it is impossible to predict the nonce with which the hash will comply with
this requirement, nodes must “brute-force” nonce values by trying to add them to
the block and hash the result. This process can be optimized to make better use
of the hardware, moved to specialized hardware such as GPUs, parallelized, and
scaled, but it is still computationally hard. The process requires an investment
of computing power and electricity, and at the same time, it is essential for the
proper functioning of the blockchain network. To provide an economic reward
for network participants, bitcoin and some other protocols compensate the nodes
that succeed in signing a block with some amount of the currency, and the overall
process is called mining.
To perform operations over a blockchain, consumers submit transactions to
a pool from which the mining nodes retrieve them, pack them into blocks, and
sign them. Many blockchain implementations have a mechanism for specifying
a fee that will be paid by the submitting consumer to the mining node in case of
a successful signing of the transaction into a block.
3.1 Overview
Prior to the introduction of EMV, the magnetic stripe technology relied on three
relatively simple measures: visual elements on the physical card itself, a signa-
ture on the back, and the CVC/CVV1 value embedded into the track data.
The EMV technology added several much more advanced card authentication
and cardholder verification methods, to which the contactless technology added
relatively little. The technology relies on communications between the terminal
and the chip embedded into the plastic. The chip is capable of cryptographic
calculations, as well as the terminal. Furthermore, the chip can store secret keys
securely.
The chip allows many opportunities for improved security of payments. For
instance, the terminal can use cryptography to establish the chip’s authenticity.
The chip and the terminal can exchange messages to approve the transaction
offline. The chip can generate an encrypted message to the issuer and then con-
firm the authenticity of the issuer’s response. Finally, the chip can independently
DOI: 10.1201/9781003371366-3 47
48 Cryptography for Payment Professionals
validate the cardholder’s PIN without sending dedicated messages through the
payment network.
The detailed steps of the card interaction with the terminal are pretty com-
plex and lengthy, and most do not relate directly to security or cryptography. An
introductory overview is given in “Acquiring Card Payments.” [47] Also, the full
standard is available for free from the EMVCo website [48].
EMV transactions happen in two ways: either by inserting the card into the
terminal, i.e., via an electrical contact and in a time-unbound manner, or by tap-
ping the card or mobile device on a compatible contactless reader.
The original EMV standard was contact-only, and contactless methods grad-
ually emerged several years later. Hence, the latter built off the former; further-
more, several bodies made concurrent attempts at defining a contactless proto-
col, and so, multiple concurrent implementations, also called Kernels, exist in
the EMV Contactless standard.
However, all security mechanisms in the Contactless kernels are a subset of
the original EMV standard; therefore, describing them for EMV Contact will
cover all potential implementation needs for the Contactless protocol.
various usage limitations for the card. Its most frequent use today is to indicate
the presence of a chip on the card. When a card with such a service code is swiped
through its magstripe reader, a supporting terminal will prompt the cardholder to
insert the chip or tap the card on the contactless reader instead.
There are at least three possible fraud scenarios: modifying the expiry date,
modifying the service code, or constructing the track data from stolen partial
information (e.g., the PAN).
Both Track 1 and Track 2 allocate several digits for the so-called “discre-
tionary data” to allow for a digital signature. These are decimal digits rather
than any binary value since Track 2 encoding supports a limited set of charac-
ters. Card schemes and issuers can decide which values to embed there and the
exact location. Specifically, the digits of the card verification value (CVV) or
card verification code (CVC) are stored in the discretionary data parts of Tracks
1 and 2.
With the spread of online payments, schemes use the same mechanism to
generate the CVV2 value that is printed on the card. The two CVV values sign
different service codes to prevent the use of online data for in-store payments and
vice versa. The Track 1/Track 2 CVV uses the actual service code of the card,
while the CVV2 value uses the service code of 000.
The other use for discretionary data follows from some factors in the world
of card payment that require certain flexibility for validation of the cardholder’s
PIN. On the one hand, the PIN must be kept secret, and the issuer cannot entrust
it to a third party. On the other hand, the issuer often delegates transaction pro-
cessing to either a processing center or to the scheme’s “stand-in” facilities1 . To
meet these constraints, issuers can opt to use PIN verification value (PVV).
egates the processing for particular transaction criteria. The stand-in maintains counters for accumulated
amounts and can approve or decline transactions.
50 Cryptography for Payment Professionals
For example, the PAN number of 98765432109876, with the expiry date
of October 2025 and service code of 101, will be represented as B1 =
0098765432109876, B2 = 1025101000000000.
Then, the flow is as follows:
1. B1 is encrypted using the DES algorithm with KA .
2. The result is XOR-ed with B2 .
3. The result of the XOR operation is encrypted using the TDES algorithm
in the EDE pattern with KB .
4. The outcome is decimalized (i.e., the decimal digits are extracted from the
result in a particular order, see Appendix A.7 Decimalization).
The further use of the outcome depends on the type of the CVV. CVV1 will
be embedded in the discretionary data section of Tracks 1 and 2 on the magnetic
stripe. CVV2 will be printed on the card for online and telephone purchases.
In the case of a chip-based card, the third CVV value called iCVV is cal-
culated with a service code value of 999. Since the chip on the card returns a
“mock” Track 2 value as part of the exchange with the terminal, it must con-
tain valid discretionary data. However, if this Track 2 value is intercepted, the
fraudster will be able to use it neither for a magnetic stripe counterfeit (since the
service code signed by the CVV on the magstripe reflects the permitted functions
and differs from 999) nor for online purchases (since the CVV2 value reflects the
service code of 000).
An example C implementation of CVV calculation can be found in
src/payments.c, function compute cvv().
4. The input vector is encrypted using the TDES algorithm in the EDE pat-
tern with PVK-A and PVK-B.
5. The result is decimalized, and four digits are extracted.
The PVV and the PVKI are then embedded into the discretionary data area of
the magnetic stripe tracks. The probability of a wrong PIN yielding the correct
PVV is negligible.
The method has the obvious drawback of limiting the PIN to 4 meaningful
digits. However, it has the advantage of greatly simplifying managing the PIN
values.
With PVV, the issuer no longer needs to store encrypted PIN values for each
cardholder. It is sufficient to generate the PVV, write it to the card, and discard the
actual PIN, keeping only the PVK pair. It is much easier to manage and protect
a handful of PVK values than a million encrypted PIN values.
Furthermore, by (securely) communicating the PVK values to the schemes,
the issuer can delegate PIN verification to the network, offloading their own sys-
tems.
Of course, in case of a compromise of a PVK pair, all cards that use it must
be replaced. Another disadvantage of this method besides the PIN length is that
the PIN is immutable once written to the card.
An example C implementation of PVV generation can be found in
src/payments.c, function compute pvv().
The terminal and the card begin from the application selection, matching
schemes and applications that are supported on the terminal to counterparts on
the card. In some scenarios, the terminal may ask the cardholder to choose the
application (e.g., debit or credit payment).
At later stages of the flow, both terminal and the card will need to decide how
to proceed with the transaction, if at all. For that, the terminal needs to learn more
about the card, and, conversely, the card needs to learn details about the terminal
and the transaction it will be requested to perform.
Once the terminal retrieves the card data, it can perform a card authentication.
It is an optional step as card authentication can be combined with one of the later
steps of the transaction.
After that, the terminal and the card negotiate the cardholder verification
method and execute the chosen method.
The terminal and the card then negotiate the method to confirm the transac-
tion, including whether to involve the card issuer. The card and the terminal can
negotiate a transaction result (approval or decline) without involving the issuer.
Alternatively, an application request cryptogram (ARQC) may be sent to the is-
suer, and the card uses the issuer’s response (application response cryptogram or
ARPC) to authenticate the issuer.
The issuer may transmit some commands to the card that the latter will exe-
cute.
Finally, according to the protocol, the card should produce a transaction cer-
tificate confirming the transaction’s conclusion. In practice, once the authoriza-
tion request to the issuer has been approved, many implementations opt not to
transmit that final certificate.
With the static data authentication, the terminal retrieves a list of static fields,
a public key, and the signature during the initial retrieval of card data. The termi-
nal recovers and validates a public key (later on) using one of the card scheme
certificates it has to hold and then uses the recovered value to validate the static
data.
With the dynamic and combined data authentication, the terminal recovers
and validates a public key and then sends data to the card. The data includes a
random number, protecting the interaction from possible replay attacks. The card
responds with a signature value that the terminal can validate using the recovered
public key.
The dynamic/combined data authentication places more demand on the card’s
resources, requiring a more powerful chip.
Besides a difference in the process itself, the public keys with which the au-
thentication is performed differ. For the static authentication process, the terminal
recovers an issuer public key, which is signed with a card scheme key. For the
dynamic authentication process, the terminal recovers the issuer key, then uses it
to recover the ICC public key, which is individually generated per card.
The terminal reads from the card the following data to be able to recover the
issuer public key: the CA public key index, the issuer public key certificate, the
optional issuer public key remainder, and the issuer public key exponent.
to the correct card. Hence, the terminal must compare the PAN that it has recov-
ered from the ICC Public Key Certificate value to the one it obtained via other
commands from the card application.
The input value for the ICC key validation hash also includes the static au-
thentication data, gathered precisely in the same way as described in Section
3.4.1 Static data authentication. See also Figure 3.2 for details on the ICC key
recovery header.
An example of ICC key recovery can be found in C.3 ICC Certificate Gener-
ation and Public Key Recovery.
data and an “unpredictable” number and sends it to the card as part of a dedicated
command2 .
The mechanism that defines the exact values is called the dynamic data object
list (DDOL). The list will contain the tags and the lengths of the values the card
requires for the dynamic data authentication. If there is no DDOL on the card, the
terminal must have a default DDOL defined by the payment scheme in question.
If the card’s DDOL does not contain the unpredictable number value, the DDA
is considered failed.
If there is a suitable DDOL either on the card or the terminal, the latter sends
the command to the card, concatenating values in accordance with the DDOL.
The card generates a dynamic data value, either random or a counter, and con-
catenates the header fields, the dynamic data value, the padding, and the original
data as provided by the terminal. The card then calculates the hash of the data
(see Figure 3.3).
To sign the data with the ICC key, the card concatenates the sentinel value
0x6A with format indicators, appends the dynamic data that it has used as part
of the hash input, adds padding, the hash result, and the data trailer 0xBC, then
encrypts the resulting sequence with its private key. The ciphertext, the firstter-
mdynamic signature, is sent to the terminal in the command response.
The terminal uses the recovered ICC public key to decrypt the response. The
terminal knew all the hash function inputs except the card’s dynamic data. The
card provides this value separately, and the terminal can access it by decrypting
the dynamic signature value. Once the terminal has obtained this value, it can
calculate and validate the hash.
An example of Dynamic Signature Generation can be found at Section C.4
Dynamic Signature Generation.
2 INTERNAL AUTHENTICATE [53]
Securing the Plastic: Magnetic Stripe and EMV 59
the Pin Encipherment Public Key or the ICC Public Key), encrypts the result,
and sends it to the card for verification.
The card, in its turn, deciphers the input, checks the header value, and then
confirms that the unpredictable number is the one formerly provided in response
to the GET CHALLENGE command.
The second block is the same in both formats and contains 12 right-most
digits of the PAN (denoted by N in the figure), excluding the check digit, left-
padded with zeroes.
Format 4 is an extension and improvement of Formats 0 and 3, which enlarges
the block size to 16 bytes, suitable for the AES algorithm. Format 4 also uses two
16-byte blocks, but instead of XOR-ing them together prior to encryption, they
are encrypted in the CBC mode (see Section 1.8.1 Padding and cipher block
chaining), i.e., the first block is encrypted, XOR-ed with the second block, and
the result is encrypted again.
The first block of Format 4 is similar to the first block of Format 0, except
that the value of 0xA is used for padding instead of 0xF. The vector is further
padded with 8 more random bytes.
The second block contains the PAN without the check digit, padded with
zero. The first nibble of the second block indicates the PAN length l without the
check digit and minus 12. That is, if the PAN has 14 digits, the length nibble is
14 − 1 − 12 = 1, where the extra −1 accounts for the check digit.
The PIN Block Format 1 is intended for use when no PAN data is available. It
is formed by concatenating a single nibble indicator of the format, 0x1, with the
PIN length in nibbles, followed by the PIN value and padded to the full length of
the block with either a random value or some unique transaction identifier.
receive the PIN block under a symmetric algorithm, and, therefore, the use of
RSA for the online PIN encryption, while theoretically possible, is rare if not
nonexistent.
Therefore, point-of-sale devices, terminals, acquirers, schemes, and issuers
need key management schemes or processes to ensure the keys’ secure con-
veyance for these algorithms.
A direct approach would be to store unique keys per each issuer or acquirer
and each terminal, which is clearly not feasible. Two principles were adopted to
simplify the task.
First, the chain of hosts which participates in a transaction flow is divided into
zones, with a translation of encrypted data from one key to another happening
within hosts on zone boundaries.
Second, since the cards and terminals operate in an environment far less se-
cure than the host, the system should resist a mass compromise: keys stolen from
a single POS should not endanger all other POS devices.
However, storing individual keys per card or terminal would create a con-
siderable overhead for the corresponding hosts. Hence, in these scenarios, the
preferred key management mechanism is to have each card or terminal possess
an individual key, while the acquirer or the issuer host can easily derive it from a
securely stored master value.
Methods for managing keys between hosts are described in Section 5.2 Zon-
ing.
There are two popular methods for the segment between the terminal and
the terminal management system: the Master/Session protocol and the DUKPT
algorithm.
With the Master/Session protocol, a master key is securely preloaded into
the POS devices. The host generates and communicates a session key, encrypted
with the master key, to the terminal during device operation. Since the device
already has the preinstalled master key, ut can decrypt the session key and then
use it to encrypt PIN blocks.
The Derived Unique Key Per Transaction (DUKPT) protocol allows gener-
ating an individual key per transaction on the POS device based on an initial
preloaded key. The host can independently generate the same key value, thus
establishing the mutually shared key for the symmetric encryption algorithm.
The protocol is described in Section 5.4 Derived Unique Key Per Transaction
(DUKPT).
A terminal can make three possible decisions: approve the transaction of-
fline, request an online issuer confirmation, and decline the transaction. The card
confirms the decision by generating a cryptogram. Three cryptogram types cor-
respond to the three possible decisions: the Transaction Certificate (TC) for an
approved transaction, the Authorization Request Cryptogram (ARQC), and the
Application Authentication Cryptogram (AAC).
The terminal suggests its decision to the card, which can then either agree and
cooperate with the terminal or lower the level of the cryptogram it will generate.
The priority of the cryptograms is TC, ARQC, and AAC. Correspondingly, the
card can always agree to the terminal’s suggestion, decline the transaction with
an AAC, or respond with an ARQC to the request of the TC.
The ARQC is sent to the issuer alongside standard transaction details. The
issuer provides an Authorization Response Cryptogram (ARPC) and, possibly,
some commands for the card to execute.
Since, in the case of contact transactions, the time for the interaction with the
card is unlimited, the protocol allows waiting for the issuer’s response to arrive
and be sent to the card for an additional issuer authentication, thus ensuring the
response indeed came from a valid issuer. Most implementations do not support
this step with contactless transactions due to the lack of time.
For the cryptogram, the EMV standard does not mandate a specific imple-
mentation of the ARQC, allowing each scheme or even issuer to devise a scheme
of their own. However, the standard implementation is widely used with minor
changes in practice.
The recommended approach for the symmetric key used to generate cryp-
tograms is to create a master key on the issuer side and store it securely, then use
it to derive the ICC Master Keys per each card. During the interaction with the
terminal, the card can generate a one-time Session key which will be used for the
cryptogram itself.
For the TDES encryption, the standard provides two options. The options,
named Option A and Option B, generate the base of the input for the TDES
algorithm, denoted as Y .
Option A applies when the application PAN is equal to or less than 16 digits
long. The steps are as follows:
1. The sequence number (CSN) is appended to the PAN.
2. The result is right-padded with zeros if its length is less than 8 bytes.
3. The rightmost 8 bytes of the result is the sought Y .
Option B applies when the application PAN is longer than 16 digits. The steps
of option B are as follows:
1. The CSN is appended to the PAN. If no CSN is available, two zeroes are
appended instead. If the total number of nibbles is odd, the value is left
padded with a single zero.
2. The result is hashed using SHA-1.
3. The result of the hash, in turn, is decimalized (see Section A.7 Decimal-
ization) to obtain 16 decimal digits.
4. The output of the decimalization is the value Y .
Once Y is available, it can be used to calculate the ICC master key. The left
and the right halves of the ICC master key are calculated separately:
1. The value of Y is encrypted with TDES using the issuer master key to
obtain the left half of the key.
2. The value of Y is negated by applying either bitwise NOT or XOR with
the mask of 0xFF.
3. The outcome of Step 2 is encrypted with TDES using the issuer master
key to obtain the right half of the key.
4. The left and the right halves are concatenated, and parity bits are corrected
in the outcome to form a valid TDES key.
The method for master key derivation with AES (also called Option C) is as
follows:
1. The PAN and the CSN are concatenated and right padded with zeros to the
length of 16 bytes to obtain Y .
2. Y is encrypted with AES using the issuer master key.
66 Cryptography for Payment Professionals
3. If the desired key length is greater than 128 bit, Y is negated to obtain Y 0 :
Y 0 =∼ Y .
4. Y 0 is encrypted with AES using the issuer master key.
5. Depending on the desired key length, either 64 or 128 bits of encrypted
Y 0 are appended to the encrypted Y to obtain the AES key of the desired
length.
Examples of ICC key derivation can be found in C.5 ICC Master Key Deriva-
tion.
5. The two outputs are concatenated. If the desired key is an AES-192 key,
the output is truncated to 192 bits. If the desired key is a TDES key, the
parity bit of each byte is adjusted.
An example of session key derivation can be found in Section 3.4.4.2 Session
key derivation.
confirming that the responder to the transaction request is the entity that is au-
thorized to do so.
The authentication response cryptogram, ARPC, is received from the issuer
and passed to the card for that purpose.
Before generating the response cryptogram, the issuer decides on the transac-
tion’s authorization response code (ARC). It is primarily a business/risk decision,
although the issuer can reject the transaction due to failed online PIN verification
or other authentication error. The ARC is a two-byte field.
The EMV standard recommends two alternatives for issuer authentication.
Method 1 generates an 8-byte ARPC value. With this method, the issuer performs
the following actions:
1. The card’s ICC master key is derived from the issuer master key.
2. The card’s session key is derived from the ICC master key.
3. The ARC is right-padded with zeroes to the required block size (depending
on the algorithm used), and the ARQC, as received, is XORed with the
padded ARC. It is the same as XORing the first two bytes of the ARQC
with the ARC.
4. The outcome is encrypted using TDES or AES with the previously recov-
ered session key.
5. The output of the encryption is the ARPC value.
This method allows the card to confirm that the issuer has indeed responded
while also digitally signing the authorization response code. While performing
the reverse actions to confirm the ARPC, the card will only obtain the original
ARQC value if the independently provided ARC is identical to the one used by
the issuer. Therefore, an attacker cannot modify that cleartext field to make a
decline transaction appear approved.
Method 2 generates a 4-byte ARPC value. The issuer can return additional or
more elaborate response elements to the card. One such element is called the card
status update (CSU), and it is of 4-byte length. In addition to it, the issuer may
return up to 8 bytes of proprietary authentication data to the card. To calculate
the ARPC, the issuer performs the following actions:
1. The card’s ICC master key is derived from the issuer master key.
2. The card’s session key is derived from the ICC master key.
3. The ARQC, as received, is concatenated with the CSU, and the proprietary
authentication data.
4. The signature of the resulting value is generated using the same method as
with the ARQC (see Section 3.4.4.3 Generation of the cryptogram).
Securing the Plastic: Magnetic Stripe and EMV 69
5. Four leading bytes of the signature are concatenated with the CSU and the
proprietary authentication data.
6. The output is the ARPC value.
An example of the ARPC generation can be found in Section C.8 ARPC Gen-
eration.
Chapter 4
Unlike other topics this book covers, network security is not specific to payments.
However, it is an essential component at the backbone of any payment solution
and, therefore, must be well understood by a payments professional.
Two protocol families are particularly relevant to the field of payments. One
is the Transport Layer Security (TLS), formerly known as the secure sockets layer
(SSL); the other is the JSON Web Encryption, JSON Web Signature, JSON Web
Algorithms, JSON Web Key, and more (JWE, JWS, JWA, and JWK, accordingly),
jointly known as JSON Object Signing and Encryption (JOSE).
The TLS protocol is used practically everywhere. It is a standard, convenient
and secure way to safeguard the transmission of sensitive data in a manner that is
nearly transparent to many payment applications. The design goals of the proto-
col were to achieve confidentiality, authentication, and integrity over a network
that is entirely under the control of an attacker.
The JOSE family of protocols provides JSON-based structures for commu-
nicating encrypted or signed data in the JSON format. Adherence to this format
considerably simplifies the development of security features in internet and mo-
bile applications, allowing for another layer of application-level security. These
protocols are, for example, used in the EMV 3D-S Secure authentication protocol
to secure the mobile SDK.
In addition to these network layer protocols, the chapter contains the descrip-
tion of the EMV 3-D Secure protocol, which was designed purposefully to au-
thenticate online card payments.
DOI: 10.1201/9781003371366-4 71
72 Cryptography for Payment Professionals
1. Key Exchange – during this phase, the parties agree on a key exchange
method and choose cipher options.
2. Server Parameters – during this phase, other handshake parameters are
established (for example, whether client authentication is required).
3. Authentication – the client authenticates the server and vice versa. The
server can request to delay the client authentication to a later stage in the
data exchange.
4.1.1 Extensions
Besides the basic protocol, TLS allows for multiple protocol extensions. IANA
maintains the official registry of standard extensions, while private-use exten-
sions are also allowed [57].
The extensions are part of a fixed field in the protocol messages. Instead, they
are sent in the tag-length-value format. Despite the name, some extensions must
be present to establish the session successfully.
For example, if the server supports multiple virtual hosts, the client can spec-
ify during the handshake which specific host it is willing to access. The client
can do so by using the standard extension server name.
Another example is the cryptographic negotiation. Let us assume that the
client has indicated that it wishes to exchange keys using the elliptic curve Diffie-
Helman method. The client can further specify the particular elliptic curves it
supports through the supported groups extension.
The client and the server use the supported versions extension to indicate
TLS versions they are willing to support in this session.
The parties also exchange a list of supported CAs via the certificate
authorities extension. The CAs are provided as X.501 distinguished names
[58].
While there are many very well-built implementations of the protocol in li-
braries and network appliances, it has many options and parameters, a high-level
understanding of which can help troubleshoot a system or enhance its security.
can also offer public values for some of the supported algorithms to
the server. E.g., if the protocol is Diffie-Hellman, the client can send
A ≡ ga mod p in the key share extension. Alternatively, the client can
send nothing, in which case the server will respond with a list of supported
algorithms, but then another handshake roundtrip will be required.
3. As part of the later Authentication stage, the server and the client sign
the messages they send. Furthermore, the certificates which are validated
during the Authentication stage are also signed using some signature algo-
rithms. Both are independent of the signature method used in the AEAD
scheme. Hence, the message contains a signature algorithms and a
signature algorithms cert extensions to specify the methods that
will be used for these purposes.
4. It is possible to pre-share keys for the data encryption via some secure
external channel. Such a pre-shared key (PSK) can be used to encrypt the
application data or as a basis to generate a new session key (thus protecting
new sessions from a possible compromise of an old key). These two op-
tions are provided via pre shared key and psk key exchange modes
extensions, accordingly. Furthermore, TLS uses the PSK facility to resume
a previously established session.
Upon receiving the ClientHello message, the server responds with a Server-
Hello or a HelloRetryRequest.
The HelloRetryRequest message means that the server would like the client
to retry and provide more details with its next ClientHello session. For example,
as mentioned above, if the client did not provide the first value of the final field
DH protocol, but the protocol itself is acceptable to both parties, the server will
use the HelloRetryRequest message to request that the client would share the
value of A.
The server would send the ServerHello message if it could select the neces-
sary cryptographic methods and find a set of handshake parameters that is suf-
ficient and acceptable to proceed. The message can only include a single cipher
method.
Both the client and the server include a random number in their respective
Hello messages. This measure helps prevent replay attacks.
4.1.4 Authentication
The authentication phase of the handshake consists of three messages, Certifi-
cate, CertificateVerify, and Finished.
Besides the message-specific data, each message contains an HMAC of all
previous messages of the ongoing handshake.
The Certificate message communicates the certificate chain of the party that
sends it. The CertificateVerify message contains proof that the sending party in-
deed possesses the private key of the certificate. Finished indicates that the party
has completed its side of the handshake, has received and validated all messages
from the counterpart, and is ready to exchange data once it receives and validates
the Finished message from the other party.
The server always sends the Certificate and the CertificateVerify messages to
the client, followed by the Finished message.
The client sends its Certificate and CertificateVerify only if requested by the
server. If the server did not ask for the client’s certificates, it sends the Finished
message only.
the removal of root CAs which was mandatory for a compliant solution.
Securing the Network 77
alg parameter, identifying the algorithm used for the signature. The value
is according to JWA and the corresponding IANA registry (see Section
4.2.1 JSON web algorithms (JWA)) [64].
jku parameter, containing a URL through which keys for signature vali-
dation can be retrieved.
jwk parameter contains the public key corresponding to the key used to
sign the JWS in the JWK format (see Section 4.2.2 JSON web key (JWK)).
kid parameter, specifying the key ID.
x5u, x5c, x5t, x5t#S256 optional parameters, containing X.509 certifi-
cate details (see Section 4.2.2 JSON web key (JWK)).
typ and cty parameters, identifying the content type, and crit param-
eter, identifying a protocol extension parameter critical for the receiving
application to process.
The JWS Compact Serialization contains three base64-encoded strings, sep-
arated by a dot: the header, the payload, and the signature.
To calculate a basic JWS, first, the protected header must be composed. As-
suming that no certificate needs to be checked and that the key and the content
type are known to the recipient, the protected header only needs to contain the
algorithm. Assuming that the header is Base64-encoded to V and the payload is
Base64-encoded to P, the signature is obtained as S = H(V k . k P). The result is
Base64 encoded, and the full compact JSON representation of the JWS will be
of the form V k . k P k . k S.
4.3.2 Overview
At a certain point, a private company has developed a new method for card-
holder authentication in the online environment. The method, later renamed “3D
Secure,” delegated the cardholder authentication to the issuer [47].
82 Cryptography for Payment Professionals
The card payment industry, via the EMV standards body, had considered the
drawbacks and the new developments and defined a new, open standard called
EMV 3-D Secure [66].
For example, the standard includes an additional message from the ACS back
to the intermediate directory service with the authentication result, which now
addresses cases when the cardholder abandoned the authentication.
The frictionless flow and its opposite, the challenge flow, were made an in-
tegral part of the standard, removing the need to actually redirect the user if the
issuer agrees to authenticate without any user interaction.
In order to simplify the issuer’s decision on frictionless authentication, the
standard has added an optional step of device fingerprinting. The consumer
would be silently redirected to the issuer ACS, which would capture the source
IP and additional browser characteristics. If found matching the previously au-
thenticated transactions, this additional information can lead the issuer to agree
to a frictionless flow. In addition, an out-of-band (decoupled) authentication is
supported: for example, the issuer can opt to confirm the payment by sending a
push notification to the issuer app on the cardholder’s phone.
The standard also introduces support for mobile applications, allowing sup-
port for a native interface rendered by the app instead of the issuer ACS.
sends a results request (RReq) to the Directory Server, which forwards it to the
3DS Server. See Figure 4.2.
The ACS generates its own key pair (QT , dT ) on the ACS side. The server
possesses a key pair and a certificate for it, signed by the Directory Server CA.
If the server requires a challenge, it performs the following steps:
1. Signs the received ephemeral key QC , its own ephemeral key QT , and the
URL for the secure channel with its private key.
2. Generates two encryption keys based on the elliptic curve Diffie Hellman
exchange process, one for each direction. See Section 1.6.2 Elliptic curve
Diffie-Hellman (ECDH) for the secret establishment process description.
3. Includes the signature and the certificate in the response message.
The key generation process for the encryption of the initial device informa-
tion and the subsequent challenge flow is using the Concat KDF method (see
Section 1.10.2 ConcatKDF key derivation function) with the following parame-
ters:
Key data length is 256.
AlgorithmID is an empty string (meaning that only four 0x00 bytes of
length are transmitted as an empty value indicator).
PartyUIInfo is an empty string. Likewise, four zero bytes indicate the
absence of value.
PartyVInfo is an ASCII string with either the directory server ID or the
reference number of the app SDK, depending on the specific use case.
Both values are also transmitted in the clear alongside the key.
SuppPubInfo is the 32-bit key data length value, 0x0000 0100.
SuppPrivInfo is empty.
The outgoing CReq is encrypted and signed using either AES 128 in the
GCM mode (see Section 1.9.6 Galois/Counter mode) or AES 128 in CBC mode
with SHA-256 HMAC signature and is sent according to the JWE representation
(see Section 4.2.4 JSON web encryption (JWE)). The response CRes is encrypted
and signed using one of the same methods but with a different key.
Chapter 5
5.1 Introduction
The card schemes use the PIN entry as the ultimate method to authenticate the
cardholder. It does, indeed, introduce a second-factor authentication to an in-
store card payment: if the card itself is a possession factor (“something you
have”), the PIN is a knowledge factor (“something you know”).
Because of that, the PIN value is considered the most sensitive part of a card
payment transaction. Special physical and logical requirements apply to devices
that capture the PIN entered by the cardholder.
The legacy method of PIN verification values (see Section 3.2.2 PVV, PVKs,
and PVKI) would not allow the change of the PIN by the cardholder. Further-
more, it is limited to four digits PINs and is not collision-free.
The chip on the card can validate the PIN without turning to the issuer (see
Sections 3.4.3.1 PIN block format 2 and offline plaintext validation and 3.4.3.2
Encrypted offline PIN). However, relying solely on the offline PIN validation
is risky: for example, without communicating with the issuer, the card may not
know that the issuer has blocked it.
Since multiple hosts relay the transaction on its path to the issuer, steps should
be taken to ensure that none of the intermediaries can learn the PIN. Furthermore,
POS devices operate in a generally unsafe environment of the merchant store,
so their level of trust is limited. It is a classical problem for cryptography to
solve.
DOI: 10.1201/9781003371366-5 87
88 Cryptography for Payment Professionals
5.2 Zoning
In order to address the challenges, the payment network is segmented into log-
ical zones. Each zone begins or terminates at an acquirer or an issuer host. The
host is responsible for maintaining security within its zone. Each zone uses its
own key or keys for encrypted PIN blocks. Before sending the EPB to another
entity (scheme or host), the sender ensures that the EPB is encrypted with the
appropriate key defined for the next zone (see Figure 5.1).
The key used to encrypt PIN blocks within a particular zone is called the zone
PIN key (ZPK) or worker key (WK, with AWK used for the acquirer worker key
and IWK for the issuer worker key), or a PIN encryption key (PEK). Within one
zone, the host would operate on a key under a ZPK. Before sending the key to a
second zone, the host must decrypt the key with the first ZPK and encrypt it with
the second ZPK. This process is called PIN translation.
In order to make it impossible for the host to learn the PIN block in the middle
of PIN translation, the PCI PIN standard prescribes performing this operation
using a dedicated secure device, the hardware security module (HSM).
However, for the HSM to be able to translate PIN encryption, it must some-
how learn the two ZPKs to use. These, too, can be compromised in transit. There-
fore, the ZPKs themselves are transmitted while being encrypted by another key.
That other key is called the zone master key (ZMK), transport key, or key encryp-
tion key (KEK).
Protecting the PIN 89
While the HSM devices can perform secure operations fast, resist tampering,
and lock upon detection of suspicious activity, they are not designed to store a
large number of keys. An HSM would store a limited number of local master
keys (LMK) or master file keys (MFK).
Any key that the HSM operates upon or works with can only exist outside of
the HSM if encrypted by the HSM’s local key or the zone master key.
inter-zonal transportation, and any HSM in another zone which has previously
imported the ZMK can also use the ZPK.
Since outside an HSM, both the ZMK and the ZPK exist only in the encrypted
form, the key check value (KCV) was defined to help users confirm key identity
and validity. For TDES keys, the key check value can be generated by encrypting
an input vector of zero bytes (0x00) and taking the first few digits of the result,
at most 6, or by calculating a CMAC of a zero input block (see Section 1.9.1
CMAC) and taking the first 10 digits. For AES keys, only the CMAC method is
used.
Protecting the PIN 91
The HSM also can create a new ZPK and output it under the LMK and under
a transport key so that the locally encrypted value can be entered into the host
database while the value under the transport key can be sent to the owner of the
next zone for deployment. This operation is called key generation.
However, to know the ZMK, the HSM also needs it encrypted under the
LMK.
This challenge is addressed by allowing recombination of ZMKs from multi-
ple clear-text components, usually by XORing the values. The operation is called
forming the key. Since it is the most sensitive step of a key exchange process,
the PCI PIN standard elaborates on the procedures surrounding it. For example,
there must be at least three clear-text components borne by three separate key
custodians and typed into the HSM while no surveillance is possible.
In the chronological order, the process of establishing a mutual ZPK between
two zones, Zone 1 and Zone 2, may look as follows:
1. Key custodians in Zone 1 gather and generate three clear-text components
of the ZMK, taking care not to expose or compromise these components.
2. Key custodians in Zone 1 form the ZMK from the components.
3. The HSM in Zone 1 encrypts the ZMK with its local master key, LMK1 ,
and outputs the result.
4. Key custodians in Zone 1 safely send the clear-text components to custodi-
ans in Zone 2, typically using paper sheets and tamper-evident envelopes.
5. Key custodians in Zone 2 gather and form the ZMK from the clear-text
components by using the HSM, which XORs the values.
6. The HSM encrypts the ZMK with its local master key, LMK2 , and outputs
the result.
At this point, each party possesses a copy of the ZMK, encrypted with the
respective local key. The parties can now exchange the ZPKs freely since they
can ship these keys encrypted by the shared ZMK.
In some solutions, an automated protocol can communicate the current ZPK
from a host to its counterpart, rotating it periodically. The process is possible
since the ZPK always arrives while encrypted with a manually established mutual
ZMK. The process is also called dynamic key exchange (DKE).
In a less automated setting, one of the parties, let us assume Zone 1, follows
the steps below:
1. The owner of Zone 1 invokes the HSM key generation function, providing
the ZMK under LMK1 as an input parameter.
2. The HSM generates a new ZPK and prints it in two representations: en-
crypted by its LMK, LMK1 , and by the ZMK.
92 Cryptography for Payment Professionals
3. The ZPK value under LMK1 can be deployed in the host’s database in
Zone 1.
4. The ZPK, encrypted with the ZMK, can be sent to the owner of Zone 2.
5. The owner of Zone 2 receives the ZPK under the ZMK and invokes the
HSM key import function, providing ZPK under the ZMK and ZMK under
LMK2 as the parameters.
6. The HSM in Zone 2 decrypts the ZMK using its local key, LMK2 .
7. The HSM in Zone 2 decrypts the ZPK with the ZMK it has recovered.
8. The HSM in Zone 2 encrypts the ZPK with its local key, LMK2 .
9. The output is provided to the owner of Zone 2, who then deploys ZPK
under LMK2 in the host database.
If a transport key is also used as a worker key, this exposes the transport key
to this type of attack and jeopardizes key transmission and rotation security.
Therefore, the PCI PIN standard requires that the processes for key manage-
ment separate keys by type, and there are measures to ensure that a key of a
particular type cannot be used for another purpose.
With the X9.17 key encryption method, this requirement can only be met via
method and procedure.
An alternative that came into widespread use was to implement variants. The
variant is a predefined value that is XORed with the key encryption key prior to
encryption of each part of the input.
For example, if the input (a ZPK) is double-length, then there will be two
masks, XOR-ed with the KEK prior to encryption of the first and the second half
of the ZPK. For an example of a variant calculation, see Section D.2 Variant Key
Encryption.
The applied variant differs per key type and also per key part. In other words,
the halves or thirds of the key cannot be used in a different order since, with a
high degree of probability, the parity check on the decrypted key will fail.
Since AES keys have no parity bits, this method is only applicable to TDES
keys.
The variant method is no longer recommended for use and is being phased
out by the PCI PIN standard and card schemes in favor of the key block method.
For clarity, the key encrypted into a key block is referred to as the payload.
Key usage. Specifies the intended usage of the payload. ‘P0’ indicates
PIN encryption (corresponds to ZPK), and ‘K1’ indicates that the key is
used as a TR-31 key block protection key, corresponding to ZMK.
Algorithm. Specifies the algorithm used to encrypt the payload. The value
of ‘T’ corresponds to the TDES, ‘A’ for AES, with other algorithms and
proprietary values supported, too.
Mode of use. Specifies how the payload may be used. For example, PIN
encryption key (ZPK) and key block protection key (in our case, ZMK)
may support the modes ‘B’ – both encrypt and decrypt, ‘D’ – decrypt
only, and ‘E’ – encrypt only.
Key version number. Specifies the payload version number. This field has
two alphanumeric characters. If the field begins with the lowercase letter
‘c’, the key block does not carry a key but rather a component of a key.
Exportability. The payload export (in a sense mentioned in Section 5.2.2
Key exchange and management) can be prohibited by setting ‘N’ in this
header field. Alternatively, the key export can be allowed under a KEK.
Optional blocks information and the optional blocks themselves. These
values are specific per implementation but are also protected by the MAC.
4. The key length, the payload, and the padding (value of V without the
header) are encrypted with CBC TDES encryption. The key block encryp-
tion key is used as the encryption key. The value M is the initialization
vector for the CBC encryption: C = EK,M (V ).
5. The key block is formed by the concatenation of the header, the output of
the encryption, and the MAC: KB = H k C k M.
The key block is generated as follows for keys derived using the variant key
method:
1. The payload to be encrypted (in our case, the ZPK) is padded with random
bytes to a multiple of 8 bytes. The payload can be padded with more than
8 bytes to obscure the actual length of the key.
2. Two bytes of key length, the actual payload key, and the padding are con-
catenated into the input vector V .
3. V is encrypted with CBC TDES with the key block encryption key. If
there are not at least 42 random bits in the first block of the payload, a
random IV is generated and added to the key block header. In the case of
96 Cryptography for Payment Professionals
the payments, since the payload is just a worker key, all of it is considered
random.
4. CBC-MAC value, M, is calculated on H k V .
5. The key block is formed by concatenating the header, the output of the
encryption, and the MAC: KB = H k C k M.
5.3.2.3 Key block encryption key and key block MAC key generation
The standard specifies two possible methods for generating keys: variant and
derivation. With the variant method, the key block encryption key is obtained
from the key block protection key by XORing it with the value of ‘EEEEEEEE’
(8 bytes of 0x45) across all parts of the key. The key block MAC key is obtained
by XORing the key block protection key with the value of ‘MMMMMMMM’ (8
bytes of 0x4D).
The derivation method for generating keys uses similar principles for TDES
and AES protocols, in compliance with the NIST KDF method in the counter
mode (see Section 1.10 Randomness and key derivation) [69]. A data vector with
a running counter is fed as the input to the CMAC algorithm, with the key block
protection key being the algorithm’s key. The procedure repeats until enough bits
for the target key length are generated.
The input data for both TDES and AES keys contains:
1. A counter for blocks of the output, starting at 1. For example, if a triple-
length TDES key is being derived using the TDES algorithm, the counter
traverses values of 0x01, 0x02, and 0x03 for every 8 bytes being output.
Since AES keys are 128, 192, or 256 bit, the counter for the AES deriva-
tion tracks 16-byte outputs and can only assume values of 0x01 and 0x02.
2. The label – in this case, an indicator of the key usage, with 0x0000 indi-
cating that the encryption key is being generated and the value of 0x0001
corresponding to the MAC key.
3. A separator byte of 0x00.
4. The context value – an indicator of the algorithm which will use the de-
rived key, with 0x0000 and 0x0001 corresponding to double and triple-
length TDES keys and values of 0x0002 through 0x0004 corresponding
to AES keys.
5. Length, in bits, of the keying material generated for the derived key. E.g.,
0x0080 for double-length TDES or 128-bit AES keys.
Each round of the algorithm outputs 64 (for DES) or 128 (for AES) bits. The
desired number of bits is then taken from the output. I.e., for the AES algorithm,
Protecting the PIN 97
the full output of the first round and the first 8 bytes of the second round will be
used.
Since the TDES algorithm ignores parity bits in the key, they may or may not
be corrected after the derivation round, depending on whether the implementa-
tion of the algorithm will reject a key with incorrect parity bits.
An example derivation of the keys can be found in D.3 Key Block Key Deriva-
tion.
an attacker can learn the IK and thus gain access to all future transactions from
that terminal.
The key load process into terminals is called key injection, and the PCI PIN
security standard(see Section 6.2.1 PCI PIN standard) contains strict require-
ments on the process itself and the facilities where it can take place. The goal is
to protect the initial key.
There is a challenge with a sequential key derivation. While the terminal can
store the last key and derive the next one from it in one derivation round, the
acquiring host only stores the BDK. Therefore, it will first have to derive the IK
and then repeat the key derivation steps as many times as that particular terminal
processed transactions. Such a solution is not feasible.
Instead, the DUKPT protocol uses an ingenious method of key derivation,
organizing the future keys into a tree. The acquiring host is guaranteed to reach
any future key value with a limited number of iterations (10 for TDES and 16
for AES-based DUKPT), while the terminal itself stores several future keys in
parallel, at any given time, using one of them to obtain the next encryption key.
Upon the key injection, the terminal generates its initial set of future keys (21
for TDES and 32 for AES-based DUKPT). At this point in time, the IK is no
longer needed, and it can be deleted from the terminal, thus allowing for another
layer of protection.
It is worth pointing out that the keys originating from the IK are not used as-is
for data encryption and authentication. Instead, specific working keys are derived
for each purpose. Therefore, these keys are called intermediate derivation keys.
Protecting the PIN 99
Key Set ID (KSI) – an identifier of the particular BDK used for this key,
5 bytes.
PED ID – an identifier of the specific PED device. Its length is 19 bits (4
bytes and 3 bits).
Transaction counter – a running counter of the keys derived within each
device, 21 bits.
The concatenation of the KSI and the PED ID in the TDES version con-
stitutes the initial key ID and is called “legacy initial key ID” in the standard
documentation.
In the AES mode, the KSN has a total length of 12 bytes (96 bits), and its
structure is as follows:
BDK ID – an identifier of the particular BDK used for this key, 4 bytes.
Derivation ID – an identifier of the specific initial key as derived from
this BDK. Note that instead of explicitly defining this part of the initial
key ID as the device identifier, the new standard uses a broader term of
“derivation.”
Transaction counter – 4 bytes (32 bits) are the transaction counter.
In the KSN compatibility mode, the KSI and the PED ID are padded with 4
bits from the left and 1 bit from the right to the length of 64 bits and placed in
the initial key ID part of the AES KSN. The transaction counter is left-padded
with 11 bits and is placed in the transaction counter part of the AES KSN. See
also Figure 5.5.
The KSN must be sent alongside each transaction from the PED device for
the acquiring host to reconstruct the keys used with the transaction.
The transaction counter for the TDES algorithm has 21 bits. However, only
values with a bit cardinality of 10 or less are allowed, and the POS will skip
values with more set bits than that. The total maximum number of transactions
for TDES DUKPT is slightly over 1 million transactions. 1
The AES algorithm supports the same number of transactions in the KSN
compatibility mode. However, the native KSN value of the AES DUKPT has 32
bits, allows maximum cardinality of 16, and supports over 2.4 billion transac-
tions from a single initial key. 2 Furthermore, the AES DUKPT algorithm allows
sending of a new initial key to the terminal without the need to take the terminal
back to an injection facility.
k n 10 21
1 The exact number is ∑ = ∑ = 1, 048, 576
i=0 i i=0 i
k n 16 32
2 The exact number is = ∑ = 2, 448, 023, 843
∑
i=0 i i=0 i
Protecting the PIN 101
A device that implements the TDES DUKPT algorithm may not send the full
KSN with every transaction. In that case, the KSN will be left-padded with 0xFF
by the acquiring host. Since this operation affects the derivation of the initial
key (see Section 5.4.3 Initial key derivation), for such devices, the padded KSN
should also be used for the generation of the IK before key injection.
1. The KSN is left-padded with 0xFF to the full length of 10 bytes if needed
(see Section 5.4.2 Key serial number (KSN)).
2. The 21 least significant bytes of the KSN are set to zero.
3. The 8 most significant bytes of the KSN (V ) are encrypted with TDES
using the BDK as the key: IK1 = EBDK (V ). The result, IK1 , is the left half
of the initial key.
4. A variant is applied to the BDK: both of its halves are XORed
with the fixed value of 0xC0C0C0C0 00000000: BDK 0 = BDK ⊕
0xC0C0C0C000000000C0C0C0C000000000.
5. The 8 most significant bytes of the KSN (V ) are encrypted with TDES
using the BDK variant as the key: IK2 = EBDK 0 (V ).
6. The initial key is the concatenation of the two values: IK = IK1 k IK2 .
7. Since the parity bits do not affect the TDES computation, the IK may or
may not be adjusted for parity.
The AES initial key derivation algorithm uses the counter mode of the stan-
dard key derivation function, as defined in NIST 800-108 [27] (see Section 1.10
Randomness and Key Derivation). However, since the input length is identical to
the AES block size, the PRF is an AES encryption, not a CMAC computation.
Furthermore, the input value to the PRF is slightly different.
The BDK is an AES key of a standard size. The AES encryption algorithm
receives the BDK as its key and the derivation data as the input block. The first
8-byte block of the derivation data for the initial key is as follows:
Constant byte, 0x01.
Key block counter, 0x01 for the first and 0x02 for the second iteration of
the algorithm if applicable.
Key usage indicator, 0x8001 (Key Derivation, Initial key).
Algorithm indicator of the algorithm that is going to use the derived key:
0x0002 for AES-128, 0x0003 for AES-192, and 0x0004 for AES-256.
Length of the derived key in bits, 0x0080, 0x00C0, and 0x0100 for AES-
128, 192, and 256, correspondingly.
The second 8-byte block of the derivation data is the 8 leftmost bytes of the
initial KSN. Note that, considering the KSN structure, these bytes will contain
the BDK ID and the Derivation ID in the full AES mode and the KSI and the
PED ID in the KSN compatibility mode (see also Section 5.7).
The input value is generated as above and provided as the input to the AES
encryption, alongside the BDK as the key, up to two times. For 192-bit keys, the
last 4 bytes of the second output are discarded.
Protecting the PIN 103
The second 8-byte block of the derivation data is the 8 rightmost bytes of
the initial KSN. Considering the KSN structure (see figure 5.7), these bytes will
contain the Derivation ID and the transaction counter in the full AES mode, and
the transaction counter and the rightmost 31 bits of the legacy initial key ID in
the KSN compatibility mode.
If a bit is set in the input KSN, set it in the register R. Perform the inter-
mediate key derivation, using R as the KSN input and K as the key.
Replace the K with the value obtained as the result of the intermediate
key derivation.
For example, if the transaction counter of the KSN is 5 (its trailing bits are
...0101), the acquiring host will perform the following actions:
1. Load the BDK.
2. Derive the initial key, IK.
3. Derive the key for the counter value of ...0100 using the initial key.
4. Derive the key for the counter value of ...0101 using the previously de-
rived intermediate key.
Since the transaction counter for the TDES DUKPT can only contain 10 set bits,
this operation is guaranteed to only require at most 10 intermediate key derivation
steps for the TDES version of the algorithm or the AES version algorithm in the
KSN compatibility mode. The AES DUKPT is guaranteed to take no more than
16 intermediate key derivation steps.
the current KSN value. The terminal simply erases the key if the KSN has the
maximum allowed number of bits set.
For example, if the terminal uses the KSN value of 12 (binary 1100), the
rightmost bit of the KSN is bit 3. The terminal will then “iterate right” over bits
2 and 1 and generate keys for 14 (binary 1110) and 13 (1101), placing them in
registers 2 and 1, accordingly.
To further illustrate, let us consider the case of three bits (see figure 5.8),
and instead of derived keys, let us note just the KSN values to which these keys
correspond. We will need three key registers and initially will generate future
keys for values 4, 2, and 1, placing them in registers 3, 2, and 1.
For the first transaction, binary 001, there is a ready key in register 1. The
terminal uses it and clears the register.
For the second transaction, binary 010, there is a ready key in register 2. The
terminal uses it, then starts iterating to the right. In this case, the terminal turns
on bit 1, obtains binary 011 or 3, generates the future key for this value, and
stores it in register 1.
At this point, the terminal has just processed KSN 2 and has already pre-
pared a future key for KSN 3. Its registers 3 and 1 have keys for KSN 4 and 3,
correspondingly, and register 2 is empty.
108 Cryptography for Payment Professionals
KSN 3 is processed using the future key from register 1, and this key is dis-
carded afterward.
KSN 4 (binary 100) is processed using the future key from register 3.
Upon using it, the terminal iterates rightwards, generating keys for 6 (110)
and 5 (101) and placing them in registers 2 and 1. Then, the terminal deletes the
key for KSN 4.
The value of KSN 5 is serviced by the already-prepared future key from reg-
ister 1.
The value of KSN 6 (binary 110) is serviced from register 2. Then, the ter-
minal iterates rightwards, generating the future key for KSN 7 (binary 111) in
register 1 and deleting the key for KSN 6.
Finally, the value of KSN 7 is serviced from register 1 (see Figure 5.9).
While the description of the algorithm steps may be clear from the tabular
form, it is still hard to understand why it succeeds. In order to understand the
mechanics, let us represent the key derivation process as a directed graph of
KSN values. It is acyclic and is, therefore, a tree.
Mapping the terminal key derivation process above to the tree helps see the
following principles of the algorithm. We will use 4-bit values for that purpose.
If the KSN counter reached the KSN value of a particular initial future key,
then all previous registers contain keys that were already used and can therefore
be replaced. For example (see Figure 5.10), once the value of 4 (0100) has been
reached, the previous values of 1, 2, and 3 have all been handled. Once the value
of 8 has been reached, all values from 1 to 7 have been handled, and so on.
Iterating to the right replaces the values in these registers with the immediate
children of the current KSN value in the tree. All other valid KSN values which
are smaller than the current node value will be generated from these immediate
children.
The AES DUKPT working key derivation algorithm follows the same steps
as other AES DUKPT intermediate derived keys, with the only difference of
having a different usage value in the Key usage indicator field.
Chapter 6
Regulation and
Compliance
3-D Secure protocol, the cardholder was to interact with the card issuer, the issuer
then could authenticate the cardholder using an additional authentication factor,
thus complying with the 2-factor authentication requirement.
Ironically, one of the drivers behind 3-D Secure 2.0 development was the
need to significantly increase the percentage of frictionless flows in authenticated
online interactions, encouraging all participants in the ecosystem to perform fric-
tionless authentication as often as possible.
Due to the PSD2 law, the schemes simply moved the needle, making the
frictionless flow the exception rather than the rule, and mandating the adoption
of 3-D Secure 2.0.
While the majority of 3-D Secure 2.0 flows are secured using TLS
(see Section 4.3.4 TLS requirements), the mobile app flows require addi-
tional application-level authentication using JOSE standards (see Sections 4.3.5
Application-level security and 4.2 JSON object signing and encryption (JOSE)).
standard. Finally, the card-present environment may also comply with the PCI
Point-to-Point Encryption (PCI P2PE) standard.
Usually, card schemes require full compliance with security standards. How-
ever, in some cases, they may grant a waiver for a particular kind of non-
compliance. For instance, version 3.1 of the PCI PIN standard mandated tran-
sition to key block representation and exchange and specified a timeline for tran-
sition from TDES to AES pin block encryption (see Chapter 5 Protecting the
PIN).
use of key blocks (see Section 5.3.2 Key block), allowing a transition period from
the earlier methods (see Section 5.3.1 X9.17 and variant formats).
From the point of view of key management, the standard implies that the
SCRP will undergo key injection as a more traditional POS in a dedicated key
injection facility and will support a PCI PIN compliant PIN and encryption key
management schemes. The SCRP and the application on the COTS device will
use one of the approved algorithms with the approved key strength (see above the
PCI HSM standard) to set up an encryption key to communicate the PIN from
the COTS device to the SCRP.
The CPoC standard sets security requirements for solutions that allow con-
tactless transactions on a COTS device. Unlike SPoC, there is no secure card
reader involved. Consequently, a CPoC solution cannot process any PIN-based
cardholder verification methods.
The cryptographic algorithms and the approved key strengths correspond to
the PCI HSM standard.
security standards. The P2PE standard comes to patch this gap in the security of
POS solutions.
The standard describes, at length, various security aspects of a secure envi-
ronment. However, from the cryptography point of view, the approved algorithms
and the valid key lengths align with the PCI HSM and the PCI PIN standards.
In particular, in the case of a method such as the DUKPT, the keys used for
PIN encryption cannot be used for encryption and data authentication. With a
compliant DUKPT solution, PIN and data encryption keys can be derived from
the same intermediate key (see Section 5.4.5 Working key derivation).
However, in some existing solutions, vendors used two separate BDKs, de-
riving keys for PIN encryption from one derivation tree and keys for message
encryption and authentication from another. While not necessarily more secure,
this solution is more complex and wasteful. Still, it may be encountered in the
field.
⊕ 0 1
0 0 1
1 1 0
Figure A.1: XOR values.
For example, consider the 7-bit sequence of 0101110. It has four 1-bits, so if
we work with even parity, the 8th bit will be 0, and with odd parity, 1.
The original DES algorithm used 64-bit or 8-byte keys but allocated each
8th bit for odd parity (see 1.4.1 DES (DEA)). This design decision brought its
keyspace down from 264 to 256 . However, specialized hardware’s built-in valida-
tion of the key proved to be a useful way of detecting key transmission errors.
There are built-in methods in various platforms for parity calculation. For
instance:
odd parity flag for the low 8 bits of the machine word in Intel processors
since 8008.
even parity built-in function in the GCC compiler, builtin parity.
ParityBit class in Java Card API.
Integer.bitCount() and textttBitSet.cardinality() methods in Java
API (both return the number of 1-bits).
However, they are platform-specific and may require some typecasting.
The fastest method to obtain the parity bit value is a lookup table of 256
entries [74]. However, with a simple transformation of the original byte, it is
possible to get a fast parity bit operation that uses a single 16-bit word as its
lookup table.
Following is the C code for it, assuming the value is in the variable v and the
parity bit is stored in the variable textttp:
size_t even_parity_8 (uint8_t v) {
size_t p;
/* Shorten the value of v to a nibble while preserving parity
*/ v ^=v>>4;
/* cut off the upper nibble as it is no longer needed */
v &= 0xf;
/* use the magic number 0x6996 as the lookup table
with 16 entries */
p = (0x6996 >> v) &1;
return p;
}
Here, the idea is to XOR the first four bits of the value with the last four bits.
The resulting nibble will contain a value from 0 to 15, which is used to search a
lookup table (the value 0x6996) for the parity bit.
To understand why it works, consider that we XOR bits 7, 6, 5, and 4 with
bits 3, 2, 1, and 0. This operation does not affect the overall parity: if only one of
the corresponding bits is set, one bit will be set after the XOR. If neither or both
124 Cryptography for Payment Professionals
corresponding bits are set, the result will contain zero, but removing two set bits
will not affect the overall parity.
In code, we achieve this by shifting the value v right by 4 bits, XOR it with
itself, then clearing the upper 4 bits using 0xf as the bitmask. The result is be-
tween 0 and 15 and has the same parity as the original value. The magic number
of 0x6996 has bits of 0 and 1 in positions corresponding to the even parity of
numbers from 0 to 15, and, therefore, shifting it right by this number and clear-
ing all upper bits will yield the even parity bit (see figure A.2).
An example implementation of this algorithm in C is in src/bits.c, functions
even parity 8(), even parity 16(), and even parity 32().
size_t c = 32;
/* if v is not zero, set all bits of c to 1 */
if (v) c--;
/* descend through the tree, unsetting bits of c if needed */
if (v & 0x0000FFFF) c -=16;
if (v & 0x00FF00FF) c -=8;
if (v & 0x0F0F0F0F) c -=4;
if (v & 0x33333333) c -=2;
if (v & 0x55555555) c -=1;
return c;
}
This method works by first isolating the rightmost set bit and then finding its
exact position using a binary search tree.
The first step relies on two’s complement representation of negative integers.
Given an integer v, the value of -v is computed by flipping all bits of v and adding
1 to the result. We know that v has some bits set, followed by the rightmost set
bit, followed by zero bits:
v = xxxx1000
~v = XXXX0111
-v = ~v +1 = XXXX1000
When we negate v, all bits to the left of the rightmost set bit will flip. The right-
most set bit will become a 0, and all the trailing zeroes after it will be set to 1.
Once we add 1 to the resulting value, it will not affect bits to the left of the right-
most bit, but all preceding 1s to its right will turn into 0 due to carry. Finally, the
bit itself will flip to 1.
The resulting value will contain negated bits to the left of the rightmost sig-
nificant bit and will be identical to the original value from that point onwards. If
we now apply a logical AND, the operation will yield a value in which only the
rightmost bit is set.
All that is left is to find its position. It can be done by iteratively shifting the
value by one bit to the right until it is zero, but a faster method using a binary
tree exists.
For brevity of the further explanation, assume that the input value is only one
byte. We will only need three bits to store the result, as the worst case here is 7.
The variable v has a value in which only one bit is set. To find its exact
position, we first initialize the counter to the maximum possible value, in this
case, 8. We perform a boundary check - if the value is not zero, we subtract one
from it. This step makes the value of c equal to 7 ( binary 111).
We then apply the mask of 0x0F to the value. If the result is non-zero, the bit
we seek is in the lower nibble of the source byte, and we should subtract 4 from
c, setting it to binary 011. Otherwise, the value is in the upper nibble, and the
most significant bit of c should remain 1.
126 Cryptography for Payment Professionals
Next, we check if the bit is set in the upper or the lower two bits of the nibble.
For that, we use the mask of 0x33 (00110011). If the result is non-zero, the bit
is in the lower two bits of the nibble, and the middle bit of c should be cleared
(subtraction of 2 does exactly that).
Finally, we check if, out of the two-bit chunk to which c points, the sought-
for bit is the least significant of the two. We do that with the mask of 0x55
(01010101). If the value is non-zero, the bit is in an even position, and we sub-
tract 1 from c. See also Figure A.3 for an illustration.
An example implementation of this algorithm in C is in src/bits.c, func-
tions count trailing zero bits 8, count trailing zero bits 16(),
and count trailing zero bits 32().
v >>= S[i];
r |= S[i];
}
}
return r;
}
The method works as follows. Two lookup tables are prepared. The tables
contain bitmasks and the bit shifts which correspond to them.
The algorithm begins with the mask that covers the upper half of the input
machine word. If the value is located in the upper 16 bits, the value of 16 is added
to the result, and the input is shifted by these 16 bits to the right since the least
significant 16 bits can now be safely discarded.
The process repeats for the mask of 0xFF00 and the shift value of 8. At this
point, the rightmost bits of the input are guaranteed to be zero either due to bit
shift or because no bit was set there initially. If the result of the AND operation is
non-zero, there is a set bit in the upper byte. The value of 8 is added to the result,
and the input is shifted right by 8.
The process continues for a sequence of masks and shift values of 4, 2, and
1, setting the relevant bit in the output and shifting the input accordingly.
An example implementation of this algorithm in C is in src/bits.c, functions
log2 8(), log2 16(), and log2 32().
size_t bit_cardinality_8(uint8_t v) {
size_t c; // c accumulates the total bits set in v
for (c = 0; v; c++) {
v &= v - 1; // clear the least significant bit set
}
return c;
}
value is 1000. After subtracting 1 from it, the value will become 0111. Applying
the logical AND with the original value yields: xxxxx1000 & xxxxx0111 =
xxxxx0000, clearing the least significant bit.
The procedure should continue until the value is zero; the counter c will ac-
cumulate the number of bits cleared by this operation.
A C implementation of this function can be found in src/bits.c, functions
bit cardinality 8(), bit cardinality 16(), and bit cardinality
32().
A.7 Decimalization
In certain scenarios, such as with Track 1/Track 2 data, the medium uses an
encoding that supports a limited character set. For instance, with the Track 2
data, the ANSI/ISO BCD data format only supports 16 characters, 10 of which
are digits, and the rest are reserved as control characters.
The decimalization algorithm produces a sequence of decimal digits, usually
of the length 3 or 4.
Bits and Digits 129
The algorithm scans the input byte vector, treating each nibble as a separate
digit, from the most significant to the least significant. If a nibble contains a value
between 0 and 9, i.e., a decimal digit, the digit is added to the output sequence.
After the first scan, if not enough digits are found, the input vector is scanned
again, subtracting 10 from each nibble.
For example, an input vector of 2A3B C4EF will yield 234 at the first pass.
If the required sequence should be of four digits, during the second scan, A will
get converted to 0, and the output will thus be 2340.
An example C implementation of the decimalization algorithm can be found
in src/bits.c, function decimalize vector().
Cryptographic Examples
B.1.1.1 Input/Output
Input/output functions of the library work via BIO, the basic IO abstraction
layer. In C, it is imported by including. To instantiate the basic data structure,
one allocates a pointer to the BIO struct, and then uses one of the functions
BIO new xxx().
For example, to allocate a basic IO structure that would point to a dynamic
memory buffer, function BIO new mem buf() is used. For a file descriptor,
BIO new fd() is used. Functions also exist for accessing a file via FILE* struc-
ture and by file name; there are also functions for active and passive sockets.
These functions allocate memory, so BIO free all() or BIO free() calls
should be applied to free it up.
B.2 RSA
A quick recap of the algorithm:
The public key for the algorithm is a pair of two numbers, a product of two
primes p and q, denoted as n, and a number e that is coprime with the least com-
mon multiple of p − 1 and q − 1, λ . The private key is the pair of two numbers,
n and d, where the latter is a modular multiplicative inverse of e modulo λ .
Cryptographic Examples 133
1-e5
2-f9
3-5b
4-d3
5-63
Combining any three shares out of the five above using ssss-combine yields
the secret value.
DES example() and TDES example(). Running the binary test crypto
primitives will print out several test vectors for these algorithms.
B.6 AES
The AES algorithm is easy to use with the OpenSSL library. Firstly, the
AES KEY structure has to be initialized by calling AES set encrypt key() or
AES set decrypt key(). The functions get the number of bits in the key as an
input parameter.
Unlike the DES, since there are no parity bits, no mismatching keys are pos-
sible with the AES algorithm.
Then, AES ecb encrypt() function does all the necessary encryption.
An example of DES and TDES implementation in C with the OpenSSL li-
brary can be found in test/test crypto primitives.c, function
AES example(). Running the binary test crypto primitives will print out
several test vectors for the algorithm.
B.7 OAEP
The OpenSSL library contains functions to support OAEP. In particular,
RSA padding add PKCS1 OAEP() is a function that pads a value using the
OAEP method. The RSA encrypt() function can be told to apply OAEP trans-
parently by using a padding parameter. In addition, the PKCS1 MGF1() function
implements the MGF1.
Below is an example of such a computation with an empty label.
The padded data is: 0093 0761 BA8E 7139 D9E3 4B79 87D1 4ACC 1534
1C5A DAF1 D542 3655 1AFD E930 341F 6A54 C256 721E 0FA2 F68F 65CD
3777 891A A19E.
Here, we can already identify the first byte, 0x00. We know that the SHA-1
hash function was used. Hence, the following 20 bytes (93 0761 BA8E 7139
D9E3 4B79 87D1 4ACC 1534 1C5A DA) are the masked seed, and the rest is
the seed for the mask of the masked seed. The remainder (byte sequence begin-
ning from 0xF1 above) is fed into MGF1 to obtain a mask. The mask is applied
to the 20-byte seed. The seed is fed into MGF1 again to obtain a mask. The
mask is applied to the remainder of the data to obtain: DA39 A3EE 5E6B 4B0D
3255 BFEF 9560 1890 AFD8 0709 0000 0000 01DE ADBE EF. Here, the
sequence 0xDA ... 0709 is a fixed hash value that is used whenever the la-
bel is empty, and the remainder is the zero padding, followed by 0x01, followed
by 0xDEAD BEEF, the payload.
An example unwrapping of an OAEP padding in C with the OpenSSL library
can be found in test/test crypto primitives.c, function OAEP example().
Cryptographic Examples 137
Running the binary test crypto primitives will print a step-by-step recov-
ery of a test array from the padding based on a random sequence.
For example, let us consider creating an internal root CA and signing a server
certificate with it.
In order to do so, the following steps are required:
1. Generate the root CA certificate and the root CA private key. The latter
will have to be very well-protected.
2. Generate a server private key.
3. Generate a certificate request for the server.
4. Sign the certificate request using the root CA private key to obtain the
server certificate.
5. Deploy the root CA certificate in the keystores of relevant clients and
servers to enable automatic recognition of certificates it signs. In the ex-
ample in TLS Client, the root CA should be added to the cert.pem file.
For the sake of the exercise, 512-bit RSA keys will be used. In production, at
least 2048-bit strength is strongly recommended.
The OpenSSL framework supports password-protected storage. Its mecha-
nism is simple: the passphrase is used to derive a unique key, without which it
will be impossible to decipher the stored cryptographic materials. For this exer-
cise, we will not use passphrases (-nodes flag will take care of that).
We will need the certificate subject. The canonical name (CN) will be
“Test Root CA,” the country (C) will be New Zealand, and the location
(L) will be Taumatawhakatangihangakoauauotamateaturipukakapikimaunga-
horonukupokaiwhenuakitanatahu. The expiry date will be set to 356 days, and
the hash function will be sha1. These settings correspond to the following com-
mand line:
openssl req -x509 -sha1 -days 365 -nodes -newkey rsa:512 -subj
"/CN=Test Root CA/C=NZ/L=Taumatawhakatangihangakoauauotamate
aturipukakapikimaungahoronukupokaiwhenuakitanatahu"
-keyout rootCA.key -out rootCA.crt
[alt_names]
DNS.1 = localhost.root
140 Cryptography for Payment Professionals
EMV Examples
Hash input: 0240 5825 FF10 2512 3445 0101 C101 00C1 17AD ED75
677D 4D74 5412 8417 45F6 CA48 E5C3 7211 FB4C 7CA8
4957 47ED 183E 8A9F 3995 6FE3 54FF 657D E2D4 6401
9BA3 1B2D 6F85 83D9 832A A802 8D8A 75FF 8D63 4E92
CA6D 1A29 6205 585F 1127 1340 A537 E93E B0EC D5B3
4109 FF8B FA1F 8C1E 0692 E489 B930 9BCF 8E18 17D9
7D8D 074F 88D5 15C8 C1AE 8134 0596 DB58 A901 D49E
23EC AEA7 3297 9BCD F3EF 5DCF 25BF 867F FC08 F1C1
AAF8 83D4 6004 B467 6E53 4FC8 DE00 2E20 B603 804F
2D73 41B2 D0C8 E176 C214 E446 A1E3 766E 6E86 1B81
A93A C298 30CA 61BB BBBB BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB 03
The hash output will be 8E00 3638 CEF0 F8D6 942C 0BE3 F25A E5E2
7E86 E9D3.
This value is appended to the full buffer (i.e., with the leading sentinel value
of 0x6A) instead of the exponent to obtain the below input to the encryption
algorithm:
Buffer: 6A02 4058 25FF 1025 1234 4501 01C1 0100 C117 ADED
7567 7D4D 7454 1284 1745 F6CA 48E5 C372 11FB 4C7C
A849 5747 ED18 3E8A 9F39 956F E354 FF65 7DE2 D464
019B A31B 2D6F 8583 D983 2AA8 028D 8A75 FF8D 634E
92CA 6D1A 2962 0558 5F11 2713 40A5 37E9 3EB0 ECD5
B341 09FF 8BFA 1F8C 1E06 92E4 89B9 309B CF8E 1817
D97D 8D07 4F88 D515 C8C1 AE81 3405 96DB 58A9 01D4
9E23 ECAE A732 979B CDF3 EF5D CF25 BF86 7FFC 08F1
C1AA F883 D460 04B4 676E 534F C8DE 002E 20B6 0380
4F2D 7341 B2D0 C8E1 76C2 14E4 46A1 E376 6E6E 861B
81A9 3AC2 9830 CA61 BBBB BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB BB8E 0036 38CE F0F8 D694 2C0B E3F2
5AE5 E27E 86E9 D3BC
Finally, the result is encrypted with the private key of the scheme to obtain
the output:
Output certificate:
5C3F 1A47 55FF BC41 C4D6 139D 5270 6544 CABF D865
F80C 8EC7 389E 57A8 9545 2AF1 EBF1 1977 3397 E3ED
BEB2 4BAD 951F 43C8 0BA6 81D4 D944 E8FF 2831 5821
7468 2530 F416 0834 E1F7 D710 18AE 3F09 8724 414D
1968 3EC4 1C01 E92F 6CBA 9A9C 5D62 D333 ECA4 C827
320E 5D50 8D87 4E0A ABFB 8A92 6318 512F A30B A38F
7297 B24C 5C33 41AD BFD4 30F4 F59F A752 635C 7BE7
1F2E 6C60 6EFC 7B17 DB6A 71C1 64CC 4B40 461C 4391
EMV Examples 143
B613 D93B BD98 0230 2027 BC6E 43A8 66D4 6C12 0A63
0E73 E0C5 B0F0 CA5B 0206 12ED 0DE3 367D D97C 3224
7B14 655B 6CB4 B15F 04DF F949 76BC 1589 8813 5B18
E103 42E9 CA02 9A3C 90B0 D875 F692 F1D7 BBBE C0AF
A317 B06A 061C 2952
The recovery of the issuer public key from the certificate can be seen below.
The certificate is deciphered using the scheme’s public key to obtain the re-
covered raw data:
Recovered raw data: 6A02 4058 25FF 1025 1234 4501 01C1 0100 C117 ADED
7567 7D4D 7454 1284 1745 F6CA 48E5 C372 11FB 4C7C
A849 5747 ED18 3E8A 9F39 956F E354 FF65 7DE2 D464
019B A31B 2D6F 8583 D983 2AA8 028D 8A75 FF8D 634E
92CA 6D1A 2962 0558 5F11 2713 40A5 37E9 3EB0 ECD5
B341 09FF 8BFA 1F8C 1E06 92E4 89B9 309B CF8E 1817
D97D 8D07 4F88 D515 C8C1 AE81 3405 96DB 58A9 01D4
9E23 ECAE A732 979B CDF3 EF5D CF25 BF86 7FFC 08F1
C1AA F883 D460 04B4 676E 534F C8DE 002E 20B6 0380
4F2D 7341 B2D0 C8E1 76C2 14E4 46A1 E376 6E6E 861B
81A9 3AC2 9830 CA61 BBBB BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB BB8E 0036 38CE F0F8 D694 2C0B E3F2
5AE5 E27E 86E9 D3BC
Since the key is shorter than the maximum length and was padded, it doesn’t
have to be combined with the remainder. The issuer key is therefore:
Combined key: 00C1 17AD ED75 677D 4D74 5412 8417 45F6 CA48 E5C3
7211 FB4C 7CA8 4957 47ED 183E 8A9F 3995 6FE3 54FF
657D E2D4 6401 9BA3 1B2D 6F85 83D9 832A A802 8D8A
75FF 8D63 4E92 CA6D 1A29 6205 585F 1127 1340 A537
E93E B0EC D5B3 4109 FF8B FA1F 8C1E 0692 E489 B930
9BCF 8E18 17D9 7D8D 074F 88D5 15C8 C1AE 8134 0596
DB58 A901 D49E 23EC AEA7 3297 9BCD F3EF 5DCF 25BF
867F FC08 F1C1 AAF8 83D4 6004 B467 6E53 4FC8 DE00
2E20 B603 804F 2D73 41B2 D0C8 E176 C214 E446 A1E3
766E 6E86 1B81 A93A C298 30CA 61
7D8D 074F 88D5 15C8 C1AE 8134 0596 DB58 A901 D49E
23EC AEA7 3297 9BCD F3EF 5DCF 25BF 867F FC08 F1C1
AAF8 83D4 6004 B467 6E53 4FC8 DE00 2E20 B603 804F
2D73 41B2 D0C8 E176 C214 E446 A1E3 766E 6E86 1B81
A93A C298 30CA 61BB BBBB BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB 03
The key hash as computed would therefore be 8E00 3638 CEF0 F8D6
942C 0BE3 F25A E5E2 7E86 E9D3 which is equal to the value provided:
8E00 3638 CEF0 F8D6 942C 0BE3 F25A E5E2 7E86 E9D3.
The key is therefore valid and the recovered value is:
Recovered key: 00C1 17AD ED75 677D 4D74 5412 8417 45F6 CA48 E5C3
7211 FB4C 7CA8 4957 47ED 183E 8A9F 3995 6FE3 54FF
657D E2D4 6401 9BA3 1B2D 6F85 83D9 832A A802 8D8A
75FF 8D63 4E92 CA6D 1A29 6205 585F 1127 1340 A537
E93E B0EC D5B3 4109 FF8B FA1F 8C1E 0692 E489 B930
9BCF 8E18 17D9 7D8D 074F 88D5 15C8 C1AE 8134 0596
DB58 A901 D49E 23EC AEA7 3297 9BCD F3EF 5DCF 25BF
867F FC08 F1C1 AAF8 83D4 6004 B467 6E53 4FC8 DE00
2E20 B603 804F 2D73 41B2 D0C8 E176 C214 E446 A1E3
766E 6E86 1B81 A93A C298 30CA 61
The first byte of each 4-byte sequence indicates the file, the second and the
third bytes define the range of records to be read from the file, and the last,
fourth, byte indicates how many records, starting from byte 2, should be used for
the static data authentication.
The file number is contained in the five most significant bits of the first
byte. In other words, values 0x08, 0x10, 0x18 (binary 00001000, 00010000,
00011000) correspond to files 1, 2, and 3. From the AFL example, we see that
the overall data records are to be read as follows:
According to the EMV standard, each file record has a nested tag-length-
value structure, with the outmost tag being 0x70. This tag, in turn, contains in-
dividual data elements in the same TLV format. The rule for collecting data for
the SDA is as follows:
1. Records from files 1 to 10 are concatenated without the outmost tag 0x70
and its length.
2. Records from files with file numbers over 10 are concatenated with tag
0x70.
In that case, the static data for authentication will be 5A08 4567 8901 2345
6789 5F24 0332 1231 (see Figure C.1).
A C example of SDA computation and validation can be found in
src/emv.c, functions emv validate sda() and emv sign static data().
Running the bin/test emv file will print an example computation.
00C7 B3F1 F50B B858 A2F6 BD3B EF32 6AC7 5701 1CDD
0EF9 C1B3 9811 4819 CDCC 933C AE1A D51F 6C1B 2A1F
612E 8663 CE39 D314 D3A2 FFB0 8772 DFA7 1A30 12A0
6BF5 57EF 9C18 FA75 D43F 055E 915A A03A 7C7A A2F2
73AB F699 C886 BFA4 8383 D19F 2E93 1F5A C96F 9844
80D1 EA91 DC03 3206 EF03 4D62 1097 CC0F F488 C81E
02C8 111D 26D0 8034 57
6A04 1234 5678 9012 3456 7890 1230 1122 3301 0181
0100 C7B3 F1F5 0BB8 58A2 F6BD 3BEF 326A C757 011C
DD0E F9C1 B398 1148 19CD CC93 3CAE 1AD5 1F6C 1B2A
1F61 2E86 63CE 39D3 14D3 A2FF B087 72DF A71A 3012
A06B F557 EF9C 18FA 75D4 3F05 5E91 5AA0 3A7C 7AA2
F273 ABF6 99C8 86BF A483 83D1 9F2E 931F 5AC9 6F98
4480 D1EA 91DC 0332 06EF 034D 6210 97CC 0FF4 88C8
1E02 C811 1D26 D080 3457 BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB BBBB BBBB BB8C E181 991F A19C F9F4
D8D0 7954 FC7D 662B 3247 ECBC
Encryption by the private key that corresponds to the above public key will
yield the ICC certificate:
B727 A657 5F99 68B8 65B0 BF7B 47A1 8009 A31F E45B
55D4 D13E 0DAE E5AD 7AA6 FDC2 F5A2 8C2C 2EDD 1416
1B1D 6794 3531 27EA 5BEE CAF2 6EFF B6C5 C3BD 0A1F
041B D7EA 1625 4CD8 7B6E EAA4 E8D3 4F0C EB52 A13A
8FD2 F9E8 90E4 A400 FFF8 EEED 29A5 13CB FEEB BB03
EMV Examples 147
FCBE 1181 8417 3F1D FD65 4296 5C09 C5BC 3291 EB75
19BA 5F1D 000D D9C7 DF4B EF74 F236 6067 77C4 9042
FE8E 93F9 2CC3 F102 ED5D 0C4E 6878 FA75 CC95 52FA
43CC CDBA B025 4303 C0E1 5C8C EDA4 3709 3C96 13CA
7B14 C94E 0169 F7A0 8C79 0D79
The key recovery process would work as follows: the certificate is decrypted
with the issuer public key to yield:
6A04 1234 5678 9012 3456 7890 1230 1122 3301 0181
0100 C7B3 F1F5 0BB8 58A2 F6BD 3BEF 326A C757 011C
DD0E F9C1 B398 1148 19CD CC93 3CAE 1AD5 1F6C 1B2A
1F61 2E86 63CE 39D3 14D3 A2FF B087 72DF A71A 3012
A06B F557 EF9C 18FA 75D4 3F05 5E91 5AA0 3A7C 7AA2
F273 ABF6 99C8 86BF A483 83D1 9F2E 931F 5AC9 6F98
4480 D1EA 91DC 0332 06EF 034D 6210 97CC 0FF4 88C8
1E02 C811 1D26 D080 3457 BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB BBBB BBBB BB8C E181 991F A19C F9F4
D8D0 7954 FC7D 662B 3247 ECBC
While the public key is already visible in the decrypted value, starting with
the C7B3 F1F5 sequence, its authenticity still needs to be confirmed.
For that, the hash value at the end of the decrypted array is truncated and the
public exponent is appended instead:
0412 3456 7890 1234 5678 9012 3011 2233 0101 8101
00C7 B3F1 F50B B858 A2F6 BD3B EF32 6AC7 5701 1CDD
0EF9 C1B3 9811 4819 CDCC 933C AE1A D51F 6C1B 2A1F
612E 8663 CE39 D314 D3A2 FFB0 8772 DFA7 1A30 12A0
6BF5 57EF 9C18 FA75 D43F 055E 915A A03A 7C7A A2F2
73AB F699 C886 BFA4 8383 D19F 2E93 1F5A C96F 9844
80D1 EA91 DC03 3206 EF03 4D62 1097 CC0F F488 C81E
02C8 111D 26D0 8034 57BB BBBB BBBB BBBB BBBB BBBB
BBBB BBBB BBBB BBBB BBBB
Hashing this value with the SHA-1 yields 8CE1 8199 1FA1 9CF9 F4D8
D079 54FC 7D66 2B32 47EC, which is identical to the value provided in the
decrypted certificate.
The ICC public key certificate contains the hash value of data provided in the
certificate and the static data used for the static data authentication (see Sections
3.4.1 Static data authentication and C.2 Static Data Authentication and AFL).
148 Cryptography for Payment Professionals
C.5.2 Option B
Option B is relevant for the TDES algorithm and PANs longer than 16 digits.
It essentially adds a shortening phase that maps the PAN and the CSN into a
16-digit value.
A quick recap of the algorithm:
1. The sequence number (CSN) is appended to the PAN.
150 Cryptography for Payment Professionals
2. If the total number of nibbles is odd, the result is left-padded with a single
zero.
3. The result is hashed using SHA-1 to obtain a 20-byte hash value.
4. The output of the hash is decimalized to obtain a 16-digit value.
5. The steps of the Option A algorithm are applied to the outcome.
Let us assume the PAN value of 9876 5432 1012 3456 789 (19 digits)
and the CSN of 01.
1. The input vector after the concatenation and the alignment will contain
0987 6543 2101 2345 6789 0100.
2. Its SHA-1 hash digest will be FBC4 FDF0 2B0E F7F0 801D 8C0D
02DD 609D 8BEA D233.
3. Decimalization of this value yields 4020 7080 1800 2609. This value
is used as the input to the process described above under Option A.
4. The output of the process yields the key value of E5AB 98AB 5E76 F757
FEDC 7F01 6E5E 2358.
C.5.3 Option C
Option C is relevant for the AES algorithm and PANs of any length. The key
derivation algorithm is as follows:
1. The sequence number (CSN) is appended to the PAN.
2. The result is left-padded with zeroes to the full length of 16 bytes to obtain
Y.
3. The output is encrypted with the provided key: R1 = EK (Y ).
4. If the desired key length is larger than the AES block size, the value Y is
inversed: Y 0 = Y ⊕ FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF.
5. Y 0 is encrypted to obtain R2 : R2 = EK (Y 0 ).
6. The necessary number of bytes is used from R1 k R2 to obtain the desired
result.
Assuming the PAN value of 9012 3454 3210 1232, the CSN of 01, and
the AES key K = DFAD BFEF 0123 4567 8986 6443 DFAD BFEF, let the desired
key length be 192 bits. Then, the process is as follows:
1. The input vector is Y = 0000 0000 0000 0090 1234 5432 10123201.
EMV Examples 151
2. The output of this value’s encryption using AES with the key K, R1 , is as
follows: R1 = 3AD9 5FE6 AD75 062D FCF1 9D83 4504 AC23.
3. Since the output is 128 bits long and the required key length
is 192, the process continues. The inverted input vector, Y 0 =
FFFF FFFF FFFF FF6F EDCB ABCD EFED CDFE.
4. Its encryption result using AES with the key K is 7A49 8222 9225 7A16
F2B7 6168 B4E2 B574. Out of this result, only the first 8 bytes are used.
5. The end result is 3AD9 5FE6 AD75 062D FCF1 9D83 4504 AC23
7A49 8222 9225 7A16.
An example C implementation of the process can be found in src/emv.c,
function emv derive icc master key().
5. The combined output is B027 E58F 2D55 6547 ABA7 1B83 E6EC
4495, which after fixing the parity bit yields B026 E58F 2C54 6446
ABA7 1A83 E6EC 4594.
A C implementation of the process can be found in src/emv.c, function
emv derive icc session key().
1. The value of 0x80 is appended to the data which turns into 0000 0000
0840 0000 0000 0008 4018 1200 0106 0112 0300 0000 0080.
2. Since the length of the result is now 26, it needs to be padded to the length
of 32 by adding zeroes and split into 8-byte chunks: 0000000008400000,
0000000840181200, 0106011203000000, 0080000000000000.
EMV Examples 153
3. The data is encrypted with a zero IV using CBC and K as the key.
4. The result is 9635 5B7C 0010 05F7 which is the ARQC.
1. XORs the ARC with the ARQC, obtaining 9634 5B7C 0010 05F7.
2. Encrypts the result with the session key.
3. The output, 8453 5883 EAC6 557A, is the ARPC.
PIN Examples
Format 3 PIN block differs from format 0 in that it uses random padding
instead of the 0xF constant. For the example above, the first block of format 3
will contain 3512 345R RRRR RRRR, the second block will contain 0000 3456
7890 1234, and the final output will be the XOR of these two values.
The encryption and decryption of these PIN block formats are performed
with TDES in the ECB mode.
Format 4 PIN block would be obtained via a more complex procedure. First,
the two input blocks, I1 and I2 , are formed as follows:
1. I1 contains the format indication, PIN, and padding of 0xA to 8
bytes, followed by 8 random bytes. For the example above, I1 =
4512 345A AAAA AAAA RRRR RRRR RRRR RRRR.
2. I2 contains the length of PAN minus 12 as the first nibble, followed by the
zero-padded PAN: I2 = 4012 3456 7890 1234 5000 0000 0000 0000.
Since the PAN has 16 digits, the first nibble is 16 – 12 = 4.
The encryption of the PIN block is done using AES in CBC mode with a zero
IV. In other words, assuming the key is K:
C1 = EK (I1 )
C2 = EK (I2 ⊕C1 )
The result, C2 , is the PIN block. Its decryption process is also AES in the
CBC mode, but it works as follows: assuming that C2 is the ciphertext above, I2
is reconstructed from the PAN and is appended to C2 as the second block of the
ciphertext. CBC decryption is then performed. The reason it works is below:
1. I2 is constructed from the known data.
2. The PIN block is decrypted using K to obtain M1 = DK (C2 ) = DK (EK (I2 ⊕
C1 )) = I2 ⊕C1 .
3. The result is XORed with I2 : M2 = M1 ⊕ I2 = I2 ⊕C1 ⊕ I2 = C1 .
4. The result is decrypted using K again: DK (C1 ) = DK (EK (I1 )) = I1 .
An example C implementation of PIN block formatting can be found in
src/pin.c, function make pin block(). Running bin/test pin will print
out several test examples of PIN blocks in various formats.
An example C implementation of PIN block format 4 encryption and decryp-
tion can be found in src/pin.c, functions encrypt format 4 block() and
decrypt format 4 block(). The aforementioned test pin binary will also
print out a test example of format 4 encryption and decryption.
PIN Examples 157
5. The process is repeated for the first half. The KSN is XORed with K20 :
C1 = C1 ⊕ K20 = 4060737AE0B4D608.
6. The result is encrypted using K10 . C1 = EK0 1 (C1 ) = 1F228F640CF50AB0.
7. C1 is once again XORed with K20 : C1 = C1 ⊕ K20 = 81EF42F1CE61DCB9.
The intermediate key for the KSN of FFFF DEAD BEEF 2220 0001 is,
therefore, C1 k C2 = 81EF42F1CE61DCB98968AEC18B84D473.
Now assume the KSN in question is KSN: FFFF DEAD BEEF 0100 200B.
The least significant bits of it are 1011. According to the derivation process, the
value will be derived as follows:
1. The key corresponding to the KSN with the trailing bits of 1000 (0x08) is
derived from the initial key. For the example above (initial key of C133
69A4 8710 4C04 5E0D 0D55 C294 D609), the intermediate key will
be 4335 A065 CA4C 3CB5 03B1 A97D 6D66 543D.
2. The key corresponding to the trailing bits of 1010 (0x0A) is derived from
the key for KSN 0x08. The intermediate key will be 44CD DFD1 AB03
8054 2CB5 07E7 7BD1 5B46.
3. The key corresponding to the trailing bits of 1011 (0x0B) is derived from
the key above. The result will be 773F CDA4 4A97 6C4B 1123 CB29
4406 01DE.
The result is D318 ACE3 D42C 812B 2B9F 7B37 9DDB DF7E.
PIN Examples 161
JOSE Examples
Standard Bodies
F.6 EMVCo
Europay, Mastercard, and Visa originally formed EMVCo to develop secure
technology that would combat card fraud. EMVCo is now owned by all major
card schemes.
The technical body had initially focused on the EMV Chip specification. Con-
tactless, mobile, and other relevant standards have been developed with time.
The EMV standard references differ depending on the standard.
The EMV specifications are available for free from the EMVCo website [48].
177
178 Index