1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46:
47: import ;
48:
49:
53: public abstract class BaseKeyWrappingAlgorithm
54: implements IKeyWrappingAlgorithm
55: {
56:
57: protected String name;
58:
59: private PRNG prng;
60:
61:
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:
138: protected PRNG getDefaultPRNG()
139: {
140: if (prng == null)
141: prng = PRNG.getInstance();
142:
143: return prng;
144: }
145: }