public class EdDHAuthenticatedDataDemo extends java.lang.Object
AuthenticatedDataStream
,
AuthenticatedData
and AuthenticatedDataOutputStream
for authenticated data with the CMS content type AuthenticatedData using the
Elliptic Curve Diffie-Hellman (ECDH) key agreement algorithm with curve25519 and
curve448 according to RFC 5652
and RFC 8418.
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..
AuthenticatedDataStream
,
AuthenticatedData
,
AuthenticatedDataOutputStream
,
RecipientInfo
,
KeyAgreeRecipientInfo
Constructor and Description |
---|
EdDHAuthenticatedDataDemo()
Setup the demo certificate chains.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
createAuthenticatedData(byte[] message,
iaik.asn1.structures.AlgorithmID macAlgorithm,
int macKeyLength,
iaik.asn1.structures.AlgorithmID digestAlgorithm,
int mode,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
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,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
Creates a CMS
AuthenticatedData message using the
AuthenticatedDataOutputStream
class. |
byte[] |
createAuthenticatedDataStream(byte[] message,
iaik.asn1.structures.AlgorithmID macAlgorithm,
int macKeyLength,
iaik.asn1.structures.AlgorithmID digestAlgorithm,
int mode,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
Creates a CMS
AuthenticatedDataStream for the given message message. |
iaik.cms.RecipientInfo[] |
createRecipients(iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
Creates the RecipientInfos.
|
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 content of the given
AuthenticatedData object for
the recipient identified by recipient identifier. |
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 |
parseAuthenticatedDataWithRecipientCert(boolean stream,
byte[] encodedAuthenticatedData,
byte[] message)
Parses an AuthenticatedData and decrypts the content for all test recipients
using their recipient certificate for identifying the recipient.
|
void |
parseAuthenticatedDataWithRecipientIdentifier(boolean stream,
byte[] encodedAuthenticatedData,
byte[] message)
Parses an AuthenticatedData, decrypts the mac keys for all test recipients
using their recipient identifiers for identifying the recipient
and verifies the content mac.
|
void |
parseAuthenticatedDataWithRecipientInfoIndex(boolean stream,
byte[] encodedAuthenticatedData,
byte[] message)
Parses an AuthenticatedData and decrypts the content for all test recipients
using the index into the recipientInfos field for identifying the recipient.
|
void |
start()
Starts the test.
|
void |
start(iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
Starts the test for the given content-authenticated encryption algorithm.
|
public EdDHAuthenticatedDataDemo() throws java.io.IOException
SetupCMSKeyStore
.java.io.IOException
- if an file read error occurspublic byte[] createAuthenticatedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedDataStream
for the given message message.message
- the message to be authenticated, as byte representationmacAlgorithm
- the mac algorithm to be usedmacKeyLength
- the length of the temporary MAC key to be generateddigestAlgorithm
- the digest algorithm to be used to calculate a digest
from the content if authenticated attributes should
be includedmode
- whether to include the content into the AuthenticatedData (implicit
) or to not include it
(explicit
)keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret mac key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the mac keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itAuthenticatedData
object just creatediaik.cms.CMSException
- if the AuthenticatedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] createAuthenticatedDataOutputStream(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedData
message using the
AuthenticatedDataOutputStream
class.message
- the message to be authenticated, as byte representationmacAlgorithm
- the mac algorithm to be usedmacKeyLength
- the length of the temporary MAC key to be generateddigestAlgorithm
- the digest algorithm to be used to calculate a digest
from the content if authenticated attributes should
be includedmode
- whether to include the content into the AuthenticatedData (implicit
) or to not include it
(explicit
)keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret mac key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the mac keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itAuthenticatedData
object just creatediaik.cms.CMSException
- if the AuthenticatedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
This way of decrypting the MAC key and verifying the content requires to know at what index of the recipientInfos field the RecipientInfo for the particular recipient in mind can be found. For RecipientInfos 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.
encoding
- the AuthenticatedData
object as BER encoded byte arraymessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the mac keyrecipientInfoIndex
- the index of the right RecipientInfo
to
which the given key belongsiaik.cms.CMSException
- if the authenticated data cannot be verifiedjava.io.IOException
- if a stream read/write error occurspublic byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, iaik.cms.KeyIdentifier recipientID) throws iaik.cms.CMSException, java.io.IOException
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.
encoding
- the DER encoeded AuthenticatedData
object#message
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the messagerecipientID
- the recipient identifier uniquely identifying the key of the
recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.Key key, iaik.x509.X509Certificate recipientCert) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedData
object for
the recipient identified by its recipient certificate.
encoding
- the AuthenticatedData
object as DER encoded byte arraymessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the messagerecipientCert
- the certificate of the recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfoiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if a stream read/write error occurspublic byte[] createAuthenticatedData(byte[] message, iaik.asn1.structures.AlgorithmID macAlgorithm, int macKeyLength, iaik.asn1.structures.AlgorithmID digestAlgorithm, int mode, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength) throws iaik.cms.CMSException
AuthenticatedData
for the given message message.message
- the message to be authenticated, as byte representationmacAlgorithm
- the mac algorithm to be usedmacKeyLength
- the length of the temporary MAC key to be generateddigestAlgorithm
- the digest algorithm to be used to calculate a digest
from the content if authenticated attributes should
be includedmode
- whether to include the content into the AuthenticatedData (implicit
) or to not include it
(explicit
)keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret mac key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the mac keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itAuthenticatedData
object just creatediaik.cms.CMSException
- if the AuthenticatedData
object cannot
be createdpublic byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
This way of decrypting the MAC key and verifying the content requires to know at what index of the recipientInfos field the RecipientInfo for the particular recipient in mind can be found. For RecipientInfos 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.
encoding
- the AuthenticatedData
object as BER encoded byte arraymessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the mac keyrecipientInfoIndex
- the index of the right RecipientInfo
to
which the given key belongsiaik.cms.CMSException
- if the authenticated data cannot be verifiedjava.io.IOException
- if a IO read/write error occurspublic byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, iaik.cms.KeyIdentifier recipientID) throws iaik.cms.CMSException, java.io.IOException
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.
encoding
- the DER encoeded AuthenticatedData
object#message
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the messagerecipientID
- the recipient identifier uniquely identifying the key of the
recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.Key key, iaik.x509.X509Certificate recipientCert) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedData
object for
the recipient identified by its recipient certificate.
encoding
- the DER encoded AuthenticatedData
ASN.1 objectmessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the messagerecipientCert
- the certificate of the recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfoiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
public iaik.cms.RecipientInfo[] createRecipients(iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength) throws iaik.cms.CMSException
keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itiaik.cms.CMSException
- if an error occurs when creating the recipient infospublic void parseAuthenticatedDataWithRecipientInfoIndex(boolean stream, byte[] encodedAuthenticatedData, byte[] message) throws java.lang.Exception
stream
- whether to use AuthenticatedDataStream or AuthenticatedDataencodedAuthenticatedData
- the encoded AuthenticatedData objectmessage
- the content message, if transmitted by other means (explicit mode)java.lang.Exception
- if some error occurs during mac key decryption / mac verificationpublic void parseAuthenticatedDataWithRecipientIdentifier(boolean stream, byte[] encodedAuthenticatedData, byte[] message) throws java.lang.Exception
stream
- whether to use AuthenticatedDataStream or AuthenticatedDataencodedAuthenticatedData
- the encoded AuthenticatedData objectmessage
- the content message, if transmitted by other means (explicit mode)java.lang.Exception
- if some error occurs during mac key decryption / mac verificationpublic void parseAuthenticatedDataWithRecipientCert(boolean stream, byte[] encodedAuthenticatedData, byte[] message) throws java.lang.Exception
stream
- whether to use AuthenticatedDataStream or AuthenticatedDatasencodedAuthenticatedData
- the encoded AuthenticatedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void start()
public void start(iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength)
keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itpublic static void main(java.lang.String[] argv) throws java.lang.Exception
java.io.IOException
- if an I/O error occurs when reading required keys
and certificates from filesjava.lang.Exception