DI Management Home > Cryptography > Public key cryptography using discrete logarithms > Part 2: MQV Key Agreement

# Public key cryptography using discrete logarithms. Part 2: MQV Key Agreement

The original Diffie-Hellman Key Exchange method is still susceptible to a man-in-the-middle attack where an active adversary can manipulate the messages and discover the shared secret.

 << previous: Diffie-Hellman key exchange next: ElGamal Encryption >>

## MQV Key Agreement

MQV key agreement is an improvement on the basic Diffie-Hellman designed to eliminate a man-in-the-middle attack. It is named after Menezes, Qu and Vanstone [MQV98]. The original paper is written for elliptic curve cryptography, but the protocol also works with discrete logarithms. Unfortunately there may be patents associated with it.

MQV assumes that the two parties, Alice and Bob, have already established and authenticated their static key pairs $(a,A)$ and $(b,B)$ with each other; that is, Alice trusts that Bob's public key $B$ really is his, and Bob trusts Alice's public key $A$. Each party generates a random sessional or ephemeral key pair, $(x,X=g^x)$ and $(y,Y=g^y)$, and these are used together with the static keys to create a new shared secret.

Algorithm: MQV Key Agreement
INPUT: Domain parameters $(p,q,g)$; pre-established D-H key pairs $(a,A=g^a)$ and $(b,B=g^b)$.
OUTPUT: Shared secret $Z$ in the range $0\lt Z\lt p$.
1. Alice chooses a random number $x$ in the range $[2,q-2]$ and sends $X=g^x\mod p$ to Bob.
2. Bob chooses a random number $y$ in the range $[2,q-2]$ and sends $Y=g^y\mod p$ to Alice.
3. Alice and Bob both compute $\overline{X} = X \mod 2^{\ell} + 2^{\ell}$ and $\overline{Y} = Y \mod 2^{\ell} + 2^{\ell}$ where $\ell = \lceil f/2\rceil$ and $f$ is the bit length of $q$.
4. Alice computes her implicit signature $S_A = (x + \overline{X}a) \mod q$.
5. Alice computes $t_A = YB^{\overline{Y}} \mod p$.
6. Alice computes $Z_A = \left(t_A\right)^{S_A} \mod p$.
7. Return shared secret $Z = Z_A$.

Similarly, Bob can also compute the same value, $Z$, using steps 4 to 7 above.

• 4. Bob computes his implicit signature $S_B = (y + \overline{Y}b) \mod q$.
• 5. Bob computes $t_B = XA^{\overline{X}} \mod p$.
• 6. Bob computes $Z_B = \left(t_B\right)^{S_B} \mod p$.
• 7. Return shared secret $Z = Z_B$.

The value of $\ell$ is half the bit length of $q$ rounded upwards. The bit length of $q$ is given by $f = \lfloor\log_2 q\rfloor + 1$. The value $\overline{Y}$ is the right-most $\ell$ bits of $Y$ (i.e. bits $0$ to $\ell-1$) with bit $\ell$ set to one. The idea behind using a truncated $X$ and $Y$ is to reduce the computational effort in step 5.

### ▷ Why does this work?

Working modulo $p$ we have

 $YB^{\overline{Y}} = g^y\cdot\left(g^b\right)^{\overline{Y}}$ $XA^{\overline{X}} = g^x\cdot\left(g^a\right)^{\overline{X}}$ $\qquad\; = g^{y+b\overline{Y}}$ $\qquad\; = g^{x+a\overline{X}}$ $\qquad\; = g^{(y+b\overline{Y})\mod q}$ $\qquad\; = g^{(x+a\overline{X})\mod q}$ $\qquad\; = g^{S_B}, \quad$ $\qquad\; = g^{S_A}$, and thus $Z_A\;=\left(YB^{\overline{Y}}\right)^{S_A} = \left(g^{S_B}\right)^{S_A}$ $\qquad = g^{S_BS_A}$. $Z_B\;=\left(XA^{\overline{X}}\right)^{S_B} = \left(g^{S_A}\right)^{S_B}$ $\qquad = g^{S_AS_B}$.

We are allowed to do the "trick" with the exponents modulo $q$ because even if $n\neq m$ but $n\equiv m\pmod{q}$ then $g^n\equiv g^m\pmod{p}$. This follows because $n\equiv m\pmod{q}$ means $n=m+kq$ for some integer $k$ and so $g^n\equiv g^{m+kq} \equiv g^m\left(g^q\right)^k \equiv g^m(1)^k \equiv g^m \pmod{p}$ since $g^q\equiv 1\pmod{p}$ by the definition of $g$.

### Example of MQV key agreement by Alice

• INPUT: Domain parameters $(p=283, q=47, g=60)$
• INPUT: Pre-established static key pairs $(a,A)=(24,158)$ and $(b,B)=(7,216)$
• 1. Alice chooses a random $x=25$, computes $X=g^x = 60^{25}\mod p = 114$, and sends $X$ to Bob.
• 2. Bob chooses a random $y=32$, computes $Y=g^y = 60^{32}\mod p = 175$, and sends $Y$ to Alice.
• 3. Both parties compute:
$\qquad$ $f = 6$, the bit length of $q$, so $\ell=\lceil f/2\rceil = 3$,
$\qquad$ $\overline{X}=X\mod 2^{\ell} + 2^{\ell} = 141\mod 2^3 + 2^3 = 5 + 8 = 13$ and
$\qquad$ $\overline{Y}=Y\mod 2^{\ell} + 2^{\ell} = 175\mod 2^3 + 2^3 = 7 + 8 = 15$

Alice computes:

• 4. $S_A = (x + \overline{X}a) \mod q = (25 + 13\cdot 24)\mod 47 = 8$
• 5. $t_A = Y\cdot B^{\overline{Y}}\mod p = 175\cdot 216^{15}\mod 283 = 151$.
• 6. $Z_A = t_A^{S_A}\mod p = 151^8\mod 283 = 207$.

Bob computes:

• 4. $S_B = (y + \overline{Y}b) \mod q = (32 + 15\cdot 7)\mod 47 = 43$
• 5. $t_B = X\cdot A^{\overline{X}}\mod p = 141\cdot 158^{13}\mod 283 = 225$.
• 6. $Z_B = t_B^{S_B}\mod p = 225^{43}\mod 283 = 207$.

The shared secret known only to Alice and Bob is $Z=207$. Note that this works despite the very small numbers we end up with after truncating $X$ and $Y$.

## References

• [MQV98] Law, Laurie, Alfred Menezes, Minghua Qu, Jerry Solinas, and Scott Vanstone (1998). An Efficient Protocol for Authenticated Key Agreement, Technical report CORR 98-05, University of Waterloo, Canada, March 1998. Revised August 28 1998. <pdf-link>