Botan  1.11.4
Public Member Functions | Protected Member Functions | List of all members
Botan::PBE_PKCS5v15 Class Reference

#include <pbes1.h>

Inheritance diagram for Botan::PBE_PKCS5v15:
Botan::PBE Botan::Filter

Public Member Functions

virtual bool attachable ()
 
std::vector< byteencode_params () const
 
void end_msg ()
 
OID get_oid () const
 
std::string name () const
 
 PBE_PKCS5v15 (BlockCipher *cipher, HashFunction *hash, const std::string &passphrase, std::chrono::milliseconds msec, RandomNumberGenerator &rng)
 
 PBE_PKCS5v15 (BlockCipher *cipher, HashFunction *hash, const std::vector< byte > &params, const std::string &passphrase)
 
void start_msg ()
 
void write (const byte[], size_t)
 
 ~PBE_PKCS5v15 ()
 

Protected Member Functions

virtual void send (const byte in[], size_t length)
 
void send (byte in)
 
void send (const secure_vector< byte > &in)
 
void send (const std::vector< byte > &in)
 
void send (const secure_vector< byte > &in, size_t length)
 
void send (const std::vector< byte > &in, size_t length)
 

Detailed Description

PKCS #5 v1.5 PBE

Definition at line 22 of file pbes1.h.

Constructor & Destructor Documentation

Botan::PBE_PKCS5v15::PBE_PKCS5v15 ( BlockCipher cipher,
HashFunction hash,
const std::string &  passphrase,
std::chrono::milliseconds  msec,
RandomNumberGenerator rng 
)
Parameters
cipherthe block cipher to use (DES or RC2)
hashthe hash function to use
passphrasethe passphrase to use
msechow many milliseconds to run the PBKDF
rnga random number generator

Definition at line 116 of file pbes1.cpp.

References Botan::HashFunction::clone(), Botan::PBKDF::derive_key(), and Botan::Algorithm::name().

121  :
122  m_direction(ENCRYPTION),
123  m_block_cipher(cipher),
124  m_hash_function(hash),
125  m_salt(rng.random_vec(8))
126  {
127  if(cipher->name() != "DES" && cipher->name() != "RC2")
128  {
129  throw Invalid_Argument("PBE_PKCS5v1.5: Unknown cipher " +
130  cipher->name());
131  }
132 
133  if(hash->name() != "MD2" && hash->name() != "MD5" &&
134  hash->name() != "SHA-160")
135  {
136  throw Invalid_Argument("PBE_PKCS5v1.5: Unknown hash " +
137  hash->name());
138  }
139 
140  PKCS5_PBKDF1 pbkdf(m_hash_function->clone());
141 
142  secure_vector<byte> key_and_iv =
143  pbkdf.derive_key(16, passphrase,
144  &m_salt[0], m_salt.size(),
145  msec, m_iterations).bits_of();
146 
147  m_key.assign(&key_and_iv[0], &key_and_iv[8]);
148  m_iv.assign(&key_and_iv[8], &key_and_iv[16]);
149 
}
Botan::PBE_PKCS5v15::PBE_PKCS5v15 ( BlockCipher cipher,
HashFunction hash,
const std::vector< byte > &  params,
const std::string &  passphrase 
)

Definition at line 151 of file pbes1.cpp.

References Botan::HashFunction::clone(), Botan::BER_Decoder::decode(), Botan::PBKDF::derive_key(), Botan::BER_Decoder::end_cons(), Botan::Algorithm::name(), Botan::OCTET_STRING, Botan::SEQUENCE, Botan::BER_Decoder::start_cons(), and Botan::BER_Decoder::verify_end().

155  :
156  m_direction(DECRYPTION),
157  m_block_cipher(cipher),
158  m_hash_function(hash)
159  {
160  if(cipher->name() != "DES" && cipher->name() != "RC2")
161  {
162  throw Invalid_Argument("PBE_PKCS5v1.5: Unknown cipher " +
163  cipher->name());
164  }
165 
166  if(hash->name() != "MD2" && hash->name() != "MD5" &&
167  hash->name() != "SHA-160")
168  {
169  throw Invalid_Argument("PBE_PKCS5v1.5: Unknown hash " +
170  hash->name());
171  }
172 
173  BER_Decoder(params)
174  .start_cons(SEQUENCE)
175  .decode(m_salt, OCTET_STRING)
176  .decode(m_iterations)
177  .verify_end()
178  .end_cons();
179 
180  if(m_salt.size() != 8)
181  throw Decoding_Error("PBES1: Encoded salt is not 8 octets");
182 
183  PKCS5_PBKDF1 pbkdf(m_hash_function->clone());
184 
185  secure_vector<byte> key_and_iv =
186  pbkdf.derive_key(16, passphrase,
187  &m_salt[0], m_salt.size(),
188  m_iterations).bits_of();
189 
190  m_key.assign(&key_and_iv[0], &key_and_iv[8]);
191  m_iv.assign(&key_and_iv[8], &key_and_iv[16]);
}
Botan::PBE_PKCS5v15::~PBE_PKCS5v15 ( )

Definition at line 193 of file pbes1.cpp.

194  {
195  delete m_block_cipher;
196  delete m_hash_function;
197  }

Member Function Documentation

virtual bool Botan::Filter::attachable ( )
inlinevirtualinherited

Check whether this filter is an attachable filter.

Returns
true if this filter is attachable, false otherwise

Reimplemented in Botan::SecureQueue, and Botan::DataSink.

Definition at line 52 of file filter.h.

52 { return true; }
std::vector< byte > Botan::PBE_PKCS5v15::encode_params ( ) const
virtual

DER encode the params (the number of iterations and the salt value)

Returns
encoded params

Implements Botan::PBE.

Definition at line 74 of file pbes1.cpp.

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents_unlocked(), Botan::OCTET_STRING, Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

75  {
76  return DER_Encoder()
77  .start_cons(SEQUENCE)
78  .encode(m_salt, OCTET_STRING)
79  .encode(m_iterations)
80  .end_cons()
81  .get_contents_unlocked();
82  }
void Botan::PBE_PKCS5v15::end_msg ( )
virtual

Notify that the current message is finished; flush buffers and do end-of-message processing (if any).

Reimplemented from Botan::Filter.

Definition at line 48 of file pbes1.cpp.

References Botan::Pipe::end_msg(), and Botan::Pipe::reset().

49  {
50  m_pipe.end_msg();
51  flush_pipe(false);
52  m_pipe.reset();
53  }
OID Botan::PBE_PKCS5v15::get_oid ( ) const
virtual

Get this PBE's OID.

Returns
object identifier

Implements Botan::PBE.

Definition at line 87 of file pbes1.cpp.

References Botan::Algorithm::name().

88  {
89  const OID base_pbes1_oid("1.2.840.113549.1.5");
90 
91  const std::string cipher = m_block_cipher->name();
92  const std::string digest = m_hash_function->name();
93 
94  if(cipher == "DES" && digest == "MD2")
95  return (base_pbes1_oid + 1);
96  else if(cipher == "DES" && digest == "MD5")
97  return (base_pbes1_oid + 3);
98  else if(cipher == "DES" && digest == "SHA-160")
99  return (base_pbes1_oid + 10);
100  else if(cipher == "RC2" && digest == "MD2")
101  return (base_pbes1_oid + 4);
102  else if(cipher == "RC2" && digest == "MD5")
103  return (base_pbes1_oid + 6);
104  else if(cipher == "RC2" && digest == "SHA-160")
105  return (base_pbes1_oid + 11);
106  else
107  throw Internal_Error("PBE-PKCS5 v1.5: get_oid() has run out of options");
108  }
std::string Botan::PBE_PKCS5v15::name ( ) const
virtual
Returns
descriptive name for this filter

Implements Botan::Filter.

Definition at line 110 of file pbes1.cpp.

References Botan::Algorithm::name().

111  {
112  return "PBE-PKCS5v15(" + m_block_cipher->name() + "," +
113  m_hash_function->name() + ")";
114  }
void Botan::Filter::send ( const byte  in[],
size_t  length 
)
protectedvirtualinherited
Parameters
insome input for the filter
lengththe length of in

Reimplemented in Botan::Threaded_Fork.

Definition at line 28 of file filter.cpp.

References Botan::Filter::write().

Referenced by Botan::PK_Encryptor_Filter::end_msg(), Botan::Zlib_Compression::end_msg(), Botan::Bzip_Compression::end_msg(), Botan::Lzma_Compression::end_msg(), Botan::Hex_Encoder::end_msg(), Botan::Base64_Encoder::end_msg(), Botan::PK_Decryptor_Filter::end_msg(), Botan::Bzip_Decompression::end_msg(), Botan::PK_Signer_Filter::end_msg(), Botan::Lzma_Decompression::end_msg(), Botan::Zlib_Decompression::end_msg(), Botan::Hex_Decoder::end_msg(), Botan::Base64_Decoder::end_msg(), Botan::PK_Verifier_Filter::end_msg(), Botan::Hash_Filter::end_msg(), Botan::MAC_Filter::end_msg(), Botan::Bzip_Compression::flush(), Botan::Zlib_Compression::flush(), Botan::Lzma_Compression::flush(), Botan::Bzip_Compression::write(), Botan::Zlib_Compression::write(), Botan::Lzma_Compression::write(), Botan::StreamCipher_Filter::write(), Botan::Bzip_Decompression::write(), Botan::Lzma_Decompression::write(), Botan::Zlib_Decompression::write(), Botan::Hex_Decoder::write(), and Botan::Base64_Decoder::write().

29  {
30  bool nothing_attached = true;
31  for(size_t j = 0; j != total_ports(); ++j)
32  if(next[j])
33  {
34  if(write_queue.size())
35  next[j]->write(&write_queue[0], write_queue.size());
36  next[j]->write(input, length);
37  nothing_attached = false;
38  }
39 
40  if(nothing_attached)
41  write_queue += std::make_pair(input, length);
42  else
43  write_queue.clear();
44  }
void Botan::Filter::send ( byte  in)
inlineprotectedinherited
Parameters
insome input for the filter

Definition at line 65 of file filter.h.

References Botan::Filter::send().

Referenced by Botan::Filter::send().

65 { send(&in, 1); }
void Botan::Filter::send ( const secure_vector< byte > &  in)
inlineprotectedinherited
Parameters
insome input for the filter

Definition at line 70 of file filter.h.

References Botan::Filter::send().

Referenced by Botan::Filter::send().

70 { send(&in[0], in.size()); }
void Botan::Filter::send ( const std::vector< byte > &  in)
inlineprotectedinherited
Parameters
insome input for the filter

Definition at line 75 of file filter.h.

References Botan::Filter::send().

Referenced by Botan::Filter::send().

75 { send(&in[0], in.size()); }
void Botan::Filter::send ( const secure_vector< byte > &  in,
size_t  length 
)
inlineprotectedinherited
Parameters
insome input for the filter
lengththe number of bytes of in to send

Definition at line 81 of file filter.h.

82  {
83  send(&in[0], length);
84  }
void Botan::Filter::send ( const std::vector< byte > &  in,
size_t  length 
)
inlineprotectedinherited
Parameters
insome input for the filter
lengththe number of bytes of in to send

Definition at line 90 of file filter.h.

91  {
92  send(&in[0], length);
93  }
void Botan::PBE_PKCS5v15::start_msg ( )
virtual

Start a new message. Must be closed by end_msg() before another message can be started.

Reimplemented from Botan::Filter.

Definition at line 29 of file pbes1.cpp.

References Botan::Pipe::append(), Botan::BlockCipher::clone(), Botan::Pipe::default_msg(), Botan::ENCRYPTION, Botan::Pipe::message_count(), Botan::Pipe::set_default_msg(), and Botan::Pipe::start_msg().

30  {
31  if(m_direction == ENCRYPTION)
32  m_pipe.append(new CBC_Encryption(m_block_cipher->clone(),
33  new PKCS7_Padding,
34  m_key, m_iv));
35  else
36  m_pipe.append(new CBC_Decryption(m_block_cipher->clone(),
37  new PKCS7_Padding,
38  m_key, m_iv));
39 
40  m_pipe.start_msg();
41  if(m_pipe.message_count() > 1)
42  m_pipe.set_default_msg(m_pipe.default_msg() + 1);
43  }
void Botan::PBE_PKCS5v15::write ( const byte  input[],
size_t  length 
)
virtual

Write a portion of a message to this filter.

Parameters
inputthe input as a byte array
lengththe length of the byte array input

Implements Botan::Filter.

Definition at line 20 of file pbes1.cpp.

References Botan::Pipe::write().

21  {
22  m_pipe.write(input, length);
23  flush_pipe(true);
24  }

The documentation for this class was generated from the following files: