Blame view

src/crypter.cpp 3.58 KB
7ba6afb5   Steven de Ridder   Initial commit. d...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include "crypter.h"
  
  // std
  #include <iomanip>
  #include <memory>
  #include <sstream>
  #include <stdexcept>
  #include <system_error>
  
  // openssl
  #include <openssl/evp.h>
  
  // osdev::components
  #include "log.h"
  #include "scopeguard.h"
  
  using namespace osdev::components;
  
  namespace {
  
  const EVP_MD* getAlgo( Crypter::AlgorithmEnum algo )
  {
      switch ( algo )
      {
          case Crypter::AlgorithmEnum::MD0:
              return EVP_md_null();
          case Crypter::AlgorithmEnum::MD2:
              // return EVP_md2();
          case Crypter::AlgorithmEnum::MD5:
              return EVP_md5();
          case Crypter::AlgorithmEnum::MDC2:
              // deprecated.
              // md = EVP_mdc2();
              return EVP_md_null();
          case Crypter::AlgorithmEnum::SHA1:
              return EVP_sha1();
          case Crypter::AlgorithmEnum::SHA224:
              return EVP_sha224();
          case Crypter::AlgorithmEnum::SHA256:
              return EVP_sha256();
          case Crypter::AlgorithmEnum::SHA384:
              return EVP_sha384();
          case Crypter::AlgorithmEnum::SHA512:
              return EVP_sha512();
          case Crypter::AlgorithmEnum::RIPEMD160:
              return EVP_ripemd160();
      }
  
      throw std::invalid_argument("Crypto algorithm not found.");
  }
  
  } // anonymous
  
  Crypter::Crypter()
  {
  }
  
  std::string Crypter::encrypt( const std::string& message, Crypter::AlgorithmEnum algo )
  {
      // Create the environment
      auto md = getAlgo(algo);
  
  #if OPENSSL_VERSION_NUMBER >= 0x1010008fL
      auto mdContext = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>(
                  EVP_MD_CTX_create(),
                  [](EVP_MD_CTX* ptr){EVP_MD_CTX_free(ptr);});
  
      // This will call EVP_cleanup if the guard goes out of scope.
      ScopeGuard oGuard( &EVP_PBE_cleanup );
  #else
      auto mdContext = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_destroy)>(
                  EVP_MD_CTX_create(),
                  [](EVP_MD_CTX* ptr){EVP_MD_CTX_destroy(ptr);});
  
      // This will call EVP_cleanup if the guard goes out of scope.
      ScopeGuard oGuard( &EVP_cleanup );
  #endif
  
      (void)oGuard; // Satisfy the compiler for unused variables.
  
      auto errorCode = EVP_DigestInit_ex( mdContext.get(), md, NULL );
      if( 1 != errorCode )
      {
0288e1ab   Peter M. Groen   Fixed Loggin
84
          LogError( "[Crypter::encrypt]", std::string( "No encryption digest environment created." ) );
7ba6afb5   Steven de Ridder   Initial commit. d...
85
86
87
88
89
90
91
          throw std::system_error( errorCode, std::system_category(), "No encryption digest environment created." );
      }
  
      // Update the environment with the message
      errorCode = EVP_DigestUpdate( mdContext.get(), message.c_str(), message.length() );
      if( 1 != errorCode )
      {
0288e1ab   Peter M. Groen   Fixed Loggin
92
          LogError( "[Crypter::encrypt]", std::string("Digest failed.") );
7ba6afb5   Steven de Ridder   Initial commit. d...
93
94
95
96
97
98
99
100
101
          throw std::system_error( errorCode, std::system_category(), "Digest failed.." );
      }
  
      // End the Digest so we can read the crypted message.
      unsigned int mdLen;
      unsigned char mdValue[EVP_MAX_MD_SIZE];
      errorCode = EVP_DigestFinal_ex( mdContext.get(), mdValue, &mdLen );
      if( 1 != errorCode )
      {
0288e1ab   Peter M. Groen   Fixed Loggin
102
          LogError( "[Crypter::encrypt]", std::string("There was an error closing the digest environment.") );
7ba6afb5   Steven de Ridder   Initial commit. d...
103
104
105
106
107
108
109
110
111
112
113
114
115
          throw std::system_error( errorCode, std::system_category(), "There was an error closing the digest environment." );
      }
  
      // If we got here, all went well. We retrieve the crypted message
      // through a stringstream : convert to hex, padding and width and return the string.
      std::stringstream ss;
      for( unsigned int nIndex = 0; nIndex < mdLen; nIndex++ )
      {
          ss << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<int>( mdValue[nIndex] );
      }
  
      return ss.str();
  }