Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.conscrypt;
     18 
     19 import java.security.Key;
     20 import java.security.KeyPair;
     21 import java.security.KeyPairGenerator;
     22 import java.security.NoSuchAlgorithmException;
     23 import javax.crypto.KeyGenerator;
     24 import org.bouncycastle.jce.provider.BouncyCastleProvider;
     25 
     26 /**
     27  * Supported cipher transformations.
     28  */
     29 @SuppressWarnings({"ImmutableEnumChecker", "unused"})
     30 public enum Transformation {
     31     AES_CBC_PKCS5("AES", "CBC", "PKCS5Padding", new AesKeyGen()),
     32     AES_ECB_PKCS5("AES", "ECB", "PKCS5Padding", new AesKeyGen()),
     33     AES_GCM_NO("AES", "GCM", "NoPadding", new AesKeyGen()),
     34     RSA_ECB_PKCS1("RSA", "ECB", "PKCS1Padding", new RsaKeyGen());
     35 
     36     Transformation(String algorithm, String mode, String padding, KeyGen keyGen) {
     37         this.algorithm = algorithm;
     38         this.mode = mode;
     39         this.padding = padding;
     40         this.keyGen = keyGen;
     41     }
     42 
     43     final String algorithm;
     44     final String mode;
     45     final String padding;
     46     final KeyGen keyGen;
     47 
     48     String toFormattedString() {
     49         return algorithm + "/" + mode + "/" + padding;
     50     }
     51 
     52     Key newEncryptKey() {
     53         return keyGen.newEncryptKey();
     54     }
     55 
     56     private interface KeyGen { Key newEncryptKey(); }
     57 
     58     private static final class RsaKeyGen implements KeyGen {
     59         @Override
     60         public Key newEncryptKey() {
     61             try {
     62                 // Use Bouncy castle
     63                 KeyPairGenerator generator =
     64                         KeyPairGenerator.getInstance("RSA", new BouncyCastleProvider());
     65                 generator.initialize(2048);
     66                 KeyPair pair = generator.generateKeyPair();
     67                 return pair.getPublic();
     68             } catch (NoSuchAlgorithmException e) {
     69                 throw new RuntimeException(e);
     70             }
     71         }
     72     }
     73 
     74     private static final class AesKeyGen implements KeyGen {
     75         @Override
     76         public Key newEncryptKey() {
     77             try {
     78                 // Just use the JDK's provider.
     79                 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
     80                 keyGen.init(256);
     81                 return keyGen.generateKey();
     82             } catch (NoSuchAlgorithmException e) {
     83                 throw new RuntimeException(e);
     84             }
     85         }
     86     }
     87 }
     88