blob: 40d277fa0107274bbc4ded70e184ac7c89d84c42 [file] [log] [blame]
Tom Joseph8c0446c2016-08-05 07:13:07 -05001#include "auth_algo.hpp"
2
George Liu7b7f25f2022-07-04 17:07:32 +08003#include <error.h>
Vernon Mauery9b307be2017-11-22 09:28:16 -08004#include <openssl/evp.h>
Tom Joseph8c0446c2016-08-05 07:13:07 -05005#include <openssl/hmac.h>
6#include <openssl/sha.h>
George Liu7b7f25f2022-07-04 17:07:32 +08007#include <string.h>
Tom Joseph8c0446c2016-08-05 07:13:07 -05008
George Liu7b7f25f2022-07-04 17:07:32 +08009#include <phosphor-logging/lg2.hpp>
Tom Joseph8c0446c2016-08-05 07:13:07 -050010
11namespace cipher
12{
13
14namespace rakp_auth
15{
16
Tom Joseph56527b92018-03-21 19:31:58 +053017const std::string userName = "admin";
18
Vernon Mauery9e801a22018-10-12 13:20:49 -070019std::vector<uint8_t>
20 AlgoSHA1::generateHMAC(const std::vector<uint8_t>& input) const
Tom Joseph8c0446c2016-08-05 07:13:07 -050021{
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 Liu7b7f25f2022-07-04 17:07:32 +080028 lg2::error("Generate HMAC failed: {ERROR}", "ERROR", strerror(errno));
Tom Joseph8c0446c2016-08-05 07:13:07 -050029 output.resize(0);
30 }
31
32 return output;
33}
34
Vernon Mauery9e801a22018-10-12 13:20:49 -070035std::vector<uint8_t>
36 AlgoSHA1::generateICV(const std::vector<uint8_t>& input) const
Tom Joseph8c0446c2016-08-05 07:13:07 -050037{
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 Liu7b7f25f2022-07-04 17:07:32 +080044 lg2::error("Generate Session Integrity Key failed: {ERROR}", "ERROR",
45 strerror(errno));
Tom Joseph8c0446c2016-08-05 07:13:07 -050046 output.resize(0);
47 }
Vernon Mauery2207f512017-11-30 10:48:08 -080048 output.resize(integrityCheckValueLength);
Tom Joseph8c0446c2016-08-05 07:13:07 -050049
50 return output;
51}
52
Vernon Mauery9e801a22018-10-12 13:20:49 -070053std::vector<uint8_t>
54 AlgoSHA256::generateHMAC(const std::vector<uint8_t>& input) const
Vernon Mauery7e9e2ef2017-11-29 08:36:29 -080055{
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 Liu7b7f25f2022-07-04 17:07:32 +080062 lg2::error("Generate HMAC_SHA256 failed: {ERROR}", "ERROR",
63 strerror(errno));
Vernon Mauery7e9e2ef2017-11-29 08:36:29 -080064 output.resize(0);
65 }
66
67 return output;
68}
69
Vernon Mauery9e801a22018-10-12 13:20:49 -070070std::vector<uint8_t>
71 AlgoSHA256::generateICV(const std::vector<uint8_t>& input) const
Vernon Mauery7e9e2ef2017-11-29 08:36:29 -080072{
73 std::vector<uint8_t> output(SHA256_DIGEST_LENGTH);
74 unsigned int mdLen = 0;
75
Vernon Mauery9e801a22018-10-12 13:20:49 -070076 if (HMAC(EVP_sha256(), sessionIntegrityKey.data(),
77 sessionIntegrityKey.size(), input.data(), input.size(),
78 output.data(), &mdLen) == NULL)
Vernon Mauery7e9e2ef2017-11-29 08:36:29 -080079 {
George Liu7b7f25f2022-07-04 17:07:32 +080080 lg2::error(
81 "Generate HMAC_SHA256_128 Integrity Check Value failed: {ERROR}",
82 "ERROR", strerror(errno));
Vernon Mauery7e9e2ef2017-11-29 08:36:29 -080083 output.resize(0);
84 }
85 output.resize(integrityCheckValueLength);
86
87 return output;
88}
89
Vernon Mauery9e801a22018-10-12 13:20:49 -070090} // namespace rakp_auth
Tom Joseph8c0446c2016-08-05 07:13:07 -050091
92} // namespace cipher