Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 1 | #include "auth_algo.hpp" |
| 2 | |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 3 | #include <openssl/evp.h> |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 4 | #include <openssl/hmac.h> |
| 5 | #include <openssl/sha.h> |
| 6 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
| 9 | using namespace phosphor::logging; |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 10 | |
| 11 | namespace cipher |
| 12 | { |
| 13 | |
| 14 | namespace rakp_auth |
| 15 | { |
| 16 | |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 17 | const std::string userName = "admin"; |
| 18 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 19 | std::vector<uint8_t> |
| 20 | AlgoSHA1::generateHMAC(const std::vector<uint8_t>& input) const |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 21 | { |
| 22 | std::vector<uint8_t> output(SHA_DIGEST_LENGTH); |
| 23 | unsigned int mdLen = 0; |
| 24 | |
| 25 | if (HMAC(EVP_sha1(), userKey.data(), userKey.size(), input.data(), |
| 26 | input.size(), output.data(), &mdLen) == NULL) |
| 27 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 28 | log<level::ERR>("Generate HMAC failed"); |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 29 | output.resize(0); |
| 30 | } |
| 31 | |
| 32 | return output; |
| 33 | } |
| 34 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 35 | std::vector<uint8_t> |
| 36 | AlgoSHA1::generateICV(const std::vector<uint8_t>& input) const |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 37 | { |
| 38 | std::vector<uint8_t> output(SHA_DIGEST_LENGTH); |
| 39 | unsigned int mdLen = 0; |
| 40 | |
| 41 | if (HMAC(EVP_sha1(), sessionIntegrityKey.data(), SHA_DIGEST_LENGTH, |
| 42 | input.data(), input.size(), output.data(), &mdLen) == NULL) |
| 43 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 44 | log<level::ERR>("Generate Session Integrity Key failed"); |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 45 | output.resize(0); |
| 46 | } |
Vernon Mauery | 2207f51 | 2017-11-30 10:48:08 -0800 | [diff] [blame] | 47 | output.resize(integrityCheckValueLength); |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 48 | |
| 49 | return output; |
| 50 | } |
| 51 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 52 | std::vector<uint8_t> |
| 53 | AlgoSHA256::generateHMAC(const std::vector<uint8_t>& input) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 54 | { |
| 55 | std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); |
| 56 | unsigned int mdLen = 0; |
| 57 | |
| 58 | if (HMAC(EVP_sha256(), userKey.data(), userKey.size(), input.data(), |
| 59 | input.size(), output.data(), &mdLen) == NULL) |
| 60 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 61 | log<level::ERR>("Generate HMAC_SHA256 failed"); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 62 | output.resize(0); |
| 63 | } |
| 64 | |
| 65 | return output; |
| 66 | } |
| 67 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | std::vector<uint8_t> |
| 69 | AlgoSHA256::generateICV(const std::vector<uint8_t>& input) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 70 | { |
| 71 | std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); |
| 72 | unsigned int mdLen = 0; |
| 73 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | if (HMAC(EVP_sha256(), sessionIntegrityKey.data(), |
| 75 | sessionIntegrityKey.size(), input.data(), input.size(), |
| 76 | output.data(), &mdLen) == NULL) |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 77 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 78 | log<level::ERR>( |
| 79 | "Generate HMAC_SHA256_128 Integrity Check Value failed"); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 80 | output.resize(0); |
| 81 | } |
| 82 | output.resize(integrityCheckValueLength); |
| 83 | |
| 84 | return output; |
| 85 | } |
| 86 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 87 | } // namespace rakp_auth |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 88 | |
| 89 | } // namespace cipher |