Public Key Cryptography
Public Key Cryptography
Anoop MS
Tata Elxsi Ltd, India
[email protected]
Abstract: The paper discusses public key cryptography and its use in
applications such as Key Agreement, Data Encryption and Digital Signature. The
paper discusses some public key algorithms such as DH, RSA, DSA, ECDH and
ECDSA and also gives mathematical explanations on the working of these
algorithms. The paper also gives a brief introduction to modular arithmetic,
which is the core arithmetic of almost all public key algorithms.
1. Introduction
The data transferred from one system to another over public network can be protected by
the method of encryption. On encryption the data is encrypted/scrambled by any
encryption algorithm using the ‘key’. Only the user having the access to the same ‘key’ can
decrypt/de-scramble the encrypted data. This method is known as private key or
symmetric key cryptography. There are several standard symmetric key algorithms
defined. Examples are AES, 3DES etc. These standard symmetric algorithms defined are
proven to be highly secured and time tested. But the problem with these algorithms is the
key exchange. The communicating parties require a shared secret, ‘key’, to be exchanged
between them to have a secured communication. The security of the symmetric key
algorithm depends on the secrecy of the key. Keys are typically hundreds of bits in length,
depending on the algorithm used. Since there may be number of intermediate points
between the communicating parties through which the data passes, these keys cannot
exchanged online in a secured manner. In a large network, where there are hundreds of
system connected, offline key exchange seems too difficult and even unrealistic. This is
where public key cryptography comes to help. Using public key algorithm a shared secret
can be established online between communicating parties with out the need for exchanging
any secret data.
In public key cryptography each user or the device taking part in the communication have
a pair of keys, a public key and a private key, and a set of operations associated with the
keys to do the cryptographic operations. Only the particular user/device knows the private
key whereas the public key is distributed to all users/devices taking part in the
communication. Since the knowledge of public key does not compromise the security of the
algorithms, it can be easily exchanged online.
A shared secret can be established between two communicating parties online by
exchanging only public keys and public constants if any. Any third party, who has access
only to the exchanged public information, will not be able to calculate the shared secret
unless it has access to the private key of any of the communicating parties. This is key
agreement and is defined in section 2.
Apart from Key Agreement the other important applications of public key cryptography are
Data Encryption and Digital Signature, which are explained in sections 3 and 4 respectively.
1
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
by the mathematical function called the one-way function. One-way functions are
mathematical functions in which the forward operation can be done easily but the reverse
operation is so difficult that it is practically impossible. In public key cryptography the
public key is calculated using private key on the forward operation of the one-way function.
Obtaining of private key from the public key is a reverse operation. If the reverse operation
can be done easily, that is if the private key is obtained from the public key and other
public data, then the public key algorithm for the particular key is cracked. The reverse
operation gets difficult as the key size increases. The public key algorithms operate on
sufficiently large numbers to make the reverse operation practically impossible and thus
make the system secure. For e.g. RSA algorithm operates on large numbers of thousands
of bits long.
2. Key Agreement
Key agreement is a method in which the device communicating in the network establishes
a shared secret between them without exchanging any secret data. In this method the
devices that need to establish shared secret between them exchange their public keys.
Both the devices on receiving the other device’s public key performs key generation
operation using its private key to obtain the shared secret.
As we see in the previous section the public keys are generated using private key and other
shared constants. Let P be the private key of a device and U(P, C) be the public key. Since
public key is generated using private key, the representation U(P, C) shows that the public
key contain the components of private key P and some constants C where C is known by all
the device taking part in the communication.
Consider two devices A and B. Let PA and UA(PA, C) be the private key and public key of
device A, and PB and UB(PB, C) be the private key and public key of device B respectively.
Both device exchanges their public keys.
Device A, having got the public key of B, uses its private key to calculate shared secret
KA=Generate_Key(PA, UB(PB, C))
Device B, having got the public key of A, uses its private key to calculate the shared secret
KB=Generate_Key(PB, UA(PA, C))
A UB(PB, C) B
The key generation algorithm ‘Generate_Key’ will be such that the generated keys at the
device A and B will be the same, that is shared secret KA=KB=K(PA, PB, C).
Since it is practically impossible to obtain private key from the public key any middleman,
having access only to the public keys UA(PA, C) and UB(PB, C), will never be able to obtain
the shared secret K.
Examples of key agreement algorithms are DH, RSA and ECDH. The algorithms and
explanations are given in sections 6, 7.2 and 10 respectively.
During the key exchange process the public keys may pass through different intermediate
points. Any middleman can thus tamper or change the public keys to its public key.
Therefore for establishing shared secret it is important that device A receives the correct
public key from device B and vice versa. Digital Certificate helps to deliver the public key in
authenticated method. Digital Certificate is explained in section 4.1.
2
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
3. Encryption
Encryption is a process in which the sender encrypts/scrambles the message in such a way
that only the recipient will be able to decrypt/ descramble the message.
Consider a device B whose private key and public key are PB and UB respectively. Since UB
is public key all devices will be able to get it. For any device that needs to send the
message ‘Msg’ in a secured way to device B, it will encrypt the data using B’s public key to
obtain the cipher text ‘Ctx’. The encrypted message, cipher text, can only be decrypted
using B’s private key. On receiving the message the B decrypts it using its private key PB.
Since only B knows its private key PB none other including A can decrypt the message.
Private Key = PB
Public Key = UB
UB
A B
Msg
Ctx = Encrypt(Msg, UB) Msg = Decrypt(Ctx, PB)
It is important that device A receives the correct public key from device B, i.e. no
middleman must tamper or change the public key to its public key. Digital Certificate helps
to deliver the public key in authenticated method. Digital Certificate is explained in section
4.1.
One of the popular public key encryption algorithms is RSA. RSA encryption is explained in
section 7.1.
4. Digital Signature
Using Digital signature a message can be signed by a device using its private key to ensure
authenticity of the message. Any device that has got the access to the public key of the
signed device can verify the signature. Thus the device receiving the message can ensure
that the message is indeed signed by the intended device and is not modified during the
transit. If any the data or signature is modified, the signature verification fails.
Private Key = PA
Public Key = UA
UA
A B
Msg, Sgn
Sgn = Sign(Msg, PA) Status = Verify(Sgn, Msg, UA)
If Status = 1 signature verified, else
not verified
For e.g. if a device A need to ensure the authenticity of its message, the device A signs its
message using its private key PA. The device A will then send the message ‘Msg’ and
signature ‘Sgn’ to device B. The device B, on receiving the message, can verify the
message using A’s public key UA and there by ensuring that the message is indeed sent by
A and is also not tampered during the transit. Since only the device A knows its private PA
key, it is impossible for any other device to forge the signature.
3
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
The examples of Digital Signature algorithms are RSA, DSA and ECDSA that are explained
in sections 7.3, 8 and 11 respectively.
4.1. Certificate
As seen in section 2, shared secret can be established between two devices using a key
agreement algorithm by exchanging their public keys. However the credibility of received
public key has to be ensured for a secured communication. For example consider two
devices A and B establishing a shared secret. Both devices exchange their public keys. The
devices calculate the shared secret using their private key and the other device’s public
key. Now consider an intermediate point H through which all the communication happens.
If H captures B’s public key and sends H’s public key instead with B’s identity, then A will
end up in establishing shared secret with H and will communicate with H thinking that it is
communicating with B. This happened because there is no way for A to verify that the
received public key is indeed that of B. Here is where the Digital Certificate comes to play.
For data transfer in a network consider an authority trusted by all devices. This Trusted
Certificate Authority (CA) signs the public keys and the unique identifiers of all devices.
These signed data (public key, IDs etc.) along with the signature arranged in a standard
format is called as the certificate. All the devices that take part in secured and trusted
communication have to obtain a certificate from the trusted authority
Now the device A and B exchanges their respective certificate instead of public key. These
certificates are verified using CA’s public key. Even if the intermediate point H modifies the
public key or any other data in any of the certificate, the certificate verification will fail. The
public keys of the CA are generally obtained as self-signed certificate.
Still the problem is not over. How to get the public key of the CA in a credible way? Since
the CAs are few in numbers, the public key of the CA is obtained by some other trusted
method. For example, in cases of secure Internet surfing the certificate of CA installed in
the device along with the web browser.
The device that requires a certificate will send the certificate request to the CA. The request
contains the device data such as device ID and device public key. The CA first finds the
digest of the device data and CA specific data using a hash algorithm. CA then signs the
hash using its private key and combines the data and signature in a standard format to
form a certificate and is given to the device. The CA usually does some background check
to ensure the device is not hostile before issuing the certificate. An example of a standard
digital certificate format is X.509 certificates. [7]
Signature
Certificate
On receiving a certificate, a device extracts the data from the certificate, checks the ID and
other data in the certificate. The signature in the certificate is verified using CA’s public
key.
4
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
Hash Algorithm
Digest
Trusted Root CA
In certificate Hierarchy there will be a trusted root CA who will give permission to other CAs
to give certificate to the communicating devices. The root CA will issue the certificate to
these intermediate CAs. These intermediate CAs then issue certificates to the device. In
addition to issuing certificate to the devices the intermediate CA’s will also give their
respective certificate, issued by root CA, to the devices.
There can be multiple levels of certificate hierarchy in which the intermediate CAs will give
permission to other CA to issue certificate to the communicating devices.
5
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
6
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
Since the result of a+b=35 which is out of the range [0,22], the result is wrapped around
in to the range [0 22] by subtracting 35 with 23 till the result is in range [0,22].
a mod b is thus explained as remainder of division a/b.
Subtraction and multiplication can also be explained similarly.
A negative number is added repeatedly with n till it can be represented in the range [0,n-1]
The modular division a/b mod p is defined as a*b-1 mod p. b-1 is the multiplicative inverse
of b.
Multiplicative inverse of number b with respect to mod p is defined as a number b-1 such
that b*b-1 mod p = 1.
7
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
7. RSA
RSA is a public key algorithm that is used for Encryption, Signature and Key Agreement.
RSA typically uses keys of size 1024 to 2048. The RSA standard is specified RFC 3447, RSA
Cryptography Specifications Version 2.1 [3] . Overviews of RSA algorithms are given below.
Parameter generation
R1. Select two prime numbers p and q.
R2. Find n=p*q, Where n is the modulus that is made public. The length of n is
considered as the RSA key length.
R3. Choose a random number ‘e’ as a public key in the range 0<e<(p-1)(q-1) such that
gcd(e,(p-1)(q-1))=1.
R4. Find private key d such that ed≡1(mod (p-1)(q-1)).
Encryption
Consider the device A that needs to send a message to B securely.
R5. Let e be B’s public key. Since e is public, A has access to e.
R6. To encrypt the message M, represent the message as an integer in the range
0<M<n.
R7. Cipher text C = Memod n, where n is the modulus.
Decryption
R8. Let C be the cipher text received from A.
R9. Calculate Message M = Cd mod n, where d is B’s private key and n is the modulus.
8
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
Parameter generation
The parameter generation process is same as that in RSA Encryption. See section 7.1
Signing
Consider the device A that needs to sign the data that it sends to B.
R13. Let d be A’s private key
R14. To sign a data M, represent the data as an integer in the range 0<M<n
R15. Signature C = Mdmod n
Verification
R16. Let M be the message and C be the signature received from A
R17. Calculate M’=Cemod n, where e is A’s public key. Since e is public, B has access to e
R18. If M’=M, the signature is verified, else failed.
From the above equation ed≡1(mod (p-1)(q-1)) and property P1 it follows that
ed-1=K (p-1)(q-1) , which can also be written as
ed-1=k (p-1), and ---- [RX1]
ed-1=k’(q-1) ---- [RX2]
Where K, k and k’ are positive integers
9
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
Since p and q are prime numbers they are coprime to each other. Therefore by using
property P7 the above two equations can be combined as
Med≡M (mod p*q), by property P5
M≡Med(mod p*q)
Since M is chosen in the range 0 and (p*q–1)
M=Medmod p*q, i.e. M = Med mod n
Parameter generation
S1. Choose a 160-bit prime q.
S2. For an integer z, choose an L-bit prime p, such that p=qz+1, 512≤ L ≤ 1024, and L
is divisible by 64.
S3. Choose h, where 1<h<p-1 such that g=hZ mod p>1.
S4. Choose a random number x, where 0<x<q.
S5. Calculate y=gx mod p.
S6. Public key is (p, q, g, y). Private key is x.
Signing
Consider the device A that sign the data M that it sends to B.
S7. Let x be A’s private key and (p, q, g, y) be A’s public key.
S8. Generate a random per-message value k, where 0<k<q.
S9. Calculate r = (gk mod p) mod q.
S10. Calculate s = (k-1(M+x*r))mod q, where M is the hash SHA1 of the message
S11. The signature is (r, s).
Verification
S12. Let M be the message and (r, s) be the signature received from A
S13. Let (p, q, g, y) be A’s public key. Since (p, q, g, y) is public, B has access to it.
S14. Calculate w = s-1 mod q.
S15. Calculate u1 = (M*w) mod q, where M is the hash SHA1 of the message.
S16. Calculate u2 = (r*w) mod q.
S17. Calculate v = ((gu1*yu2) mod p) mod q.
S18. The signature is valid if v=r, invalid otherwise.
10
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
11
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
chosen curve, the modulus p, order of the curve n and the cofactor h. There are several
standard domain parameters defined by SEC, Standards for Efficient Cryptography[6] .
12
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
key pair consisting of a private key dA (a randomly selected integer less than n, where n is
the order of the curve, an elliptic curve domain parameter) and a public key QA = dA*G (G
is the generator point, an elliptic curve domain parameter). An overview of ECDSA process
is defined below.
Signing
Consider the device A that signs the data M that it sends to B.
E7. Let dA be A’s private key
E8. Calculate m = HASH (M), where HASH is a hash function, such as SHA-1
E9. Select a random integer k such that 0<k<n
E10. Calculate r = x1 mod n, where (x1, y1) = k*G
E11. Calculate s = k − 1(m + dA*r) mod n
E12. The signature is the pair (r, s)
Verification
E13. Let M be the message and (r, s) be the signature received from A
E14. Let QA be A’s public key. Since QA is public, B has access to it.
E15. Calculate m = HASH (M)
E16. Calculate w = s −1 mod n
E17. Calculate u1 = m*w mod n and u2 = r*w mod n
E18. Calculate (x1, y1) = u1*G + u2*QA
E19. The signature is valid if x1 = r mod n, invalid otherwise
12. Conclusion
Public key cryptography is an innovation and is an unavoidable part of almost all security
protocol and application. Being able to negotiate a shared secret between two devices
online with out the need of any exchange of secret data created a breakthrough in secure
network/internet communication. Though theoretically it is possible to find the shared
secret from the available public information, it will take exponentially longer time making it
practically impossible. It is the belief in age-old mathematics, that finding an easy method
for reverse process of one-way function is unlikely, keeps the public key cryptography
going.
13
Public Key Cryptography - Applications Algorithms and Mathematical Explanations
Reference
[1] Alfred J. Menezes, Paul C. van Oorschot and Scott A. Vanstone, Handbook of Applied
Cryptography, CRC Press, 1996
[2] FIPS PUB 186-2, Digital Signature Standard (DSS), January 2000, Available at
http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf
[3] RSA Laboratories, PKCS#1 v2.1: RSA Cryptography Standard, June 2002,
http://www.rsa.com/rsalabs/node.asp?id=2125
[4] RFC 2631, Diffie-Hellman Key Agreement Method, June 1999, Available at
http://tools.ietf.org/html/rfc2631
[5] Certicom, Standards for Efficient Cryptography, SEC 1: Elliptic Curve Cryptography,
Version 1.0, September 2000, Available at http://www.secg.org/download/aid-
385/sec1_final.pdf
[6] Certicom, Standards for Efficient Cryptography, SEC 2: Recommended Elliptic Curve
Domain Parameters, Version 1.0, September 2000, Available at
http://www.secg.org/download/aid-386/sec2_final.pdf
[7] ITU, Recommendation X.509, Available at http://www.itu.int/rec/T-REC-X.509-200508-I
[8] Anoop MS, Elliptic Curve Cryptography - An Implementation Guide, January 2007,
Available at http://hosteddocs.ittoolbox.com/AN1.5.07.pdf
[9] Openssl, http://www.openssl.org
[10] Certicom, http://www.certicom.com/index.php?action=ecc_tutorial,home
[11] RSA Laboratories, http://www.rsa.com/rsalabs/node.asp?id=2193
14