Commit 71d9a433 authored by garciay's avatar garciay
Browse files

STF545: Validate ECIES implementation

parent bc3c236f
Loading
Loading
Loading
Loading
+107 −6
Original line number Diff line number Diff line
@@ -143,8 +143,8 @@ module TestCodec_SignedAndEncryptedMessages {
    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 Oct32 v_publicEphemeralKeyX;
    var Oct32 v_publicEphemeralKeyY;
    var Oct16 v_authentication_vector;
    var Oct16 v_encrypted_sym_key;
    var Opaque v_cypheredPayload;
@@ -181,7 +181,7 @@ module TestCodec_SignedAndEncryptedMessages {
                                              );
    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_encrypted_sym_key, v_authentication_vector, v_nonce);
    v_cypheredPayload := f_encryptWithEciesNistp256WithSha256(bit2oct(v_encMsg), v_tsPublicKeyX, v_tsPublicKeyY, v_publicEphemeralKeyX, v_publicEphemeralKeyY, v_encrypted_sym_key, v_authentication_vector, v_nonce);
    v_recipientId := f_HashedId8FromSha256(f_hashWithSha256(bit2oct(v_encMsg))); // IEEE Std 1609.2a-2017 Clause 6.3.34 PKRecipientInfo
    v_encrypted_data := m_etsiTs103097Data_encrypted(
                                                   m_encryptedData(
@@ -192,8 +192,8 @@ module TestCodec_SignedAndEncryptedMessages {
                                                                                                                           m_encryptedDataEncryptionKey_eciesNistP256(
                                                                                                                                                                      m_evciesP256EncryptedKey(
                                                                                                                                                                                               m_eccP256CurvePoint_uncompressed(
                                                                                                                                                                                                                                v_publicEncKeyX, 
                                                                                                                                                                                                                                v_publicEncKeyY
                                                                                                                                                                                                                                v_publicEphemeralKeyX, 
                                                                                                                                                                                                                                v_publicEphemeralKeyY
                                                                                                                                                                                                                                ),
                                                                                                                                                                                               v_encrypted_sym_key, 
                                                                                                                                                                                               v_authentication_vector
@@ -219,6 +219,107 @@ module TestCodec_SignedAndEncryptedMessages {
    }
    setverdict(pass, "Decoding passed.");
    
  } // End of testcase tc_encrypted_message_unsecured
  } // End of testcase tc_encrypted_signed_message
  
  testcase tc_decrypted_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 octetstring v_decrypted_data;
    var HashedId8 v_digest := '0000000000000000'O;

    var template (value) EtsiTs103097Data v_encrypted_data;
    var bitstring v_encMsg;
    var Oct32 v_obuPrivateKey, v_obuPublicKeyX, v_obuPublicKeyY;
    var Oct32 v_tsPrivateKey, v_tsPublicKeyX, v_tsPublicKeyY;
    var Oct32 v_publicEphemeralKeyX;
    var Oct32 v_publicEphemeralKeyY;
    var Oct16 v_authentication_vector;
    var Oct16 v_encrypted_sym_key;
    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_publicEphemeralKeyX, v_publicEphemeralKeyY, v_encrypted_sym_key, v_authentication_vector, v_nonce);
    v_recipientId := f_HashedId8FromSha256(f_hashWithSha256(bit2oct(v_encMsg))); // IEEE Std 1609.2a-2017 Clause 6.3.34 PKRecipientInfo
    v_encrypted_data := m_etsiTs103097Data_encrypted(
                                                     m_encryptedData(
                                                                     {
                                                                       m_recipientInfo_signedDataRecipInfo(
                                                                                                           m_pKRecipientInfo(
                                                                                                                             v_recipientId,
                                                                                                                             m_encryptedDataEncryptionKey_eciesNistP256(
                                                                                                                                                                        m_evciesP256EncryptedKey(
                                                                                                                                                                                                 m_eccP256CurvePoint_uncompressed(
                                                                                                                                                                                                                                  v_publicEphemeralKeyX, 
                                                                                                                                                                                                                                  v_publicEphemeralKeyY
                                                                                                                                                                                                                                  ),
                                                                                                                                                                                                 v_encrypted_sym_key, 
                                                                                                                                                                                                 v_authentication_vector
                                                                                                                                                                                                 ))))
                                                                       },
                                                                     m_SymmetricCiphertext_aes128ccm(
                                                                                                     m_aesCcmCiphertext(
                                                                                                                        v_nonce, 
                                                                                                                        v_cypheredPayload
                                                                                                                        )
                                                                                                     )
                                                                     )
                                                     );
    log("v_encrypted_data = ", v_encrypted_data);
    v_decrypted_data := f_decryptWithEciesNistp256WithSha256(
                                                             valueof(v_encrypted_data).content.encryptedData.ciphertext.aes128ccm.ccmCiphertext, // The encrypted message
                                                             v_tsPrivateKey,
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesNistP256.v.uncompressedP256.x, // The public ephemeral key X-coordinate
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesNistP256.v.uncompressedP256.y, // The public ephemeral key Y-coordinate
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesNistP256.c, // The encrypted symmetric key
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesNistP256.t, // The encrypted symmetric key tag
                                                             valueof(v_encrypted_data).content.encryptedData.ciphertext.aes128ccm.nonce // The AES 128 nonce
                                                             );
  v_encMsg := oct2bit(v_decrypted_data);
    if (decvalue(v_encMsg, v_signed_data_dec) != 0) {
      setverdict(fail, "decvalue failed");
      stop;
    } else {
      log("v_decrypted_data = ", v_signed_data_dec);
      if (not(match(valueof(v_signed_data), v_signed_data_dec))) {
        setverdict(fail, "Encode/decode mismatch");
        stop;
      }
    }
    setverdict(pass, "Full encryption/decryption passed.");
    
  } // End of testcase tc_decrypted_signed_message
  
} // End of module TestCodec_SignedAndEncryptedMessages