<s>
In	O
cryptography	O
,	O
an	O
HMAC	B-Algorithm
(	O
sometimes	O
expanded	O
as	O
either	O
keyed-hash	B-Algorithm
message	I-Algorithm
authentication	I-Algorithm
code	I-Algorithm
or	O
hash-based	B-Algorithm
message	I-Algorithm
authentication	I-Algorithm
code	I-Algorithm
)	O
is	O
a	O
specific	O
type	O
of	O
message	B-Algorithm
authentication	I-Algorithm
code	I-Algorithm
(	O
MAC	O
)	O
involving	O
a	O
cryptographic	B-Algorithm
hash	I-Algorithm
function	I-Algorithm
and	O
a	O
secret	O
cryptographic	O
key	O
.	O
</s>
<s>
HMAC	B-Algorithm
can	O
provide	O
authentication	O
using	O
a	O
shared	O
secret	O
instead	O
of	O
using	O
digital	O
signatures	O
with	O
asymmetric	B-Application
cryptography	I-Application
.	O
</s>
<s>
It	O
trades	O
off	O
the	O
need	O
for	O
a	O
complex	O
public	B-Architecture
key	I-Architecture
infrastructure	I-Architecture
by	O
delegating	O
the	O
key	O
exchange	O
to	O
the	O
communicating	O
parties	O
,	O
who	O
are	O
responsible	O
for	O
establishing	O
and	O
using	O
a	O
trusted	O
channel	O
to	O
agree	O
on	O
the	O
key	O
prior	O
to	O
communication	O
.	O
</s>
<s>
Any	O
cryptographic	B-Algorithm
hash	I-Algorithm
function	I-Algorithm
,	O
such	O
as	O
SHA-2	B-Algorithm
or	O
SHA-3	B-Algorithm
,	O
may	O
be	O
used	O
in	O
the	O
calculation	O
of	O
an	O
HMAC	B-Algorithm
;	O
the	O
resulting	O
MAC	O
algorithm	O
is	O
termed	O
HMAC-X	O
,	O
where	O
X	O
is	O
the	O
hash	B-Error_Name
function	I-Error_Name
used	O
(	O
e.g.	O
</s>
<s>
HMAC-SHA256	B-Algorithm
or	O
HMAC-SHA3-512	O
)	O
.	O
</s>
<s>
The	O
cryptographic	O
strength	O
of	O
the	O
HMAC	B-Algorithm
depends	O
upon	O
the	O
cryptographic	O
strength	O
of	O
the	O
underlying	O
hash	B-Error_Name
function	I-Error_Name
,	O
the	O
size	O
of	O
its	O
hash	B-Error_Name
output	O
,	O
and	O
the	O
size	O
and	O
quality	O
of	O
the	O
key	O
.	O
</s>
<s>
HMAC	B-Algorithm
uses	O
two	O
passes	O
of	O
hash	B-Error_Name
computation	O
.	O
</s>
<s>
Next	O
,	O
the	O
first	O
pass	O
of	O
the	O
hash	B-Error_Name
algorithm	I-Error_Name
produces	O
an	O
internal	O
hash	B-Error_Name
derived	O
from	O
the	O
message	O
and	O
the	O
inner	O
key	O
.	O
</s>
<s>
The	O
second	O
pass	O
produces	O
the	O
final	O
HMAC	B-Algorithm
code	O
derived	O
from	O
the	O
inner	O
hash	B-Error_Name
result	O
and	O
the	O
outer	O
key	O
.	O
</s>
<s>
An	O
iterative	O
hash	B-Error_Name
function	I-Error_Name
(	O
one	O
that	O
uses	O
the	O
Merkle	B-Algorithm
–	I-Algorithm
Damgård	I-Algorithm
construction	I-Algorithm
)	O
breaks	O
up	O
a	O
message	O
into	O
blocks	O
of	O
a	O
fixed	O
size	O
and	O
iterates	O
over	O
them	O
with	O
a	O
compression	B-Algorithm
function	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
SHA-256	B-Algorithm
operates	O
on	O
512-bit	O
blocks	O
.	O
</s>
<s>
The	O
size	O
of	O
the	O
output	O
of	O
HMAC	B-Algorithm
is	O
the	O
same	O
as	O
that	O
of	O
the	O
underlying	O
hash	B-Error_Name
function	I-Error_Name
(	O
e.g.	O
,	O
256	O
and	O
512	O
bits	O
in	O
the	O
case	O
of	O
SHA-256	B-Algorithm
and	O
SHA3-512	B-Algorithm
,	O
respectively	O
)	O
,	O
although	O
it	O
can	O
be	O
truncated	O
if	O
desired	O
.	O
</s>
<s>
HMAC	B-Algorithm
does	O
not	O
encrypt	O
the	O
message	O
.	O
</s>
<s>
Instead	O
,	O
the	O
message	O
(	O
encrypted	O
or	O
not	O
)	O
must	O
be	O
sent	O
alongside	O
the	O
HMAC	B-Algorithm
hash	B-Error_Name
.	O
</s>
<s>
Parties	O
with	O
the	O
secret	O
key	O
will	O
hash	B-Error_Name
the	O
message	O
again	O
themselves	O
,	O
and	O
if	O
it	O
is	O
authentic	O
,	O
the	O
received	O
and	O
computed	O
hashes	B-Error_Name
will	O
match	O
.	O
</s>
<s>
The	O
definition	O
and	O
analysis	O
of	O
the	O
HMAC	B-Algorithm
construction	O
was	O
first	O
published	O
in	O
1996	O
in	O
a	O
paper	O
by	O
Mihir	O
Bellare	O
,	O
Ran	O
Canetti	O
,	O
and	O
Hugo	O
Krawczyk	O
,	O
and	O
they	O
also	O
wrote	O
RFC	O
2104	O
in	O
1997	O
.	O
</s>
<s>
The	O
1996	O
paper	O
also	O
defined	O
a	O
nested	O
variant	O
called	O
NMAC	B-Algorithm
(	O
Nested	O
MAC	O
)	O
.	O
</s>
<s>
FIPS	O
PUB	O
198	O
generalizes	O
and	O
standardizes	O
the	O
use	O
of	O
HMACs	B-Algorithm
.	O
</s>
<s>
HMAC	B-Algorithm
is	O
used	O
within	O
the	O
IPsec	B-Protocol
,	O
SSH	B-Protocol
and	O
TLS	B-Protocol
protocols	I-Protocol
and	O
for	O
JSON	B-General_Concept
Web	I-General_Concept
Tokens	I-General_Concept
.	O
</s>
<s>
is	O
a	O
cryptographic	B-Algorithm
hash	I-Algorithm
function	I-Algorithm
.	O
</s>
<s>
is	O
the	O
block-sized	O
outer	O
padding	O
,	O
consisting	O
of	O
repeated	O
bytes	B-Application
valued	O
0x5c	O
.	O
</s>
<s>
is	O
the	O
block-sized	O
inner	O
padding	O
,	O
consisting	O
of	O
repeated	O
bytes	B-Application
valued	O
0x36	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
demonstrates	O
how	O
HMAC	B-Algorithm
may	O
be	O
implemented	O
.	O
</s>
<s>
The	O
block	O
size	O
is	O
512	O
bits	O
(	O
64	O
bytes	B-Application
)	O
when	O
using	O
one	O
of	O
the	O
following	O
hash	B-Error_Name
functions	I-Error_Name
:	O
SHA-1	B-Algorithm
,	O
MD5	B-Algorithm
,	O
RIPEMD-128	O
.	O
</s>
<s>
The	O
design	O
of	O
the	O
HMAC	B-Algorithm
specification	O
was	O
motivated	O
by	O
the	O
existence	O
of	O
attacks	O
on	O
more	O
trivial	O
mechanisms	O
for	O
combining	O
a	O
key	O
with	O
a	O
hash	B-Error_Name
function	I-Error_Name
.	O
</s>
<s>
For	O
example	O
,	O
one	O
might	O
assume	O
the	O
same	O
security	O
that	O
HMAC	B-Algorithm
provides	O
could	O
be	O
achieved	O
with	O
MAC	O
=	O
H( key	O
∥	O
message	O
)	O
.	O
</s>
<s>
However	O
,	O
this	O
method	O
suffers	O
from	O
a	O
serious	O
flaw	O
:	O
with	O
most	O
hash	B-Error_Name
functions	I-Error_Name
,	O
it	O
is	O
easy	O
to	O
append	O
data	O
to	O
the	O
message	O
without	O
knowing	O
the	O
key	O
and	O
obtain	O
another	O
valid	O
MAC	O
(	O
"	O
length-extension	O
attack	O
"	O
)	O
.	O
</s>
<s>
The	O
alternative	O
,	O
appending	O
the	O
key	O
using	O
MAC	O
=	O
H( message	O
∥	O
key	O
)	O
,	O
suffers	O
from	O
the	O
problem	O
that	O
an	O
attacker	O
who	O
can	O
find	O
a	O
collision	O
in	O
the	O
(	O
unkeyed	O
)	O
hash	B-Error_Name
function	I-Error_Name
has	O
a	O
collision	O
in	O
the	O
MAC	O
(	O
as	O
two	O
messages	O
m1	O
and	O
m2	O
yielding	O
the	O
same	O
hash	B-Error_Name
will	O
provide	O
the	O
same	O
start	O
condition	O
to	O
the	O
hash	B-Error_Name
function	I-Error_Name
before	O
the	O
appended	O
key	O
is	O
hashed	O
,	O
hence	O
the	O
final	O
hash	B-Error_Name
will	O
be	O
the	O
same	O
)	O
.	O
</s>
<s>
No	O
known	O
extension	O
attacks	O
have	O
been	O
found	O
against	O
the	O
current	O
HMAC	B-Algorithm
specification	O
which	O
is	O
defined	O
as	O
H( key	O
∥	O
H( key	O
∥	O
message	O
)	O
)	O
because	O
the	O
outer	O
application	O
of	O
the	O
hash	B-Error_Name
function	I-Error_Name
masks	O
the	O
intermediate	O
result	O
of	O
the	O
internal	O
hash	B-Error_Name
.	O
</s>
<s>
The	O
security	O
reduction	O
of	O
HMAC	B-Algorithm
does	O
require	O
them	O
to	O
be	O
different	O
in	O
at	O
least	O
one	O
bit	O
.	O
</s>
<s>
The	O
Keccak	B-Algorithm
hash	B-Error_Name
function	I-Error_Name
,	O
that	O
was	O
selected	O
by	O
NIST	O
as	O
the	O
SHA-3	B-Algorithm
competition	O
winner	O
,	O
does	O
n't	O
need	O
this	O
nested	O
approach	O
and	O
can	O
be	O
used	O
to	O
generate	O
a	O
MAC	O
by	O
simply	O
prepending	O
the	O
key	O
to	O
the	O
message	O
,	O
as	O
it	O
is	O
not	O
susceptible	O
to	O
length-extension	O
attacks	O
.	O
</s>
<s>
The	O
cryptographic	O
strength	O
of	O
the	O
HMAC	B-Algorithm
depends	O
upon	O
the	O
size	O
of	O
the	O
secret	O
key	O
that	O
is	O
used	O
and	O
the	O
security	O
of	O
the	O
underlying	O
hash	B-Error_Name
function	I-Error_Name
used	O
.	O
</s>
<s>
It	O
has	O
been	O
proven	O
that	O
the	O
security	O
of	O
an	O
HMAC	B-Algorithm
construction	O
is	O
directly	O
related	O
to	O
security	O
properties	O
of	O
the	O
hash	B-Error_Name
function	I-Error_Name
used	O
.	O
</s>
<s>
The	O
most	O
common	O
attack	O
against	O
HMACs	B-Algorithm
is	O
brute	O
force	O
to	O
uncover	O
the	O
secret	O
key	O
.	O
</s>
<s>
HMACs	B-Algorithm
are	O
substantially	O
less	O
affected	O
by	O
collisions	O
than	O
their	O
underlying	O
hashing	B-Error_Name
algorithms	I-Error_Name
alone	O
.	O
</s>
<s>
In	O
particular	O
,	O
Mihir	O
Bellare	O
proved	O
that	O
HMAC	B-Algorithm
is	O
a	O
PRF	O
under	O
the	O
sole	O
assumption	O
that	O
the	O
compression	B-Algorithm
function	I-Algorithm
is	O
a	O
PRF	O
.	O
</s>
<s>
Therefore	O
,	O
HMAC-MD5	B-Algorithm
does	O
not	O
suffer	O
from	O
the	O
same	O
weaknesses	O
that	O
have	O
been	O
found	O
in	O
MD5	B-Algorithm
.	O
</s>
<s>
RFC	O
2104	O
requires	O
that	O
"	O
keys	O
longer	O
than	O
B	O
bytes	B-Application
are	O
first	O
hashed	O
using	O
H	O
"	O
which	O
leads	O
to	O
a	O
confusing	O
pseudo-collision	O
:	O
if	O
the	O
key	O
is	O
longer	O
than	O
the	O
hash	B-Error_Name
block	O
size	O
(	O
e.g.	O
</s>
<s>
64	O
bytes	B-Application
for	O
SHA-1	B-Algorithm
)	O
,	O
then	O
HMAC(k, m )	O
is	O
computed	O
as	O
HMAC(H(k )	O
,	O
m	O
)	O
.This	O
property	O
is	O
sometimes	O
raised	O
as	O
a	O
possible	O
weakness	O
of	O
HMAC	B-Algorithm
in	O
password-hashing	O
scenarios	O
:	O
it	O
has	O
been	O
demonstrated	O
that	O
it	O
's	O
possible	O
to	O
find	O
a	O
long	O
ASCII	O
string	O
and	O
a	O
random	O
value	O
whose	O
hash	B-Error_Name
will	O
be	O
also	O
an	O
ASCII	O
string	O
,	O
and	O
both	O
values	O
will	O
produce	O
the	O
same	O
HMAC	B-Algorithm
output	O
.	O
</s>
<s>
In	O
2006	O
,	O
Jongsung	O
Kim	O
,	O
Alex	O
Biryukov	O
,	O
Bart	O
Preneel	O
,	O
and	O
Seokhie	O
Hong	O
showed	O
how	O
to	O
distinguish	O
HMAC	B-Algorithm
with	O
reduced	O
versions	O
of	O
MD5	B-Algorithm
and	O
SHA-1	B-Algorithm
or	O
full	O
versions	O
of	O
HAVAL	B-Algorithm
,	O
MD4	B-Algorithm
,	O
and	O
SHA-0	B-Algorithm
from	O
a	O
random	O
function	O
or	O
HMAC	B-Algorithm
with	O
a	O
random	O
function	O
.	O
</s>
<s>
Differential	O
distinguishers	O
allow	O
an	O
attacker	O
to	O
devise	O
a	O
forgery	B-General_Concept
attack	O
on	O
HMAC	B-Algorithm
.	O
</s>
<s>
HMAC	B-Algorithm
with	O
the	O
full	O
version	O
of	O
MD4	B-Algorithm
can	O
be	O
forged	B-General_Concept
with	O
this	O
knowledge	O
.	O
</s>
<s>
These	O
attacks	O
do	O
not	O
contradict	O
the	O
security	O
proof	O
of	O
HMAC	B-Algorithm
,	O
but	O
provide	O
insight	O
into	O
HMAC	B-Algorithm
based	O
on	O
existing	O
cryptographic	B-Algorithm
hash	I-Algorithm
functions	I-Algorithm
.	O
</s>
<s>
presented	O
a	O
distinguishing	O
attack	O
on	O
HMAC-MD5	B-Algorithm
without	O
using	O
related	O
keys	O
.	O
</s>
<s>
It	O
can	O
distinguish	O
an	O
instantiation	O
of	O
HMAC	B-Algorithm
with	O
MD5	B-Algorithm
from	O
an	O
instantiation	O
with	O
a	O
random	O
function	O
with	O
297	O
queries	O
with	O
probability	O
0.87	O
.	O
</s>
<s>
In	O
2011	O
an	O
informational	O
RFC	O
6151	O
was	O
published	O
to	O
summarize	O
security	O
considerations	O
in	O
MD5	B-Algorithm
and	O
HMAC-MD5	B-Algorithm
.	O
</s>
<s>
For	O
HMAC-MD5	B-Algorithm
the	O
RFC	O
summarizes	O
that	O
–	O
although	O
the	O
security	O
of	O
the	O
MD5	B-Algorithm
hash	I-Algorithm
function	O
itself	O
is	O
severely	O
compromised	O
–	O
the	O
currently	O
known	O
"	O
attacks	O
on	O
HMAC-MD5	B-Algorithm
do	O
not	O
seem	O
to	O
indicate	O
a	O
practical	O
vulnerability	O
when	O
used	O
as	O
a	O
message	B-Algorithm
authentication	I-Algorithm
code	I-Algorithm
"	O
,	O
but	O
it	O
also	O
adds	O
that	O
"	O
for	O
a	O
new	O
protocol	O
design	O
,	O
a	O
ciphersuite	O
with	O
HMAC-MD5	B-Algorithm
should	O
not	O
be	O
included	O
"	O
.	O
</s>
<s>
In	O
May	O
2011	O
,	O
RFC	O
6234	O
was	O
published	O
detailing	O
the	O
abstract	O
theory	O
and	O
source	O
code	O
for	O
SHA-based	O
HMACs	B-Algorithm
.	O
</s>
<s>
Here	O
are	O
some	O
HMAC	B-Algorithm
values	O
,	O
assuming	O
8-bit	O
ASCII	O
encoding	O
:	O
</s>
