• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Key Agreement Using ECDH (ArkTS)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10For details about the algorithm specifications, see [ECDH](crypto-key-agreement-overview.md#ecdh).
11
12## How to Develop
13
141. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator), [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1), and [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to generate a 256-bit ECC key pair (**KeyPair**).
15
16   For details about how to generate an ECC asymmetric key pair, see the following example. To learn more, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md). There may be differences between the input parameters in the reference documents and those in the following example.
17
182. Call [cryptoFramework.createKeyAgreement](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatekeyagreement) with the string parameter **'ECC256'** to create a 256-bit ECC key agreement (**KeyAgreement**) instance.
19
203. Call [KeyAgreement.generateSecret](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatesecret-1) to perform key agreement with the specified private key (**KeyPair.priKey**) and public key (**KeyPair.pubKey**), and return the shared secret.
21
22- Example: Perform key agreement using **await**.
23
24  ```ts
25  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
26
27  async function ecdhAwait() {
28    // The public and private key pair data is transferred from an external system.
29    let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
30    let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
31    let eccGen = cryptoFramework.createAsyKeyGenerator('ECC256');
32    // Key pair A transferred from an external system.
33    let keyPairA = await eccGen.convertKey({ data: pubKeyArray }, { data: priKeyArray });
34    // Key pair B generated internally.
35    let keyPairB = await eccGen.generateKeyPair();
36    let eccKeyAgreement = cryptoFramework.createKeyAgreement('ECC256');
37    // Use the public key of A and the private key of B to perform key agreement.
38    let secret1 = await eccKeyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey);
39    // Use the private key of A and the public key of B to perform key agreement.
40    let secret2 = await eccKeyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey);
41    // The two key agreement results should be the same.
42    if (secret1.data.toString() === secret2.data.toString()) {
43      console.info('ecdh success');
44      console.info('ecdh output is ' + secret1.data);
45    } else {
46      console.error('ecdh result is not equal');
47    }
48  }
49  ```
50
51- Example (using synchronous APIs):
52
53  ```ts
54  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
55
56  function ecdhSync() {
57    // The public and private key pair data is transferred from an external system.
58    let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
59    let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
60    let eccGen = cryptoFramework.createAsyKeyGenerator('ECC256');
61    // Key pair A transferred from an external system.
62    let keyPairA = eccGen.convertKeySync({ data: pubKeyArray }, { data: priKeyArray });
63    // Key pair B generated internally.
64    let keyPairB = eccGen.generateKeyPairSync();
65    let eccKeyAgreement = cryptoFramework.createKeyAgreement('ECC256');
66    // Use the public key of A and the private key of B to perform key agreement.
67    let secret1 = eccKeyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey);
68    // Use the private key of A and the public key of B to perform key agreement.
69    let secret2 = eccKeyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey);
70    // The two key agreement results should be the same.
71    if (secret1.data.toString() === secret2.data.toString()) {
72      console.info('ecdh success');
73      console.info('ecdh output is ' + secret1.data);
74    } else {
75      console.error('ecdh result is not equal');
76    }
77  }
78  ```
79