blob: da236355e6639477d27abf61fa368275fa03fb5b [file] [log] [blame]
taymonf6a87a32020-08-07 00:16:40 -07001/**
2 * @license
3 * Copyright 2020 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
5 */
thaidn397d2a72019-03-06 09:40:31 -08006
taymon5124d2e2020-08-17 06:48:19 -07007import {AeadKeyTemplates} from '../aead/aead_key_templates';
8import {PbEciesAeadHkdfKeyFormat, PbEllipticCurveType, PbHashType, PbOutputPrefixType, PbPointFormat} from '../internal/proto';
Tink Team734b0af2022-03-14 10:04:35 -07009import {assertMessageEquals} from '../testing/internal/test_utils';
taymon5124d2e2020-08-17 06:48:19 -070010
11import {EciesAeadHkdfPrivateKeyManager} from './ecies_aead_hkdf_private_key_manager';
12import {HybridKeyTemplates} from './hybrid_key_templates';
thaidn397d2a72019-03-06 09:40:31 -080013
taymon4c702122020-02-28 12:29:45 -080014describe('hybrid key templates test', function() {
15 it('ecies p256 hkdf hmac sha256 aes128 gcm', function() {
thaidn397d2a72019-03-06 09:40:31 -080016 // Expects function to create a key with following parameters.
17 const expectedCurve = PbEllipticCurveType.NIST_P256;
18 const expectedHkdfHashFunction = PbHashType.SHA256;
19 const expectedAeadTemplate = AeadKeyTemplates.aes128Gcm();
20 const expectedPointFormat = PbPointFormat.UNCOMPRESSED;
21 const expectedOutputPrefix = PbOutputPrefixType.TINK;
22
23 // Expected type URL is the one supported by EciesAeadHkdfPrivateKeyManager.
24 const manager = new EciesAeadHkdfPrivateKeyManager();
25 const expectedTypeUrl = manager.getKeyType();
26
27 const keyTemplate = HybridKeyTemplates.eciesP256HkdfHmacSha256Aes128Gcm();
28
taymon4c702122020-02-28 12:29:45 -080029 expect(keyTemplate.getTypeUrl()).toBe(expectedTypeUrl);
30 expect(keyTemplate.getOutputPrefixType()).toBe(expectedOutputPrefix);
thaidn397d2a72019-03-06 09:40:31 -080031
32 // Test values in key format.
33 const keyFormat =
Tink Team38b986c2021-12-28 13:49:52 -080034 PbEciesAeadHkdfKeyFormat.deserializeBinary(keyTemplate.getValue_asU8());
thaidn397d2a72019-03-06 09:40:31 -080035 const params = keyFormat.getParams();
taymon5124d2e2020-08-17 06:48:19 -070036 expect(params!.getEcPointFormat()).toBe(expectedPointFormat);
thaidn397d2a72019-03-06 09:40:31 -080037
38 // Test KEM params.
taymon5124d2e2020-08-17 06:48:19 -070039 const kemParams = params!.getKemParams();
40 expect(kemParams!.getCurveType()).toBe(expectedCurve);
41 expect(kemParams!.getHkdfHashType()).toBe(expectedHkdfHashFunction);
thaidn397d2a72019-03-06 09:40:31 -080042
43 // Test DEM params.
taymon5124d2e2020-08-17 06:48:19 -070044 const demParams = params!.getDemParams();
Tink Team734b0af2022-03-14 10:04:35 -070045 assertMessageEquals(demParams!.getAeadDem()!, expectedAeadTemplate);
thaidn397d2a72019-03-06 09:40:31 -080046
47 // Test that the template works with EciesAeadHkdfPrivateKeyManager.
48 manager.getKeyFactory().newKey(keyTemplate.getValue_asU8());
taymon4c702122020-02-28 12:29:45 -080049 });
thaidn397d2a72019-03-06 09:40:31 -080050
taymon4c702122020-02-28 12:29:45 -080051 it('ecies p256 hkdf hmac sha256 aes128 ctr hmac sha256', function() {
thaidn397d2a72019-03-06 09:40:31 -080052 // Expects function to create a key with following parameters.
53 const expectedCurve = PbEllipticCurveType.NIST_P256;
54 const expectedHkdfHashFunction = PbHashType.SHA256;
55 const expectedAeadTemplate = AeadKeyTemplates.aes128CtrHmacSha256();
56 const expectedPointFormat = PbPointFormat.UNCOMPRESSED;
57 const expectedOutputPrefix = PbOutputPrefixType.TINK;
58
59 // Expected type URL is the one supported by EciesAeadHkdfPrivateKeyManager.
60 const manager = new EciesAeadHkdfPrivateKeyManager();
61 const expectedTypeUrl = manager.getKeyType();
62
63 const keyTemplate =
64 HybridKeyTemplates.eciesP256HkdfHmacSha256Aes128CtrHmacSha256();
65
taymon4c702122020-02-28 12:29:45 -080066 expect(keyTemplate.getTypeUrl()).toBe(expectedTypeUrl);
67 expect(keyTemplate.getOutputPrefixType()).toBe(expectedOutputPrefix);
thaidn397d2a72019-03-06 09:40:31 -080068
69 // Test values in key format.
70 const keyFormat =
Tink Team38b986c2021-12-28 13:49:52 -080071 PbEciesAeadHkdfKeyFormat.deserializeBinary(keyTemplate.getValue_asU8());
thaidn397d2a72019-03-06 09:40:31 -080072 const params = keyFormat.getParams();
taymon5124d2e2020-08-17 06:48:19 -070073 expect(params!.getEcPointFormat()).toBe(expectedPointFormat);
thaidn397d2a72019-03-06 09:40:31 -080074
75 // Test KEM params.
taymon5124d2e2020-08-17 06:48:19 -070076 const kemParams = params!.getKemParams();
77 expect(kemParams!.getCurveType()).toBe(expectedCurve);
78 expect(kemParams!.getHkdfHashType()).toBe(expectedHkdfHashFunction);
thaidn397d2a72019-03-06 09:40:31 -080079
80 // Test DEM params.
taymon5124d2e2020-08-17 06:48:19 -070081 const demParams = params!.getDemParams();
Tink Team734b0af2022-03-14 10:04:35 -070082 assertMessageEquals(demParams!.getAeadDem()!, expectedAeadTemplate);
thaidn397d2a72019-03-06 09:40:31 -080083
84 // Test that the template works with EciesAeadHkdfPrivateKeyManager.
85 manager.getKeyFactory().newKey(keyTemplate.getValue_asU8());
taymon4c702122020-02-28 12:29:45 -080086 });
thaidn397d2a72019-03-06 09:40:31 -080087});
OSZAR »