1 /*
2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.crypto.provider;
27 
28 import java.security.SecureRandom;
29 import java.security.InvalidParameterException;
30 import java.security.InvalidAlgorithmParameterException;
31 import java.security.spec.AlgorithmParameterSpec;
32 import javax.crypto.KeyGeneratorSpi;
33 import javax.crypto.SecretKey;
34 import javax.crypto.spec.SecretKeySpec;
35 
36 /**
37  * This class generates a secret key for use with the HMAC-MD5 algorithm.
38  *
39  * @author Jan Luehe
40  *
41  */
42 
43 public final class HmacMD5KeyGenerator extends KeyGeneratorSpi {
44 
45     private SecureRandom random = null;
46     private int keysize = 64; // default keysize (in number of bytes)
47 
48     /**
49      * Empty constructor
50      */
HmacMD5KeyGenerator()51     public HmacMD5KeyGenerator() {
52     }
53 
54     /**
55      * Initializes this key generator.
56      *
57      * @param random the source of randomness for this generator
58      */
engineInit(SecureRandom random)59     protected void engineInit(SecureRandom random) {
60         this.random = random;
61     }
62 
63     /**
64      * Initializes this key generator with the specified parameter
65      * set and a user-provided source of randomness.
66      *
67      * @param params the key generation parameters
68      * @param random the source of randomness for this key generator
69      *
70      * @exception InvalidAlgorithmParameterException if <code>params</code> is
71      * inappropriate for this key generator
72      */
engineInit(AlgorithmParameterSpec params, SecureRandom random)73     protected void engineInit(AlgorithmParameterSpec params,
74                               SecureRandom random)
75         throws InvalidAlgorithmParameterException
76     {
77         throw new InvalidAlgorithmParameterException
78             ("HMAC-MD5 key generation does not take any parameters");
79     }
80 
81     /**
82      * Initializes this key generator for a certain keysize, using the given
83      * source of randomness.
84      *
85      * @param keysize the keysize. This is an algorithm-specific
86      * metric specified in number of bits.
87      * @param random the source of randomness for this key generator
88      */
engineInit(int keysize, SecureRandom random)89     protected void engineInit(int keysize, SecureRandom random) {
90         this.keysize = (keysize+7) / 8;
91         this.engineInit(random);
92     }
93 
94     /**
95      * Generates an HMAC-MD5 key.
96      *
97      * @return the new HMAC-MD5 key
98      */
engineGenerateKey()99     protected SecretKey engineGenerateKey() {
100         if (this.random == null) {
101             this.random = SunJCE.getRandom();
102         }
103 
104         byte[] keyBytes = new byte[this.keysize];
105         this.random.nextBytes(keyBytes);
106 
107         return new SecretKeySpec(keyBytes, "HmacMD5");
108     }
109 }
110