18c0446c1STom Joseph #include "auth_algo.hpp"
28c0446c1STom Joseph 
39b307be6SVernon Mauery #include <openssl/evp.h>
48c0446c1STom Joseph #include <openssl/hmac.h>
58c0446c1STom Joseph #include <openssl/sha.h>
68c0446c1STom Joseph 
78c0446c1STom Joseph #include <iostream>
88c0446c1STom Joseph 
98c0446c1STom Joseph namespace cipher
108c0446c1STom Joseph {
118c0446c1STom Joseph 
128c0446c1STom Joseph namespace rakp_auth
138c0446c1STom Joseph {
148c0446c1STom Joseph 
1570fd29cfSVernon Mauery std::vector<uint8_t> AlgoSHA1::generateHMAC(
1670fd29cfSVernon Mauery         const std::vector<uint8_t>& input) const
178c0446c1STom Joseph {
188c0446c1STom Joseph     std::vector<uint8_t> output(SHA_DIGEST_LENGTH);
198c0446c1STom Joseph     unsigned int mdLen = 0;
208c0446c1STom Joseph 
218c0446c1STom Joseph     if (HMAC(EVP_sha1(), userKey.data(), userKey.size(), input.data(),
228c0446c1STom Joseph              input.size(), output.data(), &mdLen) == NULL)
238c0446c1STom Joseph     {
248c0446c1STom Joseph         std::cerr << "Generate HMAC failed\n";
258c0446c1STom Joseph         output.resize(0);
268c0446c1STom Joseph     }
278c0446c1STom Joseph 
288c0446c1STom Joseph     return output;
298c0446c1STom Joseph }
308c0446c1STom Joseph 
3170fd29cfSVernon Mauery std::vector<uint8_t> AlgoSHA1::generateICV(
3270fd29cfSVernon Mauery         const std::vector<uint8_t>& input) const
338c0446c1STom Joseph {
348c0446c1STom Joseph     std::vector<uint8_t> output(SHA_DIGEST_LENGTH);
358c0446c1STom Joseph     unsigned int mdLen = 0;
368c0446c1STom Joseph 
378c0446c1STom Joseph     if (HMAC(EVP_sha1(), sessionIntegrityKey.data(), SHA_DIGEST_LENGTH,
388c0446c1STom Joseph              input.data(), input.size(), output.data(), &mdLen) == NULL)
398c0446c1STom Joseph     {
408c0446c1STom Joseph         std::cerr << "Generate Session Integrity Key failed\n";
418c0446c1STom Joseph         output.resize(0);
428c0446c1STom Joseph     }
432207f51cSVernon Mauery     output.resize(integrityCheckValueLength);
448c0446c1STom Joseph 
458c0446c1STom Joseph     return output;
468c0446c1STom Joseph }
478c0446c1STom Joseph 
48*7e9e2ef6SVernon Mauery std::vector<uint8_t> AlgoSHA256::generateHMAC(
49*7e9e2ef6SVernon Mauery         const std::vector<uint8_t>& input) const
50*7e9e2ef6SVernon Mauery {
51*7e9e2ef6SVernon Mauery     std::vector<uint8_t> output(SHA256_DIGEST_LENGTH);
52*7e9e2ef6SVernon Mauery     unsigned int mdLen = 0;
53*7e9e2ef6SVernon Mauery 
54*7e9e2ef6SVernon Mauery     if (HMAC(EVP_sha256(), userKey.data(), userKey.size(), input.data(),
55*7e9e2ef6SVernon Mauery              input.size(), output.data(), &mdLen) == NULL)
56*7e9e2ef6SVernon Mauery     {
57*7e9e2ef6SVernon Mauery         std::cerr << "Generate HMAC_SHA256 failed\n";
58*7e9e2ef6SVernon Mauery         output.resize(0);
59*7e9e2ef6SVernon Mauery     }
60*7e9e2ef6SVernon Mauery 
61*7e9e2ef6SVernon Mauery     return output;
62*7e9e2ef6SVernon Mauery }
63*7e9e2ef6SVernon Mauery 
64*7e9e2ef6SVernon Mauery std::vector<uint8_t> AlgoSHA256::generateICV(
65*7e9e2ef6SVernon Mauery         const std::vector<uint8_t>& input) const
66*7e9e2ef6SVernon Mauery {
67*7e9e2ef6SVernon Mauery     std::vector<uint8_t> output(SHA256_DIGEST_LENGTH);
68*7e9e2ef6SVernon Mauery     unsigned int mdLen = 0;
69*7e9e2ef6SVernon Mauery 
70*7e9e2ef6SVernon Mauery     if (HMAC(EVP_sha256(),
71*7e9e2ef6SVernon Mauery              sessionIntegrityKey.data(), sessionIntegrityKey.size(),
72*7e9e2ef6SVernon Mauery              input.data(), input.size(), output.data(), &mdLen) == NULL)
73*7e9e2ef6SVernon Mauery     {
74*7e9e2ef6SVernon Mauery         std::cerr << "Generate HMAC_SHA256_128 Integrity Check Value failed\n";
75*7e9e2ef6SVernon Mauery         output.resize(0);
76*7e9e2ef6SVernon Mauery     }
77*7e9e2ef6SVernon Mauery     output.resize(integrityCheckValueLength);
78*7e9e2ef6SVernon Mauery 
79*7e9e2ef6SVernon Mauery     return output;
80*7e9e2ef6SVernon Mauery }
81*7e9e2ef6SVernon Mauery 
828c0446c1STom Joseph } // namespace auth
838c0446c1STom Joseph 
848c0446c1STom Joseph } // namespace cipher
85