TestCodec_SignedAndEncryptedMessages.ttcn 8.28 KB
Newer Older
/*
 * @author
 *     
 * @version
 *     1.0
 * @desc
 *     
 * @remark
 *     
 * @see
 *     
 */ 
module TestCodec_SignedAndEncryptedMessages {
    
  // LibCommon
  import from LibCommon_BasicTypesAndValues all;
  import from LibCommon_DataStrings all;
    
  // LibIts
  import from IEEE1609dot2BaseTypes language "ASN.1:1997" all;
  import from IEEE1609dot2 language "ASN.1:1997" all;
  import from EtsiTs103097Module language "ASN.1:1997" all;
    
  // LibItsGeoNetworking
  import from LibItsGeoNetworking_EncdecDeclarations all;
  import from LibItsGeoNetworking_TypesAndValues all;
  import from LibItsGeoNetworking_Templates all;
    
  // LibItsSecurity
  import from LibItsSecurity_EncdecDeclarations all;
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Functions all;
  import from LibItsSecurity_Pixits all;
    
  // TestCodec
  import from TestCodec_TestAndSystem all;
    
  testcase tc_encrypted_signed_message() runs on TCType system TCType {
      
    var template (value) EtsiTs103097Data v_signed_data;
    var EtsiTs103097Data v_signed_data_dec;
    var octetstring v_raw_payload_to_be_signed := 'CAFFEDECA0000001'O;
    var HashedId8 v_digest := '0000000000000000'O;

    var template (value) EtsiTs103097Data v_encryped_data;
    var EtsiTs103097Data v_encrypted_data_dec;
    var bitstring v_encMsg;
    var Oct32 v_obuPrivateKey, v_obuPublicKeyX, v_obuPublicKeyY;
    var Oct32 v_tsPrivateKey, v_tsPublicKeyX, v_tsPublicKeyY;
    var Oct32 v_publicEncKeyX;
    var Oct32 v_publicEncKeyY;
    var Oct16 v_tag;
    var Oct16 v_ephKey;
    var Opaque v_cypheredPayload;
    var Oct12 v_nonce;
    var HashedId8 v_recipientId;

    // Simulate OCU & Test System certificate, OBU and Test system exchange their public key
    f_generate_key_pair_nistp256(v_obuPrivateKey, v_obuPublicKeyX, v_obuPublicKeyY);
    f_generate_key_pair_nistp256(v_tsPrivateKey, v_tsPublicKeyX, v_tsPublicKeyY);

    // The OBU is the sender, the Test System is te receiver

  v_signed_data := m_etsiTs103097Data_signed(
                                              m_signedData(
                                                           sha256,
                                                           m_toBeSignedData(
                                                                            m_signedDataPayload(
                                                                                                m_etsiTs103097Data_unsecured(v_raw_payload_to_be_signed)
                                                                                                ),
                                                                            m_headerInfo_gn(
                                                                                            -,
                                                                                            12345
                                                                                            )
                                                                            ),
                                                           { digest := v_digest },
                                                           m_signature_ecdsaNistP256(
                                                                                     m_ecdsaP256Signature(
                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                     '08B2030104020A0D010C0105C0F80BB1460239348D17405C1A845151D4061200'O
                                                                                                                                     ),
                                                                                                          '2617CF4E6B25097F03F502AD0C6F2F125974700D31A60FD1EF12040E4D8231AB'O
                                                                                                          )
                                                                                     )
                                                           )
                                              );
    log("v_signed_data = ", v_signed_data);
    v_encMsg := encvalue(valueof(v_signed_data));
    v_cypheredPayload := f_encryptWithEciesNistp256WithSha256(bit2oct(v_encMsg), v_tsPublicKeyX, v_tsPublicKeyY, v_publicEncKeyX, v_publicEncKeyY, v_ephKey, v_tag, v_nonce);
    v_recipientId := f_HashedId8FromSha256(f_hashWithSha256(bit2oct(v_encMsg))); // IEEE Std 1609.2a-2017 Clause 6.3.34 PKRecipientInfo
    v_encryped_data := m_etsiTs103097Data_encrypted(
                                                   m_encryptedData(
                                                                   {
                                                                    m_recipientInfo_signedDataRecipInfo(
                                                                                                         m_pKRecipientInfo(
                                                                                                                           v_recipientId,
                                                                                                                           m_encryptedDataEncryptionKey_eciesNistP256(
                                                                                                                                                                      m_evciesP256EncryptedKey(
                                                                                                                                                                                               m_eccP256CurvePoint_uncompressed(
                                                                                                                                                                                                                                v_publicEncKeyX, 
                                                                                                                                                                                                                                v_publicEncKeyY
                                                                                                                                                                                                                                ),
                                                                                                                                                                                               v_ephKey, 
                                                                                                                                                                                               v_tag
                                                                       ))))
                                                                   },
                                                                   m_SymmetricCiphertext_aes128ccm(
                                                                                                   m_aesCcmCiphertext(
                                                                                                                      v_nonce, 
                                                                                                                      v_cypheredPayload
                                                                                                                      )
                                                                                                   )
                                                                  )
                                                   );
    log("v_encryped_data = ", v_encryped_data);
    v_encMsg := encvalue(valueof(v_encryped_data));
    setverdict(pass, "Encoding passed.");
    if (decvalue(v_encMsg, v_encrypted_data_dec) != 0) {
      setverdict(fail);
      stop;
    } else if (not(match(valueof(v_signed_data), v_encrypted_data_dec))) {
      setverdict(fail);
      stop;
    }
    setverdict(pass, "Decoding passed.");
      
  } // End of testcase tc_encrypted_message_unsecured
    
} // End of module TestCodec_SignedAndEncryptedMessages