Commit a28a57ca authored by garciay's avatar garciay
Browse files

STF545: Validate full support of ECIES encryption

parent 722f77d8
Loading
Loading
Loading
Loading
+134 −9
Original line number Original line Diff line number Diff line
@@ -106,6 +106,7 @@ module LibItsSecurity_Functions {
        } // End of function f_signWithEcdsaBrainpoolp384WithSha384
        } // End of function f_signWithEcdsaBrainpoolp384WithSha384
        
        
        function f_decrypt(
        function f_decrypt(
                           in octetstring         p_encryptPrivateKey,
                           in EtsiTs103097Data    p_encrypedSecuredMessage,
                           in EtsiTs103097Data    p_encrypedSecuredMessage,
                           out EtsiTs103097Data   p_decrypedSecuredMessage
                           out EtsiTs103097Data   p_decrypedSecuredMessage
        ) return boolean {
        ) return boolean {
@@ -113,6 +114,12 @@ module LibItsSecurity_Functions {
                var PKRecipientInfo v_pKRecipientInfo;
                var PKRecipientInfo v_pKRecipientInfo;
                var RecipientInfo v_recipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0];
                var RecipientInfo v_recipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0];
                
                
                // Check the private encryption key
                if (not(isbound(p_encryptPrivateKey))) {
                  log("*** " & testcasename() & ":ERROR: Failed to load encryption private key ***");
                  return false;
                }
                
                if (ischosen(v_recipientInfo.certRecipInfo)) {
                if (ischosen(v_recipientInfo.certRecipInfo)) {
                    v_pKRecipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0].certRecipInfo;
                    v_pKRecipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0].certRecipInfo;
                    // Read the certificate based on the recipientId
                    // Read the certificate based on the recipientId
@@ -126,9 +133,9 @@ module LibItsSecurity_Functions {
                if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256)) {
                if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256)) {
                  var octetstring v_decryptedSecuredMessage;
                  var octetstring v_decryptedSecuredMessage;
                  var SymmetricCiphertext v_ciphertext := p_encrypedSecuredMessage.content.encryptedData.ciphertext;
                  var SymmetricCiphertext v_ciphertext := p_encrypedSecuredMessage.content.encryptedData.ciphertext;
                  // FIXME Add the private encryption key
                  v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                  /*v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                                                                                    v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                    v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                    p_encryptPrivateKey,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.v.uncompressedP256.x,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.v.uncompressedP256.x,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.v.uncompressedP256.y,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.v.uncompressedP256.y,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.c,
                                                                                    v_pKRecipientInfo.encKey.eciesNistP256.c,
@@ -140,17 +147,33 @@ module LibItsSecurity_Functions {
                    if (decvalue(v_decode, p_decrypedSecuredMessage) == 0) {
                    if (decvalue(v_decode, p_decrypedSecuredMessage) == 0) {
                      return true;
                      return true;
                    }
                    }
                    }*/
                  }
                } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1)) {
                } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1)) {
                  // TODO
                  var octetstring v_decryptedSecuredMessage;
                  var SymmetricCiphertext v_ciphertext := p_encrypedSecuredMessage.content.encryptedData.ciphertext;
                  v_decryptedSecuredMessage := f_decryptWithEciesBrainpoolp256WithSha256(
                                                                                         v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                         p_encryptPrivateKey,
                                                                                         v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.uncompressedP256.x,
                                                                                         v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.uncompressedP256.y,
                                                                                         v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.c,
                                                                                         v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.t,
                                                                                         v_ciphertext.aes128ccm.nonce
                                                                                    );
                  if (isbound(v_decryptedSecuredMessage)) {
                    var bitstring v_decode := oct2bit(v_decryptedSecuredMessage);
                    if (decvalue(v_decode, p_decrypedSecuredMessage) == 0) {
                      return true;
                    }
                  }
                }
                }
            }
            }
            
            
            return false;
            return false;
        } // End of function f_signWithEcdsaBrainpoolp384WithSha384
        } // End of function f_decrypt


        /**
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Nist-P256 algorithm
         * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
         * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
         * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
         * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
         * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
         * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
@@ -159,6 +182,9 @@ module LibItsSecurity_Functions {
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @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
         * @return  The encrypted message
         */
         */
        function f_encryptWithEciesNistp256WithSha256(
        function f_encryptWithEciesNistp256WithSha256(
@@ -184,7 +210,7 @@ module LibItsSecurity_Functions {
        } // End of function f_encryptWithEciesNistp256WithSha256
        } // End of function f_encryptWithEciesNistp256WithSha256
        
        
        /**
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Nist-P256 algorithm
         * @param   p_encryptedSecuredMessage    The data to be decrypted
         * @param   p_encryptedSecuredMessage    The data to be decrypted
         * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
         * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
         * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
         * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
@@ -192,6 +218,9 @@ module LibItsSecurity_Functions {
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The decrypted message
         * @return  The decrypted message
         * @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
         */
         */
        function f_decryptWithEciesNistp256WithSha256(
        function f_decryptWithEciesNistp256WithSha256(
                                                      in octetstring p_encryptedSecuredMessage,
                                                      in octetstring p_encryptedSecuredMessage,
@@ -213,6 +242,76 @@ module LibItsSecurity_Functions {
                                                       );
                                                       );
        } // End of function f_decryptWithEcdsaNistp256WithSha256
        } // End of function f_decryptWithEcdsaNistp256WithSha256
        
        
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Brainpool-P256 algorithm
         * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
         * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
         * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
         * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
         * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The encrypted message
         * @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
         */
        function f_encryptWithEciesBrainpoolp256WithSha256(
                                                      in octetstring p_toBeEncryptedSecuredMessage,
                                                      in Oct32 p_recipientsPublicKeyX,
                                                      in Oct32 p_recipientsPublicKeyY,
                                                      out Oct32 p_publicEphemeralKeyX,
                                                      out Oct32 p_publicEphemeralKeyY,
                                                      out Oct16 p_encrypted_sym_key,
                                                      out Oct16 p_authentication_vector,
                                                      out Oct12 p_nonce
        ) return octetstring {
          return fx_encryptWithEciesBrainpoolp256WithSha256(
                                                       p_toBeEncryptedSecuredMessage,
                                                       p_recipientsPublicKeyX,
                                                       p_recipientsPublicKeyY,
                                                       p_publicEphemeralKeyX,
                                                       p_publicEphemeralKeyY,
                                                       p_encrypted_sym_key,
                                                       p_authentication_vector,
                                                       p_nonce
                                                       );
        } // End of function f_encryptWithEciesBrainpoolp256WithSha256
        
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Brainpool-P256 algorithm
         * @param   p_encryptedSecuredMessage    The data to be decrypted
         * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
         * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The decrypted message
         * @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
         */
        function f_decryptWithEciesBrainpoolp256WithSha256(
                                                      in octetstring p_encryptedSecuredMessage,
                                                      in Oct32 p_privateEncKey,
                                                      in Oct32 p_publicEphemeralKeyX, 
                                                      in Oct32 p_publicEphemeralKeyY,
                                                      in Oct16 p_encrypted_sym_key,
                                                      in Oct16 p_authentication_vector,
                                                      in Oct12 p_nonce
        ) return octetstring {
          return fx_decryptWithEciesBrainpoolp256WithSha256(
                                                       p_encryptedSecuredMessage,
                                                       p_privateEncKey,
                                                       p_publicEphemeralKeyX, 
                                                       p_publicEphemeralKeyY,
                                                       p_encrypted_sym_key,
                                                       p_authentication_vector,
                                                       p_nonce
                                                       );
        } // End of function f_decryptWithEcdsaBrainpoolp256WithSha256
        
        /**
        /**
         * @desc Compute the HashedId8 value from the hash value
         * @desc Compute the HashedId8 value from the hash value
         * @param p_hash The hash value
         * @param p_hash The hash value
@@ -1801,7 +1900,7 @@ module LibItsSecurity_Functions {
            external function fx_test_decrypt_aes_128_ccm_test(in octetstring p_k, in octetstring p_n, in octetstring p_pt) return octetstring;
            external function fx_test_decrypt_aes_128_ccm_test(in octetstring p_k, in octetstring p_n, in octetstring p_pt) return octetstring;
            
            
            /**
            /**
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Nist-P256 algorithm
             * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
             * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
             * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
             * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
             * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
             * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
@@ -1815,7 +1914,7 @@ module LibItsSecurity_Functions {
            external function fx_encryptWithEciesNistp256WithSha256(in octetstring p_toBeEncryptedSecuredMessage, in Oct32 p_recipientsPublicKeyX, in Oct32 p_recipientsPublicKeyY, out Oct32 p_publicEphemeralKeyX, out Oct32 p_publicEphemeralKeyY, out Oct16 p_encrypted_sym_key, out Oct16 p_authentication_vector, out Oct12 p_nonce) return octetstring;
            external function fx_encryptWithEciesNistp256WithSha256(in octetstring p_toBeEncryptedSecuredMessage, in Oct32 p_recipientsPublicKeyX, in Oct32 p_recipientsPublicKeyY, out Oct32 p_publicEphemeralKeyX, out Oct32 p_publicEphemeralKeyY, out Oct16 p_encrypted_sym_key, out Oct16 p_authentication_vector, out Oct12 p_nonce) return octetstring;
            
            
            /**
            /**
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Nist-P256 algorithm
             * @param   p_encryptedSecuredMessage    The data to be decrypted
             * @param   p_encryptedSecuredMessage    The data to be decrypted
             * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
             * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
             * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
             * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
@@ -1826,6 +1925,32 @@ module LibItsSecurity_Functions {
             */
             */
            external function fx_decryptWithEciesNistp256WithSha256(in octetstring p_encryptedSecuredMessage, in Oct32 p_privateEncKey, in Oct32 p_publicEphemeralKeyX, in Oct32 p_publicEphemeralKeyY, in Oct16 p_encrypted_sym_key, in Oct16 p_authentication_vector, in Oct12 p_nonce) return octetstring;
            external function fx_decryptWithEciesNistp256WithSha256(in octetstring p_encryptedSecuredMessage, in Oct32 p_privateEncKey, in Oct32 p_publicEphemeralKeyX, in Oct32 p_publicEphemeralKeyY, in Oct16 p_encrypted_sym_key, in Oct16 p_authentication_vector, in Oct12 p_nonce) return octetstring;
            
            
            /**
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Brainpool-P256 algorithm
             * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
             * @param   p_recipientsPublicKeyX           The Recipient's public encryption key X-coordinate
             * @param   p_recipientsPublicKeyY           The Recipient's public encryption key Y-coordinate
             * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
             * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
             * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
             * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
             * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
             * @return  The encrypted message
             */
            external function fx_encryptWithEciesBrainpoolp256WithSha256(in octetstring p_toBeEncryptedSecuredMessage, in Oct32 p_recipientsPublicKeyX, in Oct32 p_recipientsPublicKeyY, out Oct32 p_publicEphemeralKeyX, out Oct32 p_publicEphemeralKeyY, out Oct16 p_encrypted_sym_key, out Oct16 p_authentication_vector, out Oct12 p_nonce) return octetstring;
            
            /**
             * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Brainpool-P256 algorithm
             * @param   p_encryptedSecuredMessage    The data to be decrypted
             * @param   p_publicEphemeralKeyX            The generated ephemeral key X-coordinate
             * @param   p_publicEphemeralKeyY            The generated ephemeral key Y-coordinate
             * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
             * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
             * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
             * @return  The decrypted message
             */
            external function fx_decryptWithEciesBrainpoolp256WithSha256(in octetstring p_encryptedSecuredMessage, in Oct32 p_privateEncKey, in Oct32 p_publicEphemeralKeyX, in Oct32 p_publicEphemeralKeyY, in Oct16 p_encrypted_sym_key, in Oct16 p_authentication_vector, in Oct12 p_nonce) return octetstring;
            
            /**
            /**
             * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature
             * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature
             * @param   p_toBeSignedSecuredMessage    The data to be signed
             * @param   p_toBeSignedSecuredMessage    The data to be signed