Commit 019f1b7f authored by garciay's avatar garciay
Browse files

STF545: Validate full support of ECIES encryption

parent f8c50b98
Loading
Loading
Loading
Loading
+33 −33
Original line number Diff line number Diff line
@@ -11859,7 +11859,7 @@ module ItsGeoNetworking_TpFunctions {
                    f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
                }
                
                log("*** " & testcasename() & ": GAC packet passed to Upper layer \***");
                log("*** " & testcasename() & ": GAC packet passed to Upper layer ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
                
                // Postamble
+2 −2
Original line number Diff line number Diff line
@@ -6848,7 +6848,7 @@ module ItsSecurity_TestCases {
                    )))))) -> value v_geoNwInd {
                        tc_ac.stop;
                        
                        if (f_decrypt(f_getSecuredMessage(v_geoNwInd.msgIn), v_decryptedMsg) == false) {
                        if (f_decrypt(vc_encryptPrivateKey, f_getSecuredMessage(v_geoNwInd.msgIn), v_decryptedMsg) == false) {
                            log("*** " & testcasename() & ": FAIL: Unable to process encryption data ***");
                            f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
                        } else if (match(v_decryptedMsg, mw_etsiTs103097Data) == false) {
@@ -6937,7 +6937,7 @@ module ItsSecurity_TestCases {
                    )))))) -> value v_geoNwInd {
                        tc_ac.stop;
                        
                        if (f_decrypt(f_getSecuredMessage(v_geoNwInd.msgIn), v_decryptedMsg) == false) {
                        if (f_decrypt(vc_encryptPrivateKey, f_getSecuredMessage(v_geoNwInd.msgIn), v_decryptedMsg) == false) {
                            log("*** " & testcasename() & ": FAIL: Unable to process encryption data ***");
                            f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
                        } else if (match(v_decryptedMsg, mw_etsiTs103097Data_signed) == false) {
+251 −4
Original line number Diff line number Diff line
@@ -36,6 +36,11 @@ module TestCodec_SignedAndEncryptedMessages {
  // TestCodec
  import from TestCodec_TestAndSystem all;
  
  /**
   * @desc    Test HMAC-SH256 using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @return  The encrypted message
   */
  testcase tc_test_hmac_sha256_test1() runs on TCType system TCType {
    var octetstring v_k := '0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'O;
    var octetstring v_m := '4869205468657265'O;
@@ -49,6 +54,10 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  /**
   * @desc    Test HMAC-SH256 using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @return  The encrypted message
   */
  testcase tc_test_hmac_sha256_test2() runs on TCType system TCType {
    var octetstring v_k := '4a656665'O;
    var octetstring v_m := '7768617420646f2079612077616e7420666f72206e6f7468696e673f'O;
@@ -62,6 +71,10 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  /**
   * @desc    Test HMAC-SH256 using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @return  The encrypted message
   */
  testcase tc_test_hmac_sha256_test3() runs on TCType system TCType {
    var octetstring v_k := 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'O;
    var octetstring v_m := 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'O;
@@ -75,6 +88,10 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  /**
   * @desc    Test AES 128 CCM using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @return  The encrypted message
   */
  testcase tc_test_encrypt_aes_128_ccm_test1() runs on TCType system TCType {
    var octetstring v_k := 'E58D5C8F8C9ED9785679E08ABC7C8116'O;
    var octetstring v_n := 'A9F593C09EAEEA8BF0C1CF6A'O;
@@ -96,6 +113,10 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  /**
   * @desc    Test AES 128 CCM using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @return  The encrypted message
   */
  testcase tc_test_encrypt_aes_128_ccm_test2() runs on TCType system TCType {
    var octetstring v_k := 'E58D5C8F8C9ED9785679E08ABC7C8116'O;
    var octetstring v_n := 'A9F593C09EAEEA8BF0C1CF6A'O;
@@ -117,6 +138,11 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  /**
   * @desc    Test AES 128 CCM using test vectors provided by IEEE Std 1609.2-2017 Annex D
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @return  The encrypted message
   */
  testcase tc_test_encrypt_aes_128_ccm_test3() runs on TCType system TCType {
    var octetstring v_wrong_k := 'A58D5C8F8C9ED9785679E08ABC7C8116'O;
    var octetstring v_n := 'A9F593C09EAEEA8BF0C1CF6A'O;
@@ -131,7 +157,14 @@ module TestCodec_SignedAndEncryptedMessages {
    }
  }
  
  testcase tc_encrypted_signed_message() runs on TCType system TCType {
  /**
   * @desc    Test ECIES encryption using NIST-P256
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
   * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
   * @return  The encrypted message
   */
  testcase tc_encrypted_signed_message_1() runs on TCType system TCType {
      
    var template (value) EtsiTs103097Data v_signed_data;
    var EtsiTs103097Data v_signed_data_dec;
@@ -219,9 +252,16 @@ module TestCodec_SignedAndEncryptedMessages {
    }
    setverdict(pass, "Decoding passed.");
    
  } // End of testcase tc_encrypted_signed_message
  } // End of testcase tc_encrypted_signed_message_1
  
  testcase tc_decrypted_signed_message() runs on TCType system TCType {
  /**
   * @desc    Test ECIES encryption/decryption using NIST-P256
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
   * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
   * @return  The encrypted message
   */
  testcase tc_decrypted_signed_message_1() runs on TCType system TCType {
      
    var template (value) EtsiTs103097Data v_signed_data;
    var EtsiTs103097Data v_signed_data_dec;
@@ -320,6 +360,213 @@ module TestCodec_SignedAndEncryptedMessages {
    }
    setverdict(pass, "Full encryption/decryption passed.");
    
  } // End of testcase tc_decrypted_signed_message
  } // End of testcase tc_decrypted_signed_message_1
  
  /**
   * @desc    Test ECIES encryption/decryption using NIST-P256
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
   * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
   * @return  The encrypted message
   */
  testcase tc_decrypted_signed_message_2() 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_encrypted_data;
    var EtsiTs103097Data v_decrypted_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);
    
    if (f_decrypt(v_tsPrivateKey, valueof(v_encrypted_data), v_decrypted_data) == false) {
      setverdict(fail, "f_decrypt failed");
      stop;
    }
    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_2
  
  /**
   * @desc    Test ECIES encryption/decryption using BRAINPOOL-P256
   * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
   * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
   * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
   * @return  The encrypted message
   */
  testcase tc_decrypted_signed_message_3() 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_brainpoolp256(v_obuPrivateKey, v_obuPublicKeyX, v_obuPublicKeyY);
    f_generate_key_pair_brainpoolp256(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_ecdsaBrainpoolP256r1(
                                                                                            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_encryptWithEciesBrainpoolp256WithSha256(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_eciesBrainpoolP256r1(
                                                                                                                                                                               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_decryptWithEciesBrainpoolp256WithSha256(
                                                             valueof(v_encrypted_data).content.encryptedData.ciphertext.aes128ccm.ccmCiphertext, // The encrypted message
                                                             v_tsPrivateKey,
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesBrainpoolP256r1.v.uncompressedP256.x, // The public ephemeral key X-coordinate
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesBrainpoolP256r1.v.uncompressedP256.y, // The public ephemeral key Y-coordinate
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesBrainpoolP256r1.c, // The encrypted symmetric key
                                                             valueof(v_encrypted_data).content.encryptedData.recipients[0].signedDataRecipInfo.encKey.eciesBrainpoolP256r1.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_3
  
} // End of module TestCodec_SignedAndEncryptedMessages