/******************************************************************************
 * Copyright (c) 2000-2025 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 ******************************************************************************/
//
//  File:               TCCSecurity_Functions.ttcn
//  Description:        TCC Useful Functions: Security Functions
//  Rev:                <RnXnn>
//  Prodnr:             CNL 113 472
//  Updated:            2013-02-08
//  Contact:            http://ttcn.ericsson.se
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//  Module: TCCSecurity_Functions
// 
//  Purpose:
//    This module supports security based operations e.g. fingerprint
//    generation
// 
//  Module Parameters:
//      - 
// 
//  Module depends on:
//    -
// 
//  Current Owner:
//    EFLOATT
// 
//  Last Review Date:
//    -
// 
///////////////////////////////////////////////////////////////////////////////

//This file is modified to eliminate all encryption functionality
//The external functions are replaced by internal functions returning with dummy values without real meaning
//This way the load generating and the algoritm searching maximal cps remained the original
//This module cannot produce real security related values

module TCCSecurity_Functions {

///////////////////////////////////////////////////////////////////////////////
//  Record Type: TCCSecurity_Keys
// 
//  Purpose:
//      Stores the RES, CK, IK and AK keys generated by the f functions
//
///////////////////////////////////////////////////////////////////////////////
type record TCCSecurity_Keys {
  octetstring RES,
  octetstring CK,
  octetstring IK,
  octetstring AK
};

///////////////////////////////////////////////////////////////////////////////
//  General Types: OCT_2, OCT_6, OCT_8, OCT_16
// 
//  Purpose:
//     using by function input parameter types
//
///////////////////////////////////////////////////////////////////////////////
type octetstring OCT_2 length(2);
type octetstring OCT_4 length(4);
type octetstring OCT_6 length(6);
type octetstring OCT_8 length(8);
type octetstring OCT_16 length(16);
type octetstring OCT_64 length(64);
type bitstring BIT_1 length(1);
type integer INT_5 (0..31);
type bitstring BIT_1_65504 length(1..65504);
type bitstring BIT_32 length(32);

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculateDigestResponse
// 
//  Purpose:
//    Calculate digest response
// 
//  Parameters:
//      nonce - *in* *charstring* -   a server-specified data string which may 
//  `                                 be uniquely generated each time a 401 
//                                    response is made
//      cnonce - *in* *charstring* -  client nonce
//      user - *in* *charstring* -    user name
//      realm - *in* *charstring* -   user realm
//      passwd - *in* *charstring* -  user password
//      alg - *in* *charstring* -     a string indicating a pair of algorithms
//                                    used to produce the digest and a checksum
//      nonceCount - *in* *charstring* - nonce count (8 hex digits)
//      method - *in* *charstring* -  method (from the request)
//      qop - *in* *charstring* -     qop-value: "", "auth", "auth-int"
//      URI - *in* *charstring* -     digest URI
//      HEntity - *in* *charstring* - H(entity body) if qop="auth-int"
//
//  Return Value:
//    charstring - digest response
// 
//  Errors:
//    - 
// 
//  Detailed description:
//    Support HTTP authentication (detailed description in RFC 2617) using
//    uses one-way hash (md5) specified in RFC 1321. 
//    When a request arrives to server for an access-protected object, it
//    responds an "401 Unauthorized" status code and a WWW-Authenticate
//    header (encapsulate nonce and other necessary parameters). The client  
//    is expected to retry the request, passing an Authorization header with 
//    response field calculated with f_calculateDigestResponse().
//
//    Overview: http://en.wikipedia.org/wiki/Digest_access_authentication
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculateDigestResponse(
  charstring nonce,
  charstring cnonce,
  charstring user,
  charstring realm,
  charstring passwd,
  charstring alg,
  charstring nonceCount,
  charstring method,
  charstring qop,
  charstring URI,
  charstring HEntity) return charstring{
    return "Dummy_f_calculateDigestResponse_retval";
  }

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculateDigestHA1
// 
//  Purpose:
//    Calculate digest H(A1) hash
// 
//  Parameters:
//      nonce - *in* *charstring* -   a server-specified data string which may 
//  `                                 be uniquely generated each time a 401 
//                                    response is made
//      cnonce - *in* *charstring* -  client nonce
//      user - *in* *charstring* -    user name
//      realm - *in* *charstring* -   user realm
//      passwd - *in* *charstring* -  user password
//      alg - *in* *charstring* -     a string indicating a pair of algorithms
//                                    used to produce the digest and a checksum
//
//  Return Value:
//    charstring - digest response
// 
//  Errors:
//    - 
// 
//  Detailed description:
//    Overview: http://en.wikipedia.org/wiki/Digest_access_authentication
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculateDigestHA1(
  charstring nonce,
  charstring cnonce,
  charstring user,
  charstring realm,
  charstring passwd,
  charstring alg) return charstring{
    return "Dummy_f_calculateDigestHA1_retval";
  };

///////////////////////////////////////////////////////////////////////////////
//  Function: f__TCCSecurity__hex2char
// 
//  Purpose:
//    Computes the 8 bit ASCII representation of an hexstring
//
//  Parameters:
//      hex - *in* *charstring* -  input value
//      str - *out* *charstring* - output value
// 
//  Return Value:
//      -
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_TCCSecurity_hex2char(
  in charstring hex,
  out charstring str){
    str := oct2char(str2oct(hex)); //Dummy
  };

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculateMD5
// 
//  Purpose:
//    Compute MD5 hash value
//
//  Parameters:
//      pszHashInput - *in* *charstring* -  input value to compute MD5 hash
// 
//  Return Value:
//      charstring - hexa hash value of input
//
//  Errors:
//      - 
// 
//  Detailed description:
//      - MD5() is an openssl specific function, should be found under openssl/md5.h
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculateMD5(in charstring pszHashInput) return charstring{
  return "Dummy_"& pszHashInput;
}; 

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f1
// 
//  Purpose:
//  Computes network authentication code MAC-A from key K, random
//  challenge RAND, sequence number SQN and authentication management
//  field AMF.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
//      pl_sqn - *in* *octetstring* -  sequence number
//      pl_amf - *in* *octetstring* -  authentication management field
// 
//  Return Value:
//      octetstring - network authentication code MAC-A
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f1(in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand, in octetstring pl_sqn, in octetstring pl_amf) 
return octetstring {
  return pl_OP; //dummy
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f2
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns response RES.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      octetstring - response RES
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f2 (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) return octetstring {
  return pl_OP;
};


///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f3
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns confidentiality key CK.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      octetstring - confidentiality key CK
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f3 (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) 
return octetstring {
  return pl_OP;
};


///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f4
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns integrity key IK.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      octetstring - integrity key IK
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f4 (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) 
return octetstring {
  return pl_OP;
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f2345
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns AK, CK, RES
//
//  Parameters:
//      pl__OP - *in* *octetstring* - the operator variant algorithm configuration field
//      pl__k - *in* *octetstring* -  key 
//      pl__rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      TCCSecurity_Keys typed value
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f2345 (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) 
return TCCSecurity_Keys {
  var TCCSecurity_Keys k := {'00'O,'01'O,'02'O,'03'O}
  return k; //dummy
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f5
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns anonymity key AK.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      octetstring - anonymity key AK
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f5 (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) 
return octetstring {
  return pl_OP;
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f1star
// 
//  Purpose:
//  Computes resynch authentication code MAC-S from key K, random
//  challenge RAND, sequence number SQN and authentication management
//  field AMF.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
//      pl_sqn - *in* *octetstring* -  sequence number
//      pl_amf - *in* *octetstring* -  authentication management field
// 
//  Return Value:
//      octetstring - resynch authentication code MAC-S
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f1star (
in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand, in octetstring pl_sqn, in octetstring pl_amf) 
return octetstring{
  return pl_OP; //dummy
};


///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_f5star
// 
//  Purpose:
//  Takes key K and random challenge RAND, and returns resynch
//  anonymity key AK.
//
//  Parameters:
//      pl_k - *in* *octetstring* -  key 
//      pl_rand - *in* *octetstring* -  random challenge
// 
//  Return Value:
//      octetstring - resynch anonymity key AK
//
//  Errors:
//      - 
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_f5star (in octetstring pl_OP, in octetstring pl_k, in octetstring pl_rand) 
return octetstring {
  return pl_OP;
};


///////////////////////////////////////////////////////////////////////////////
//  Function: f_EAPSIM_A3A8
// 
//  Purpose:
//  Takes key Ki and random challenge RAND, and returns Kc and SRES keys
//
//  Parameters:
//      pl__Ki - *in* *OCT_16* - the shared key
//      pl__rand - *in* *octetstring* -  random challenge
//      pl__sres - *out* *octetstring* -  authentication result param
//      pl__Kc - *out* *octetstring* - encryption key
// 
//  Return Value:
//      -
//
//  Errors:
//     -
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_EAPSIM_A3A8(in OCT_16 Ki, OCT_16 rand, out octetstring sres, out octetstring Kc) {
  sres := '0102030405060708090A0B0C0D0E0F00'O;
  Kc := '0304'O;
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_IMSAKA_calculateAUTN
// 
//  Purpose:
//  Computes authentication token AUTN from anonymity key AK, random
//  challenge RAND, sequence number SQN and authentication management
//  field AMF.
//
//  Parameters:
//      pl__SQN - *in* *OCT_6* - sequence number
//      pl__AK - *in* *OCT_6* -  anonymity key
//      pl__AMF - *in* *OCT_2* - authentication management field
//      pl__MAC - *in* *OCT_8* - encryption key
// 
//  Return Value:
//      octetstring - Authentication token - AUTN
//
//  Errors:
//      -
// 
//  Detailed description:
//      -
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_IMSAKA_calculateAUTN(in OCT_6 SQN, in OCT_6 AK, in OCT_2 AMF, in OCT_8 MAC) 
return octetstring {
  return '00'O
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculateHMACMD5
// 
//  Purpose:
//      Calculate the 128 bit HMAC MD5 value of a message with specified 64 bit key.
//
//  Parameters:
//      msg - *in* *octetstring* - message to be hashed
//      key - *in* *OCT_64*      - 64 bit key of the hash function
// 
//  Return Value:
//      octetstring - Hash value (16 octet)
//
//  Errors:
//      -
// 
//  Detailed description:
//      - key should be 64 bit long, output is 128 bit long
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculateHMACMD5(in octetstring msg, in OCT_64 key) 
return octetstring {
  return '01'O
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculate_HMAC_MD5
// 
//  Purpose:
//      Calculate the HMAC MD5 value of a message with specified key.
//
//  Parameters:
//      pl_key - *in* *octetstring*   - key of the hash function
//      pl_input - *in* *octetstring* - message to be hashed
//      pl_length - *in* *integer*    - length of the output hash value (should be 16 in most of the cases)
// 
//  Return Value:
//      octetstring - Hash value
//
//  Errors:
//      -
// 
//  Detailed description:
//      - key can be any length
//      - the length of output can be set in pl_length
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function  f_calculate_HMAC_MD5(in octetstring pl_key, in octetstring pl_input, in integer pl_length) 
return octetstring {
  return '01'O
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculate_HMAC_SHA1
// 
//  Purpose:
//      Calculate the HMAC SHA1 value of a message with specified key.
//
//  Parameters:
//      pl_key - *in* *octetstring*   - key of the hash function
//      pl_input - *in* *octetstring* - message to be hashed
//      pl_length - *in* *integer*    - length of the output hash value (should be 16 in most of the cases)
// 
//  Return Value:
//      octetstring - Hash value
//
//  Errors:
//      -
// 
//  Detailed description:
//      - key can be any length
//      - the length of output can be set in pl_length
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function  f_calculate_HMAC_SHA1(in octetstring pl_key, in octetstring pl_input, in integer pl_length) 
return octetstring {
  return '01'O
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculate_HMAC_SHA256
// 
//  Purpose:
//      Calculate the HMAC SHA256 value of a message with specified key.
//
//  Parameters:
//      pl_key - *in* *octetstring*   - key of the hash function
//      pl_input - *in* *octetstring* - message to be hashed
//      pl_length - *in* *integer*    - length of the output hash value (should be 32 in most of the cases)
// 
//  Return Value:
//      octetstring - Hash value
//
//  Errors:
//      -
// 
//  Detailed description:
//      - key can be any length
//      - the length of output can be set in pl_length
// 
///////////////////////////////////////////////////////////////////////////////
//external 
function  f_calculate_HMAC_SHA256(in octetstring pl_key, in octetstring pl_input, in integer pl_length) 
return octetstring {
  return '01'O
};

///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculate128_EEA3
// 
//  Purpose: Encrypt message using the confidentiality algorithm 128-EEA3
// 
//  Parameters: 
//          key          - *in* *OCT_16*   - confidentiality key
//          count        - *in* *OCT_4*    - counter
//          bearer       - *in* *INT_5*    - bearer identity
//          direction    - *in* *BIT_1*    - direction of transmission
//          msg          - *in* *BIT_1_65504* - input bit stream (message to be encrypted)
//
//  Return Value:
//        BIT_1_65504 - output bit stream (encrypted message)
//
//  Errors:
//      -
//
//  Detailed description:
//      -
//
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculate128_EEA3
(
  in OCT_16 key,
  in OCT_4 count,
  in INT_5 bearer,
  in BIT_1 direction,
  in BIT_1_65504 msg
) return BIT_1_65504 {
  return '0101'B
}


///////////////////////////////////////////////////////////////////////////////
//  Function: f_calculate128_EIA3
// 
//  Purpose: Calculate 32 bit message authentication code (MAC)
//           using integrity algorithm 128-EIA3 
// 
//  Parameters: 
//          key          - *in* *OCT_16*   - integrity key key
//          count        - *in* *OCT_4*    - counter
//          bearer       - *in* *INT_5*    - bearer identity
//          direction    - *in* *BIT_1*    - direction of transmission
//          msg          - *in* *BIT_1_65504* - input bit stream (the message)
//
//  Return Value:
//         BIT_32 - message authentication code (MAC)
//
//  Errors:
//      -
//
//  Detailed description:
//      -
//
///////////////////////////////////////////////////////////////////////////////
//external 
function f_calculate128_EIA3
(
  in OCT_16 key,
  in OCT_4 count,
  in INT_5 bearer,
  in BIT_1 direction,
  in BIT_1_65504 msg
) return BIT_32 {
  return '00001111000011110000111100001111'B;
}

} with {extension "version <RnXnn>"}
