1//
2// RSADigestEngine.cpp
3//
4// Library: Crypto
5// Package: RSA
6// Module: RSADigestEngine
7//
8// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Crypto/RSADigestEngine.h"
16#include <openssl/rsa.h>
17
18
19namespace Poco {
20namespace Crypto {
21
22
23RSADigestEngine::RSADigestEngine(const RSAKey& key, DigestType digestType):
24 _key(key),
25 _engine(digestType == DIGEST_MD5 ? "MD5" : "SHA1")
26{
27}
28
29RSADigestEngine::RSADigestEngine(const RSAKey& key, const std::string &name):
30 _key(key),
31 _engine(name)
32{
33}
34
35
36RSADigestEngine::~RSADigestEngine()
37{
38}
39
40
41std::size_t RSADigestEngine::digestLength() const
42{
43 return _engine.digestLength();
44}
45
46
47void RSADigestEngine::reset()
48{
49 _engine.reset();
50 _digest.clear();
51 _signature.clear();
52}
53
54
55const DigestEngine::Digest& RSADigestEngine::digest()
56{
57 if (_digest.empty())
58 {
59 _digest = _engine.digest();
60 }
61 return _digest;
62}
63
64
65const DigestEngine::Digest& RSADigestEngine::signature()
66{
67 if (_signature.empty())
68 {
69 digest();
70 _signature.resize(_key.size());
71 unsigned sigLen = static_cast<unsigned>(_signature.size());
72 RSA_sign(_engine.nid(), &_digest[0], static_cast<unsigned>(_digest.size()), &_signature[0], &sigLen, _key.impl()->getRSA());
73 // truncate _sig to sigLen
74 if (sigLen < _signature.size())
75 _signature.resize(sigLen);
76 }
77 return _signature;
78}
79
80
81bool RSADigestEngine::verify(const DigestEngine::Digest& sig)
82{
83 digest();
84 DigestEngine::Digest sigCpy = sig; // copy becausse RSA_verify can modify sigCpy
85 int ret = RSA_verify(_engine.nid(), &_digest[0], static_cast<unsigned>(_digest.size()), &sigCpy[0], static_cast<unsigned>(sigCpy.size()), _key.impl()->getRSA());
86 return ret != 0;
87}
88
89
90void RSADigestEngine::updateImpl(const void* data, std::size_t length)
91{
92 _engine.update(data, length);
93}
94
95
96} } // namespace Poco::Crypto
97