1 |
769 |
jeremybenn |
/* BaseKeyWrappingAlgorithm.java -- FIXME: briefly describe file purpose
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.kwa;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.util.PRNG;
|
42 |
|
|
|
43 |
|
|
import java.security.InvalidKeyException;
|
44 |
|
|
import java.util.Collections;
|
45 |
|
|
import java.util.Map;
|
46 |
|
|
|
47 |
|
|
import javax.crypto.ShortBufferException;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* A base class to facilitate implementation of concrete Key Wrapping
|
51 |
|
|
* Algorithms.
|
52 |
|
|
*/
|
53 |
|
|
public abstract class BaseKeyWrappingAlgorithm
|
54 |
|
|
implements IKeyWrappingAlgorithm
|
55 |
|
|
{
|
56 |
|
|
/** The canonical name of the key wrapping algorithm. */
|
57 |
|
|
protected String name;
|
58 |
|
|
/** A source of randomness if/when needed by concrete implementations. */
|
59 |
|
|
private PRNG prng;
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Protected constructor.
|
63 |
|
|
*
|
64 |
|
|
* @param name the key wrapping algorithm canonical name.
|
65 |
|
|
*/
|
66 |
|
|
protected BaseKeyWrappingAlgorithm(String name)
|
67 |
|
|
{
|
68 |
|
|
super();
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
public String name()
|
72 |
|
|
{
|
73 |
|
|
return this.name;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
public void init(Map attributes) throws InvalidKeyException
|
77 |
|
|
{
|
78 |
|
|
if (attributes == null)
|
79 |
|
|
attributes = Collections.EMPTY_MAP;
|
80 |
|
|
|
81 |
|
|
engineInit(attributes);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
public int wrap(byte[] in, int inOffset, int length, byte[] out, int outOffset)
|
85 |
|
|
throws ShortBufferException
|
86 |
|
|
{
|
87 |
|
|
if (outOffset < 0)
|
88 |
|
|
throw new IllegalArgumentException("Output offset MUST NOT be negative");
|
89 |
|
|
byte[] result = wrap(in, inOffset, length);
|
90 |
|
|
if (outOffset + result.length > out.length)
|
91 |
|
|
throw new ShortBufferException();
|
92 |
|
|
System.arraycopy(result, 0, out, outOffset, result.length);
|
93 |
|
|
return result.length;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
public byte[] wrap(byte[] in, int inOffset, int length)
|
97 |
|
|
{
|
98 |
|
|
if (inOffset < 0)
|
99 |
|
|
throw new IllegalArgumentException("Input offset MUST NOT be negative");
|
100 |
|
|
if (length < 0)
|
101 |
|
|
throw new IllegalArgumentException("Input length MUST NOT be negative");
|
102 |
|
|
|
103 |
|
|
return engineWrap(in, inOffset, length);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
public int unwrap(byte[] in, int inOffset, int length,
|
107 |
|
|
byte[] out, int outOffset)
|
108 |
|
|
throws ShortBufferException, KeyUnwrappingException
|
109 |
|
|
{
|
110 |
|
|
if (outOffset < 0)
|
111 |
|
|
throw new IllegalArgumentException("Output offset MUST NOT be negative");
|
112 |
|
|
byte[] result = engineUnwrap(in, inOffset, length);
|
113 |
|
|
if (outOffset + result.length > out.length)
|
114 |
|
|
throw new ShortBufferException();
|
115 |
|
|
System.arraycopy(result, 0, out, outOffset, result.length);
|
116 |
|
|
return result.length;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
public byte[] unwrap(byte[] in, int inOffset, int length)
|
120 |
|
|
throws KeyUnwrappingException
|
121 |
|
|
{
|
122 |
|
|
if (inOffset < 0)
|
123 |
|
|
throw new IllegalArgumentException("Input offset MUST NOT be negative");
|
124 |
|
|
if (length < 0)
|
125 |
|
|
throw new IllegalArgumentException("Input length MUST NOT be negative");
|
126 |
|
|
|
127 |
|
|
return engineUnwrap(in, inOffset, length);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
protected abstract void engineInit(Map attributes) throws InvalidKeyException;
|
131 |
|
|
|
132 |
|
|
protected abstract byte[] engineWrap(byte[] in, int inOffset, int length);
|
133 |
|
|
|
134 |
|
|
protected abstract byte[] engineUnwrap(byte[] in, int inOffset, int length)
|
135 |
|
|
throws KeyUnwrappingException;
|
136 |
|
|
|
137 |
|
|
/** @return a strong pseudo-random number generator if/when needed. */
|
138 |
|
|
protected PRNG getDefaultPRNG()
|
139 |
|
|
{
|
140 |
|
|
if (prng == null)
|
141 |
|
|
prng = PRNG.getInstance();
|
142 |
|
|
|
143 |
|
|
return prng;
|
144 |
|
|
}
|
145 |
|
|
}
|