sm2_keyexchange_1ab {smcryptoR} | R Documentation |
SM2 Key Exchange
Description
SM2 is an asymmetric encryption algorithm, therefore, it can also be used for key agreement or key exchange. If A and B want to generate a recognized key for encryption or authentication, this algorithm can ensure that the key itself will not be transmitted through untrusted channels, and the private keys of A and B will not be disclosed. Even if an attacker intercepts the data exchanged by A and B, they cannot calculate the key agreed upon by A and B.
Usage
sm2_keyexchange_1ab(klen, id, private_key)
sm2_keyexchange_2a(id, private_key, private_key_r, recive_bytes)
sm2_keyexchange_2b(id, private_key, private_key_r, recive_bytes)
Arguments
klen |
the key length, must be an integer |
id |
id of A or B, must be a raw vector |
private_key |
private key of A or B represented as a hexadecimal string |
private_key_r |
temp private_key of A or B |
recive_bytes |
for A or B, the recived data from B or A |
Value
- sm2_keyexchange_1ab
returns a list, 'data' for the raw data sent to B(for A) or A(for B), 'private_key_r' for the temporary private key
- sm2_keyexchange_2a
returns a list, 'k' for the key of length 'klen', 's12' for the sm3 hash in asn.1 encoding
- sm2_keyexchange_2b
returns a list, 'k' for the key of length 'klen', 's12' for the sm3 hash in asn.1 encoding
Examples
## Step 1
klen <- 16
id_a <- "a@company.com" |> charToRaw()
id_b <- "b@company.com" |> charToRaw()
private_key_a <- sm2_gen_keypair()$private_key
private_key_b <- sm2_gen_keypair()$private_key
step_1_a <- sm2_keyexchange_1ab(klen, id_a, private_key_a)
step_1_b <- sm2_keyexchange_1ab(klen, id_b, private_key_b)
## Step 2
step_2_a <- sm2_keyexchange_2a(id_a, private_key_a, step_1_a$private_key_r, step_1_b$data)
step_2_b <- sm2_keyexchange_2b(id_b, private_key_b, step_1_b$private_key_r, step_1_a$data)
step_2_a$k
step_2_b$k