monotone

monotone Mtn Source Tree

Root/botan/pkcs5.cpp

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

Archive Download this file

Branches

Tags

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