Commit 6c8737a4 authored by Yann Garcia's avatar Yann Garcia
Browse files

Add SUCI encryption support

parent a805b7ac
Loading
Loading
Loading
Loading
+2 −1
Changes for Makefile: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -133,7 +133,8 @@ $(cc_objects) : $(outdir)/%.o : %.cc
#$(gen_sources):
#	$(TTCN3_DIR)/bin/asn1_compiler $(TTCN3_COMPILER_OPTIONS) -o $(outdir) $(tt_sources)

$(gen_sources): $(outdir)/.generate
# Empty recipe: make re-stats the regenerated sources and recompiles their objects in the same run
$(gen_sources): $(outdir)/.generate ;

$(outdir)/.generate: Makefile $(tt_sources)
	$(TTCN3_DIR)/bin/compiler $(TTCN3_COMPILER_OPTIONS) -o $(outdir) $(tt_sources)
+5 −2
Changes for NAS_ETSI.code-workspace: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -10,10 +10,13 @@
			"path": "../titan-test-system-framework-Release4-onwards"
		},
		{
			"path": "../../tmp/UERANSIM"
			"path": "../open5gs"
		},
		{
			"path": "../../tmp/open5gs"
			"path": "../UERANSIM"
		},
		{
			"path": "../Quectel_QConnectManager_Linux_V1.6.8"
		}
	],
	"settings": {
−35 KiB (6.45 KiB)

File changed.

No diff preview for this file type.

+122 −0
Changes for ccsrc/Externals/Lib_NG_NAS_Functions_ext.cc: 122 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

#include "rijndael.hh"
#include "opc.hh"
#include "ecies.hh"

/**
 * @see https://www.ericsson.com/en/blog/2021/9/authentication-and-key-agreements
@@ -355,5 +356,126 @@ namespace Lib__NG__NAS__Security__Functions {
    return 0;
  }

  /**
   * @desc Helpers to convert TITAN octetstring from/to std::vector<uint8_t>
   */
  static std::vector<uint8_t> os2vector(const OCTETSTRING& p_os) {
    return std::vector<uint8_t>(static_cast<const unsigned char*>(p_os), static_cast<const unsigned char*>(p_os) + p_os.lengthof());
  }

  static OCTETSTRING vector2os(const std::vector<uint8_t>& p_vector) {
    return OCTETSTRING(p_vector.size(), p_vector.data());
  }

  /**
   * @desc Convert the ECIES profile identifier into ecies::profile
   * @return 0 on success, -1 if the profile is neither Profile A nor Profile B
   */
  static int int2profile(const INTEGER& p_profile, ecies::profile& p_ecies_profile) {
    if (p_profile == 1) {
      p_ecies_profile = ecies::profile::profile_a;
    } else if (p_profile == 2) {
      p_ecies_profile = ecies::profile::profile_b;
    } else {
      loggers::get_instance().warning("int2profile: Unsupported ECIES profile: %d", static_cast<int>(p_profile));
      return -1;
    }
    return 0;
  }

  INTEGER fx__ecies__generate__private__key(const INTEGER& p_profile, OCTETSTRING& p_private_key) {
    loggers::get_instance().log_msg(">>> fx__ecies__generate__private__key: p_profile: ", p_profile);

    ecies::profile profile;
    std::vector<uint8_t> private_key;
    if ((int2profile(p_profile, profile) == -1) || (ecies::generate_private_key(profile, private_key) == -1)) {
      p_private_key = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_private_key = vector2os(private_key);

    loggers::get_instance().log_msg("<<< fx__ecies__generate__private__key: p_private_key: ", p_private_key);
    return 0;
  }

  INTEGER fx__ecies__compute__public__key(const INTEGER& p_profile, const OCTETSTRING& p_private_key, OCTETSTRING& p_public_key) {
    loggers::get_instance().log_msg(">>> fx__ecies__compute__public__key: p_profile: ", p_profile);

    ecies::profile profile;
    std::vector<uint8_t> public_key;
    if ((int2profile(p_profile, profile) == -1) || (ecies::compute_public_key(profile, os2vector(p_private_key), public_key) == -1)) {
      p_public_key = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_public_key = vector2os(public_key);

    loggers::get_instance().log_msg("<<< fx__ecies__compute__public__key: p_public_key: ", p_public_key);
    return 0;
  }

  INTEGER fx__ecies__compute__shared__key(const INTEGER& p_profile, const OCTETSTRING& p_private_key, const OCTETSTRING& p_peer_public_key, OCTETSTRING& p_shared_key) {
    loggers::get_instance().log_msg(">>> fx__ecies__compute__shared__key: p_profile: ", p_profile);

    ecies::profile profile;
    std::vector<uint8_t> shared_key;
    if ((int2profile(p_profile, profile) == -1) || (ecies::compute_shared_key(profile, os2vector(p_private_key), os2vector(p_peer_public_key), shared_key) == -1)) {
      p_shared_key = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_shared_key = vector2os(shared_key);

    loggers::get_instance().log_msg("<<< fx__ecies__compute__shared__key: p_shared_key: ", p_shared_key);
    return 0;
  }

  INTEGER fx__ecies__kdf(const OCTETSTRING& p_shared_key, const OCTETSTRING& p_shared_info, OCTETSTRING& p_enc_key, OCTETSTRING& p_icb, OCTETSTRING& p_mac_key) {
    loggers::get_instance().log_msg(">>> fx__ecies__kdf: p_shared_key: ", p_shared_key);
    loggers::get_instance().log_msg(">>> fx__ecies__kdf: p_shared_info: ", p_shared_info);

    std::vector<uint8_t> enc_key, icb, mac_key;
    if (ecies::kdf(os2vector(p_shared_key), os2vector(p_shared_info), enc_key, icb, mac_key) == -1) {
      p_enc_key = OCTETSTRING(0, nullptr);
      p_icb = OCTETSTRING(0, nullptr);
      p_mac_key = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_enc_key = vector2os(enc_key);
    p_icb = vector2os(icb);
    p_mac_key = vector2os(mac_key);

    loggers::get_instance().log_msg("<<< fx__ecies__kdf: p_enc_key: ", p_enc_key);
    loggers::get_instance().log_msg("<<< fx__ecies__kdf: p_icb: ", p_icb);
    loggers::get_instance().log_msg("<<< fx__ecies__kdf: p_mac_key: ", p_mac_key);
    return 0;
  }

  INTEGER fx__ecies__aes__128__ctr(const OCTETSTRING& p_enc_key, const OCTETSTRING& p_icb, const OCTETSTRING& p_input, OCTETSTRING& p_output) {
    loggers::get_instance().log_msg(">>> fx__ecies__aes__128__ctr: p_input: ", p_input);

    std::vector<uint8_t> output;
    if (ecies::aes_128_ctr(os2vector(p_enc_key), os2vector(p_icb), os2vector(p_input), output) == -1) {
      p_output = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_output = vector2os(output);

    loggers::get_instance().log_msg("<<< fx__ecies__aes__128__ctr: p_output: ", p_output);
    return 0;
  }

  INTEGER fx__ecies__compute__mac__tag(const OCTETSTRING& p_mac_key, const OCTETSTRING& p_cipher_text, OCTETSTRING& p_mac_tag) {
    loggers::get_instance().log_msg(">>> fx__ecies__compute__mac__tag: p_cipher_text: ", p_cipher_text);

    std::vector<uint8_t> mac_tag;
    if (ecies::compute_mac_tag(os2vector(p_mac_key), os2vector(p_cipher_text), mac_tag) == -1) {
      p_mac_tag = OCTETSTRING(0, nullptr);
      return -1;
    }
    p_mac_tag = vector2os(mac_tag);

    loggers::get_instance().log_msg("<<< fx__ecies__compute__mac__tag: p_mac_tag: ", p_mac_tag);
    return 0;
  }

} // End of namespace namespace Lib__NG__NAS__Security__Functions {
+1 −1
Changes for ccsrc/Protocols/FiveG_AKA/module.mk: 1 added line, 1 removed line.
Original line number Diff line number Diff line
sources := fiveg_aka.cc rijndael.cc opc.cc
sources := fiveg_aka.cc rijndael.cc opc.cc ecies.cc
includes := .
Loading