monotone

monotone Mtn Source Tree

Root/botan/pkcs5.cpp

1/*************************************************
2* PKCS #5 Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/pkcs5.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9#include <botan/hmac.h>
10#include <memory>
11
12namespace Botan {
13
14/*************************************************
15* Return a PKCS#5 PBKDF1 derived key *
16*************************************************/
17OctetString PKCS5_PBKDF1::derive(u32bit key_len,
18 const std::string& passphrase,
19 const byte salt[], u32bit salt_size,
20 u32bit iterations) const
21 {
22 if(iterations == 0)
23 throw Invalid_Argument("PKCS#5 PBKDF1: Invalid iteration count");
24
25 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
26 if(key_len > hash->OUTPUT_LENGTH)
27 throw Exception("PKCS#5 PBKDF1: Requested output length too long");
28
29 hash->update(passphrase);
30 hash->update(salt, salt_size);
31 SecureVector<byte> key = hash->final();
32
33 for(u32bit j = 1; j != iterations; j++)
34 {
35 hash->update(key);
36 hash->final(key);
37 }
38
39 return OctetString(key, std::min(key_len, key.size()));
40 }
41
42/*************************************************
43* Return the name of this type *
44*************************************************/
45std::string PKCS5_PBKDF1::name() const
46 {
47 return "PBKDF1(" + hash_name + ")";
48 }
49
50/*************************************************
51* PKCS5_PBKDF1 Constructor *
52*************************************************/
53PKCS5_PBKDF1::PKCS5_PBKDF1(const std::string& h_name) : hash_name(h_name)
54 {
55 if(!have_hash(hash_name))
56 throw Algorithm_Not_Found(hash_name);
57 }
58
59/*************************************************
60* Return a PKCS#5 PBKDF2 derived key *
61*************************************************/
62OctetString PKCS5_PBKDF2::derive(u32bit key_len,
63 const std::string& passphrase,
64 const byte salt[], u32bit salt_size,
65 u32bit iterations) const
66 {
67 if(iterations == 0)
68 throw Invalid_Argument("PKCS#5 PBKDF2: Invalid iteration count");
69
70 if(passphrase.length() == 0)
71 throw Invalid_Argument("PKCS#5 PBKDF2: Empty passphrase is invalid");
72
73 HMAC hmac(hash_name);
74 hmac.set_key((const byte*)passphrase.c_str(), passphrase.length());
75 SecureVector<byte> key(key_len);
76
77 byte* T = key.begin();
78
79 u32bit counter = 1;
80 while(key_len)
81 {
82 u32bit T_size = std::min(hmac.OUTPUT_LENGTH, key_len);
83 SecureVector<byte> U(hmac.OUTPUT_LENGTH);
84
85 hmac.update(salt, salt_size);
86 for(u32bit j = 0; j != 4; j++)
87 hmac.update(get_byte(j, counter));
88 hmac.final(U);
89 xor_buf(T, U, T_size);
90
91 for(u32bit j = 1; j != iterations; j++)
92 {
93 hmac.update(U);
94 hmac.final(U);
95 xor_buf(T, U, T_size);
96 }
97
98 key_len -= T_size;
99 T += T_size;
100 counter++;
101 }
102
103 return key;
104 }
105
106/*************************************************
107* Return the name of this type *
108*************************************************/
109std::string PKCS5_PBKDF2::name() const
110 {
111 return "PBKDF2(" + hash_name + ")";
112 }
113
114/*************************************************
115* PKCS5_PBKDF2 Constructor *
116*************************************************/
117PKCS5_PBKDF2::PKCS5_PBKDF2(const std::string& h_name) : hash_name(h_name)
118 {
119 if(!have_hash(hash_name))
120 throw Algorithm_Not_Found(hash_name);
121 }
122
123}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status