1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.crypto; 20 21 import org.apache.hadoop.classification.InterfaceAudience; 22 import org.apache.hadoop.util.StringUtils; 23 24 /** 25 * Defines properties of a CipherSuite. Modeled after the ciphers in 26 * {@link javax.crypto.Cipher}. 27 */ 28 @InterfaceAudience.Private 29 public enum CipherSuite { 30 UNKNOWN("Unknown", 0), 31 AES_CTR_NOPADDING("AES/CTR/NoPadding", 16); 32 33 private final String name; 34 private final int algoBlockSize; 35 36 private Integer unknownValue = null; 37 CipherSuite(String name, int algoBlockSize)38 CipherSuite(String name, int algoBlockSize) { 39 this.name = name; 40 this.algoBlockSize = algoBlockSize; 41 } 42 setUnknownValue(int unknown)43 public void setUnknownValue(int unknown) { 44 this.unknownValue = unknown; 45 } 46 getUnknownValue()47 public int getUnknownValue() { 48 return unknownValue; 49 } 50 51 /** 52 * @return name of cipher suite, as in {@link javax.crypto.Cipher} 53 */ getName()54 public String getName() { 55 return name; 56 } 57 58 /** 59 * @return size of an algorithm block in bytes 60 */ getAlgorithmBlockSize()61 public int getAlgorithmBlockSize() { 62 return algoBlockSize; 63 } 64 65 @Override toString()66 public String toString() { 67 StringBuilder builder = new StringBuilder("{"); 68 builder.append("name: " + name); 69 builder.append(", algorithmBlockSize: " + algoBlockSize); 70 if (unknownValue != null) { 71 builder.append(", unknownValue: " + unknownValue); 72 } 73 builder.append("}"); 74 return builder.toString(); 75 } 76 77 /** 78 * Convert to CipherSuite from name, {@link #algoBlockSize} is fixed for 79 * certain cipher suite, just need to compare the name. 80 * @param name cipher suite name 81 * @return CipherSuite cipher suite 82 */ convert(String name)83 public static CipherSuite convert(String name) { 84 CipherSuite[] suites = CipherSuite.values(); 85 for (CipherSuite suite : suites) { 86 if (suite.getName().equals(name)) { 87 return suite; 88 } 89 } 90 throw new IllegalArgumentException("Invalid cipher suite name: " + name); 91 } 92 93 /** 94 * Returns suffix of cipher suite configuration. 95 * @return String configuration suffix 96 */ getConfigSuffix()97 public String getConfigSuffix() { 98 String[] parts = name.split("/"); 99 StringBuilder suffix = new StringBuilder(); 100 for (String part : parts) { 101 suffix.append(".").append(StringUtils.toLowerCase(part)); 102 } 103 104 return suffix.toString(); 105 } 106 } 107