Newer
Older
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
goog.module('tink.aead.AesGcmKeyManagerTest');
goog.setTestOnly('tink.aead.AesGcmKeyManagerTest');
const Aead = goog.require('tink.Aead');
const AesGcmKeyManager = goog.require('tink.aead.AesGcmKeyManager');
const Mac = goog.require('tink.Mac');
const PbAesCtrKey = goog.require('proto.google.crypto.tink.AesCtrKey');
const PbAesCtrKeyFormat = goog.require('proto.google.crypto.tink.AesCtrKeyFormat');
const PbAesGcmKey = goog.require('proto.google.crypto.tink.AesGcmKey');
const PbAesGcmKeyFormat = goog.require('proto.google.crypto.tink.AesGcmKeyFormat');
const PbKeyData = goog.require('proto.google.crypto.tink.KeyData');
const Random = goog.require('tink.subtle.Random');
const testSuite = goog.require('goog.testing.testSuite');
const KEY_TYPE = 'type.googleapis.com/google.crypto.tink.AesGcmKey';
const VERSION = 0;
const PRIMITIVE = Aead;
testSuite({
/////////////////////////////////////////////////////////////////////////////
// tests for newKey method
// newKey method -- key formats
testNewKey_invalidKeyFormat() {
const keyFormat = new PbAesCtrKeyFormat();
const manager = new AesGcmKeyManager();
try {
manager.getKeyFactory().newKey(keyFormat);
fail('An exception should be thrown.');
} catch (e) {
assertEquals(ExceptionText.invalidKeyFormat(), e.toString());
}
},
testNewKey_invalidSerializedKeyFormat() {
const keyFormat = new Uint8Array(0);
const manager = new AesGcmKeyManager();
try {
manager.getKeyFactory().newKey(keyFormat);
fail('An exception should be thrown.');
} catch (e) {
assertEquals(ExceptionText.invalidSerializedKeyFormat(), e.toString());
}
},
testNewKey_unsupportedKeySizes() {
const manager = new AesGcmKeyManager();
for (let keySize = 0; keySize < 40; keySize++) {
if (keySize === 16 || keySize === 32) {
// Keys of size 16 and 32 bytes are supported.
continue;
}
const keyFormat = createTestKeyFormat(keySize);
try {
manager.getKeyFactory().newKey(keyFormat);
fail('An exception should be thrown.');
} catch (e) {
assertEquals(ExceptionText.unsupportedKeySize(keySize), e.toString());
}
}
},
testNewKey_viaFormatProto() {
const manager = new AesGcmKeyManager();
const keyFormat = createTestKeyFormat();
const key =
/** @type {!PbAesGcmKey}*/ (manager.getKeyFactory().newKey(keyFormat));
assertEquals(keyFormat.getKeySize(), key.getKeyValue().length);
},
testNewKey_viaSerializedFormatProto() {
const manager = new AesGcmKeyManager();
const keyFormat = createTestKeyFormat();
const serializedKeyFormat = keyFormat.serializeBinary();
const key = /** @type {!PbAesGcmKey} */ (
manager.getKeyFactory().newKey(serializedKeyFormat));
assertEquals(keyFormat.getKeySize(), key.getKeyValue().length);
},
/////////////////////////////////////////////////////////////////////////////
// tests for NewKeyData method
testNewKeyData_shouldWork() {
const keyFormat = createTestKeyFormat();
const serializedKeyFormat = keyFormat.serializeBinary();
const manager = new AesGcmKeyManager();
const keyData = manager.getKeyFactory().newKeyData(serializedKeyFormat);
assertEquals(KEY_TYPE, keyData.getTypeUrl());
assertEquals(
PbKeyData.KeyMaterialType.SYMMETRIC, keyData.getKeyMaterialType());
const key = PbAesGcmKey.deserializeBinary(keyData.getValue());
assertEquals(keyFormat.getKeySize(), key.getKeyValue().length);
},
/////////////////////////////////////////////////////////////////////////////
// tests for getPrimitive method
async testGetPrimitive_unsupportedKeyDataType() {
const manager = new AesGcmKeyManager();
const keyData = createTestKeyData().setTypeUrl('bad_type_url');
try {
await manager.getPrimitive(PRIMITIVE, keyData);
fail('An exception should be thrown');
} catch (e) {
assertEquals(
ExceptionText.unsupportedKeyType(keyData.getTypeUrl()), e.toString());
}
},
async testGetPrimitive_unsupportedKeyType() {
const manager = new AesGcmKeyManager();
const key = new PbAesCtrKey();
try {
await manager.getPrimitive(PRIMITIVE, key);
fail('An exception should be thrown');
} catch (e) {
assertEquals(ExceptionText.unsupportedKeyType(), e.toString());
}
},
async testGetPrimitive_badVersion() {
const version = 1;
const manager = new AesGcmKeyManager();
const key = createTestKey().setVersion(version);
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
try {
await manager.getPrimitive(PRIMITIVE, key);
fail('An exception should be thrown');
} catch (e) {
assertEquals(ExceptionText.versionOutOfBounds(), e.toString());
}
},
async testGetPrimitive_unsupportedKeySizes() {
const manager = new AesGcmKeyManager();
for (let keySize = 0; keySize < 40; keySize++) {
if (keySize === 16 || keySize === 32) {
// Keys of sizes 16 and 32 bytes are supported.
continue;
}
const /** !PbAesGcmKey */ key = createTestKey(keySize);
try {
await manager.getPrimitive(PRIMITIVE, key);
fail('An exception should be thrown');
} catch (e) {
assertEquals(ExceptionText.unsupportedKeySize(keySize), e.toString());
}
}
},
async testGetPrimitive_badSerialization() {
const manager = new AesGcmKeyManager();
const keyData = createTestKeyData().setValue(new Uint8Array([]));
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
try {
await manager.getPrimitive(PRIMITIVE, keyData);
fail('An exception should be thrown');
} catch (e) {
assertEquals(ExceptionText.invalidSerializedKey(), e.toString());
}
},
async testGetPrimitive_unsupportedPrimitive() {
const manager = new AesGcmKeyManager();
const keyData = createTestKeyData();
try {
await manager.getPrimitive(Mac, keyData);
fail('An exception should be thrown.');
} catch (e) {
assertEquals(ExceptionText.unsupportedPrimitive(), e.toString());
}
},
// Tests for getting primitive from valid key/keyData.
async testGetPrimitive_fromKey() {
const manager = new AesGcmKeyManager();
const key = createTestKey();
// Get the primitive from key manager.
const /** Aead */ primitive = await manager.getPrimitive(PRIMITIVE, key);
// Test the returned primitive.
const plaintext = Random.randBytes(8);
const aad = Random.randBytes(8);
const ciphertext = await primitive.encrypt(plaintext, aad);
const decryptedCiphertext = await primitive.decrypt(ciphertext, aad);
assertObjectEquals(plaintext, decryptedCiphertext);
},
async testGetPrimitive_fromKeyData() {
const manager = new AesGcmKeyManager();
const keyData = createTestKeyData();
// Get primitive.
const /** Aead */ primitive =
await manager.getPrimitive(PRIMITIVE, keyData);
// Test the returned primitive.
const plaintext = Random.randBytes(8);
const aad = Random.randBytes(8);
const ciphertext = await primitive.encrypt(plaintext, aad);
const decryptedCiphertext = await primitive.decrypt(ciphertext, aad);
assertObjectEquals(plaintext, decryptedCiphertext);
},
/////////////////////////////////////////////////////////////////////////////
// tests for getVersion, getKeyType and doesSupport methods
testGetVersion_shouldBeZero() {
const manager = new AesGcmKeyManager();
assertEquals(0, manager.getVersion());
},
testGetKeyType_shouldBeAesGcmKeyType() {
const manager = new AesGcmKeyManager();
assertEquals(KEY_TYPE, manager.getKeyType());
},
testDoesSupport_shouldSupportAesGcmKeyType() {
const manager = new AesGcmKeyManager();
assertTrue(manager.doesSupport(KEY_TYPE));
},
testGetPrimitiveType_shouldBeAead() {
const manager = new AesGcmKeyManager();
assertEquals(PRIMITIVE, manager.getPrimitiveType());
},
});
/////////////////////////////////////////////////////////////////////////////
// Helper functions for tests
class ExceptionText {
/** @return {string} */
static unsupportedPrimitive() {
return 'CustomError: Requested primitive type which is not supported ' +
'by this key manager.';
}
/**
* @param {number} keySize
* @return {string}
*/
static unsupportedKeySize(keySize) {
return 'CustomError: unsupported AES key size: ' + keySize;
}
/**
* @return {string}
*/
static versionOutOfBounds() {
return 'CustomError: Version is out of bound, must be between 0 and ' +
VERSION + '.';
}
/**
* @param {string=} opt_unsupportedKeyType
* @return {string}
*/
static unsupportedKeyType(opt_unsupportedKeyType) {
const prefix = 'CustomError: Key type';
const suffix =
'is not supported. This key manager supports ' + KEY_TYPE + '.';
if (opt_unsupportedKeyType) {
return prefix + ' ' + opt_unsupportedKeyType + ' ' + suffix;
} else {
return prefix + ' ' + suffix;
}
}
/**
* @return {string}
*/
static invalidSerializedKey() {
return 'CustomError: Could not parse the input as a serialized proto of ' +
KEY_TYPE + ' key.';
}
static invalidSerializedKeyFormat() {
return 'CustomError: Could not parse the input as a serialized proto of ' +
KEY_TYPE + ' key format.';
}
/**
* @return {string}
*/
static invalidKeyFormat() {
return 'CustomError: Expected AesGcmKeyFormat-proto';
}
}
/**
* @param {number=} opt_keySize
*
* @return {!PbAesGcmKeyFormat}
*/
const createTestKeyFormat = function(opt_keySize = 16) {
const keyFormat = new PbAesGcmKeyFormat().setKeySize(opt_keySize);
return keyFormat;
};
/**
* @param {number=} opt_keySize
* @return {!PbAesGcmKey}
*/
const createTestKey = function(opt_keySize = 16) {
const key = new PbAesGcmKey().setVersion(0).setKeyValue(
Random.randBytes(opt_keySize));
return key;
};
/**
* @param {number=} opt_keySize
* @return {!PbKeyData}
*/
const createTestKeyData = function(opt_keySize) {
const keyData = new PbKeyData()
.setTypeUrl(KEY_TYPE)
.setValue(createTestKey(opt_keySize).serializeBinary())
.setKeyMaterialType(PbKeyData.KeyMaterialType.SYMMETRIC);