Commit f8843ac9 authored by Yann Garcia's avatar Yann Garcia
Browse files

Enhance the hmac::generate method

parent c0d15f46
Loading
Loading
Loading
Loading
+297 −273
Original line number Diff line number Diff line
@@ -10,6 +10,10 @@
 */
#pragma once

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cstdarg>
#include <ctime>
#include <map>
@@ -166,6 +170,14 @@ public: //! \publicsection
   * \inline
   */
  inline void set_stop_time(std::string& p_time_key, float &p_time);
  /*!
   * \fn void log_rijndael_state(const std::string p_label, const uint8_t p_state[4][4]);
   * \brief Display the state table of the Rijndael encryption algorithm as defined in ETSI TS 135 207 V16.0.0 (2020-08) Clause 3.2 Format
   * \param[in] p_time_key The timer identifier provided while calling \see loggers::set_start_time method
   * \param[out] p_time    The execution time measured in milliseconds
   * \inline
   */
  inline void log_rijndael_state(const std::string p_label, const uint8_t p_state[4][4]);
}; // End of class loggers

void loggers::log_to_hexa(const char *p_prompt, const TTCN_Buffer &buffer) {
@@ -271,3 +283,15 @@ void loggers::log_time_exec(const char *p_fmt, ...) {
  va_end(args);
  TTCN_Logger::end_event();
}

void loggers::log_rijndael_state(const std::string p_label, const uint8_t p_state[4][4]) {
  std::ostringstream oss;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      oss << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(p_state[j][i]);
    } // End of 'for' statement
    oss << " ";
  } // End of 'for' statement
  loggers::get_instance().log("%s:%s", p_label.c_str(), oss.str().c_str());
}
+3 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include <openssl/hmac.h>

class OCTETSTRING; //! TITAN forward declaration
class BOOLEAN; //! TITAN forward declaration

/*!
 * \enum Supported hash algorithms
@@ -55,7 +56,7 @@ public:
   * \param[out] p_hmac The HMAC value based of the provided data
   * \return 0 on success, -1 otherwise
   */
  int generate(const OCTETSTRING& p_buffer, const OCTETSTRING& p_secret_key, OCTETSTRING& p_hmac);
  int generate(const OCTETSTRING& p_buffer, const OCTETSTRING& p_secret_key, OCTETSTRING& p_hmac, const BOOLEAN& p_resize = true);

  /*!
   * \fn int generate(const uint8_t* p_buffer, const size_t p_buffer_length, const uint8_t* p_secret_key, const size_t p_secret_key_length,
@@ -64,6 +65,6 @@ public:
   * p_hmac The HMAC value based of the provided data \return 0 on success, -1 otherwise
   */
  int generate(const uint8_t *p_buffer, const size_t p_buffer_length, const uint8_t *p_secret_key, const size_t p_secret_key_length,
               OCTETSTRING &p_hmac);
               OCTETSTRING &p_hmac, const BOOLEAN& p_resize = true);

}; // End of class hmac
+6 −4
Original line number Diff line number Diff line
@@ -14,18 +14,18 @@

#include "loggers.hh"

int hmac::generate(const OCTETSTRING& p_buffer, const OCTETSTRING& p_secret_key, OCTETSTRING& p_hmac) {
int hmac::generate(const OCTETSTRING& p_buffer, const OCTETSTRING& p_secret_key, OCTETSTRING& p_hmac, const BOOLEAN& p_resize) {
  // Sanity check
  if (p_buffer.lengthof() == 0) {
    return -1;
  }

  return generate(static_cast<const uint8_t *>(p_buffer), p_buffer.lengthof(), static_cast<const uint8_t *>(p_secret_key), p_secret_key.lengthof(),
                  p_hmac);
                  p_hmac, p_resize);
}

int hmac::generate(const uint8_t *p_buffer, const size_t p_buffer_length, const uint8_t *p_secret_key, const size_t p_secret_key_length,
                   OCTETSTRING &p_hmac) {
                   OCTETSTRING &p_hmac, const BOOLEAN& p_resize) {
  // Sanity check
  if ((p_buffer == nullptr) || (p_secret_key == nullptr)) {
    return -1;
@@ -46,8 +46,10 @@ int hmac::generate(const uint8_t *p_buffer, const size_t p_buffer_length, const
  ::HMAC_Final(_ctx, (uint8_t *)static_cast<const uint8_t *>(p_hmac), &length);
  loggers::get_instance().log_to_hexa("hmac::generate: ", (uint8_t *)static_cast<const uint8_t *>(p_hmac), length);
  // Resize the hmac
  if (_hash_algorithms == hash_algorithms::sha_256) {
  if ((_hash_algorithms == hash_algorithms::sha_256) && (p_resize == true)) {
    p_hmac = OCTETSTRING(16, static_cast<const uint8_t *>(p_hmac));
  } else {
    p_hmac = OCTETSTRING(length, static_cast<const uint8_t *>(p_hmac));
  } // FIXME Check length for the other hash algorithm

  return 0;