taymon | f6a87a3 | 2020-08-07 00:16:40 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2020 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 6 | |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 7 | import {AeadKeyTemplates} from '../aead/aead_key_templates'; |
| 8 | import {PbEciesAeadHkdfKeyFormat, PbEllipticCurveType, PbHashType, PbOutputPrefixType, PbPointFormat} from '../internal/proto'; |
Tink Team | 734b0af | 2022-03-14 10:04:35 -0700 | [diff] [blame] | 9 | import {assertMessageEquals} from '../testing/internal/test_utils'; |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 10 | |
| 11 | import {EciesAeadHkdfPrivateKeyManager} from './ecies_aead_hkdf_private_key_manager'; |
| 12 | import {HybridKeyTemplates} from './hybrid_key_templates'; |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 13 | |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 14 | describe('hybrid key templates test', function() { |
| 15 | it('ecies p256 hkdf hmac sha256 aes128 gcm', function() { |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 16 | // 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 | |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 29 | expect(keyTemplate.getTypeUrl()).toBe(expectedTypeUrl); |
| 30 | expect(keyTemplate.getOutputPrefixType()).toBe(expectedOutputPrefix); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 31 | |
| 32 | // Test values in key format. |
| 33 | const keyFormat = |
Tink Team | 38b986c | 2021-12-28 13:49:52 -0800 | [diff] [blame] | 34 | PbEciesAeadHkdfKeyFormat.deserializeBinary(keyTemplate.getValue_asU8()); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 35 | const params = keyFormat.getParams(); |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 36 | expect(params!.getEcPointFormat()).toBe(expectedPointFormat); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 37 | |
| 38 | // Test KEM params. |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 39 | const kemParams = params!.getKemParams(); |
| 40 | expect(kemParams!.getCurveType()).toBe(expectedCurve); |
| 41 | expect(kemParams!.getHkdfHashType()).toBe(expectedHkdfHashFunction); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 42 | |
| 43 | // Test DEM params. |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 44 | const demParams = params!.getDemParams(); |
Tink Team | 734b0af | 2022-03-14 10:04:35 -0700 | [diff] [blame] | 45 | assertMessageEquals(demParams!.getAeadDem()!, expectedAeadTemplate); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 46 | |
| 47 | // Test that the template works with EciesAeadHkdfPrivateKeyManager. |
| 48 | manager.getKeyFactory().newKey(keyTemplate.getValue_asU8()); |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 49 | }); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 50 | |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 51 | it('ecies p256 hkdf hmac sha256 aes128 ctr hmac sha256', function() { |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 52 | // 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 | |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 66 | expect(keyTemplate.getTypeUrl()).toBe(expectedTypeUrl); |
| 67 | expect(keyTemplate.getOutputPrefixType()).toBe(expectedOutputPrefix); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 68 | |
| 69 | // Test values in key format. |
| 70 | const keyFormat = |
Tink Team | 38b986c | 2021-12-28 13:49:52 -0800 | [diff] [blame] | 71 | PbEciesAeadHkdfKeyFormat.deserializeBinary(keyTemplate.getValue_asU8()); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 72 | const params = keyFormat.getParams(); |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 73 | expect(params!.getEcPointFormat()).toBe(expectedPointFormat); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 74 | |
| 75 | // Test KEM params. |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 76 | const kemParams = params!.getKemParams(); |
| 77 | expect(kemParams!.getCurveType()).toBe(expectedCurve); |
| 78 | expect(kemParams!.getHkdfHashType()).toBe(expectedHkdfHashFunction); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 79 | |
| 80 | // Test DEM params. |
taymon | 5124d2e | 2020-08-17 06:48:19 -0700 | [diff] [blame] | 81 | const demParams = params!.getDemParams(); |
Tink Team | 734b0af | 2022-03-14 10:04:35 -0700 | [diff] [blame] | 82 | assertMessageEquals(demParams!.getAeadDem()!, expectedAeadTemplate); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 83 | |
| 84 | // Test that the template works with EciesAeadHkdfPrivateKeyManager. |
| 85 | manager.getKeyFactory().newKey(keyTemplate.getValue_asU8()); |
taymon | 4c70212 | 2020-02-28 12:29:45 -0800 | [diff] [blame] | 86 | }); |
thaidn | 397d2a7 | 2019-03-06 09:40:31 -0800 | [diff] [blame] | 87 | }); |