cryptolink.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cryptolink/cryptolink.h>
  15. #include <cryptolink/crypto_hmac.h>
  16. #include <botan/botan.h>
  17. namespace isc {
  18. namespace cryptolink {
  19. // For Botan, we use the CryptoLink class object in RAII style
  20. class CryptoLinkImpl {
  21. private:
  22. Botan::LibraryInitializer botan_init_;
  23. };
  24. CryptoLink::~CryptoLink() {
  25. delete impl_;
  26. }
  27. CryptoLink&
  28. CryptoLink::getCryptoLink() {
  29. CryptoLink& c = getCryptoLinkInternal();
  30. if (c.impl_ == NULL) {
  31. c.initialize();
  32. }
  33. return (c);
  34. }
  35. CryptoLink&
  36. CryptoLink::getCryptoLinkInternal() {
  37. static CryptoLink instance;
  38. return (instance);
  39. }
  40. void
  41. CryptoLink::initialize() {
  42. CryptoLink& c = getCryptoLinkInternal();
  43. if (c.impl_ == NULL) {
  44. try {
  45. c.impl_ = new CryptoLinkImpl();
  46. } catch (const Botan::Exception& ex) {
  47. isc_throw(InitializationError, ex.what());
  48. }
  49. }
  50. }
  51. HMAC*
  52. CryptoLink::createHMAC(const void* secret, size_t secret_len,
  53. const HashAlgorithm hash_algorithm)
  54. {
  55. return (new HMAC(secret, secret_len, hash_algorithm));
  56. }
  57. } // namespace cryptolink
  58. } // namespace isc