# ElGamal encryption

The ElGamal algorithm is an asymmetric key encryption algorithm for public key cryptography which is based on Diffie-Hellman key agreement. It was described by Taher Elgamal in 1984. The ElGamal algorithm is used in the free GNU Privacy Guard software, recent versions of PGP, and other cryptosystems. The Digital Signature Algorithm, even though it is a signature scheme and performs no encryption, is similar to ElGamal in many respects.

ElGamal can be defined over any cyclic group [itex]G[itex]. Its security depends upon the difficulty of a certain problem in [itex]G[itex] related to computing discrete logs (see below).

 Contents

## The algorithm

ElGamal consists of three components: the key generator, the encryption algorithm, and the decryption algorithm.

The key generator works as follows:

• Alice generates an efficient description of a cyclic group [itex]G[itex] of order [itex]q[itex] with generator [itex]g[itex]. See below for specific examples of how this can be done.
• Alice chooses a random [itex]x[itex] from [itex]\{0, \ldots, q-1\}[itex].
• Alice computes [itex]h = g^x[itex].
• Alice publishes [itex]h[itex], along with the description of [itex]G, q, g[itex], as her public key. Alice retains [itex]x[itex] as her secret key.

The encryption algorithm works as follows: to encrypt a message [itex]m[itex] to Alice under her public key [itex](G,q,g,h)[itex],

• Bob converts [itex]m[itex] into an element of [itex]G[itex].
• Bob chooses a random [itex]k[itex] from [itex]\{0, \ldots, q-1\}[itex], then calculates [itex]c_1=g^k[itex] and [itex]c_2=m\cdot h^k[itex].
• Bob sends the ciphertext [itex](c_1,c_2)[itex] to Alice.

The decryption algorithm works as follows: to decrypt a ciphertext [itex](c_1,c_2)[itex] with her secret key [itex]x[itex],

• Alice computes [itex]c_2(c_1^x)^{-1}[itex] as the plaintext message.

Note that the decryption algorithm does indeed produce the intended message since:

[itex] c_2(c_1^x)^{-1} \equiv \frac{m\cdot h^k}{g^{xk}} \equiv \frac{m\cdot g^{xk}}{g^{xk}} \equiv m \pmod q[itex]

If the space of possible messages is larger than the size of [itex]G[itex], then the message can be split into several pieces and each piece can be encrypted independently. Typically, however, a short key to a symmetric-key cipher is first encrypted under ElGamal, and the (much longer) intended message is encrypted more efficiently using the symmetric-key cipher — this is termed hybrid encryption.

## Security

ElGamal is a simple example of a semantically secure asymmetric key encryption algorithm (under reasonable assumptions). It is probabilistic, meaning that a single plaintext can be encrypted to many possible ciphertexts, with the consequence that a general ElGamal encryption produces a 2:1 expansion in size from plaintext to ciphertext.

ElGamal's security rests, in part, on the difficulty of solving the discrete logarithm problem in [itex]G[itex]. Specifically, if the discrete logarithm problem could be solved efficiently, then ElGamal would be broken. However, the security of ElGamal actually relies on the so-called Decisional Diffie-Hellman (DDH) assumption. This assumption is often stronger than the discrete log assumption, but is still believed to be true for many classes of groups.

## Use as a signature scheme

Alice may have sent Bob a message m (using RSA for example) and may want to sign the message (for example to preserve integrity or to prove its authenticity to Bob - see digital signature).

To do this, Alice may take a collision-resistant hash function H; choose a random k as Bob would to send a message to Alice, except that k must satisfy hcf(k,p-1) = 1; and then find numbers r,s satisfying

1. [itex] r \, \equiv \, h^k \pmod p[itex]
2. [itex] H(m) \, \equiv \, xr + ks \pmod {p-1}.[itex]

Such a pair (r,s) is guaranteed to exist and to be uniquely defined since we imposed the conditition hcf(k,p-1) = 1. Bob may then accept the message as (almost certainly) authentic and unmodified if

[itex] g^H(m) \, \equiv \, h^r r^s \pmod p[itex]

This will normally be the case since:

[itex]

\begin{matrix} g^H(m) & \equiv & g^{xr} g^{ks} \\ & \equiv & (g^{x})^r (g^{k})^s \\ & \equiv & (h)^r (r)^s \\ \end{matrix} [itex]

In order to forge a signature, a third party must either know Alice's private key x (perhaps by solving the discrete logarithm problem) or find collisions in the hash function H. However in the latter case the said third party would only be able to sign messages M such that H(m) = H(M). It may be that these messages are in fact meaningless or at least not very useful.

Alice must be careful to choose a different k for each signature, because otherwise a third party will be able to deduce the secret key x with significantly less difficulty.

## Generating the group G

As described above, ElGamal can be defined over any cyclic group [itex]G[itex], and is secure if a certain computational assumption (the "DDH Assumption") about that group is true. Unfortunately, the straightforward use of [itex]G=Z_p[itex] for a prime [itex]p[itex] is insecure, because the DDH Assumption is false in this group. (In contrast, note that computing discrete logs is believed to be hard in [itex]Z_p[itex], but this is not enough for the security of ElGamal.)

The two most popular types of groups used in ElGamal are subgroups of [itex]Z_p[itex] and groups defined over certain elliptic curves. Here is one popular way of choosing an appropriate subgroup of [itex]Z_p[itex] which is believed to be secure:

• Choose a random large prime [itex]p[itex] such that [itex]p-1=kq[itex] for some small integer [itex]k[itex] and large prime [itex]q[itex]. This can be done, for example with [itex]k=2[itex], by first choosing a random large prime [itex]q[itex] and checking if [itex]p=2q+1[itex] is prime.
• Choose a random element [itex]g \in Z_p[itex] such that [itex]g \neq 1[itex] and [itex]g^q=1 \bmod p[itex], i.e. such that [itex]g[itex] is of order [itex]q[itex].
• The group [itex]G[itex] is the subgroup of [itex]Z_p[itex] generated by [itex]g[itex], i.e. the set of [itex]k[itex]th residues mod [itex]p[itex].

Note also that when encrypting, care must be taken to properly encode the message [itex]m[itex] as an element of [itex]G[itex], and not, say, as just an arbitrary element of [itex]Z_p[itex].

## Efficiency

Encryption under ElGamal requires two exponentiations; however, these exponentiations are independent of the message and can be computed ahead of time if need be. Decryption only requires one exponentiation (plus one division, which is typically much faster). Unlike in the RSA and Rabin systems, ElGamal decryption cannot be sped up via the Chinese remainder theorem.

## Miscellaneous

Note that ElGamal is malleable in an extreme way: for example, given an encryption [itex](c_1, c_2)[itex] of some (possibly unknown) message [itex]m[itex], one can easily construct an encryption [itex](c_1, 2 \cdot c_2)[itex] of the message [itex]2m[itex]. Therefore ElGamal is not secure under chosen ciphertext attack. On the other hand, the Cramer-Shoup system (which is based on ElGamal) is secure under chosen ciphertext attack.

## References

• Taher Elgamal, "A Public-Key Cryptosystem and a Signature Scheme Based on Discrete Logarithms", IEEE Transactions on Information Theory, v. IT-31, n. 4, 1985, pp469–472 or CRYPTO 84, pp10–18, Springer-Verlag.
• Handbook of Applied Cryptography (http://www.cacr.math.uwaterloo.ca/hac/), contains a detailed description of ElGamal Algorithm in Chapter 8 (http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf) (PDF file).de:Elgamal-Kryptosystem

• Art and Cultures
• Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
• Space and Astronomy