[ 
https://issues.apache.org/jira/browse/NIFI-1257?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15128994#comment-15128994
 ] 

ASF GitHub Bot commented on NIFI-1257:
--------------------------------------

Github user alopresto commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/201#discussion_r51632097
  
    --- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/crypto/CipherUtility.java
 ---
    @@ -0,0 +1,271 @@
    +/*
    + * 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.nifi.processors.standard.util.crypto;
    +
    +import org.apache.commons.codec.binary.Base64;
    +import org.apache.commons.lang3.StringUtils;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.stream.io.ByteArrayOutputStream;
    +import org.apache.nifi.stream.io.StreamUtils;
    +
    +import javax.crypto.Cipher;
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.io.OutputStream;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +import java.util.regex.Matcher;
    +import java.util.regex.Pattern;
    +
    +public class CipherUtility {
    +
    +    public static final int BUFFER_SIZE = 65536;
    +
    +    /**
    +     * Returns the cipher algorithm from the full algorithm name. Useful 
for getting key lengths, etc.
    +     * <p/>
    +     * Ex: PBEWITHMD5AND128BITAES-CBC-OPENSSL -> AES
    +     *
    +     * @param algorithm the full algorithm name
    +     * @return the generic cipher name or the full algorithm if one cannot 
be extracted
    +     */
    +    public static String parseCipherFromAlgorithm(final String algorithm) {
    +        if (StringUtils.isEmpty(algorithm)) {
    +            return algorithm;
    +        }
    +        String formattedAlgorithm = algorithm.toUpperCase();
    +
    +        // This is not optimal but the algorithms do not have a standard 
format
    +        final String AES = "AES";
    +        final String TDES = "TRIPLEDES";
    +        final String TDES_ALTERNATE = "DESEDE";
    +        final String DES = "DES";
    +        final String RC4 = "RC4";
    +        final String RC2 = "RC2";
    +        final String TWOFISH = "TWOFISH";
    +        final List<String> SYMMETRIC_CIPHERS = Arrays.asList(AES, TDES, 
TDES_ALTERNATE, DES, RC4, RC2, TWOFISH);
    +
    +        // The algorithms contain "TRIPLEDES" but the cipher name is 
"DESede"
    +        final String ACTUAL_TDES_CIPHER = "DESede";
    +
    +        for (String cipher : SYMMETRIC_CIPHERS) {
    +            if (formattedAlgorithm.contains(cipher)) {
    +                if (cipher.equals(TDES) || cipher.equals(TDES_ALTERNATE)) {
    +                    return ACTUAL_TDES_CIPHER;
    +                } else {
    +                    return cipher;
    +                }
    +            }
    +        }
    +
    +        return algorithm;
    +    }
    +
    +    /**
    +     * Returns the cipher key length from the full algorithm name. Useful 
for getting key lengths, etc.
    +     * <p/>
    +     * Ex: PBEWITHMD5AND128BITAES-CBC-OPENSSL -> 128
    +     *
    +     * @param algorithm the full algorithm name
    +     * @return the key length or -1 if one cannot be extracted
    +     */
    +    public static int parseKeyLengthFromAlgorithm(final String algorithm) {
    +        int keyLength = parseActualKeyLengthFromAlgorithm(algorithm);
    +        if (keyLength != -1) {
    +            return keyLength;
    +        } else {
    +            // Key length not explicitly named in algorithm
    +            String cipher = parseCipherFromAlgorithm(algorithm);
    +            return getDefaultKeyLengthForCipher(cipher);
    +        }
    +    }
    +
    +    private static int parseActualKeyLengthFromAlgorithm(final String 
algorithm) {
    +        Pattern pattern = Pattern.compile("([\\d]+)BIT");
    --- End diff --
    
    Will do. 


> Provide additional KDFs for EncryptContent
> ------------------------------------------
>
>                 Key: NIFI-1257
>                 URL: https://issues.apache.org/jira/browse/NIFI-1257
>             Project: Apache NiFi
>          Issue Type: Improvement
>          Components: Core Framework
>    Affects Versions: 0.4.0
>            Reporter: Andy LoPresto
>            Assignee: Andy LoPresto
>            Priority: Critical
>              Labels: encryption, security
>             Fix For: 0.5.0
>
>
> Currently, the two key derivation functions (KDF) supported are NiFi Legacy 
> (1000 iterations of MD5 digest over a password and optional salt) and OpenSSL 
> PKCS#5 v1.5 (a single iteration of MD5 digest over a password and optional 
> salt). 
> Both of these are very weak -- they use a deprecated cryptographic hash 
> function (CHF) with known weakness and susceptibility to collisions (with 
> demonstrated attacks) and a non-configurable and tightly coupled iteration 
> count to derive the key and IV. 
> Current best practice KDFs (with work factor recommendations) are as follows:
> * PBKDF2 with variable hash function (SHA1, SHA256, SHA384, SHA512, or 
> ideally HMAC variants of these functions) and variable iteration count (in 
> the 10k - 1M range). 
> * bcrypt with work factor of 12 - 16
> * scrypt with work factor of (2^14 - 2^20, 8, 1)
> The salt and iteration count should be stored alongside the hashed record 
> (bcrypt handles this natively). 
> Notes:
> * http://wildlyinaccurate.com/bcrypt-choosing-a-work-factor/
> * http://blog.ircmaxell.com/2012/12/seven-ways-to-screw-up-bcrypt.html
> * 
> http://security.stackexchange.com/questions/17207/recommended-of-rounds-for-bcrypt
> * 
> http://security.stackexchange.com/questions/3959/recommended-of-iterations-when-using-pkbdf2-sha256/3993#3993
> * 
> http://security.stackexchange.com/questions/4781/do-any-security-experts-recommend-bcrypt-for-password-storage/6415
>  
> * 
> http://web.archive.org/web/20130407190430/http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html
> * 
> https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2015/march/enough-with-the-salts-updates-on-secure-password-schemes/
> * http://www.tarsnap.com/scrypt.html
> * http://www.tarsnap.com/scrypt/scrypt.pdf



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to