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

Adding checksums class for general checksum calculations

parent 3f542e05
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -292,6 +292,50 @@ public:
    return std::string(p_value.begin(), p_value.end());
  }; // End of bytes_to_string

public:

  /*! 
   * \brief Convert a 16-bits unsigned integer into little-endian byte order
   * \param[in] p_value The 16-bits value to convert
   * \return The value represented in little-endian byte order
   */
  uint16_t to_little_endian(const uint16_t p_value);

  /*! 
   * \brief Convert a 32-bits unsigned integer into little-endian byte order
   * \param[in] p_value The 32-bits value to convert
   * \return The value represented in little-endian byte order
   */
  uint32_t to_little_endian(const uint32_t p_value);

  /*! 
   * \brief Convert a 64-bits unsigned integer into little-endian byte order
   * \param[in] p_value The 64-bits value to convert
   * \return The value represented in little-endian byte order
   */
  uint64_t to_little_endian(const uint64_t p_value);

  /*! 
   * \brief Convert a 16-bits unsigned integer into big-endian byte order
   * \param[in] p_value The 16-bits value to convert
   * \return The value represented in big-endian byte order
   */
  uint16_t to_big_endian(const uint16_t p_value);

  /*! 
   * \brief Convert a 32-bits unsigned integer into big-endian byte order
   * \param[in] p_value The 32-bits value to convert
   * \return The value represented in big-endian byte order
   */
  uint32_t to_big_endian(const uint32_t p_value);

  /*! 
   * \brief Convert a 64-bits unsigned integer into big-endian byte order
   * \param[in] p_value The 64-bits value to convert
   * \return The value represented in big-endian byte order
   */
  uint64_t to_big_endian(const uint64_t p_value);

public:
  /*!
   * \brief Convert a string into an integer
+60 −0
Original line number Diff line number Diff line
@@ -90,6 +90,66 @@ std::vector<uint8_t> converter::hexa_to_bytes(const std::string& p_value) {
  return output;
}

uint16_t converter::to_little_endian(const uint16_t p_value) {
  std::size_t len = sizeof(p_value);
  uint16_t target;
  uint8_t* dst = (uint8_t*)&target;
  for (std::size_t i = 0; i < len; ++i) {
      dst[i] = (uint8_t)((p_value >> (i * 8)) & 0xffu);
  }
  return target;
}

uint32_t converter::to_little_endian(const uint32_t p_value) {
  std::size_t len = sizeof(p_value);
  uint32_t target;
  uint8_t* dst = (uint8_t*)&target;
  for (std::size_t i = 0; i < len; ++i) {
      dst[i] = (uint8_t)((p_value >> (i * 8)) & 0xffu);
  }
  return target;
}

uint64_t converter::to_little_endian(const uint64_t p_value) {
  std::size_t len = sizeof(p_value);
  uint64_t target;
  uint8_t* dst = (uint8_t*)&target;
  for (std::size_t i = 0; i < len; ++i) {
    dst[i] = (uint8_t)((p_value >> (i * 8)) & 0xffu);
  }
  return target;
}

uint16_t converter::to_big_endian(const uint16_t p_value) {
  std::size_t len = sizeof(p_value);
  uint16_t target;
  uint8_t *dst = (uint8_t *)&target;
  for (size_t i = 0; i < len; ++i) {
      dst[i] = (uint8_t)((p_value >> ((len - 1 - i) * 8)) & 0xffu);
  }
  return target;
}

uint32_t converter::to_big_endian(const uint32_t p_value) {
  std::size_t len = sizeof(p_value);
  uint32_t target;
  uint8_t *dst = (uint8_t *)&target;
  for (std::size_t i = 0; i < len; ++i) {
      dst[i] = (uint8_t)((p_value >> ((len - 1 - i) * 8)) & 0xffu);
  }
  return target;
}

uint64_t converter::to_big_endian(const uint64_t p_value) {
  std::size_t len = sizeof(p_value);
  uint64_t target;
  uint8_t *dst = (uint8_t *)&target;
  for (std::size_t i = 0; i < len; ++i) {
      dst[i] = (uint8_t)((p_value >> ((len - 1 - i) * 8)) & 0xffu);
  }
  return target;
}

std::string converter::time_to_string(const time_t p_time) {
  struct tm *t = std::localtime(&p_time);
  return time_to_string(*t);
+0 −14
Original line number Diff line number Diff line
@@ -20,20 +20,6 @@
#include <memory>
#include <string>

/**
class Base_Type;
class OCTETSTRING;
class TTCN_Buffer;
class TTCN_Logger;
enum  TTCN_Logger::Severity;
extern void TTCN_error(const char *err_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
extern void TTCN_error_begin(const char *err_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_error_end() __attribute__ ((__noreturn__));
void TTCN_warning(const char *warning_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_warning_begin(const char *warning_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_warning_end();
**/

using namespace std; // Required for isnan()
#include <TTCN3.hh>

+180 −0
Original line number Diff line number Diff line
/*! 
 * \file      checksums.hh
 * \brief     Helper class for checksum (CRC) calculations
 * \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
 */
#pragma once

#include <cstdint>
#include <cstdbool>

namespace LibSecurity__Checksums {

  /*! 
   * \class checksums
   * \brief Provides CRC checksum calculation helpers (CRC8/CRC16/CRC32/CRC64)
   * \remark Encapsulates tables and parameters for common CRC variants
   */
  class checksums {
    /*! \brief Initial seed value used for CRC-8 calculations */
    const uint8_t CRC8_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-8 results */
    const uint8_t CRC8_XOR_VALUE;
    /*! \brief Expected check value for CRC-8 (for validation/tests) */
    const uint8_t CRC8_CHECK;
    /*! \brief Magic check constant for CRC-8 (variant specific) */
    const uint8_t CRC8_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-8 H2F calculations */
    const uint8_t CRC8H2F_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-8 H2F results */
    const uint8_t CRC8H2F_XOR_VALUE;
    /*! \brief Expected check value for CRC-8 H2F (for validation/tests) */
    const uint8_t CRC8H2F_CHECK;
    /*! \brief Magic check constant for CRC-8 H2F (variant specific) */
    const uint8_t CRC8H2F_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-16 calculations */
    const uint16_t CRC16_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-16 results */
    const uint16_t CRC16_XOR_VALUE;
    /*! \brief Expected check value for CRC-16 (for validation/tests) */
    const uint16_t CRC16_CHECK;
    /*! \brief Magic check constant for CRC-16 (variant specific) */
    const uint16_t CRC16_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-16 ARC calculations */
    const uint16_t CRC16ARC_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-16 ARC results */
    const uint16_t CRC16ARC_XOR_VALUE;
    /*! \brief Expected check value for CRC-16 ARC (for validation/tests) */
    const uint16_t CRC16ARC_CHECK;
    /*! \brief Magic check constant for CRC-16 ARC (variant specific) */
    const uint16_t CRC16ARC_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-32 calculations */
    const uint32_t CRC32_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-32 results */
    const uint32_t CRC32_XOR_VALUE;
    /*! \brief Expected check value for CRC-32 (for validation/tests) */
    const uint32_t CRC32_CHECK;
    /*! \brief Magic check constant for CRC-32 (variant specific) */
    const uint32_t CRC32_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-32 P4 calculations */
    const uint32_t CRC32P4_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-32 P4 results */
    const uint32_t CRC32P4_XOR_VALUE;
    /*! \brief Expected check value for CRC-32 P4 (for validation/tests) */
    const uint32_t CRC32P4_CHECK;
    /*! \brief Magic check constant for CRC-32 P4 (variant specific) */
    const uint32_t CRC32P4_MAGIC_CHECK;

    /*! \brief Initial seed value used for CRC-64 calculations */
    const uint64_t CRC64_INITIAL_VALUE;
    /*! \brief Final XOR value applied for CRC-64 results */
    const uint64_t CRC64_XOR_VALUE;
    /*! \brief Expected check value for CRC-64 (for validation/tests) */
    const uint64_t CRC64_CHECK;
    /*! \brief Magic check constant for CRC-64 (variant specific) */
    const uint64_t CRC64_MAGIC_CHECK;

    /*! \brief Lookup table for CRC-8 variant (256 entries) */
    static const uint8_t CRC8_TABLE[256];
    /*! \brief Lookup table for CRC-8 H2F variant (256 entries) */
    static const uint8_t CRC8H2F_TABLE[256];
    /*! \brief Lookup table for CRC-16 variant (256 entries) */
    static const uint16_t CRC16_TABLE[256];
    /*! \brief Lookup table for CRC-16 ARC variant (256 entries) */
    static const uint16_t CRC16ARC_TABLE[256];
    /*! \brief Lookup table for CRC-32 variant (256 entries) */
    static const uint32_t CRC32_TABLE[256];
    /*! \brief Lookup table for CRC-32 P4 variant (256 entries) */
    static const uint32_t CRC32P4_TABLE[256];
    /*! \brief Lookup table for CRC-64 variant (256 entries) */
    static const uint64_t CRC64_TABLE[256];

  public:
    /*! \brief Construct and initialize CRC tables and constants */
    checksums();
    /*! \brief Virtual destructor */
    virtual ~checksums();

    /*! 
     * \brief Calculate CRC-8 for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_8 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 8-bit CRC value
     */
    uint8_t calculate_crc_8(const uint8_t* p_data, const uint32_t p_length, const uint8_t p_start_value_8, const bool p_is_first_call);
  
    /*! 
     * \brief Calculate CRC-8 H2F variant for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] Crc_StartValue8H2F Starting CRC value (seed) for H2F variant
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 8-bit CRC value (H2F variant)
     */
    uint8_t calculate_crc_8_h2f(const uint8_t* p_data, const uint32_t p_length, const uint8_t Crc_StartValue8H2F, const bool p_is_first_call);

    /*! 
     * \brief Calculate CRC-16 for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_16 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 16-bit CRC value
     */
    uint16_t calculate_crc_16(const uint8_t *p_data, const uint32_t p_length, const uint16_t p_start_value_16, const bool p_is_first_call);
  
    /*! 
     * \brief Calculate CRC-16 ARC (commonly used reflected/ARC variant) for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_16 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 16-bit CRC value (ARC variant)
     */
    uint16_t calculate_crc_16_arc(const uint8_t *p_data, const uint32_t p_length, const uint16_t p_start_value_16, const bool p_is_first_call);

    /*! 
     * \brief Calculate CRC-32 for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_32 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 32-bit CRC value
     */
    uint32_t calculate_crc_32(const uint8_t *p_data, const uint32_t p_length, const uint32_t p_start_value_32, const bool p_is_first_call);

    /*! 
     * \brief Calculate CRC-32 P4 variant for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_32 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 32-bit CRC value (P4 variant)
     */
    uint32_t calculate_crc_32_p4(const uint8_t *p_data, const uint32_t p_length, const uint32_t p_start_value_32, const bool p_is_first_call);

    /*! 
     * \brief Calculate CRC-64 for the provided buffer
     * \param[in] p_data Pointer to the input buffer
     * \param[in] p_length Number of bytes in the buffer
     * \param[in] p_start_value_64 Starting CRC value (seed)
     * \param[in] p_is_first_call True when this is the first chunk in a multi-call CRC
     * \return Computed 64-bit CRC value
     */
    uint64_t calculate_crc_64(const uint8_t *p_data, const uint32_t p_length, const uint64_t p_start_value_64, const bool p_is_first_call);

  }; // End of class checksums

} // End of namespace LibSecurity__Checksums
+1 −0
Original line number Diff line number Diff line
sources := \
    src/security_externals.cc \
    src/checksums.cc \
    src/certs_db_record.cc \
    src/certs_db.cc \
    src/certs_cache.cc \
Loading