1 #include "auth_algo.hpp" 2 3 #include <openssl/evp.h> 4 #include <openssl/hmac.h> 5 #include <openssl/sha.h> 6 7 #include <iostream> 8 9 namespace cipher 10 { 11 12 namespace rakp_auth 13 { 14 15 const std::string userName = "admin"; 16 17 std::vector<uint8_t> AlgoSHA1::generateHMAC( 18 const std::vector<uint8_t>& input) const 19 { 20 std::vector<uint8_t> output(SHA_DIGEST_LENGTH); 21 unsigned int mdLen = 0; 22 23 if (HMAC(EVP_sha1(), userKey.data(), userKey.size(), input.data(), 24 input.size(), output.data(), &mdLen) == NULL) 25 { 26 std::cerr << "Generate HMAC failed\n"; 27 output.resize(0); 28 } 29 30 return output; 31 } 32 33 std::vector<uint8_t> AlgoSHA1::generateICV( 34 const std::vector<uint8_t>& input) const 35 { 36 std::vector<uint8_t> output(SHA_DIGEST_LENGTH); 37 unsigned int mdLen = 0; 38 39 if (HMAC(EVP_sha1(), sessionIntegrityKey.data(), SHA_DIGEST_LENGTH, 40 input.data(), input.size(), output.data(), &mdLen) == NULL) 41 { 42 std::cerr << "Generate Session Integrity Key failed\n"; 43 output.resize(0); 44 } 45 output.resize(integrityCheckValueLength); 46 47 return output; 48 } 49 50 std::vector<uint8_t> AlgoSHA256::generateHMAC( 51 const std::vector<uint8_t>& input) const 52 { 53 std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); 54 unsigned int mdLen = 0; 55 56 if (HMAC(EVP_sha256(), userKey.data(), userKey.size(), input.data(), 57 input.size(), output.data(), &mdLen) == NULL) 58 { 59 std::cerr << "Generate HMAC_SHA256 failed\n"; 60 output.resize(0); 61 } 62 63 return output; 64 } 65 66 std::vector<uint8_t> AlgoSHA256::generateICV( 67 const std::vector<uint8_t>& input) const 68 { 69 std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); 70 unsigned int mdLen = 0; 71 72 if (HMAC(EVP_sha256(), 73 sessionIntegrityKey.data(), sessionIntegrityKey.size(), 74 input.data(), input.size(), output.data(), &mdLen) == NULL) 75 { 76 std::cerr << "Generate HMAC_SHA256_128 Integrity Check Value failed\n"; 77 output.resize(0); 78 } 79 output.resize(integrityCheckValueLength); 80 81 return output; 82 } 83 84 } // namespace auth 85 86 } // namespace cipher 87