Botan: Botan::MD5 Class Reference

Botan 1.10.17

Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Botan::MD5 Class Reference

`#include <md5.h>`

Inheritance diagram for Botan::MD5:

## Public Member Functions
void | clear ()
HashFunction * | clone () const
void | final (byte out[])
SecureVector< byte > | final ()
size_t | hash_block_size () const
| MD5 ()
std::string | name () const
size_t | output_length () const
SecureVector< byte > | process (const byte in[], size_t length)
SecureVector< byte > | process (const MemoryRegion< byte > &in)
SecureVector< byte > | process (const std::string &in)
void | update (const byte in[], size_t length)
void | update (const MemoryRegion< byte > &in)
void | update (const std::string &str)
void | update (byte in)
template<typename T >
void | update_be (const T in)

## Protected Member Functions
void | add_data (const byte input[], size_t length)
void | compress_n (const byte[], size_t blocks)
void | copy_out (byte[])
void | final_result (byte output[])
virtual void | write_count (byte out[])

## Protected Attributes
SecureVector< u32bit > | digest
SecureVector< u32bit > | M

## Detailed Description

MD5

Definition at line 18 of file md5.h.

## Constructor & Destructor Documentation

## ◆ MD5()

Botan::MD5::MD5 | ( |  | ) |
| inline

Definition at line 27 of file md5.h.

27 : MDx_HashFunction(64, false, true), M(16), digest(4)
28 { clear(); }
Botan::MD5::M
SecureVector< u32bit > M
Definition: md5.h:36
Botan::MD5::clear
void clear()
Definition: md5.cpp:126
Botan::MD5::digest
SecureVector< u32bit > digest
Definition: md5.h:41
Botan::MDx_HashFunction::MDx_HashFunction
MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, size_t counter_size=8)
Definition: mdx_hash.cpp:17

## Member Function Documentation

## ◆ add_data()

void Botan::MDx_HashFunction::add_data | ( | const byte | input[],
|  | size_t | length
| ) |  |
| protectedvirtualinherited

Add more data to the computation

Parameters

  input | is an input buffer
  length | is the length of input in bytes

Implements Botan::Buffered_Computation.

Definition at line 41 of file mdx_hash.cpp.

References Botan::MDx_HashFunction::compress_n(), Botan::MemoryRegion< T >::copy(), and Botan::MemoryRegion< T >::size().

42 {
43 count += length;
44
45 if(position)
46 {
47 buffer.copy(position, input, length);
48
49 if(position + length >= buffer.size())
50 {
51 compress_n(&buffer[0], 1);
52 input += (buffer.size() - position);
53 length -= (buffer.size() - position);
54 position = 0;
55 }
56 }
57
58 const size_t full_blocks = length / buffer.size();
59 const size_t remaining = length % buffer.size();
60
61 if(full_blocks)
62 compress_n(input, full_blocks);
63
64 buffer.copy(position, input + full_blocks * buffer.size(), remaining);
65 position += remaining;
66 }
Botan::MDx_HashFunction::compress_n
virtual void compress_n(const byte blocks[], size_t block_n)=0
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ clear()

void Botan::MD5::clear | ( |  | ) |
| virtual

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 126 of file md5.cpp.

References Botan::MDx_HashFunction::clear(), digest, M, and Botan::zeroise().

127 {
128 MDx_HashFunction::clear();
129 zeroise(M);
130 digest[0] = 0x67452301;
131 digest[1] = 0xEFCDAB89;
132 digest[2] = 0x98BADCFE;
133 digest[3] = 0x10325476;
134 }
Botan::MD5::M
SecureVector< u32bit > M
Definition: md5.h:36
Botan::MDx_HashFunction::clear
void clear()
Definition: mdx_hash.cpp:32
Botan::MD5::digest
SecureVector< u32bit > digest
Definition: md5.h:41
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428

## ◆ clone()

HashFunction* Botan::MD5::clone | ( |  | ) | const
| inlinevirtual

Get a new object representing the same algorithm as *this

Implements Botan::HashFunction.

Reimplemented in Botan::MD5_X86_32.

Definition at line 23 of file md5.h.

23 { return new MD5; }
Botan::MD5::MD5
MD5()
Definition: md5.h:27

## ◆ compress_n()

void Botan::MD5::compress_n | ( | const byte | blocks[],
|  | size_t | block_n
| ) |  |
| protectedvirtual

Run the hash's compression function over a set of blocks

Parameters

  blocks | the input
  block_n | the number of blocks

Implements Botan::MDx_HashFunction.

Definition at line 61 of file md5.cpp.

References digest, Botan::MDx_HashFunction::hash_block_size(), Botan::load_le(), M, and Botan::MemoryRegion< T >::size().

62 {
63 u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3];
64
65 for(size_t i = 0; i != blocks; ++i)
66 {
67 load_le(&M[0], input, M.size());
68
69 FF(A,B,C,D,M[ 0], 7,0xD76AA478); FF(D,A,B,C,M[ 1],12,0xE8C7B756);
70 FF(C,D,A,B,M[ 2],17,0x242070DB); FF(B,C,D,A,M[ 3],22,0xC1BDCEEE);
71 FF(A,B,C,D,M[ 4], 7,0xF57C0FAF); FF(D,A,B,C,M[ 5],12,0x4787C62A);
72 FF(C,D,A,B,M[ 6],17,0xA8304613); FF(B,C,D,A,M[ 7],22,0xFD469501);
73 FF(A,B,C,D,M[ 8], 7,0x698098D8); FF(D,A,B,C,M[ 9],12,0x8B44F7AF);
74 FF(C,D,A,B,M[10],17,0xFFFF5BB1); FF(B,C,D,A,M[11],22,0x895CD7BE);
75 FF(A,B,C,D,M[12], 7,0x6B901122); FF(D,A,B,C,M[13],12,0xFD987193);
76 FF(C,D,A,B,M[14],17,0xA679438E); FF(B,C,D,A,M[15],22,0x49B40821);
77
78 GG(A,B,C,D,M[ 1], 5,0xF61E2562); GG(D,A,B,C,M[ 6], 9,0xC040B340);
79 GG(C,D,A,B,M[11],14,0x265E5A51); GG(B,C,D,A,M[ 0],20,0xE9B6C7AA);
80 GG(A,B,C,D,M[ 5], 5,0xD62F105D); GG(D,A,B,C,M[10], 9,0x02441453);
81 GG(C,D,A,B,M[15],14,0xD8A1E681); GG(B,C,D,A,M[ 4],20,0xE7D3FBC8);
82 GG(A,B,C,D,M[ 9], 5,0x21E1CDE6); GG(D,A,B,C,M[14], 9,0xC33707D6);
83 GG(C,D,A,B,M[ 3],14,0xF4D50D87); GG(B,C,D,A,M[ 8],20,0x455A14ED);
84 GG(A,B,C,D,M[13], 5,0xA9E3E905); GG(D,A,B,C,M[ 2], 9,0xFCEFA3F8);
85 GG(C,D,A,B,M[ 7],14,0x676F02D9); GG(B,C,D,A,M[12],20,0x8D2A4C8A);
86
87 HH(A,B,C,D,M[ 5], 4,0xFFFA3942); HH(D,A,B,C,M[ 8],11,0x8771F681);
88 HH(C,D,A,B,M[11],16,0x6D9D6122); HH(B,C,D,A,M[14],23,0xFDE5380C);
89 HH(A,B,C,D,M[ 1], 4,0xA4BEEA44); HH(D,A,B,C,M[ 4],11,0x4BDECFA9);
90 HH(C,D,A,B,M[ 7],16,0xF6BB4B60); HH(B,C,D,A,M[10],23,0xBEBFBC70);
91 HH(A,B,C,D,M[13], 4,0x289B7EC6); HH(D,A,B,C,M[ 0],11,0xEAA127FA);
92 HH(C,D,A,B,M[ 3],16,0xD4EF3085); HH(B,C,D,A,M[ 6],23,0x04881D05);
93 HH(A,B,C,D,M[ 9], 4,0xD9D4D039); HH(D,A,B,C,M[12],11,0xE6DB99E5);
94 HH(C,D,A,B,M[15],16,0x1FA27CF8); HH(B,C,D,A,M[ 2],23,0xC4AC5665);
95
96 II(A,B,C,D,M[ 0], 6,0xF4292244); II(D,A,B,C,M[ 7],10,0x432AFF97);
97 II(C,D,A,B,M[14],15,0xAB9423A7); II(B,C,D,A,M[ 5],21,0xFC93A039);
98 II(A,B,C,D,M[12], 6,0x655B59C3); II(D,A,B,C,M[ 3],10,0x8F0CCC92);
99 II(C,D,A,B,M[10],15,0xFFEFF47D); II(B,C,D,A,M[ 1],21,0x85845DD1);
100 II(A,B,C,D,M[ 8], 6,0x6FA87E4F); II(D,A,B,C,M[15],10,0xFE2CE6E0);
101 II(C,D,A,B,M[ 6],15,0xA3014314); II(B,C,D,A,M[13],21,0x4E0811A1);
102 II(A,B,C,D,M[ 4], 6,0xF7537E82); II(D,A,B,C,M[11],10,0xBD3AF235);
103 II(C,D,A,B,M[ 2],15,0x2AD7D2BB); II(B,C,D,A,M[ 9],21,0xEB86D391);
104
105 A = (digest[0] += A);
106 B = (digest[1] += B);
107 C = (digest[2] += C);
108 D = (digest[3] += D);
109
110 input += hash_block_size();
111 }
112 }
Botan::load_le
T load_le(const byte in[], size_t off)
Definition: loadstor.h:116
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::MD5::M
SecureVector< u32bit > M
Definition: md5.h:36
Botan::MDx_HashFunction::hash_block_size
size_t hash_block_size() const
Definition: mdx_hash.h:32
Botan::MD5::digest
SecureVector< u32bit > digest
Definition: md5.h:41
Botan::u32bit
unsigned int u32bit
Definition: types.h:32

## ◆ copy_out()

void Botan::MD5::copy_out | ( | byte | buffer[] | ) |
| protectedvirtual

Copy the output to the buffer

Parameters

  buffer | to put the output into

Implements Botan::MDx_HashFunction.

Definition at line 117 of file md5.cpp.

References digest, output_length(), and Botan::store_le().

118 {
119 for(size_t i = 0; i != output_length(); i += 4)
120 store_le(digest[i/4], output + i);
121 }
Botan::store_le
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
Botan::MD5::output_length
size_t output_length() const
Definition: md5.h:22
Botan::MD5::digest
SecureVector< u32bit > digest
Definition: md5.h:41

## ◆ final() [1/2]

void Botan::Buffered_Computation::final | ( | byte | out[] | ) |
| inlineinherited

Complete the computation and retrieve the final result.

Parameters

  out | The byte array to be filled with the result. Must be of length output_length()

Definition at line 80 of file buf_comp.h.

Referenced by Botan::LubyRackoff::decrypt_n(), Botan::Lion::decrypt_n(), Botan::X942_PRF::derive(), Botan::KDF2::derive(), Botan::KDF1::derive(), Botan::PKCS5_PBKDF2::derive_key(), Botan::OpenPGP_S2K::derive_key(), Botan::PKCS5_PBKDF1::derive_key(), Botan::EMSA2::EMSA2(), Botan::LubyRackoff::encrypt_n(), Botan::Lion::encrypt_n(), Botan::Hash_Filter::end_msg(), Botan::MAC_Filter::end_msg(), Botan::HandshakeHash::final(), Botan::HandshakeHash::final_ssl3(), Botan::Record_Reader::get_record(), Botan::MGF1::mask(), Botan::EMSA3::raw_data(), Botan::Randpool::reseed(), and Botan::HMAC_RNG::reseed().

80 { final_result(out); }

## ◆ final() [2/2]

SecureVector<byte> Botan::Buffered_Computation::final | ( |  | ) |
| inlineinherited

Complete the computation and retrieve the final result.

Returns
  SecureVector holding the result

Definition at line 87 of file buf_comp.h.

88 {
89 SecureVector<byte> output(output_length());
90 final_result(&output[0]);
91 return output;
92 }
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0

## ◆ final_result()

void Botan::MDx_HashFunction::final_result | ( | byte | out[] | ) |
| protectedvirtualinherited

Write the final output to out

Parameters

  out | is an output buffer of output_length()

Implements Botan::Buffered_Computation.

Definition at line 71 of file mdx_hash.cpp.

References Botan::MDx_HashFunction::clear(), Botan::MDx_HashFunction::compress_n(), Botan::MDx_HashFunction::copy_out(), Botan::MemoryRegion< T >::size(), Botan::MDx_HashFunction::write_count(), and Botan::zeroise().

72 {
73 buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
74 for(size_t i = position+1; i != buffer.size(); ++i)
75 buffer[i] = 0;
76
77 if(position >= buffer.size() - COUNT_SIZE)
78 {
79 compress_n(&buffer[0], 1);
80 zeroise(buffer);
81 }
82
83 write_count(&buffer[buffer.size() - COUNT_SIZE]);
84
85 compress_n(&buffer[0], 1);
86 copy_out(output);
87 clear();
88 }
Botan::MDx_HashFunction::compress_n
virtual void compress_n(const byte blocks[], size_t block_n)=0
Botan::MDx_HashFunction::write_count
virtual void write_count(byte out[])
Definition: mdx_hash.cpp:93
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::MDx_HashFunction::clear
void clear()
Definition: mdx_hash.cpp:32
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::MDx_HashFunction::copy_out
virtual void copy_out(byte buffer[])=0

## ◆ hash_block_size()

size_t Botan::MDx_HashFunction::hash_block_size | ( |  | ) | const
| inlinevirtualinherited

The hash block size as defined for this algorithm

Reimplemented from Botan::HashFunction.

Definition at line 32 of file mdx_hash.h.

Referenced by Botan::MD4::compress_n(), compress_n(), Botan::SHA_160::compress_n(), and Botan::MDx_HashFunction::write_count().

32 { return buffer.size(); }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ name()

std::string Botan::MD5::name | ( |  | ) | const
| inlinevirtual

Returns
  name of this algorithm

Implements Botan::Algorithm.

Definition at line 21 of file md5.h.

21 { return "MD5"; }

## ◆ output_length()

size_t Botan::MD5::output_length | ( |  | ) | const
| inlinevirtual

Returns
  length of the output of this function in bytes

Implements Botan::Buffered_Computation.

Definition at line 22 of file md5.h.

Referenced by copy_out(), and Botan::SSL3_PRF::derive().

22 { return 16; }

## ◆ process() [1/3]

SecureVector<byte> Botan::Buffered_Computation::process | ( | const byte | in[],
|  | size_t | length
| ) |  |
| inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters

  in | the input to process as a byte array
  length | the length of the byte array

Returns
  the result of the call to final()

Definition at line 101 of file buf_comp.h.

Referenced by Botan::Randpool::add_entropy(), Botan::EME1::EME1(), and Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID().

102 {
103 add_data(in, length);
104 return final();
105 }

## ◆ process() [2/3]

SecureVector<byte> Botan::Buffered_Computation::process | ( | const MemoryRegion< byte > & | in | ) |
| inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters

  in | the input to process

Returns
  the result of the call to final()

Definition at line 113 of file buf_comp.h.

References Botan::MemoryRegion< T >::size().

114 {
115 add_data(&in[0], in.size());
116 return final();
117 }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ process() [3/3]

SecureVector<byte> Botan::Buffered_Computation::process | ( | const std::string & | in | ) |
| inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters

  in | the input to process as a string

Returns
  the result of the call to final()

Definition at line 125 of file buf_comp.h.

126 {
127 update(in);
128 return final();
129 }
Botan::Buffered_Computation::update
void update(const byte in[], size_t length)
Definition: buf_comp.h:33

## ◆ update() [1/4]

void Botan::Buffered_Computation::update | ( | const byte | in[],
|  | size_t | length
| ) |  |
| inlineinherited

Add new input to process.

Parameters

  in | the input to process as a byte array
  length | of param in in bytes

Definition at line 33 of file buf_comp.h.

Referenced by Botan::HMAC_RNG::add_entropy(), Botan::Comb4P::clear(), Botan::LubyRackoff::decrypt_n(), Botan::Lion::decrypt_n(), Botan::X942_PRF::derive(), Botan::KDF2::derive(), Botan::KDF1::derive(), Botan::PKCS5_PBKDF2::derive_key(), Botan::OpenPGP_S2K::derive_key(), Botan::PKCS5_PBKDF1::derive_key(), Botan::LubyRackoff::encrypt_n(), Botan::Lion::encrypt_n(), Botan::HandshakeHash::final(), Botan::HandshakeHash::final_ssl3(), Botan::Record_Reader::get_record(), Botan::MGF1::mask(), Botan::HMAC_RNG::reseed(), Botan::EAX_Base::start_msg(), and Botan::EMSA3::update().

33 { add_data(in, length); }

## ◆ update() [2/4]

void Botan::Buffered_Computation::update | ( | const MemoryRegion< byte > & | in | ) |
| inlineinherited

Add new input to process.

Parameters

  in | the input to process as a MemoryRegion

Definition at line 39 of file buf_comp.h.

References Botan::MemoryRegion< T >::size().

40 {
41 add_data(&in[0], in.size());
42 }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ update() [3/4]

void Botan::Buffered_Computation::update | ( | const std::string & | str | ) |
| inlineinherited

Add new input to process.

Parameters

  str | the input to process as a std::string. Will be interpreted as a byte array based on the strings encoding.

Definition at line 63 of file buf_comp.h.

64 {
65 add_data(reinterpret_cast<const byte*>(str.data()), str.size());
66 }

## ◆ update() [4/4]

void Botan::Buffered_Computation::update | ( | byte | in | ) |
| inlineinherited

Process a single byte.

Parameters

  in | the byte to process

Definition at line 72 of file buf_comp.h.

72 { add_data(&in, 1); }

## ◆ update_be()

template<typename T >

void Botan::Buffered_Computation::update_be | ( | const T | in | ) |
| inlineinherited

Add an integer in big-endian order

Parameters

  in | the value

Definition at line 48 of file buf_comp.h.

References Botan::get_byte().

Referenced by Botan::KDF2::derive(), Botan::PKCS5_PBKDF2::derive_key(), Botan::Record_Reader::get_record(), and Botan::MGF1::mask().

49 {
50 for(size_t i = 0; i != sizeof(T); ++i)
51 {
52 byte b = get_byte(i, in);
53 add_data(&b, 1);
54 }
55 }
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::byte
unsigned char byte
Definition: types.h:22

## ◆ write_count()

void Botan::MDx_HashFunction::write_count | ( | byte | out[] | ) |
| protectedvirtualinherited

Write the count, if used, to this spot

Parameters

  out | where to write the counter to

Definition at line 93 of file mdx_hash.cpp.

References Botan::MDx_HashFunction::hash_block_size(), Botan::Buffered_Computation::output_length(), Botan::store_be(), and Botan::store_le().

Referenced by Botan::MDx_HashFunction::final_result().

94 {
95 if(COUNT_SIZE < 8)
96 throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8");
97 if(COUNT_SIZE >= output_length() || COUNT_SIZE >= hash_block_size())
98 throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big");
99
100 const u64bit bit_count = count * 8;
101
102 if(BIG_BYTE_ENDIAN)
103 store_be(bit_count, out + COUNT_SIZE - 8);
104 else
105 store_le(bit_count, out + COUNT_SIZE - 8);
106 }
Botan::store_le
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
Botan::MDx_HashFunction::hash_block_size
size_t hash_block_size() const
Definition: mdx_hash.h:32
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0

## Member Data Documentation

## ◆ digest

SecureVector<u32bit> Botan::MD5::digest
| protected

The digest value, exposed for use by subclasses (x86 asm)

Definition at line 41 of file md5.h.

Referenced by clear(), compress_n(), and copy_out().

## ◆ M

SecureVector<u32bit> Botan::MD5::M
| protected

The message buffer, exposed for use by subclasses (x86 asm)

Definition at line 36 of file md5.h.

Referenced by clear(), and compress_n().

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

- src/hash/md5/md5.h
- src/hash/md5/md5.cpp