spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vanzin <...@git.apache.org>
Subject [GitHub] spark pull request #15172: [SPARK-13331] AES support for over-the-wire encry...
Date Mon, 26 Sep 2016 17:17:25 GMT
Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15172#discussion_r80524986
  
    --- Diff: common/network-common/src/main/java/org/apache/spark/network/sasl/aes/SparkAesCipher.java
---
    @@ -0,0 +1,270 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.network.sasl.aes;
    +
    +import java.io.IOException;
    +import java.security.InvalidAlgorithmParameterException;
    +import java.security.InvalidKeyException;
    +import java.security.NoSuchAlgorithmException;
    +import java.util.Arrays;
    +import java.util.Properties;
    +import javax.crypto.Cipher;
    +import javax.crypto.Mac;
    +import javax.crypto.SecretKey;
    +import javax.crypto.ShortBufferException;
    +import javax.crypto.spec.SecretKeySpec;
    +import javax.crypto.spec.IvParameterSpec;
    +import javax.security.sasl.SaslException;
    +
    +import org.apache.commons.crypto.cipher.CryptoCipher;
    +import org.apache.commons.crypto.utils.Utils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * AES cipher for encryption and decryption.
    + */
    +public class SparkAesCipher {
    +  private static final Logger logger = LoggerFactory.getLogger(SparkAesCipher.class);
    +  private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
    +  public final static String supportedTransformation[] = {
    +    "AES/CBC/NoPadding", "AES/CTR/NoPadding"
    +  };
    +
    +  private final CryptoCipher encryptor;
    +  private final CryptoCipher decryptor;
    +
    +  private final Integrity integrity;
    +
    +  public SparkAesCipher(
    +      String cipherTransformation,
    +      Properties properties,
    +      byte[] inKey,
    +      byte[] outKey,
    +      byte[] inIv,
    +      byte[] outIv) throws IOException {
    +    if (!Arrays.asList(supportedTransformation).contains(cipherTransformation)) {
    +      logger.warn("AES cipher transformation is not supported: " + cipherTransformation);
    +      cipherTransformation = "AES/CTR/NoPadding";
    +      logger.warn("Use default AES/CTR/NoPadding");
    +    }
    +
    +    final SecretKeySpec inKeySpec = new SecretKeySpec(inKey, "AES");
    +    final IvParameterSpec inIvSpec = new IvParameterSpec(inIv);
    +    final SecretKeySpec outKeySpec = new SecretKeySpec(outKey, "AES");
    +    final IvParameterSpec outIvSpec = new IvParameterSpec(outIv);
    +
    +    // Encryptor
    +    encryptor = Utils.getCipherInstance(cipherTransformation, properties);
    +    try {
    +      logger.debug("Initialize encryptor");
    +      encryptor.init(Cipher.ENCRYPT_MODE, outKeySpec, outIvSpec);
    +    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
    +      throw new IOException("Failed to initialize encryptor", e);
    +    }
    +
    +    // Decryptor
    +    decryptor = Utils.getCipherInstance(cipherTransformation, properties);
    +    try {
    +      logger.debug("Initialize decryptor");
    +      decryptor.init(Cipher.DECRYPT_MODE, inKeySpec, inIvSpec);
    +    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
    +      throw new IOException("Failed to initialize decryptor", e);
    +    }
    +
    +    integrity = new Integrity(outKey, inKey);
    +  }
    +
    +  /**
    +   * Encrypts input data. The result composes of (msg, padding if needed, mac) and sequence
num.
    +   * @param data the input byte array
    +   * @param offset the offset in input where the input starts
    +   * @param len the input length
    +   * @return the new encrypted byte array.
    +   * @throws SaslException if error happens
    +   */
    +  public byte[] wrap(byte[] data, int offset, int len) throws SaslException {
    --- End diff --
    
    See previous comment. You do not need to implement JDK interfaces here, especially because
they don't really add any needed functionality.
    
    They're needed for the SASL encryption backend because that's how you do it with those
APIs. It doesn't need the AES encryption code needs to implement those APIs too.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message