001// Copyright (C) 2002 IAIK
002// https://sic.tech/
003//
004// Copyright (C) 2003 - 2025 Stiftung Secure Information and 
005//                           Communication Technologies SIC
006// https://sic.tech/
007//
008// All rights reserved.
009//
010// This source is provided for inspection purposes and recompilation only,
011// unless specified differently in a contract with IAIK. This source has to
012// be kept in strict confidence and must not be disclosed to any third party
013// under any circumstances. Redistribution in source and binary forms, with
014// or without modification, are <not> permitted in any case!
015//
016// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
017// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
018// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
019// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
020// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
021// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
022// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
023// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
024// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
025// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
026// SUCH DAMAGE.
027//
028// $Header: /IAIK-CMS/current/src/demo/cms/envelopedData/CamelliaEnvelopedDataDemo.java 7     12.02.25 17:58 Dbratko $
029// $Revision: 7 $
030//
031
032
033package demo.cms.envelopedData;
034
035import iaik.asn1.structures.AlgorithmID;
036
037import java.io.IOException;
038import java.security.NoSuchAlgorithmException;
039
040import demo.DemoUtil;
041
042
043/**
044 * Demonstrates the usage of class {@link iaik.cms.EnvelopedDataStream} and
045 * {@link iaik.cms.EnvelopedData} for encrypting data using the CMS type
046 * EnvelopedData with the Camellia cipher algorithm.
047 * <br>
048 * Camellia is used for both content encryption and content encryption key 
049 * wrapping (according to RFC 3657).
050 * <p>
051 * This demo creates an EnvelopedData object and subsequently shows several
052 * ways that may be used for decrypting the content for some particular 
053 * recipient.
054 * <p>
055 * Keys and certificates are retrieved from the demo KeyStore ("cms.keystore") 
056 * which has to be located in your current working directory and may be
057 * created by running the {@link demo.keystore.SetupCMSKeyStore
058 * SetupCMSKeyStore} program.
059 * <p>
060 * 
061 * @see iaik.cms.EnvelopedDataStream
062 * @see iaik.cms.EnvelopedData
063 * @see iaik.cms.RecipientInfo
064 * @see iaik.cms.KeyTransRecipientInfo
065 * @see iaik.cms.KeyAgreeRecipientInfo
066 * @see iaik.cms.KEKRecipientInfo
067 */
068public class CamelliaEnvelopedDataDemo extends EnvelopedDataDemo {
069
070  
071  /**
072   * Creates an CamelliaEnvelopedDataDemo and setups the demo certificates.
073   * <br>
074   * Keys and certificates are retrieved from the demo KeyStore ("cms.keystore")
075   * file which has to be located in your current working directory and may be
076   * created by running {@link demo.keystore.SetupCMSKeyStore
077   * SetupCMSKeyStore}.
078   * <br>
079   * Camellia and Camellia KeyWrap are used for content encryption and
080   * content encryption key wrapping.
081   *
082   * @throws IOException if an file read error occurs
083   * @throws NoSuchAlgorithmException if the requested algorithms are not supported
084   */
085  public CamelliaEnvelopedDataDemo() throws IOException, NoSuchAlgorithmException {
086    super((AlgorithmID)AlgorithmID.camellia128_CBC.clone(),
087         (AlgorithmID)AlgorithmID.cms_camellia128_wrap.clone(),
088         128);
089  }
090      
091  /**
092   * Main method.
093   *
094   * @throws IOException
095   *            if an I/O error occurs when reading required keys
096   *            and certificates from files
097   */
098  public static void main(String argv[]) throws Exception {
099    double iaikProviderVersion = DemoUtil.getIaikProviderVersion();
100    if (iaikProviderVersion <= 3.18) {
101      System.err.println("This demo requires a IAIK provider version > 3.18! Your IAIK provider version is " + iaikProviderVersion + ".");
102    } else {  
103        DemoUtil.initDemos();
104      // Camellia with 128 bit keys 
105      System.out.println("\n***** Camellia-128 demo *****\n");
106      (new CamelliaEnvelopedDataDemo()).start();
107      // Camellia with 192 bit keys
108      System.out.println("\n***** Camellia-192 demo *****\n");
109      (new EnvelopedDataDemo((AlgorithmID)AlgorithmID.camellia192_CBC.clone(),
110                             (AlgorithmID)AlgorithmID.cms_camellia192_wrap.clone(),
111                              192)).start();
112      // Camellia with 256 bit keys
113      System.out.println("\n***** Camellia-256 demo *****\n");
114      (new EnvelopedDataDemo((AlgorithmID)AlgorithmID.camellia256_CBC.clone(),
115                             (AlgorithmID)AlgorithmID.cms_camellia256_wrap.clone(),
116                              256)).start();
117  
118      System.out.println("\nReady!");
119    }  
120    DemoUtil.waitKey();
121  }
122}