Changeset 5361571 for core


Ignore:
Timestamp:
May 25, 2017 6:05:45 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
d50c12dc
Parents:
13d9322
Message:

Crypto: Fix AES NPE on 4-core RPi 2nd try (ticket #1989)

Location:
core/java/src/net/i2p/crypto
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/CryptixAESKeyCache.java

    r13d9322 r5361571  
    7070        /** decryption round keys */
    7171        final int[][] Kd;
    72         final int[]   tk;
    73         /** Ke, Kd */
    74         final Object[] key;
    7572
    7673        public KeyCacheEntry() {
    7774            Ke = new int[ROUNDS + 1][BC];
    7875            Kd = new int[ROUNDS + 1][BC];
    79             tk = new int[KC];
    80             key = new Object[] { Ke, Kd };
     76        }
     77
     78        /** @since 0.9.31 */
     79        public KeyCacheEntry(int rounds, int bc) {
     80            Ke = new int[rounds + 1][bc];
     81            Kd = new int[rounds + 1][bc];
    8182        }
    8283    }
  • core/java/src/net/i2p/crypto/CryptixRijndael_Algorithm.java

    r13d9322 r5361571  
    397397     * @param  result     The resulting ciphertext.
    398398     * @param  inOffset   Index of in from which to start considering data.
    399      * @param  sessionKey The session key to use for encryption. This is an array of two int[][].
    400                           We use the first one, i.e. sessionKey[0], for encryption.
     399     * @param  sessionKey The session key to use for encryption. This is a CryptixAESKeyCache.KeyCacheEntry.
     400     *                    We use the Ke field for encryption.
     401     *                    The actual parameter type is opaque, changed in 0.9.31, and is subject to change again.
    401402     */
    402403    public static final void blockEncrypt(byte[] in, byte[] result, int inOffset, int outOffset, Object sessionKey) {
    403404        //if (_RDEBUG) trace(_IN, "blockEncrypt(" + in + ", " + inOffset + ", " + sessionKey + ")");
    404         int[][] Ke = (int[][]) ((Object[]) sessionKey)[0]; // extract encryption round keys
     405        int[][] Ke = ((CryptixAESKeyCache.KeyCacheEntry) sessionKey).Ke; // extract encryption round keys
    405406        int ROUNDS = Ke.length - 1;
    406407        int[] Ker = Ke[0];
     
    472473     * @param  result     The resulting ciphertext
    473474     * @param  inOffset   Index of in from which to start considering data.
    474      * @param  sessionKey The session key to use for decryption. This is an array of two int[][].
    475                           We use the second one, i.e. sessionKey[1], for decryption.
     475     * @param  sessionKey The session key to use for decryption. This is a CryptixAESKeyCache.KeyCacheEntry.
     476     *                    We use the Kd field, for decryption.
     477     *                    The actual parameter type is opaque, changed in 0.9.31, and is subject to change again.
    476478     */
    477479    public static final void blockDecrypt(byte[] in, byte[] result, int inOffset, int outOffset, Object sessionKey) {
     
    482484            throw new IllegalArgumentException("data too small: " + in.length + " inOffset: " + inOffset);
    483485        //if (_RDEBUG) trace(_IN, "blockDecrypt(" + in + ", " + inOffset + ", " + sessionKey + ")");
    484         int[][] Kd = (int[][]) ((Object[]) sessionKey)[1]; // extract decryption round keys
     486        int[][] Kd = ((CryptixAESKeyCache.KeyCacheEntry) sessionKey).Kd; // extract decryption round keys
    485487        int ROUNDS = Kd.length - 1;
    486488        int[] Kdr = Kd[0];
     
    566568     * @param blockSize  The block size in bytes of this Rijndael, must be 16, 24, or 32.
    567569     * @throws  InvalidKeyException  If the key is invalid.
    568      * @return an array of two int[][] containing { Ke, Kd }
     570     * @return a CryptixAESKeyCache.KeyCacheEntry containing { Ke, Kd }.
     571     *         The actual return type is opaque, changed in 0.9.31, and is subject to change again.
    569572     */
    570573    public static final Object makeKey(byte[] k, int blockSize) throws InvalidKeyException {
     
    580583     * @param keyData a cached data structure to fill in, or null.
    581584     * @throws  InvalidKeyException  If the key is invalid.
    582      * @return an array of two int[][] containing { Ke, Kd }.
    583      *         If keyData is non-null, this is keyData.key.
     585     * @return a CryptixAESKeyCache.KeyCacheEntry containing { Ke, Kd }.
     586     *         If keyData is non-null, this is keyData.
    584587     *         If keyData is null, this is newly allocated.
    585588     */
     
    595598        int ROUND_KEY_COUNT = (ROUNDS + 1) * BC;
    596599        int KC = k.length / 4;
    597         int[] tk; // new int[KC];
     600        int[] tk = new int[KC];
    598601        int i, j;
    599         // the return value
    600         Object[] sessionKey;
    601602
    602603        if (keyData == null) {
    603             Ke = new int[ROUNDS + 1][BC];
    604             Kd = new int[ROUNDS + 1][BC];
    605             tk = new int[KC];
    606             sessionKey = new Object[] { Ke, Kd};
    607         } else {
    608             Ke = keyData.Ke;
    609             Kd = keyData.Kd;
    610             tk = keyData.tk;
    611             sessionKey = keyData.key;
    612         }
     604            keyData = new CryptixAESKeyCache.KeyCacheEntry(ROUNDS, BC);
     605        }
     606        Ke = keyData.Ke;
     607        Kd = keyData.Kd;
    613608
    614609        // copy user material bytes into temporary ints
     
    669664            }
    670665        }
    671         return sessionKey;
     666        return keyData;
    672667    }
    673668
     
    678673     * @param  result     The resulting ciphertext.
    679674     * @param  inOffset   Index of in from which to start considering data.
    680      * @param  sessionKey The session key to use for encryption. This is an array of two int[][].
    681                           We use the first one, i.e. sessionKey[0], for encryption.
     675     * @param  sessionKey The session key to use for encryption. This is a CryptixAESKeyCache.KeyCacheEntry.
     676     *                    We use the Ke field for encryption.
     677     *                    The actual parameter type is opaque, changed in 0.9.31, and is subject to change again.
    682678     * @param  blockSize  The block size in bytes of this Rijndael.
    683679     */
     
    688684        }
    689685        //if (_RDEBUG) trace(_IN, "blockEncrypt(" + in + ", " + inOffset + ", " + sessionKey + ", " + blockSize + ")");
    690         Object[] sKey = (Object[]) sessionKey; // extract encryption round keys
    691         int[][] Ke = (int[][]) sKey[0];
     686        int[][] Ke = ((CryptixAESKeyCache.KeyCacheEntry) sessionKey).Ke; // extract encryption round keys
    692687
    693688        int BC = blockSize / 4;
     
    739734     * @param  result     The resulting ciphertext.
    740735     * @param  inOffset   Index of in from which to start considering data.
    741      * @param  sessionKey The session key to use for decryption. This is an array of two int[][].
    742                           We use the second one, i.e. sessionKey[1], for decryption.
     736     * @param  sessionKey The session key to use for decryption. This is a CryptixAESKeyCache.KeyCacheEntry.
     737     *                    We use the Kd field, for decryption.
     738     *                    The actual parameter type is opaque, changed in 0.9.31, and is subject to change again.
    743739     * @param  blockSize  The block size in bytes of this Rijndael.
    744740     */
     
    750746
    751747        //if (_RDEBUG) trace(_IN, "blockDecrypt(" + in + ", " + inOffset + ", " + sessionKey + ", " + blockSize + ")");
    752         Object[] sKey = (Object[]) sessionKey; // extract decryption round keys
    753         int[][] Kd = (int[][]) sKey[1];
     748        int[][] Kd = ((CryptixAESKeyCache.KeyCacheEntry) sessionKey).Kd; // extract decryption round keys
    754749
    755750        int BC = blockSize / 4;
Note: See TracChangeset for help on using the changeset viewer.