blob: 16d0863f09b7ed6f5ab63e15a232881c91407b7d [file] [log] [blame]
Tom Joseph77531db2017-01-10 15:44:44 +05301#include <openssl/hmac.h>
2#include <openssl/sha.h>
3#include "integrity_algo.hpp"
4#include "message_parsers.hpp"
5
6namespace cipher
7{
8
9namespace integrity
10{
11
12Interface::Interface(const Buffer& sik, const Key& addKey, size_t authLength)
13{
14 unsigned int mdLen = 0;
15
16 // Generated K1 for the integrity algorithm with the additional key keyed
17 // with SIK.
18 if (HMAC(EVP_sha1(), sik.data(), sik.size(), addKey.data(),
19 addKey.size(), K1.data(), &mdLen) == NULL)
20 {
21 throw std::runtime_error("Generating Key1 for integrity "
22 "algorithm failed");
23 }
24
25 authCodeLength = authLength;
26}
27
Tom Josephd212a6d2017-01-10 15:48:40 +053028Buffer AlgoSHA1::generateHMAC(const uint8_t* input, const size_t len) const
29{
30 Buffer output(SHA_DIGEST_LENGTH);
31 unsigned int mdLen = 0;
32
33 if (HMAC(EVP_sha1(), K1.data(), K1.size(), input, len,
34 output.data(), &mdLen) == NULL)
35 {
36 throw std::runtime_error("Generating integrity data failed");
37 }
38
39 // HMAC generates Message Digest to the size of SHA_DIGEST_LENGTH, the
40 // AuthCode field length is based on the integrity algorithm. So we are
41 // interested only in the AuthCode field length of the generated Message
42 // digest.
43 output.resize(authCodeLength);
44
45 return output;
46}
47
48bool AlgoSHA1::verifyIntegrityData(const Buffer& packet,
49 const size_t length,
50 Buffer::const_iterator integrityData) const
51{
52
53 auto output = generateHMAC(
54 packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE,
55 length);
56
57 // Verify if the generated integrity data for the packet and the received
58 // integrity data matches.
59 return (std::equal(output.begin(), output.end(), integrityData));
60}
61
62Buffer AlgoSHA1::generateIntegrityData(const Buffer& packet) const
63{
64 return generateHMAC(
65 packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE,
66 packet.size() - message::parser::RMCP_SESSION_HEADER_SIZE);
67}
68
Tom Joseph77531db2017-01-10 15:44:44 +053069}// namespace integrity
70
71}// namespace cipher