Botan: src/kdf/prf_ssl3/prf_ssl3.cpp Source File

Botan 1.10.17

prf_ssl3.cpp
Go to the documentation of this file.
1 /*
2 * SSLv3 PRF
3 * (C) 2004-2006 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/prf_ssl3.h>
9 #include <botan/symkey.h>
10 #include <botan/exceptn.h>
11 #include <botan/sha160.h>
12 #include <botan/md5.h>
13 #include <botan/internal/assert.h>
14 #include <memory>
15
16 namespace Botan {
17
18 namespace {
19
20 /*
21 * Return the next inner hash
22 */
23 OctetString next_hash(size_t where, size_t want,
24 HashFunction& md5, HashFunction& sha1,
25 const byte secret[], size_t secret_len,
26 const byte seed[], size_t seed_len)
27 {
28 BOTAN_ASSERT(want <= md5.output_length(), "Desired output too large");
29
30 const byte ASCII_A_CHAR = 0x41;
31
32 for(size_t j = 0; j != where + 1; j++)
33 sha1.update(static_cast<byte>(ASCII_A_CHAR + where));
34 sha1.update(secret, secret_len);
35 sha1.update(seed, seed_len);
36 SecureVector<byte> sha1_hash = sha1.final();
37
38 md5.update(secret, secret_len);
39 md5.update(sha1_hash);
40 SecureVector<byte> md5_hash = md5.final();
41
42 return OctetString(&md5_hash[0], want);
43 }
44
45 }
46
47 /*
48 * SSL3 PRF
49 */
50 SecureVector<byte> SSL3_PRF::derive(size_t key_len,
51 const byte secret[], size_t secret_len,
52 const byte seed[], size_t seed_len) const
53 {
54 if(key_len > 416)
55 throw Invalid_Argument("SSL3_PRF: Requested key length is too large");
56
57 MD5 md5;
58 SHA_160 sha1;
59
60 OctetString output;
61
62 int counter = 0;
63 while(key_len)
64 {
65 const size_t produce = std::min<size_t>(key_len, md5.output_length());
66
67 output = output + next_hash(counter++, produce, md5, sha1,
68 secret, secret_len, seed, seed_len);
69
70 key_len -= produce;
71 }
72
73 return output.bits_of();
74 }
75
76 }
Botan::OctetString
Definition: symkey.h:19
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, msg)
Definition: assert.h:19
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MD5
Definition: md5.h:18
Botan::SHA_160
Definition: sha160.h:18
Botan
Definition: algo_base.h:14
Botan::MD5::output_length
size_t output_length() const
Definition: md5.h:22
Botan::SecureVector< byte >
Botan::OctetString::bits_of
SecureVector< byte > bits_of() const
Definition: symkey.h:30
Botan::SSL3_PRF::derive
SecureVector< byte > derive(size_t, const byte[], size_t, const byte[], size_t) const
Definition: prf_ssl3.cpp:50