~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/taocrypt/include/pwdbased.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2000-2007 MySQL AB
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; version 2 of the License.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; see the file COPYING. If not, write to the
 
15
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 
16
   MA  02110-1301  USA.
 
17
*/
 
18
 
 
19
/* pwdbased.hpp defines PBKDF2 from PKCS #5
 
20
*/
 
21
 
 
22
 
 
23
#ifndef TAO_CRYPT_PWDBASED_HPP
 
24
#define TAO_CRYPT_PWDBASED_HPP
 
25
 
 
26
#include <string.h>
 
27
#include "misc.hpp"
 
28
#include "block.hpp"
 
29
#include "hmac.hpp"
 
30
 
 
31
namespace TaoCrypt {
 
32
 
 
33
 
 
34
// From PKCS #5, T must be type suitable for HMAC<T> 
 
35
template <class T>
 
36
class PBKDF2_HMAC {
 
37
public:
 
38
    word32 MaxDerivedKeyLength() const { return 0xFFFFFFFFU;} // avoid overflow
 
39
 
 
40
    word32 DeriveKey(byte* derived, word32 dLen, const byte* pwd, word32 pLen,
 
41
                     const byte* salt, word32 sLen, word32 iterations) const;
 
42
}; 
 
43
 
 
44
 
 
45
 
 
46
template <class T>
 
47
word32 PBKDF2_HMAC<T>::DeriveKey(byte* derived, word32 dLen, const byte* pwd,
 
48
                                 word32 pLen, const byte* salt, word32 sLen,
 
49
                                 word32 iterations) const
 
50
{
 
51
        assert(dLen <= MaxDerivedKeyLength());
 
52
        assert(iterations > 0);
 
53
 
 
54
    ByteBlock buffer(T::DIGEST_SIZE);
 
55
        HMAC<T>   hmac;
 
56
 
 
57
    hmac.SetKey(pwd, pLen);
 
58
 
 
59
        word32 i = 1;
 
60
 
 
61
        while (dLen > 0) {
 
62
                hmac.Update(salt, sLen);
 
63
                word32 j;
 
64
                for (j = 0; j < 4; j++) {
 
65
                        byte b = i >> ((3-j)*8);
 
66
                        hmac.Update(&b, 1);
 
67
                }
 
68
                hmac.Final(buffer.get_buffer());
 
69
 
 
70
                word32 segmentLen = min(dLen, buffer.size());
 
71
                memcpy(derived, buffer.get_buffer(), segmentLen);
 
72
 
 
73
                for (j = 1; j < iterations; j++) {
 
74
                        hmac.Update(buffer.get_buffer(), buffer.size());
 
75
            hmac.Final(buffer.get_buffer());
 
76
                        xorbuf(derived, buffer.get_buffer(), segmentLen);
 
77
                }
 
78
                derived += segmentLen;
 
79
                dLen    -= segmentLen;
 
80
                i++;
 
81
        }
 
82
        return iterations;
 
83
}
 
84
 
 
85
 
 
86
 
 
87
 
 
88
} // naemspace
 
89
 
 
90
#endif // TAO_CRYPT_PWDBASED_HPP