Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "integrity_algo.hpp" |
| 2 | |
| 3 | #include "message_parsers.hpp" |
| 4 | |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 5 | #include <openssl/evp.h> |
Tom Joseph | 77531db | 2017-01-10 15:44:44 +0530 | [diff] [blame] | 6 | #include <openssl/hmac.h> |
| 7 | #include <openssl/sha.h> |
Tom Joseph | 77531db | 2017-01-10 15:44:44 +0530 | [diff] [blame] | 8 | |
| 9 | namespace cipher |
| 10 | { |
| 11 | |
| 12 | namespace integrity |
| 13 | { |
| 14 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 15 | AlgoSHA1::AlgoSHA1(const std::vector<uint8_t>& sik) : |
| 16 | Interface(SHA1_96_AUTHCODE_LENGTH) |
Tom Joseph | 77531db | 2017-01-10 15:44:44 +0530 | [diff] [blame] | 17 | { |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 18 | k1 = generateKn(sik, rmcp::const_1); |
Tom Joseph | 77531db | 2017-01-10 15:44:44 +0530 | [diff] [blame] | 19 | } |
| 20 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 21 | std::vector<uint8_t> AlgoSHA1::generateHMAC(const uint8_t* input, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 22 | const size_t len) const |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 23 | { |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 24 | std::vector<uint8_t> output(SHA_DIGEST_LENGTH); |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 25 | unsigned int mdLen = 0; |
| 26 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 27 | if (HMAC(EVP_sha1(), k1.data(), k1.size(), input, len, output.data(), |
| 28 | &mdLen) == NULL) |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 29 | { |
| 30 | throw std::runtime_error("Generating integrity data failed"); |
| 31 | } |
| 32 | |
| 33 | // HMAC generates Message Digest to the size of SHA_DIGEST_LENGTH, the |
| 34 | // AuthCode field length is based on the integrity algorithm. So we are |
| 35 | // interested only in the AuthCode field length of the generated Message |
| 36 | // digest. |
| 37 | output.resize(authCodeLength); |
| 38 | |
| 39 | return output; |
| 40 | } |
| 41 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 42 | bool AlgoSHA1::verifyIntegrityData( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 43 | const std::vector<uint8_t>& packet, const size_t length, |
| 44 | std::vector<uint8_t>::const_iterator integrityData) const |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 45 | { |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 46 | auto output = generateHMAC( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 47 | packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, length); |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 48 | |
| 49 | // Verify if the generated integrity data for the packet and the received |
| 50 | // integrity data matches. |
| 51 | return (std::equal(output.begin(), output.end(), integrityData)); |
| 52 | } |
| 53 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 54 | std::vector<uint8_t> |
| 55 | AlgoSHA1::generateIntegrityData(const std::vector<uint8_t>& packet) const |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 56 | { |
| 57 | return generateHMAC( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 58 | packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, |
| 59 | packet.size() - message::parser::RMCP_SESSION_HEADER_SIZE); |
Tom Joseph | d212a6d | 2017-01-10 15:48:40 +0530 | [diff] [blame] | 60 | } |
| 61 | |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 62 | std::vector<uint8_t> AlgoSHA1::generateKn(const std::vector<uint8_t>& sik, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 63 | const rmcp::Const_n& const_n) const |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 64 | { |
| 65 | unsigned int mdLen = 0; |
| 66 | std::vector<uint8_t> Kn(sik.size()); |
| 67 | |
| 68 | // Generated Kn for the integrity algorithm with the additional key keyed |
| 69 | // with SIK. |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 70 | if (HMAC(EVP_sha1(), sik.data(), sik.size(), const_n.data(), const_n.size(), |
| 71 | Kn.data(), &mdLen) == NULL) |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 72 | { |
| 73 | throw std::runtime_error("Generating KeyN for integrity " |
| 74 | "algorithm failed"); |
| 75 | } |
| 76 | return Kn; |
| 77 | } |
| 78 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 79 | AlgoSHA256::AlgoSHA256(const std::vector<uint8_t>& sik) : |
| 80 | Interface(SHA256_128_AUTHCODE_LENGTH) |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 81 | { |
| 82 | k1 = generateKn(sik, rmcp::const_1); |
| 83 | } |
| 84 | |
| 85 | std::vector<uint8_t> AlgoSHA256::generateHMAC(const uint8_t* input, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 86 | const size_t len) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 87 | { |
| 88 | std::vector<uint8_t> output(SHA256_DIGEST_LENGTH); |
| 89 | unsigned int mdLen = 0; |
| 90 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 91 | if (HMAC(EVP_sha256(), k1.data(), k1.size(), input, len, output.data(), |
| 92 | &mdLen) == NULL) |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 93 | { |
| 94 | throw std::runtime_error("Generating HMAC_SHA256_128 failed"); |
| 95 | } |
| 96 | |
| 97 | // HMAC generates Message Digest to the size of SHA256_DIGEST_LENGTH, the |
| 98 | // AuthCode field length is based on the integrity algorithm. So we are |
| 99 | // interested only in the AuthCode field length of the generated Message |
| 100 | // digest. |
| 101 | output.resize(authCodeLength); |
| 102 | |
| 103 | return output; |
| 104 | } |
| 105 | |
| 106 | bool AlgoSHA256::verifyIntegrityData( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 107 | const std::vector<uint8_t>& packet, const size_t length, |
| 108 | std::vector<uint8_t>::const_iterator integrityData) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 109 | { |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 110 | auto output = generateHMAC( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 111 | packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, length); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 112 | |
| 113 | // Verify if the generated integrity data for the packet and the received |
| 114 | // integrity data matches. |
| 115 | return (std::equal(output.begin(), output.end(), integrityData)); |
| 116 | } |
| 117 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 118 | std::vector<uint8_t> |
| 119 | AlgoSHA256::generateIntegrityData(const std::vector<uint8_t>& packet) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 120 | { |
| 121 | return generateHMAC( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 122 | packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, |
| 123 | packet.size() - message::parser::RMCP_SESSION_HEADER_SIZE); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::vector<uint8_t> AlgoSHA256::generateKn(const std::vector<uint8_t>& sik, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 127 | const rmcp::Const_n& const_n) const |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 128 | { |
| 129 | unsigned int mdLen = 0; |
| 130 | std::vector<uint8_t> Kn(sik.size()); |
| 131 | |
| 132 | // Generated Kn for the integrity algorithm with the additional key keyed |
| 133 | // with SIK. |
| 134 | if (HMAC(EVP_sha256(), sik.data(), sik.size(), const_n.data(), |
| 135 | const_n.size(), Kn.data(), &mdLen) == NULL) |
| 136 | { |
| 137 | throw std::runtime_error("Generating KeyN for integrity " |
| 138 | "algorithm HMAC_SHA256 failed"); |
| 139 | } |
| 140 | return Kn; |
| 141 | } |
| 142 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 143 | } // namespace integrity |
Tom Joseph | 77531db | 2017-01-10 15:44:44 +0530 | [diff] [blame] | 144 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 145 | } // namespace cipher |