sui_move_natives_v2/crypto/
hmac.rs1use crate::NativesCostTable;
4use fastcrypto::{hmac, traits::ToFromBytes};
5use move_binary_format::errors::PartialVMResult;
6use move_core_types::gas_algebra::InternalGas;
7use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext};
8use move_vm_types::{
9 loaded_data::runtime_types::Type,
10 natives::function::NativeResult,
11 pop_arg,
12 values::{Value, VectorRef},
13};
14use smallvec::smallvec;
15use std::collections::VecDeque;
16
17const HMAC_SHA3_256_BLOCK_SIZE: usize = 136;
18
19#[derive(Clone)]
20pub struct HmacHmacSha3256CostParams {
21 pub hmac_hmac_sha3_256_cost_base: InternalGas,
23 pub hmac_hmac_sha3_256_input_cost_per_byte: InternalGas,
25 pub hmac_hmac_sha3_256_input_cost_per_block: InternalGas,
27}
28pub fn hmac_sha3_256(
38 context: &mut NativeContext,
39 ty_args: Vec<Type>,
40 mut args: VecDeque<Value>,
41) -> PartialVMResult<NativeResult> {
42 debug_assert!(ty_args.is_empty());
43 debug_assert!(args.len() == 2);
44
45 let hmac_hmac_sha3_256_cost_params = &context
47 .extensions()
48 .get::<NativesCostTable>()
49 .hmac_hmac_sha3_256_cost_params
50 .clone();
51
52 native_charge_gas_early_exit!(
54 context,
55 hmac_hmac_sha3_256_cost_params.hmac_hmac_sha3_256_cost_base
56 );
57
58 let message = pop_arg!(args, VectorRef);
59 let key = pop_arg!(args, VectorRef);
60
61 let msg_len = message.as_bytes_ref().len();
62 let key_len = key.as_bytes_ref().len();
63 native_charge_gas_early_exit!(
65 context,
66 hmac_hmac_sha3_256_cost_params.hmac_hmac_sha3_256_input_cost_per_byte
67 * ((msg_len + key_len) as u64).into()
69 + hmac_hmac_sha3_256_cost_params.hmac_hmac_sha3_256_input_cost_per_block
70 * ((((msg_len + key_len) + (2 * HMAC_SHA3_256_BLOCK_SIZE - 2))
71 / HMAC_SHA3_256_BLOCK_SIZE) as u64)
72 .into()
73 );
74
75 let hmac_key = hmac::HmacKey::from_bytes(&key.as_bytes_ref())
76 .expect("HMAC key can be of any length and from_bytes should always succeed");
77 let cost = context.gas_used();
78
79 Ok(NativeResult::ok(
80 cost,
81 smallvec![Value::vector_u8(
82 hmac::hmac_sha3_256(&hmac_key, &message.as_bytes_ref()).to_vec()
83 )],
84 ))
85}