~ubuntu-branches/ubuntu/intrepid/libcrypto++/intrepid

« back to all changes in this revision

Viewing changes to hmac.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Machard
  • Date: 2004-08-27 12:35:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040827123505-7evgxiu7k8memiyk
Tags: 5.2.1a-1
* Urgency set to high because lastest upload was unclean
* Rename libcrypto++-5.2.1.orig.tar.gz in  libcrypto++-5.2.1a.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// hmac.cpp - written and placed in the public domain by Wei Dai
 
2
 
 
3
#include "pch.h"
 
4
 
 
5
#ifndef CRYPTOPP_IMPORTS
 
6
 
 
7
#include "hmac.h"
 
8
 
 
9
NAMESPACE_BEGIN(CryptoPP)
 
10
 
 
11
void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength)
 
12
{
 
13
        AssertValidKeyLength(keylength);
 
14
 
 
15
        Restart();
 
16
 
 
17
        HashTransformation &hash = AccessHash();
 
18
        unsigned int blockSize = hash.BlockSize();
 
19
 
 
20
        if (!blockSize)
 
21
                throw InvalidArgument("HMAC: can only be used with a block-based hash function");
 
22
 
 
23
        if (keylength <= blockSize)
 
24
                memcpy(AccessIpad(), userKey, keylength);
 
25
        else
 
26
        {
 
27
                AccessHash().CalculateDigest(AccessIpad(), userKey, keylength);
 
28
                keylength = hash.DigestSize();
 
29
        }
 
30
 
 
31
        assert(keylength <= blockSize);
 
32
        memset(AccessIpad()+keylength, 0, blockSize-keylength);
 
33
 
 
34
        for (unsigned int i=0; i<blockSize; i++)
 
35
        {
 
36
                AccessOpad()[i] = AccessIpad()[i] ^ OPAD;
 
37
                AccessIpad()[i] ^= IPAD;
 
38
        }
 
39
}
 
40
 
 
41
void HMAC_Base::KeyInnerHash()
 
42
{
 
43
        assert(!m_innerHashKeyed);
 
44
        HashTransformation &hash = AccessHash();
 
45
        hash.Update(AccessIpad(), hash.BlockSize());
 
46
        m_innerHashKeyed = true;
 
47
}
 
48
 
 
49
void HMAC_Base::Restart()
 
50
{
 
51
        if (m_innerHashKeyed)
 
52
        {
 
53
                AccessHash().Restart();
 
54
                m_innerHashKeyed = false;
 
55
        }
 
56
}
 
57
 
 
58
void HMAC_Base::Update(const byte *input, unsigned int length)
 
59
{
 
60
        if (!m_innerHashKeyed)
 
61
                KeyInnerHash();
 
62
        AccessHash().Update(input, length);
 
63
}
 
64
 
 
65
void HMAC_Base::TruncatedFinal(byte *mac, unsigned int size)
 
66
{
 
67
        ThrowIfInvalidTruncatedSize(size);
 
68
 
 
69
        HashTransformation &hash = AccessHash();
 
70
 
 
71
        if (!m_innerHashKeyed)
 
72
                KeyInnerHash();
 
73
        hash.Final(AccessInnerHash());
 
74
 
 
75
        hash.Update(AccessOpad(), hash.BlockSize());
 
76
        hash.Update(AccessInnerHash(), hash.DigestSize());
 
77
        hash.TruncatedFinal(mac, size);
 
78
 
 
79
        m_innerHashKeyed = false;
 
80
}
 
81
 
 
82
NAMESPACE_END
 
83
 
 
84
#endif