1 #include "auth_algo.hpp"
2 
3 #include <error.h>
4 #include <openssl/evp.h>
5 #include <openssl/hmac.h>
6 #include <openssl/sha.h>
7 #include <string.h>
8 
9 #include <phosphor-logging/lg2.hpp>
10 
11 namespace cipher
12 {
13 
14 namespace rakp_auth
15 {
16 
17 std::vector<uint8_t>
18     AlgoSHA1::generateHMAC(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         lg2::error("Generate HMAC failed: {ERROR}", "ERROR", strerror(errno));
27         output.resize(0);
28     }
29 
30     return output;
31 }
32 
33 std::vector<uint8_t>
34     AlgoSHA1::generateICV(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         lg2::error("Generate Session Integrity Key failed: {ERROR}", "ERROR",
43                    strerror(errno));
44         output.resize(0);
45     }
46     output.resize(integrityCheckValueLength);
47 
48     return output;
49 }
50 
51 std::vector<uint8_t>
52     AlgoSHA256::generateHMAC(const std::vector<uint8_t>& input) const
53 {
54     std::vector<uint8_t> output(SHA256_DIGEST_LENGTH);
55     unsigned int mdLen = 0;
56 
57     if (HMAC(EVP_sha256(), userKey.data(), userKey.size(), input.data(),
58              input.size(), output.data(), &mdLen) == NULL)
59     {
60         lg2::error("Generate HMAC_SHA256 failed: {ERROR}", "ERROR",
61                    strerror(errno));
62         output.resize(0);
63     }
64 
65     return output;
66 }
67 
68 std::vector<uint8_t>
69     AlgoSHA256::generateICV(const std::vector<uint8_t>& input) const
70 {
71     std::vector<uint8_t> output(SHA256_DIGEST_LENGTH);
72     unsigned int mdLen = 0;
73 
74     if (HMAC(EVP_sha256(), sessionIntegrityKey.data(),
75              sessionIntegrityKey.size(), input.data(), input.size(),
76              output.data(), &mdLen) == NULL)
77     {
78         lg2::error(
79             "Generate HMAC_SHA256_128 Integrity Check Value failed: {ERROR}",
80             "ERROR", strerror(errno));
81         output.resize(0);
82     }
83     output.resize(integrityCheckValueLength);
84 
85     return output;
86 }
87 
88 } // namespace rakp_auth
89 
90 } // namespace cipher
91