IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

demo.cms.ecc
Class ECDHAuthenticatedDataDemo

java.lang.Object
  extended by demo.cms.ecc.StaticStaticECDHDemo
      extended by demo.cms.ecc.ECDHAuthenticatedDataDemo

public class ECDHAuthenticatedDataDemo
extends StaticStaticECDHDemo

Demonstrates the usage of class AuthenticatedDataStream, AuthenticatedData and AuthenticatedDataOutputStream for authenticated encrypting data using the CMS type AuthenticatedData by using Static-Static ECDH according to 6278 as key agreement method.

Any keys/certificates required for this demo are read from a keystore file "cmsecc.keystore" located in your current working directory. If the keystore file does not exist you can create it by running the SetupCMSEccKeyStore program.

Additionally to iaik_cms.jar you also must have iaik_jce_(full).jar (IAIK-JCE, https://sic.tech/products/core-crypto-toolkits/jca-jce/), and iaik_eccelarate.jar (IAIK-ECCelerateTM, https://sic.tech/products/core-crypto-toolkits/eccelerate/) in your classpath.

See Also:
AuthenticatedDataStream, AuthenticatedData, AuthenticatedDataOutputStream, RecipientInfo, KeyAgreeRecipientInfo, SetupCMSEccKeyStore

Constructor Summary
ECDHAuthenticatedDataDemo()
          Setup the demo certificate chains.
 
Method Summary
 byte[] createAuthenticatedData(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode)
          Creates a CMS AuthenticatedData for the given message message.
 byte[] createAuthenticatedDataOutputStream(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode)
          Creates a CMS AuthenticatedDataOutputStream for the given message message.
 byte[] createAuthenticatedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode)
          Creates a CMS AuthenticatedDataStream for the given message message.
 byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, int recipientInfoIndex)
          Decrypts the encrypted MAC key for the recipient identified by its index into the recipientInfos field and uses the MAC key to verify the authenticated data.
 byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, iaik.cms.KeyIdentifier recipientID)
          Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by recipient identifier.
 byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, iaik.x509.X509Certificate recipientCert)
          Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by its recipient certificate.
 byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, int recipientInfoIndex)
          Decrypts the encrypted MAC key for the recipient identified by its index into the recipientInfos field and uses the MAC key to verify the authenticated data.
 byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, iaik.cms.KeyIdentifier recipientID)
          Decrypts the encrypted MAC key for the recipient identified by recipient identifier and uses the MAC key to verify the authenticated data.
 byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, iaik.x509.X509Certificate recipientCert)
          Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by its recipient certificate.
static void main(java.lang.String[] argv)
          Main method.
 void parseAuthenticatedDataWithRecipientCertificate(boolean stream, byte[] encodedAuthenticatedData, byte[] message)
          Parses an AuthenticatedData and verifies the mac for all recipients using the recipient certificate for identifying the recipient.
 void parseAuthenticatedDataWithRecipientIdentifier(boolean stream, byte[] encodedAuthenticatedData, byte[] message)
          Parses an AuthenticatedData and verifies the mac for all recipients using their recipient identifiers for identifying the recipient.
 void parseAuthenticatedDataWithRecipientInfoIndex(boolean stream, byte[] encodedAuthenticatedData, byte[] message)
          Parses an AuthenticatedData and verifies the mac for all recipients using the index into the recipientInfos field for identifying the recipient.
 void start()
          Starts the demo.
 
Methods inherited from class demo.cms.ecc.StaticStaticECDHDemo
createRecipients
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ECDHAuthenticatedDataDemo

public ECDHAuthenticatedDataDemo()
                          throws java.io.IOException
Setup the demo certificate chains. Keys and certificates are retrieved from the demo keyStore file "cmsecc.keystore" located in your current working directory. If the keystore file does not exist you can create it by running the SetupCMSEccKeyStore program.

Throws:
java.io.IOException - if keys/certificates cannot be read from the keystore
Method Detail

createAuthenticatedDataStream

public byte[] createAuthenticatedDataStream(byte[] message,
                                            iaik.asn1.structures.AlgorithmID macAlgorithm,
                                            int macKeyLength,
                                            iaik.asn1.structures.AlgorithmID digestAlgorithm,
                                            int mode)
                                     throws iaik.cms.CMSException,
                                            java.io.IOException
Creates a CMS AuthenticatedDataStream for the given message message.

Parameters:
message - the message to be authenticated, as byte representation
macAlgorithm - the mac algorithm to be used
macKeyLength - the length of the temporary MAC key to be generated
digestAlgorithm - the digest algorithm to be used to calculate a digest from the content if authenticated attributes should be included
mode - whether to include the content into the AuthenticatedData (implicit) or to not include it (explicit)
Returns:
the BER encoding of the AuthenticatedData object just created
Throws:
iaik.cms.CMSException - if the AuthenticatedData object cannot be created
java.io.IOException - if an I/O error occurs

createAuthenticatedDataOutputStream

public byte[] createAuthenticatedDataOutputStream(byte[] message,
                                                  iaik.asn1.structures.AlgorithmID macAlgorithm,
                                                  int macKeyLength,
                                                  iaik.asn1.structures.AlgorithmID digestAlgorithm,
                                                  int mode)
                                           throws iaik.cms.CMSException,
                                                  java.io.IOException
Creates a CMS AuthenticatedDataOutputStream for the given message message.

Parameters:
message - the message to be authenticated, as byte representation
macAlgorithm - the mac algorithm to be used
macKeyLength - the length of the temporary MAC key to be generated
digestAlgorithm - the digest algorithm to be used to calculate a digest from the content if authenticated attributes should be included
mode - whether to include the content into the AuthenticatedData (implicit) or to not include it (explicit)
Returns:
the BER encoding of the AuthenticatedData object just created, wrapped into a ContentInfo
Throws:
iaik.cms.CMSException - if the AuthenticatedData object cannot be created
java.io.IOException - if an I/O error occurs

getAuthenticatedDataStream

public byte[] getAuthenticatedDataStream(byte[] encoding,
                                         byte[] message,
                                         java.security.Key key,
                                         int recipientInfoIndex)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted MAC key for the recipient identified by its index into the recipientInfos field and uses the MAC key to verify the authenticated data.

This way of decrypting the MAC key and verifying the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo, PasswordRecipeintInfo, OtherRecipientInfo), but requires to know at what index of the recipientInfos field the RecipientInfo for the particular recipient in mind can be found. If the recipient in mind uses a RecipientInfo of type KeyAgreeRecipientInfo some processing overhead may take place because a KeyAgreeRecipientInfo may contain encrypted mac keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted mac key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.

Parameters:
encoding - the AuthenticatedData object as BER encoded byte array
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the mac key
recipientInfoIndex - the index of the right RecipientInfo to which the given key belongs
Returns:
the verified message, as byte array
Throws:
iaik.cms.CMSException - if the authenticated data cannot be verified
java.io.IOException - if a stream read/write error occurs

getAuthenticatedDataStream

public byte[] getAuthenticatedDataStream(byte[] encoding,
                                         byte[] message,
                                         java.security.Key key,
                                         iaik.cms.KeyIdentifier recipientID)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted MAC key for the recipient identified by recipient identifier and uses the MAC key to verify the authenticated data.

This way of decrypting the mac key may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo). The recipient in mind is identified by its recipient identifier.

Parameters:
encoding - the AuthenticatedData object as BER encoded byte array
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the encrypted mac key
recipientID - the recipient identifier uniquely identifying the key of the recipient
Returns:
the verified message, as byte array
Throws:
iaik.cms.CMSException - if the authenticated data cannot be verified
java.io.IOException - if a stream read/write error occurs

getAuthenticatedDataStream

public byte[] getAuthenticatedDataStream(byte[] encoding,
                                         byte[] message,
                                         java.security.Key key,
                                         iaik.x509.X509Certificate recipientCert)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by its recipient certificate.

Parameters:
encoding - the AuthenticatedData object as DER encoded byte array
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the message
recipientCert - the certificate of the recipient having a RecipientInfo of type KeyTransRecipientInfo or KeyAgreeRecipientInfo
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if a stream read/write error occurs

createAuthenticatedData

public byte[] createAuthenticatedData(byte[] message,
                                      iaik.asn1.structures.AlgorithmID macAlgorithm,
                                      int macKeyLength,
                                      iaik.asn1.structures.AlgorithmID digestAlgorithm,
                                      int mode)
                               throws iaik.cms.CMSException
Creates a CMS AuthenticatedData for the given message message.

Parameters:
message - the message to be authenticated, as byte representation
macAlgorithm - the mac algorithm to be used
macKeyLength - the length of the temporary MAC key to be generated
digestAlgorithm - the digest algorithm to be used to calculate a digest from the content if authenticated attributes should be included
mode - whether to include the content into the AuthenticatedData (implicit) or to not include it (explicit)
Returns:
the BER encoding of the AuthenticatedData object just created
Throws:
iaik.cms.CMSException - if the AuthenticatedData object cannot be created

getAuthenticatedData

public byte[] getAuthenticatedData(byte[] encoding,
                                   byte[] message,
                                   java.security.Key key,
                                   int recipientInfoIndex)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted MAC key for the recipient identified by its index into the recipientInfos field and uses the MAC key to verify the authenticated data.

This way of decrypting the MAC key and verifying the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo), but requires to know at what index of the recipientInfos field the RecipientInfo for the particular recipient in mind can be found. If the recipient in mind uses a RecipientInfo of type KeyAgreeRecipientInfo some processing overhead may take place because a KeyAgreeRecipientInfo may contain encrypted mac keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted mac key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.

Parameters:
encoding - the AuthenticatedData object as BER encoded byte array
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the mac key
recipientInfoIndex - the index of the right RecipientInfo to which the given key belongs
Returns:
the verified message, as byte array
Throws:
iaik.cms.CMSException - if the authenticated data cannot be verified
java.io.IOException - if a IO read/write error occurs

getAuthenticatedData

public byte[] getAuthenticatedData(byte[] encoding,
                                   byte[] message,
                                   java.security.Key key,
                                   iaik.cms.KeyIdentifier recipientID)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by recipient identifier.

This way of decrypting the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo). The recipient in mind is identified by its recipient identifier.

Parameters:
encoding - the DER encoeded AuthenticatedData object#
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the message
recipientID - the recipient identifier uniquely identifying the key of the recipient
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if an I/O error occurs

getAuthenticatedData

public byte[] getAuthenticatedData(byte[] encoding,
                                   byte[] message,
                                   java.security.Key key,
                                   iaik.x509.X509Certificate recipientCert)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted content of the given AuthenticatedData object for the recipient identified by its recipient certificate.

Parameters:
encoding - the DER encoded AuthenticatedData ASN.1 object
message - the content message, if transmitted by other means (explicit mode)
key - the key to decrypt the message
recipientCert - the certificate of the recipient having a RecipientInfo of type KeyTransRecipientInfo or KeyAgreeRecipientInfo
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException

parseAuthenticatedDataWithRecipientInfoIndex

public void parseAuthenticatedDataWithRecipientInfoIndex(boolean stream,
                                                         byte[] encodedAuthenticatedData,
                                                         byte[] message)
                                                  throws java.lang.Exception
Parses an AuthenticatedData and verifies the mac for all recipients using the index into the recipientInfos field for identifying the recipient.

Parameters:
stream - whether to use AuthenticatedDataStream or AuthenticatedData
encodedAuthenticatedData - the encoded AuthenticatedData object
message - the content message, if transmitted by other means (explicit mode)
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

parseAuthenticatedDataWithRecipientIdentifier

public void parseAuthenticatedDataWithRecipientIdentifier(boolean stream,
                                                          byte[] encodedAuthenticatedData,
                                                          byte[] message)
                                                   throws java.lang.Exception
Parses an AuthenticatedData and verifies the mac for all recipients using their recipient identifiers for identifying the recipient.

Parameters:
stream - whether to use AuthenticatedDataStream or AuthenticatedData
encodedAuthenticatedData - the encoded AuthenticatedData object
message - the content message, if transmitted by other means (explicit mode)
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

parseAuthenticatedDataWithRecipientCertificate

public void parseAuthenticatedDataWithRecipientCertificate(boolean stream,
                                                           byte[] encodedAuthenticatedData,
                                                           byte[] message)
                                                    throws java.lang.Exception
Parses an AuthenticatedData and verifies the mac for all recipients using the recipient certificate for identifying the recipient.

Parameters:
stream - whether to use AuthenticatedDataStream or AuthenticatedData
encodedAuthenticatedData - the encoded AuthenticatedData object
message - the content message, if transmitted by other means (explicit mode)
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

start

public void start()
Starts the demo.


main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Main method.

Throws:
java.io.IOException - if an I/O error occurs when reading required keys and certificates from the keystore file
java.lang.Exception

IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

v6.1
(c) 2002 IAIK, (c) 2003 - 2025 SIC