-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet_Key_Class.java
207 lines (159 loc) · 7.57 KB
/
Get_Key_Class.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.security.NoSuchProviderException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.MessageDigest;
import java.security.DigestException;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
public class Get_Key_Class {
// key struct package contains secret key and secure randoms
public static Key_Struct_Package create_cryptographic_requirements( byte[] nonce, char[] _character_password )
throws DigestException,
NoSuchAlgorithmException,
NoSuchProviderException {
final int _num_of_iterations = (int)(Math.pow(2, 16)) + 100;
final int _resulting_array_length = 64;
final int _output_key_length = _resulting_array_length/4;
byte[] _resutl = new byte[_resulting_array_length]; //temporarily storing final hash value
byte[] salt = new byte[_resulting_array_length];
byte[] _byte_passwd = new byte[_character_password.length];
byte[][] _store_in_bytes = new byte[_num_of_iterations][_resulting_array_length];
byte[]_key_bytes = new byte[_output_key_length];
byte[]iv = new byte[ _output_key_length ];
byte[]_random_seed = new byte[ _resutl.length - _key_bytes.length - iv.length ];
Key_Struct_Package keyPackage = new Key_Struct_Package();
try {
//Convert character password to byte password
_byte_passwd = Byte_Conversions.convert_char_to_byte( _character_password );
//hash function for encoding
MessageDigest _message_digest = MessageDigest.getInstance("SHA-512");
//store salt returned from nonce
_message_digest.update( nonce );
_message_digest.digest( salt, 0, salt.length );
//Create the first hash
_message_digest.update( _byte_passwd );
_message_digest.update( salt );
_message_digest.digest(_store_in_bytes[0], 0, _store_in_bytes[0].length ); //Outputs to byteSTore[0]
//Iteratively hash a num_of_iterations times
for( int i = 1; i < _num_of_iterations; i++ ) {
_message_digest.update( _store_in_bytes[i-1] ); //Previous hash
_message_digest.update( _byte_passwd );
_message_digest.update( salt );
_message_digest.digest( _store_in_bytes[i], 0, _store_in_bytes[i].length ); //Store current hash
}
Byte_Conversions.make_zero( salt );
Byte_Conversions.make_zero( _byte_passwd );
_message_digest.update( _store_in_bytes[_num_of_iterations-1] );
for ( int i = 0; i < 4096; i++ ) {
for( int j = i; j < _num_of_iterations; j+= 4096 ) {
_message_digest.update( _store_in_bytes[j] );
Byte_Conversions.make_zero( _store_in_bytes[j] ); //Zeroize as we go
}
}
_message_digest.digest( _resutl, 0, _resutl.length ); //Stores in _resutl
int i = 0;
//Copy key over to its own array
for( int j = 0; j < _key_bytes.length & i < _resutl.length; ) {
_key_bytes[i] = _resutl[i];
j++;
i++;
}
for( int j = 0; j < iv.length & i < _resutl.length; ) {
iv[j] = _resutl[i];
j++;
i++;
}
for( int j = 0; j< _random_seed.length & i < _resutl.length; ) {
_random_seed[j] = _resutl[i];
j++;
i++;
}
Byte_Conversions.make_zero( _resutl );
keyPackage.key = new SecretKeySpec( _key_bytes, "AES" );
Byte_Conversions.make_zero( _key_bytes );
keyPackage.ivParam = new IvParameterSpec( iv );
Byte_Conversions.make_zero( iv );
keyPackage.rand = SecureRandom.getInstance( "SHA1PRNG", "SUN" );
keyPackage.rand.setSeed( _random_seed );
Byte_Conversions.make_zero( _random_seed );
} catch ( Exception ex ) {
ex.printStackTrace();
try {
Byte_Conversions.make_zero( _resutl );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( _key_bytes );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( iv );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( _random_seed );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
keyPackage.make_zero();
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( _byte_passwd );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( _character_password );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( salt );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
Byte_Conversions.make_zero( nonce );
} catch ( Exception e ) {
e.printStackTrace();
}
for ( int i = 0; i < _num_of_iterations; i++ ) {
try {
Byte_Conversions.make_zero( _store_in_bytes[i] );
} catch ( Exception e ) {
e.printStackTrace();
}
}
throw ex;
}
return keyPackage;
}
// returns hash of desired length
public static byte[] fetch_SHA_512_hash( byte[] input, int _apt_byte_length ) throws NoSuchAlgorithmException {
MessageDigest _message_digest = MessageDigest.getInstance("SHA-512");
byte[] _hash_bytes = _message_digest.digest( input );
int size = Math.min( _hash_bytes.length, _apt_byte_length );
byte[] output = new byte[size];
for( int i = 0; i < size; i++ ) {
output[i] = _hash_bytes[i];
}
Byte_Conversions.make_zero( _hash_bytes );
return output;
}
// validates the hash by checking calculated hash against expected hash
public static boolean _validation_of_SHA_512_hash( byte[] input, byte[] _expected_hash_value ) throws NoSuchAlgorithmException {
boolean result = false;
if ( _expected_hash_value.length > 0 ) {
byte[] calculatedHash = Get_Key_Class.fetch_SHA_512_hash( input, _expected_hash_value.length );
result = Byte_Conversions.array_comparison( calculatedHash, _expected_hash_value );
Byte_Conversions.make_zero( calculatedHash );
}
return result;
}
}