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

Add support of BFK as defined in P1609.2.1/D7, January 2022 Clause 9.3.4.1.1 General

parent 232f04f5
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
@@ -2228,4 +2228,58 @@ namespace LibItsSecurity__Functions {

  FLOAT fx__int2ddlon(const INTEGER &p__longitude) { return (float)((float)p__longitude / 1000000.0); }

  BOOLEAN fx__generate__bkf__signing__expension__keys(const INTEGER& p__i, const INTEGER& p__j, const BOOLEAN& p_debug, OCTETSTRING& p__expension__key) {
    loggers::get_instance().log_msg(">>> fx__generate__bkf__signing__expension__keys: p__i: ", p__i);
    loggers::get_instance().log_msg(">>> fx__generate__bkf__signing__expension__keys: p__j: ", p__j);

    if (security_services_its::get_instance().generate_bkf_signing_expension_keys(p__i, p__j, p_debug, p__expension__key) == -1) {
      return FALSE;
    }
    loggers::get_instance().log_msg("<<< fx__generate__bkf__signing__expension__keys: p__expension__key: ", p__expension__key);
    return TRUE;
  }

  BOOLEAN fx__generate__bkf__enc__expension__keys(const INTEGER& p__i, const INTEGER& p__j, const BOOLEAN& p_debug, OCTETSTRING& p__expension__key) {
    loggers::get_instance().log_msg(">>> fx__generate__bkf__enc__expension__keys: p__i: ", p__i);
    loggers::get_instance().log_msg(">>> fx__generate__bkf__enc__expension__keys: p__j: ", p__j);

    if (security_services_its::get_instance().generate_bkf_enc_expension_keys(p__i, p__j, p_debug, p__expension__key) == -1) {
      return FALSE;
    }
    loggers::get_instance().log_msg("<<< fx__generate__bkf__enc__expension__keys: p__expension__key: ", p__expension__key);
    return TRUE;
  }

  BOOLEAN fx__bfk__expend__private__key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__private__key, OCTETSTRING& p__expended__private__key) {
    loggers::get_instance().log_msg(">>> fx__bfk__expend__private__key: p__expansion__key: ", p__expansion__key);
    loggers::get_instance().log_msg(">>> fx__bfk__expend__private__key: p__private__key: ", p__private__key);

    if (security_services_its::get_instance().bfk_expend_private_key(p__expansion__key, p__private__key, p__expended__private__key) == -1) {
      return FALSE;
    }
    loggers::get_instance().log_msg("<<< fx__bfk__expend__private__key: p__expended__private__key: ", p__expended__private__key);
    return TRUE;
  }

  BOOLEAN fx__bfk__expend__public__key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__compressed__public__key, const INTEGER& p__compressed__mode, OCTETSTRING& p__expended__compressed__public__key, INTEGER& p__expended__compressed__mode) {
    loggers::get_instance().log_msg(">>> fx__bfk__expend__public__key: p__expansion__key: ", p__expansion__key);
    loggers::get_instance().log_msg(">>> fx__bfk__expend__public__key: p__compressed__public__key: ", p__compressed__public__key);
    loggers::get_instance().log_msg(">>> fx__bfk__expend__public__key: p__compressed__mode: ", p__compressed__mode);

    if (security_services_its::get_instance().bfk_expend_public_key(p__expansion__key, p__compressed__public__key, p__compressed__mode, p__expended__compressed__public__key, p__expended__compressed__mode) == -1) {
      return FALSE;
    }
    loggers::get_instance().log_msg("<<< fx__bfk__expend__public__key: p__expended__compressed__public__key: ", p__expended__compressed__public__key);
    loggers::get_instance().log_msg("<<< fx__bfk__expend__public__key: p__expended__compressed__mode: ", p__expended__compressed__mode);
    return TRUE;
  }

  BOOLEAN fx__bfk__verify__expended__keys(const OCTETSTRING& p__expended__private__key, const OCTETSTRING& p__expended__compressed__public__key, const INTEGER& p__expended__compressed__mode) {
    loggers::get_instance().log_msg(">>> fx__bfk__verify__expended__keys: p__expended__private__key: ", p__expended__private__key);
    loggers::get_instance().log_msg(">>> fx__bfk__verify__expended__keys: p__expended__compressed__public__key: ", p__expended__compressed__public__key);
    loggers::get_instance().log_msg(">>> fx__bfk__verify__expended__keys: p__expended__compressed__mode: ", p__expended__compressed__mode);

    return security_services_its::get_instance().bfk_verify_expended_keys(p__expended__private__key, p__expended__compressed__public__key, p__expended__compressed__mode);
  }

} // namespace LibItsSecurity__Functions
+338 −7

File changed.

Preview size limit exceeded, changes collapsed.

+36 −2
Original line number Diff line number Diff line
@@ -45,7 +45,8 @@ enum class encryption_algotithm : unsigned char { // TODO Renamed into ecc_encry
  aes_128_ccm,
  aes_256_ccm,
  aes_128_gcm,
  aes_256_gcm
  aes_256_gcm,
  aes_128_ecb
}; // End of class ecc_encryption_algotithm

/*!
@@ -67,6 +68,7 @@ class security_ecc {
  encryption_algotithm _encryption_algotithm;    /*!< Selected encryption algotithm */
  EC_KEY *             _ec_key;                  /*!< EC_KEY reference */
  const EC_GROUP *     _ec_group;                /*!< EC_GROUP reference */
  BIGNUM*              _ec_group_order;          /*!< EC_GROUP order */
  BN_CTX *             _bn_ctx;                  /*!< Pre-alocated memory used to increase OpenSSL processing */
  OCTETSTRING          _pri_key;                 /*!< Private key storage */
  OCTETSTRING          _pub_key_x;               /*!< Public key X-coordinate storage */
@@ -248,6 +250,24 @@ public: //! \publicsection
   */
  int reconstruct_public_keys(const OCTETSTRING &p_cert_to_be_signed, const OCTETSTRING &p_issuer_public_key_x, const OCTETSTRING &p_issuer_public_key_y, OCTETSTRING &p_public_key_x, OCTETSTRING &p_public_key_y, OCTETSTRING &p_public_key_compressed, INTEGER &p_public_key_compressed_mode);

  /*!
   * \fn integer generate_bkf_signing_expension_keys(const encryption_algotithm p_enc_algorithm, const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);
   * \brief Produce a BKF expension key for signing as defined in P1609.2.1/D7 Clause 9.3.3.1 Butterfly expansion functions
   */
  int generate_bkf_signing_expension_keys(const encryption_algotithm p_enc_algorithm, const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);

  /*!
   * \fn integer generate_bkf_enc_expension_keys(const encryption_algotithm p_enc_algorithm, const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);
   * \brief Produce a BKF expension key for encryption as defined in P1609.2.1/D7 Clause 9.3.3.1 Butterfly expansion functions
   */
  int generate_bkf_enc_expension_keys(const encryption_algotithm p_enc_algorithm, const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);

  int bfk_expend_private_key(const OCTETSTRING& p_expansion_key, const OCTETSTRING& p_private_key, OCTETSTRING& p_expended_private_key);

  int bfk_expend_public_key(const OCTETSTRING& p_expansion_key, const OCTETSTRING& p_compressed_public_key, const INTEGER& p_compressed_mode, OCTETSTRING& p_expended_compressed_publi_key, INTEGER& p_expended_compressed_mode);

  boolean bfk_verify_expended_keys(const OCTETSTRING& p_private_key, const OCTETSTRING& p_compressed_public_key, const INTEGER& p_compressed_mode);

  inline const OCTETSTRING &        private_key() const { return _pri_key; };
  inline const OCTETSTRING &        public_key_x() const { return _pub_key_x; };
  inline const OCTETSTRING &        public_key_y() const { return _pub_key_y; };
@@ -297,4 +317,18 @@ private: //! \privatesection

  int compute_digest_sm3(const OCTETSTRING &p_data, const OCTETSTRING &p_entl_a, const OCTETSTRING &p_id, OCTETSTRING &p_digest);

  /*!
   * \fn integer bkf_expension_function(const encryption_algotithm p_enc_algorithm, const unsigned char* p_random_signing_aes_enc_key, const unsigned char* p_expension_key, const size_t p_key_len, unsigned char** p_bin_result);
   * \brief Produce a BKF expension key for either signing or encryption as defined in P1609.2.1/D7 Clause 9.3.3.1 Butterfly expansion functions
   * \param[in] p_enc_algorithm An encryption algorithm Symm
   * \param[in] p_random_signing_aes_enc_key A symmetric encryption algorithm Symm
   * \param[in] p_expension_key A 128 bit key, denoted as kS
   * \param[in] p_key_len The encryption key length
   * \param[out] p_bin_result The 256 bit integer
   * \return The length in bytes of the expension key on success, -1 otherwise
   */
  int bkf_expension_function(const encryption_algotithm p_enc_algorithm, const unsigned char* p_random_signing_aes_enc_key, const unsigned char* p_expension_key, const size_t p_key_len, unsigned char** p_bin_result);

  int compressed_key_to_ec_point(const OCTETSTRING& p_compressed_public_key, const INTEGER& p_compressed_mode, EC_POINT** p_ec_point);

}; // End of class security_ecc
+81 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ using namespace std; // Required for isnan()
security_services_its *security_services_its::instance = nullptr;

security_services_its::security_services_its()
  : _setup_done{false}, _ec_keys_enc(nullptr), _security_cache(new security_cache), _security_db(nullptr), _last_generation_time(0),
  : _setup_done{false}, _ec_keys_enc(nullptr), _bkf_expension_keys(nullptr), _security_cache(new security_cache), _security_db(nullptr), _last_generation_time(0),
    _unknown_certificate(0, nullptr), _requested_certificate(), _latitude(0), _longitude(0), _elevation(0), _geospacial() {
  loggers::get_instance().log(">>> security_services_its::security_services_its");

@@ -74,6 +74,8 @@ int security_services_its::setup(params_its &p_params) { // FIXME Rename this me
    return -1;
  }

  _bkf_expension_keys.reset(new security_ecc(ec_elliptic_curves::nist_p_256)); // FIXME To be ehance if aniother algorithm shall be supported

  return 0;
}

@@ -941,6 +943,84 @@ int security_services_its::sign_tbs_data(const Ieee1609Dot2::ToBeSignedData &p_t
  return 0;
}

int security_services_its::generate_bkf_signing_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key) {
  loggers::get_instance().log_msg(">>> security_services_its::generate_bkf_signing_expension_keys: ", p_i);
  loggers::get_instance().log_msg(">>> security_services_its::generate_bkf_signing_expension_keys: ", p_j);
  
  // Sanity checks
  if (_bkf_expension_keys.get() == nullptr) {
    loggers::get_instance().warning("security_services_its::generate_bkf_signing_expension_keys: Encryption not initialised");
    return -1;
  }
  int ret_code = _bkf_expension_keys.get()->generate_bkf_signing_expension_keys(encryption_algotithm::aes_128_ecb, p_i, p_j, p_debug, p_expension_key);
  loggers::get_instance().log_msg("security_services_its::generate_bkf_signing_expension_keys: p_expension_key=", p_expension_key);

  return ret_code;
}

int security_services_its::generate_bkf_enc_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key) {
  loggers::get_instance().log_msg(">>> security_services_its::generate_bkf_enc_expension_keys: ", p_i);
  loggers::get_instance().log_msg(">>> security_services_its::generate_bkf_enc_expension_keys: ", p_j);
  
  // Sanity checks
  if (_bkf_expension_keys.get() == nullptr) {
    loggers::get_instance().warning("security_services_its::generate_bkf_enc_expension_keys: Encryption not initialised");
    return -1;
  }
  int ret_code = _bkf_expension_keys.get()->generate_bkf_enc_expension_keys(encryption_algotithm::aes_128_ecb, p_i, p_j, p_debug, p_expension_key);
  loggers::get_instance().log_msg("security_services_its::generate_bkf_enc_expension_keys: p_expension_key=", p_expension_key);

  return ret_code;
}

int security_services_its::bfk_expend_private_key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__private__key, OCTETSTRING& p__expended__private__key) {
  loggers::get_instance().log_msg(">>> bfk_expend_private_key: p__expansion__key: ", p__expansion__key);
  loggers::get_instance().log_msg(">>> bfk_expend_private_key: p__private__key: ", p__private__key);
  
  // Sanity checks
  if (_bkf_expension_keys.get() == nullptr) {
    loggers::get_instance().warning("security_services_its::bfk_expend_private_key: Encryption not initialised");
    return -1;
  }
  int ret_code = _bkf_expension_keys.get()->bfk_expend_private_key(p__expansion__key, p__private__key, p__expended__private__key);
  loggers::get_instance().log_msg("security_services_its::bfk_expend_private_key: p__expended__private__key=", p__expended__private__key);

  return ret_code;
}

int security_services_its::bfk_expend_public_key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__compressed__public__key, const INTEGER& p__compressed__mode, OCTETSTRING& p__expended__compressed__public__key, INTEGER& p__expended__compressed__mode) {
  loggers::get_instance().log_msg(">>>security_services_its:: bfk_expend_public_key: p__expansion__key: ", p__expansion__key);
  loggers::get_instance().log_msg(">>> security_services_its::bfk_expend_public_key: p__compressed__public__key: ", p__compressed__public__key);
  loggers::get_instance().log_msg(">>> security_services_its::bfk_expend_public_key: p__compressed__mode: ", p__compressed__mode);
  
  // Sanity checks
  if (_bkf_expension_keys.get() == nullptr) {
    loggers::get_instance().warning("security_services_its::bfk_expend_public_key::bfk_expend_private_key: Encryption not initialised");
    return -1;
  }
  int ret_code = _bkf_expension_keys.get()->bfk_expend_public_key(p__expansion__key, p__compressed__public__key, p__compressed__mode, p__expended__compressed__public__key, p__expended__compressed__mode);
  loggers::get_instance().log_msg("security_services_its::bfk_expend_public_key::bfk_expend_private_key: p__expended__compressed__public__key=", p__expended__compressed__public__key);
  loggers::get_instance().log_msg("security_services_its::bfk_expend_public_key::bfk_expend_private_key: p__expended__compressed__mode=", p__expended__compressed__mode);

  return ret_code;
}

boolean security_services_its::bfk_verify_expended_keys(const OCTETSTRING& p__expended__private__key, const OCTETSTRING& p__expended__compressed__public__key, const INTEGER& p__expended__compressed__mode) {
  loggers::get_instance().log_msg(">>> security_services_its::bfk_verify_expended_keys: p__expended__private__key: ", p__expended__private__key);
  loggers::get_instance().log_msg(">>> security_services_its::bfk_verify_expended_keys: p__expended__compressed__public__key: ", p__expended__compressed__public__key);
  loggers::get_instance().log_msg(">>> security_services_its::bfk_verify_expended_keys: p__expended__compressed__mode: ", p__expended__compressed__mode);
  
  // Sanity checks
  if (_bkf_expension_keys.get() == nullptr) {
    loggers::get_instance().warning("bfk_verify_expended_keys::bfk_expend_private_key: Encryption not initialised");
    return -1;
  }
  boolean ret_code = _bkf_expension_keys.get()->bfk_verify_expended_keys(p__expended__private__key, p__expended__compressed__public__key, p__expended__compressed__mode);
  loggers::get_instance().log("security_services_its::bfk_verify_expended_keys::bfk_expend_private_key: ret_code=%d", ret_code);

  return ret_code;
}

int security_services_its::hash_sha256(const OCTETSTRING &p_data, OCTETSTRING &p_hash_data) {
  loggers::get_instance().log_msg(">>> security_services_its::hash_sha256: ", p_data);

+25 −1
Original line number Diff line number Diff line
@@ -49,7 +49,8 @@ class security_services_its {
  params_its                      _params;
  bool                            _setup_done;
  std::unique_ptr<security_ecc>   _ec_keys_enc;
  std::unique_ptr<security_ecc>   _ec_keys_dec;
//  std::unique_ptr<security_ecc>   _ec_keys_dec;
  std::unique_ptr<security_ecc>   _bkf_expension_keys;
  std::unique_ptr<security_cache> _security_cache;
  std::unique_ptr<security_db>    _security_db;
  unsigned long long              _last_generation_time;
@@ -69,6 +70,7 @@ class security_services_its {
   */
  ~security_services_its() {
    _ec_keys_enc.reset(nullptr);
    _bkf_expension_keys.reset(nullptr);
    _security_db.reset(nullptr);
    _security_cache.reset(nullptr);
    if (instance != NULL) {
@@ -160,6 +162,28 @@ public: /*! \publicsection */

  const int get_protocol_version() const { return ProtocolVersion; };

  /*!
   * \fn integer generate_bkf_signing_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);
   * \brief Produce a BKF expension key for either signing or encryption as defined in P1609.2.1/D7 Clause 9.3.3.1 Butterfly expansion functions
   * \param[in] p_i iINT value in the range 0 < iINT
   * \param[in] p_j jINT value in the range jINT < 232– 1.
   * \param[in] p_debug Set to true if test vectors should be used. For debugging purposes only
   * \param[out] p_bin_result The 256 bit integer
   */
  int generate_bkf_signing_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);

  /*!
   * \fn integer generate_bkf_enc_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);
   * \brief Produce a BKF expension key for either signing or encryption as defined in P1609.2.1/D7 Clause 9.3.3.1 Butterfly expansion functions
   */
  int generate_bkf_enc_expension_keys(const INTEGER& p_i, const INTEGER& p_j, const BOOLEAN& p_debug, OCTETSTRING& p_expension_key);

  int bfk_expend_private_key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__private__key, OCTETSTRING& p__expended__private__key);

  int bfk_expend_public_key(const OCTETSTRING& p__expansion__key, const OCTETSTRING& p__compressed__public__key, const INTEGER& p__compressed__mode, OCTETSTRING& p__expended__compressed__public__key, INTEGER& p__expended__compressed__mode);

  boolean bfk_verify_expended_keys(const OCTETSTRING& p__expended__private__key, const OCTETSTRING& p__expended__compressed__public__key, const INTEGER& p__expended__compressed__mode);

  /*!
   * \fn int fill_public_key_vectors(const ec_elliptic_curves p_elliptic_curve, const Ieee1609Dot2BaseTypes::EccP256CurvePoint &p_ecc_point, OCTETSTRING &p_public_comp_key, OCTETSTRING &p_public_key_x, OCTETSTRING &p_public_key_y);
   * \brief Initialize public key vectors, based on 256 bits curve
Loading