Commit 88c2489e authored by garciay's avatar garciay
Browse files

Replace usage of std::vector<unsigned char> by OCTETSTRING

parent ad3ad0bb
Loading
Loading
Loading
Loading
+263 −317

File changed.

Preview size limit exceeded, changes collapsed.

+115 −132

File changed.

Preview size limit exceeded, changes collapsed.

+5 −5
Original line number Original line Diff line number Diff line
@@ -71,15 +71,15 @@ public: /*! \publicsection */


  int build_path(const std::string& p_root_directory);
  int build_path(const std::string& p_root_directory);


  int load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s);
  int load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s);
  //int load_certificate(std::unique_ptr<security_db_record> >& p_certificate, std::map<const std::vector<unsigned char>, const std::string&>& p_hashed_id8s);
  //int load_certificate(std::unique_ptr<security_db_record> >& p_certificate, std::map<const OCTETSTRING, const std::string&>& p_hashed_id8s);
  int save_certificate(const security_db_record& p_certificate);
  int save_certificate(const security_db_record& p_certificate);


private:
private:
  int retrieve_certificates_list(std::set<std::experimental::filesystem::path>& p_files);
  int retrieve_certificates_list(std::set<std::experimental::filesystem::path>& p_files);


  int build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s);
  int build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s);
  
  
  void fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP256CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y);
  void fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP256CurvePoint& p_ecc_point, OCTETSTRING& p_public_comp_key, OCTETSTRING& p_public_key_x, OCTETSTRING& p_public_key_y);
  void fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP384CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y);
  void fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP384CurvePoint& p_ecc_point, OCTETSTRING& p_public_comp_key, OCTETSTRING& p_public_key_x, OCTETSTRING& p_public_key_y);
}; // End of class certificates_loader
}; // End of class certificates_loader
+49 −0
Original line number Original line Diff line number Diff line
/*!
 * \file      hmac.cc
 * \brief     Source file for HMAC helper methods.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#include <TTCN3.hh>

#include "hmac.hh"

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

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

int hmac::generate(const unsigned char* p_buffer, const size_t p_buffer_length, const unsigned char* p_secret_key, const size_t p_secret_key_length, OCTETSTRING& p_hmac) {
  // Sanity check
  if ((p_buffer == nullptr) || (p_secret_key == nullptr)) {
    return -1;
  }
  ::HMAC_CTX_reset(_ctx);

  p_hmac = int2oct(0, EVP_MAX_MD_SIZE);
  if (_hash_algorithms == hash_algorithms::sha_256) {
    ::HMAC_Init_ex(_ctx, (const void*)p_secret_key, (long unsigned int)p_secret_key_length, EVP_sha256(), NULL);
  } else if (_hash_algorithms == hash_algorithms::sha_384) {
    ::HMAC_Init_ex(_ctx, (const void*)p_secret_key, (long unsigned int)p_secret_key_length, EVP_sha384(), NULL);
  } else { // TODO To be continued
    return -1;
  }
  // Compute the hash value
  ::HMAC_Update(_ctx, p_buffer, p_buffer_length);
  unsigned int length = p_hmac.lengthof();
  ::HMAC_Final(_ctx, (unsigned char*)static_cast<const unsigned char*>(p_hmac), &length);
  // Resize the hmac
  if (_hash_algorithms == hash_algorithms::sha_256) {
    p_hmac = OCTETSTRING(16, static_cast<const unsigned char*>(p_hmac));
  } // FIXME Check length for the other hash algorithm

  return 0;
}
+9 −42
Original line number Original line Diff line number Diff line
@@ -13,7 +13,8 @@
#include <vector>
#include <vector>


#include <openssl/hmac.h>
#include <openssl/hmac.h>
//#include <openssl/objects.h>

class OCTETSTRING; //! TITAN forward declaration


/*!
/*!
 * \enum Supported hash algorithms
 * \enum Supported hash algorithms
@@ -23,47 +24,37 @@ enum class hash_algorithms: unsigned char {
  sha_384  /*!< HMAC with SHA-384 */
  sha_384  /*!< HMAC with SHA-384 */
}; // End of class hash_algorithms
}; // End of class hash_algorithms



/*!
/*!
 * \class hmac
 * \class hmac
 * \brief  This class provides description of HMAC helper methods
 * \brief  This class provides description of HMAC helper methods
 */
 */
class hmac {
class hmac {
  HMAC_CTX *_ctx; //! HMAC context
  HMAC_CTX *_ctx; //! HMAC context
  hash_algorithms _hash_algorithms;
  hash_algorithms _hash_algorithms; //! HMAC hash algorithm to use
public:
public:
  /*!
  /*!
   * \brief Default constructor
   * \brief Default constructor
   *        Create a new instance of the hmac class
   *        Create a new instance of the hmac class
   * \param[in] p_hash_algorithms The hash algorithm to be used to compute the HMAC
   * \param[in] p_hash_algorithms The hash algorithm to be used to compute the HMAC. Default: sha_256
   */
   */
  hmac(const hash_algorithms p_hash_algorithms): _ctx{nullptr}, _hash_algorithms(p_hash_algorithms) { _ctx = ::HMAC_CTX_new(); };
  hmac(const hash_algorithms p_hash_algorithms = hash_algorithms::sha_256): _ctx{::HMAC_CTX_new()}, _hash_algorithms(p_hash_algorithms) { };
  /*!
  /*!
   * \brief Default destructor
   * \brief Default destructor
   */
   */
  virtual ~hmac() { if (_ctx == nullptr) { ::HMAC_CTX_free(_ctx); }; };
  virtual ~hmac() { if (_ctx == nullptr) { ::HMAC_CTX_free(_ctx); }; };


  /*!
  /*!
   * \inline
   * \fn int generate(const OCTETSTRING p_buffer, const OCTETSTRING p_secret_key, OCTETSTRING& p_hmac);
   * \fn int generate(const std::vector<unsigned char> p_buffer, const std::vector<unsigned char> p_secret_key, std::vector<unsigned char>& p_hmac);
   * \brief Generate the HMAC of data using a secret key
   * \brief Generate the HMAC of data using a secret key
   * \Param[in] p_buffer The data tobe hashed
   * \Param[in] p_buffer The data tobe hashed
   * \param[in] p_secret_key The secret key to be used to generate the HMAC
   * \param[in] p_secret_key The secret key to be used to generate the HMAC
   * \param[out] p_hmac The HMAC value based of the provided data
   * \param[out] p_hmac The HMAC value based of the provided data
   * \return 0 on success, -1 otherwise
   * \return 0 on success, -1 otherwise
   */
   */
  inline int generate(const std::vector<unsigned char> p_buffer, const std::vector<unsigned char> p_secret_key, std::vector<unsigned char>& p_hmac) {
  int generate(const OCTETSTRING p_buffer, const OCTETSTRING p_secret_key, OCTETSTRING& p_hmac);
    // Sanity check
    if (p_buffer.size() == 0) {
      return -1;
    }

    return generate(p_buffer.data(), p_buffer.size(), p_secret_key.data(), p_secret_key.size(), p_hmac);
  };
  
  
  /*!
  /*!
   * \inline
   * \fn int generate(const unsigned char* p_buffer, const size_t p_buffer_length, const unsigned char* p_secret_key, const size_t p_secret_key_length, OCTETSTRING& p_hmac);
   * \fn int generate(const unsigned char *p_buffer, const size_t p_buffer_length, const unsigned char *p_secret_key, const size_t p_secret_key_length, std::vector<unsigned char>& p_hmac);
   * \brief Generate the HMAC of data using a secret key
   * \brief Generate the HMAC of data using a secret key
   * \param[in] p_buffer The data to be hashed
   * \param[in] p_buffer The data to be hashed
   * \param[in] p_buffer_length The size of the data
   * \param[in] p_buffer_length The size of the data
@@ -72,30 +63,6 @@ public:
   * \param[out] p_hmac The HMAC value based of the provided data
   * \param[out] p_hmac The HMAC value based of the provided data
   * \return 0 on success, -1 otherwise
   * \return 0 on success, -1 otherwise
   */
   */
  inline int generate(const unsigned char *p_buffer, const size_t p_buffer_length, const unsigned char *p_secret_key, const size_t p_secret_key_length, std::vector<unsigned char>& p_hmac) {
  int generate(const unsigned char* p_buffer, const size_t p_buffer_length, const unsigned char* p_secret_key, const size_t p_secret_key_length, OCTETSTRING& p_hmac);
    // Sanity check
    if ((p_buffer == nullptr) || (p_secret_key == nullptr)) {
      return -1;
    }
    ::HMAC_CTX_reset(_ctx);

    p_hmac.resize(EVP_MAX_MD_SIZE);
    if (_hash_algorithms == hash_algorithms::sha_256) {
      ::HMAC_Init_ex(_ctx, (const void*)p_secret_key, (long unsigned int)p_secret_key_length, EVP_sha256(), NULL);
    } else if (_hash_algorithms == hash_algorithms::sha_384) {
      ::HMAC_Init_ex(_ctx, (const void*)p_secret_key, (long unsigned int)p_secret_key_length, EVP_sha384(), NULL);
    } else { // TODO To be continued
      return -1;
    }
    // Compute the hash value
    ::HMAC_Update(_ctx, p_buffer, p_buffer_length);
    unsigned int length = p_hmac.size();
    ::HMAC_Final(_ctx, static_cast<unsigned char*>(p_hmac.data()), &length);
    // Resize the hmac
    if (_hash_algorithms == hash_algorithms::sha_256) {
      p_hmac.resize(16);
    } // FIXME Check length for the other hash algorithm


    return 0;
  };
}; // End of class hmac
}; // End of class hmac
Loading