edu.biu.scapi.primitives.prf.bc.BcAES.java Source code

Java tutorial

Introduction

Here is the source code for edu.biu.scapi.primitives.prf.bc.BcAES.java

Source

/**
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* 
* Copyright (c) 2012 - SCAPI (http://crypto.biu.ac.il/scapi)
* This file is part of the SCAPI project.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* 
* We request that any publication and/or code referring to and/or based on SCAPI contain an appropriate citation to SCAPI, including a reference to
* http://crypto.biu.ac.il/SCAPI.
* 
* SCAPI uses Crypto++, Miracl, NTL and Bouncy Castle. Please see these projects for any further licensing issues.
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* 
*/

package edu.biu.scapi.primitives.prf.bc;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;

import javax.crypto.SecretKey;

import org.bouncycastle.crypto.engines.AESEngine;

import edu.biu.scapi.primitives.prf.AES;

/**
 * Concrete class of prf family for AES. This class wraps the implementation of Bouncy castle.
 * 
 * @author Cryptography and Computer Security Research Group Department of Computer Science Bar-Ilan University (Meital Levy)
 *
 */
public final class BcAES extends BcPRP implements AES {

    /**
     * Passes the AESEngine of BC to the abstract super class
     */
    public BcAES() {
        super(new AESEngine());

    }

    /**
     * Receives random object to use.
     * Passes it and the DesedeEngine of BC to the abstract super class.
     * @param random SecureRandom to use
     */
    public BcAES(SecureRandom random) {
        super(new AESEngine(), random);
    }

    /**
     * Receives name of random algorithm to use.
     * Passes it and the AESEngine of BC to the abstract super class.
     * @param randNumGenAlg random algorithm to use
     * @throws NoSuchAlgorithmException 
     */
    public BcAES(String randNumGenAlg) throws NoSuchAlgorithmException {
        super(new AESEngine(), SecureRandom.getInstance(randNumGenAlg));

    }

    /**
     * initializes this AES with secret key.
     * @param secretKey the secret key
     * @throws InvalidKeyException 
     */
    public void setKey(SecretKey secretKey) throws InvalidKeyException {
        int len = secretKey.getEncoded().length;
        //AES key size should be 128/192/256 bits long
        if (len != 16 && len != 24 && len != 32) {
            throw new InvalidKeyException("AES key size should be 128/192/256 bits long");
        }
        super.setKey(secretKey);
    }

    /**
     * This function should not be used to generate a key for AES and it throws UnsupportedOperationException
     * @param keyParams algorithmParameterSpec contains the required secret key size in bits 
     * @return the generated secret key
     * @throws UnsupportedOperationException 
     */
    public SecretKey generateKey(AlgorithmParameterSpec keyParams) throws InvalidParameterSpecException {
        throw new UnsupportedOperationException(
                "To generate a key for this prf object use the generateKey(int keySize) function");
    }
}