Coverage Report

Created: 2025-11-23 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/include/Poco/HMACEngine.h
Line
Count
Source
1
//
2
// HMACEngine.h
3
//
4
// Library: Foundation
5
// Package: Crypt
6
// Module:  HMACEngine
7
//
8
// Definition of the HMACEngine class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_HMACEngine_INCLUDED
18
#define Foundation_HMACEngine_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
#include "Poco/DigestEngine.h"
23
#include <cstring>
24
25
26
namespace Poco {
27
28
29
template <class Engine>
30
class HMACEngine: public DigestEngine
31
  /// This class implements the HMAC message
32
  /// authentication code algorithm, as specified
33
  /// in RFC 2104. The underlying DigestEngine
34
  /// (MD5Engine, SHA1Engine, etc.) must be given as
35
  /// template argument.
36
  /// Since the HMACEngine is a DigestEngine, it can
37
  /// be used with the DigestStream class to create
38
  /// a HMAC for a stream.
39
{
40
public:
41
  enum
42
  {
43
    BLOCK_SIZE  = Engine::BLOCK_SIZE,
44
    DIGEST_SIZE = Engine::DIGEST_SIZE
45
  };
46
47
  HMACEngine(const std::string& passphrase)
48
23.6k
  {
49
23.6k
    init(passphrase.data(), passphrase.length());
50
23.6k
  }
Poco::HMACEngine<Poco::MD5Engine>::HMACEngine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
48
558
  {
49
558
    init(passphrase.data(), passphrase.length());
50
558
  }
Poco::HMACEngine<Poco::SHA1Engine>::HMACEngine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
48
2.38k
  {
49
2.38k
    init(passphrase.data(), passphrase.length());
50
2.38k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::HMACEngine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
48
6.92k
  {
49
6.92k
    init(passphrase.data(), passphrase.length());
50
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::HMACEngine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
48
6.92k
  {
49
6.92k
    init(passphrase.data(), passphrase.length());
50
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::HMACEngine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
48
6.92k
  {
49
6.92k
    init(passphrase.data(), passphrase.length());
50
6.92k
  }
51
52
  HMACEngine(const char* passphrase, std::size_t length)
53
  {
54
    poco_check_ptr (passphrase);
55
56
    init(passphrase, length);
57
  }
58
59
  ~HMACEngine() override
60
23.6k
  {
61
23.6k
    std::memset(_ipad, 0, BLOCK_SIZE);
62
23.6k
    std::memset(_opad, 0, BLOCK_SIZE);
63
23.6k
    delete [] _ipad;
64
23.6k
    delete [] _opad;
65
23.6k
  }
Poco::HMACEngine<Poco::MD5Engine>::~HMACEngine()
Line
Count
Source
60
558
  {
61
558
    std::memset(_ipad, 0, BLOCK_SIZE);
62
558
    std::memset(_opad, 0, BLOCK_SIZE);
63
558
    delete [] _ipad;
64
558
    delete [] _opad;
65
558
  }
Poco::HMACEngine<Poco::SHA1Engine>::~HMACEngine()
Line
Count
Source
60
2.38k
  {
61
2.38k
    std::memset(_ipad, 0, BLOCK_SIZE);
62
2.38k
    std::memset(_opad, 0, BLOCK_SIZE);
63
2.38k
    delete [] _ipad;
64
2.38k
    delete [] _opad;
65
2.38k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::~HMACEngine()
Line
Count
Source
60
6.92k
  {
61
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
62
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
63
6.92k
    delete [] _ipad;
64
6.92k
    delete [] _opad;
65
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::~HMACEngine()
Line
Count
Source
60
6.92k
  {
61
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
62
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
63
6.92k
    delete [] _ipad;
64
6.92k
    delete [] _opad;
65
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::~HMACEngine()
Line
Count
Source
60
6.92k
  {
61
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
62
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
63
6.92k
    delete [] _ipad;
64
6.92k
    delete [] _opad;
65
6.92k
  }
66
67
  HMACEngine() = delete;
68
  HMACEngine(const HMACEngine&) = delete;
69
  HMACEngine& operator=(const HMACEngine&) = delete;
70
71
0
  std::size_t digestLength() const override { return DIGEST_SIZE; }
Unexecuted instantiation: Poco::HMACEngine<Poco::MD5Engine>::digestLength() const
Unexecuted instantiation: Poco::HMACEngine<Poco::SHA1Engine>::digestLength() const
Unexecuted instantiation: Poco::HMACEngine<Poco::JWT::SHA256Engine>::digestLength() const
Unexecuted instantiation: Poco::HMACEngine<Poco::JWT::SHA384Engine>::digestLength() const
Unexecuted instantiation: Poco::HMACEngine<Poco::JWT::SHA512Engine>::digestLength() const
72
73
  void reset() override
74
47.3k
  {
75
47.3k
    _engine.reset();
76
47.3k
    _engine.update(_ipad, BLOCK_SIZE);
77
47.3k
  }
Poco::HMACEngine<Poco::MD5Engine>::reset()
Line
Count
Source
74
1.11k
  {
75
1.11k
    _engine.reset();
76
1.11k
    _engine.update(_ipad, BLOCK_SIZE);
77
1.11k
  }
Poco::HMACEngine<Poco::SHA1Engine>::reset()
Line
Count
Source
74
4.76k
  {
75
4.76k
    _engine.reset();
76
4.76k
    _engine.update(_ipad, BLOCK_SIZE);
77
4.76k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::reset()
Line
Count
Source
74
13.8k
  {
75
13.8k
    _engine.reset();
76
13.8k
    _engine.update(_ipad, BLOCK_SIZE);
77
13.8k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::reset()
Line
Count
Source
74
13.8k
  {
75
13.8k
    _engine.reset();
76
13.8k
    _engine.update(_ipad, BLOCK_SIZE);
77
13.8k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::reset()
Line
Count
Source
74
13.8k
  {
75
13.8k
    _engine.reset();
76
13.8k
    _engine.update(_ipad, BLOCK_SIZE);
77
13.8k
  }
78
79
  const DigestEngine::Digest& digest() override
80
23.6k
  {
81
23.6k
    const DigestEngine::Digest& d = _engine.digest();
82
23.6k
    poco_assert (d.size() == DIGEST_SIZE);
83
23.6k
    char db[DIGEST_SIZE];
84
23.6k
    char* pdb = db;
85
1.05M
    for (auto v: d) *pdb++ = v;
86
23.6k
    _engine.reset();
87
23.6k
    _engine.update(_opad, BLOCK_SIZE);
88
23.6k
    _engine.update(db, DIGEST_SIZE);
89
23.6k
    const DigestEngine::Digest& result = _engine.digest();
90
23.6k
    reset();
91
23.6k
    return result;
92
23.6k
  }
Poco::HMACEngine<Poco::MD5Engine>::digest()
Line
Count
Source
80
558
  {
81
558
    const DigestEngine::Digest& d = _engine.digest();
82
558
    poco_assert (d.size() == DIGEST_SIZE);
83
558
    char db[DIGEST_SIZE];
84
558
    char* pdb = db;
85
8.92k
    for (auto v: d) *pdb++ = v;
86
558
    _engine.reset();
87
558
    _engine.update(_opad, BLOCK_SIZE);
88
558
    _engine.update(db, DIGEST_SIZE);
89
558
    const DigestEngine::Digest& result = _engine.digest();
90
558
    reset();
91
558
    return result;
92
558
  }
Poco::HMACEngine<Poco::SHA1Engine>::digest()
Line
Count
Source
80
2.38k
  {
81
2.38k
    const DigestEngine::Digest& d = _engine.digest();
82
2.38k
    poco_assert (d.size() == DIGEST_SIZE);
83
2.38k
    char db[DIGEST_SIZE];
84
2.38k
    char* pdb = db;
85
47.6k
    for (auto v: d) *pdb++ = v;
86
2.38k
    _engine.reset();
87
2.38k
    _engine.update(_opad, BLOCK_SIZE);
88
2.38k
    _engine.update(db, DIGEST_SIZE);
89
2.38k
    const DigestEngine::Digest& result = _engine.digest();
90
2.38k
    reset();
91
2.38k
    return result;
92
2.38k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::digest()
Line
Count
Source
80
6.92k
  {
81
6.92k
    const DigestEngine::Digest& d = _engine.digest();
82
6.92k
    poco_assert (d.size() == DIGEST_SIZE);
83
6.92k
    char db[DIGEST_SIZE];
84
6.92k
    char* pdb = db;
85
221k
    for (auto v: d) *pdb++ = v;
86
6.92k
    _engine.reset();
87
6.92k
    _engine.update(_opad, BLOCK_SIZE);
88
6.92k
    _engine.update(db, DIGEST_SIZE);
89
6.92k
    const DigestEngine::Digest& result = _engine.digest();
90
6.92k
    reset();
91
6.92k
    return result;
92
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::digest()
Line
Count
Source
80
6.92k
  {
81
6.92k
    const DigestEngine::Digest& d = _engine.digest();
82
6.92k
    poco_assert (d.size() == DIGEST_SIZE);
83
6.92k
    char db[DIGEST_SIZE];
84
6.92k
    char* pdb = db;
85
332k
    for (auto v: d) *pdb++ = v;
86
6.92k
    _engine.reset();
87
6.92k
    _engine.update(_opad, BLOCK_SIZE);
88
6.92k
    _engine.update(db, DIGEST_SIZE);
89
6.92k
    const DigestEngine::Digest& result = _engine.digest();
90
6.92k
    reset();
91
6.92k
    return result;
92
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::digest()
Line
Count
Source
80
6.92k
  {
81
6.92k
    const DigestEngine::Digest& d = _engine.digest();
82
6.92k
    poco_assert (d.size() == DIGEST_SIZE);
83
6.92k
    char db[DIGEST_SIZE];
84
6.92k
    char* pdb = db;
85
442k
    for (auto v: d) *pdb++ = v;
86
6.92k
    _engine.reset();
87
6.92k
    _engine.update(_opad, BLOCK_SIZE);
88
6.92k
    _engine.update(db, DIGEST_SIZE);
89
6.92k
    const DigestEngine::Digest& result = _engine.digest();
90
6.92k
    reset();
91
6.92k
    return result;
92
6.92k
  }
93
94
protected:
95
  void init(const char* passphrase, std::size_t length)
96
23.6k
  {
97
23.6k
    _ipad = new char[BLOCK_SIZE];
98
23.6k
    _opad = new char[BLOCK_SIZE];
99
23.6k
    std::memset(_ipad, 0, BLOCK_SIZE);
100
23.6k
    std::memset(_opad, 0, BLOCK_SIZE);
101
23.6k
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
23.6k
    else
116
23.6k
    {
117
23.6k
      std::memcpy(_ipad, passphrase, length);
118
23.6k
      std::memcpy(_opad, passphrase, length);
119
23.6k
    }
120
2.42M
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
2.40M
    {
122
2.40M
      _ipad[i] ^= 0x36;
123
2.40M
      _opad[i] ^= 0x5c;
124
2.40M
    }
125
23.6k
    reset();
126
23.6k
  }
Poco::HMACEngine<Poco::MD5Engine>::init(char const*, unsigned long)
Line
Count
Source
96
558
  {
97
558
    _ipad = new char[BLOCK_SIZE];
98
558
    _opad = new char[BLOCK_SIZE];
99
558
    std::memset(_ipad, 0, BLOCK_SIZE);
100
558
    std::memset(_opad, 0, BLOCK_SIZE);
101
558
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
558
    else
116
558
    {
117
558
      std::memcpy(_ipad, passphrase, length);
118
558
      std::memcpy(_opad, passphrase, length);
119
558
    }
120
36.2k
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
35.7k
    {
122
35.7k
      _ipad[i] ^= 0x36;
123
35.7k
      _opad[i] ^= 0x5c;
124
35.7k
    }
125
558
    reset();
126
558
  }
Poco::HMACEngine<Poco::SHA1Engine>::init(char const*, unsigned long)
Line
Count
Source
96
2.38k
  {
97
2.38k
    _ipad = new char[BLOCK_SIZE];
98
2.38k
    _opad = new char[BLOCK_SIZE];
99
2.38k
    std::memset(_ipad, 0, BLOCK_SIZE);
100
2.38k
    std::memset(_opad, 0, BLOCK_SIZE);
101
2.38k
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
2.38k
    else
116
2.38k
    {
117
2.38k
      std::memcpy(_ipad, passphrase, length);
118
2.38k
      std::memcpy(_opad, passphrase, length);
119
2.38k
    }
120
154k
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
152k
    {
122
152k
      _ipad[i] ^= 0x36;
123
152k
      _opad[i] ^= 0x5c;
124
152k
    }
125
2.38k
    reset();
126
2.38k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::init(char const*, unsigned long)
Line
Count
Source
96
6.92k
  {
97
6.92k
    _ipad = new char[BLOCK_SIZE];
98
6.92k
    _opad = new char[BLOCK_SIZE];
99
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
100
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
101
6.92k
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
6.92k
    else
116
6.92k
    {
117
6.92k
      std::memcpy(_ipad, passphrase, length);
118
6.92k
      std::memcpy(_opad, passphrase, length);
119
6.92k
    }
120
449k
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
442k
    {
122
442k
      _ipad[i] ^= 0x36;
123
442k
      _opad[i] ^= 0x5c;
124
442k
    }
125
6.92k
    reset();
126
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::init(char const*, unsigned long)
Line
Count
Source
96
6.92k
  {
97
6.92k
    _ipad = new char[BLOCK_SIZE];
98
6.92k
    _opad = new char[BLOCK_SIZE];
99
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
100
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
101
6.92k
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
6.92k
    else
116
6.92k
    {
117
6.92k
      std::memcpy(_ipad, passphrase, length);
118
6.92k
      std::memcpy(_opad, passphrase, length);
119
6.92k
    }
120
892k
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
885k
    {
122
885k
      _ipad[i] ^= 0x36;
123
885k
      _opad[i] ^= 0x5c;
124
885k
    }
125
6.92k
    reset();
126
6.92k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::init(char const*, unsigned long)
Line
Count
Source
96
6.92k
  {
97
6.92k
    _ipad = new char[BLOCK_SIZE];
98
6.92k
    _opad = new char[BLOCK_SIZE];
99
6.92k
    std::memset(_ipad, 0, BLOCK_SIZE);
100
6.92k
    std::memset(_opad, 0, BLOCK_SIZE);
101
6.92k
    if (length > BLOCK_SIZE)
102
0
    {
103
0
      _engine.reset();
104
0
      _engine.update(passphrase, length);
105
0
      const DigestEngine::Digest& d = _engine.digest();
106
0
      char* ipad = _ipad;
107
0
      char* opad = _opad;
108
0
      int n = BLOCK_SIZE;
109
0
      for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
110
0
      {
111
0
        *ipad++ = *it;
112
0
        *opad++ = *it;
113
0
      }
114
0
    }
115
6.92k
    else
116
6.92k
    {
117
6.92k
      std::memcpy(_ipad, passphrase, length);
118
6.92k
      std::memcpy(_opad, passphrase, length);
119
6.92k
    }
120
892k
    for (int i = 0; i < BLOCK_SIZE; ++i)
121
885k
    {
122
885k
      _ipad[i] ^= 0x36;
123
885k
      _opad[i] ^= 0x5c;
124
885k
    }
125
6.92k
    reset();
126
6.92k
  }
127
128
  void updateImpl(const void* data, std::size_t length) override
129
65.5k
  {
130
65.5k
    _engine.update(data, length);
131
65.5k
  }
Poco::HMACEngine<Poco::MD5Engine>::updateImpl(void const*, unsigned long)
Line
Count
Source
129
930
  {
130
930
    _engine.update(data, length);
131
930
  }
Poco::HMACEngine<Poco::SHA1Engine>::updateImpl(void const*, unsigned long)
Line
Count
Source
129
2.38k
  {
130
2.38k
    _engine.update(data, length);
131
2.38k
  }
Poco::HMACEngine<Poco::JWT::SHA256Engine>::updateImpl(void const*, unsigned long)
Line
Count
Source
129
20.7k
  {
130
20.7k
    _engine.update(data, length);
131
20.7k
  }
Poco::HMACEngine<Poco::JWT::SHA384Engine>::updateImpl(void const*, unsigned long)
Line
Count
Source
129
20.7k
  {
130
20.7k
    _engine.update(data, length);
131
20.7k
  }
Poco::HMACEngine<Poco::JWT::SHA512Engine>::updateImpl(void const*, unsigned long)
Line
Count
Source
129
20.7k
  {
130
20.7k
    _engine.update(data, length);
131
20.7k
  }
132
133
private:
134
  Engine _engine;
135
  char*  _ipad;
136
  char*  _opad;
137
};
138
139
140
} // namespace Poco
141
142
143
#endif // Foundation_HMACEngine_INCLUDED