Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 1 | #include "auth_algo.hpp" |
| 2 | |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 3 | #include <error.h> |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 4 | #include <openssl/evp.h> |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 5 | #include <openssl/hmac.h> |
| 6 | #include <openssl/sha.h> |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 7 | #include <string.h> |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 8 | |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
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 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 28 | lg2::error("Generate HMAC failed: {ERROR}", "ERROR", strerror(errno)); |
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 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 44 | lg2::error("Generate Session Integrity Key failed: {ERROR}", "ERROR", |
| 45 | strerror(errno)); |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 46 | output.resize(0); |
| 47 | } |
Vernon Mauery | 2207f51 | 2017-11-30 10:48:08 -0800 | [diff] [blame] | 48 | output.resize(integrityCheckValueLength); |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 49 | |
| 50 | return output; |
| 51 | } |
| 52 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 53 | std::vector<uint8_t> |
| 54 | AlgoSHA256::generateHMAC(const std::vector<uint8_t>& input) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 55 | { |
| 56 | std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); |
| 57 | unsigned int mdLen = 0; |
| 58 | |
| 59 | if (HMAC(EVP_sha256(), userKey.data(), userKey.size(), input.data(), |
| 60 | input.size(), output.data(), &mdLen) == NULL) |
| 61 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 62 | lg2::error("Generate HMAC_SHA256 failed: {ERROR}", "ERROR", |
| 63 | strerror(errno)); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 64 | output.resize(0); |
| 65 | } |
| 66 | |
| 67 | return output; |
| 68 | } |
| 69 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 70 | std::vector<uint8_t> |
| 71 | AlgoSHA256::generateICV(const std::vector<uint8_t>& input) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 72 | { |
| 73 | std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); |
| 74 | unsigned int mdLen = 0; |
| 75 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 76 | if (HMAC(EVP_sha256(), sessionIntegrityKey.data(), |
| 77 | sessionIntegrityKey.size(), input.data(), input.size(), |
| 78 | output.data(), &mdLen) == NULL) |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 79 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 80 | lg2::error( |
| 81 | "Generate HMAC_SHA256_128 Integrity Check Value failed: {ERROR}", |
| 82 | "ERROR", strerror(errno)); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 83 | output.resize(0); |
| 84 | } |
| 85 | output.resize(integrityCheckValueLength); |
| 86 | |
| 87 | return output; |
| 88 | } |
| 89 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 90 | } // namespace rakp_auth |
Tom Joseph | 8c0446c | 2016-08-05 07:13:07 -0500 | [diff] [blame] | 91 | |
| 92 | } // namespace cipher |