Virgil IoT KIT
secmodule.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2020 Virgil Security, Inc.
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // (1) Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // (2) Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in
14 // the documentation and/or other materials provided with the
15 // distribution.
16 //
17 // (3) Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
22 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 // DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
34 
61 #ifndef VS_SECMODULE_INTERFACE_API_H
62 #define VS_SECMODULE_INTERFACE_API_H
63 
64 #include <stdint.h>
65 #include <stddef.h>
66 
69 
70 #ifdef __cplusplus
71 namespace VirgilIoTKit {
72 extern "C" {
73 #endif
74 
76 typedef enum {
92 
94 typedef enum {
100 
102 typedef enum {
104  VS_KDF_2 = 0,
106 
108 typedef enum {
112 
114 typedef struct {
115  uint32_t total[2];
116  uint32_t state[8];
117  unsigned char buffer[64];
119 
128 typedef vs_status_e (*vs_secmodule_slot_save_t)(vs_iot_secmodule_slot_e slot, const uint8_t *data, uint16_t data_sz);
129 
140  uint8_t *data,
141  uint16_t buf_sz,
142  uint16_t *out_sz);
143 
151 
164  const uint8_t *data,
165  uint16_t data_sz,
166  uint8_t *hash,
167  uint16_t hash_buf_sz,
168  uint16_t *hash_sz);
169 
178  vs_secmodule_keypair_type_e keypair_type);
179 
194  uint8_t *buf,
195  uint16_t buf_sz,
196  uint16_t *key_sz,
197  vs_secmodule_keypair_type_e *keypair_type);
198 
211  vs_secmodule_hash_type_e hash_type,
212  const uint8_t *hash,
213  uint8_t *signature,
214  uint16_t signature_buf_sz,
215  uint16_t *signature_sz);
216 
230  const uint8_t *public_key,
231  uint16_t public_key_sz,
232  vs_secmodule_hash_type_e hash_type,
233  const uint8_t *hash,
234  const uint8_t *signature,
235  uint16_t signature_sz);
236 
251  const uint8_t *key,
252  uint16_t key_sz,
253  const uint8_t *input,
254  uint16_t input_sz,
255  uint8_t *output,
256  uint16_t output_buf_sz,
257  uint16_t *output_sz);
258 
271  vs_secmodule_hash_type_e hash_type,
272  const uint8_t *input,
273  uint16_t input_sz,
274  uint8_t *output,
275  uint16_t output_sz);
276 
292  const uint8_t *input,
293  uint16_t input_sz,
294  const uint8_t *salt,
295  uint16_t salt_sz,
296  const uint8_t *info,
297  uint16_t info_sz,
298  uint8_t *output,
299  uint16_t output_sz);
300 
308 typedef vs_status_e (*vs_secmodule_random_t)(uint8_t *output, uint16_t output_sz);
309 
328  const uint8_t *key,
329  uint16_t key_bitlen,
330  const uint8_t *iv,
331  uint16_t iv_len,
332  const uint8_t *add,
333  uint16_t add_len,
334  uint16_t buf_len,
335  const uint8_t *input,
336  uint8_t *output,
337  uint8_t *tag,
338  uint16_t tag_len);
339 
358  const uint8_t *key,
359  uint16_t key_bitlen,
360  const uint8_t *iv,
361  uint16_t iv_len,
362  const uint8_t *add,
363  uint16_t add_len,
364  uint16_t buf_len,
365  const uint8_t *input,
366  uint8_t *output,
367  uint8_t *tag,
368  uint16_t tag_len);
369 
388  const uint8_t *key,
389  uint16_t key_bitlen,
390  const uint8_t *iv,
391  uint16_t iv_len,
392  const uint8_t *add,
393  uint16_t add_len,
394  uint16_t buf_len,
395  const uint8_t *input,
396  uint8_t *output,
397  const uint8_t *tag,
398  uint16_t tag_len);
399 
413  vs_secmodule_keypair_type_e keypair_type,
414  const uint8_t *public_key,
415  uint16_t public_key_sz,
416  uint8_t *shared_secret,
417  uint16_t buf_sz,
418  uint16_t *shared_secret_sz);
419 
427 
437  const uint8_t *message,
438  uint32_t len);
439 
448 
450 typedef void (*vs_secmodule_deinit_t)(void);
451 
458 typedef struct {
459 
462  // Slot operations
467  // RNG
470  // Key-pair in slot
474  // ECDSA
478  // ECDH
481  // AES
486  // Hash
492  // HMAC
495  // KDF
498  // HKDF
502 
517 vs_secmodule_ecies_decrypt(const vs_secmodule_impl_t *secmodule_impl,
518  const uint8_t *recipient_id,
519  size_t recipient_id_sz,
520  const uint8_t *cryptogram,
521  size_t cryptogram_sz,
522  uint8_t *decrypted_data,
523  size_t buf_sz,
524  size_t *decrypted_data_sz);
525 
540 vs_secmodule_ecies_encrypt(const vs_secmodule_impl_t *secmodule_impl,
541  const uint8_t *recipient_id,
542  size_t recipient_id_sz,
543  const uint8_t *data,
544  size_t data_sz,
545  uint8_t *cryptogram,
546  size_t buf_sz,
547  size_t *cryptogram_sz);
548 
549 #ifdef __cplusplus
550 } // extern "C"
551 } // namespace VirgilIoTKit
552 #endif
553 
554 #endif // VS_SECMODULE_INTERFACE_API_H
VS_KEYPAIR_EC_SECP192K1
@ VS_KEYPAIR_EC_SECP192K1
192-bits "Koblitz" curve
Definition: secmodule.h:84
vs_secmodule_hkdf_t
vs_status_e(* vs_secmodule_hkdf_t)(vs_secmodule_hash_type_e hash_type, const uint8_t *input, uint16_t input_sz, const uint8_t *salt, uint16_t salt_sz, const uint8_t *info, uint16_t info_sz, uint8_t *output, uint16_t output_sz)
HKDF calculation.
Definition: secmodule.h:291
VS_KEYPAIR_EC_ED25519
@ VS_KEYPAIR_EC_ED25519
Ed25519.
Definition: secmodule.h:89
vs_secmodule_keypair_create_t
vs_status_e(* vs_secmodule_keypair_create_t)(vs_iot_secmodule_slot_e slot, vs_secmodule_keypair_type_e keypair_type)
Key pair generate.
Definition: secmodule.h:177
VS_KEYPAIR_EC_SECP_MIN
@ VS_KEYPAIR_EC_SECP_MIN
Definition: secmodule.h:78
vs_secmodule_sw_sha256_init_t
void(* vs_secmodule_sw_sha256_init_t)(vs_secmodule_sw_sha256_ctx *ctx)
SHA-256 context initialization.
Definition: secmodule.h:426
vs_secmodule_impl_t::hash_update
vs_secmodule_sw_sha256_update_t hash_update
SHA-256 update.
Definition: secmodule.h:488
vs_secmodule_ecdsa_verify_t
vs_status_e(* vs_secmodule_ecdsa_verify_t)(vs_secmodule_keypair_type_e keypair_type, const uint8_t *public_key, uint16_t public_key_sz, vs_secmodule_hash_type_e hash_type, const uint8_t *hash, const uint8_t *signature, uint16_t signature_sz)
Signature verification based on ECDSA.
Definition: secmodule.h:229
vs_secmodule_aes_auth_decrypt_t
vs_status_e(* vs_secmodule_aes_auth_decrypt_t)(vs_iot_aes_type_e aes_type, const uint8_t *key, uint16_t key_bitlen, const uint8_t *iv, uint16_t iv_len, const uint8_t *add, uint16_t add_len, uint16_t buf_len, const uint8_t *input, uint8_t *output, const uint8_t *tag, uint16_t tag_len)
Data decryption by AES algorithm with authentication check.
Definition: secmodule.h:387
vs_secmodule_impl_t::hmac
vs_secmodule_hmac_t hmac
HMAC calculate.
Definition: secmodule.h:493
vs_secmodule_sw_sha256_ctx
SHA-256 context.
Definition: secmodule.h:114
vs_secmodule_impl_t
Security Module implementation.
Definition: secmodule.h:458
VS_HASH_SHA_512
@ VS_HASH_SHA_512
SHA-512.
Definition: secmodule.h:98
vs_secmodule_slot_save_t
vs_status_e(* vs_secmodule_slot_save_t)(vs_iot_secmodule_slot_e slot, const uint8_t *data, uint16_t data_sz)
Save information to the slot.
Definition: secmodule.h:128
vs_secmodule_impl_t::hash_finish
vs_secmodule_sw_sha256_final_t hash_finish
SHA-256 finalization.
Definition: secmodule.h:489
vs_secmodule_sw_sha256_final_t
vs_status_e(* vs_secmodule_sw_sha256_final_t)(vs_secmodule_sw_sha256_ctx *ctx, uint8_t *digest)
SHA-256 context finalization.
Definition: secmodule.h:447
vs_secmodule_hash_create_t
vs_status_e(* vs_secmodule_hash_create_t)(vs_secmodule_hash_type_e hash_type, const uint8_t *data, uint16_t data_sz, uint8_t *hash, uint16_t hash_buf_sz, uint16_t *hash_sz)
Hash generation.
Definition: secmodule.h:163
vs_secmodule_impl_t::kdf
vs_secmodule_kdf_t kdf
KDF calculate.
Definition: secmodule.h:496
vs_secmodule_deinit_t
void(* vs_secmodule_deinit_t)(void)
Security Module destruction.
Definition: secmodule.h:450
vs_status_e
vs_status_e
Status code.
Definition: status_code.h:77
vs_secmodule_slot_delete_t
vs_status_e(* vs_secmodule_slot_delete_t)(vs_iot_secmodule_slot_e slot)
Delete information from the slot.
Definition: secmodule.h:150
vs_secmodule_impl_t::ecdsa_sign
vs_secmodule_ecdsa_sign_t ecdsa_sign
ECDSA sign.
Definition: secmodule.h:475
vs_secmodule_sw_sha256_update_t
vs_status_e(* vs_secmodule_sw_sha256_update_t)(vs_secmodule_sw_sha256_ctx *ctx, const uint8_t *message, uint32_t len)
SHA-256 context update.
Definition: secmodule.h:436
vs_secmodule_ecdh_t
vs_status_e(* vs_secmodule_ecdh_t)(vs_iot_secmodule_slot_e slot, vs_secmodule_keypair_type_e keypair_type, const uint8_t *public_key, uint16_t public_key_sz, uint8_t *shared_secret, uint16_t buf_sz, uint16_t *shared_secret_sz)
ECDH algorithm.
Definition: secmodule.h:412
VS_HASH_SHA_INVALID
@ VS_HASH_SHA_INVALID
Invalid hash type.
Definition: secmodule.h:95
vs_secmodule_impl_t::aes_encrypt
vs_secmodule_aes_encrypt_t aes_encrypt
AES encrypt.
Definition: secmodule.h:482
vs_secmodule_ecdsa_sign_t
vs_status_e(* vs_secmodule_ecdsa_sign_t)(vs_iot_secmodule_slot_e key_slot, vs_secmodule_hash_type_e hash_type, const uint8_t *hash, uint8_t *signature, uint16_t signature_buf_sz, uint16_t *signature_sz)
Signature calculation based on ECDSA.
Definition: secmodule.h:210
vs_secmodule_impl_t::aes_auth_decrypt
vs_secmodule_aes_auth_decrypt_t aes_auth_decrypt
AES decrypt with authentication.
Definition: secmodule.h:484
VS_KDF_2
@ VS_KDF_2
Definition: secmodule.h:104
vs_iot_aes_type_e
vs_iot_aes_type_e
AES mode.
Definition: secmodule.h:108
vs_secmodule_impl_t::get_pubkey
vs_secmodule_keypair_get_pubkey_t get_pubkey
Get public key.
Definition: secmodule.h:472
vs_secmodule_ecies_encrypt
vs_status_e vs_secmodule_ecies_encrypt(const vs_secmodule_impl_t *secmodule_impl, const uint8_t *recipient_id, size_t recipient_id_sz, const uint8_t *data, size_t data_sz, uint8_t *cryptogram, size_t buf_sz, size_t *cryptogram_sz)
ECIES encryption for AES-256 based on SHA-384.
VS_KEYPAIR_EC_SECP224R1
@ VS_KEYPAIR_EC_SECP224R1
224-bits NIST curve
Definition: secmodule.h:80
vs_secmodule_keypair_get_pubkey_t
vs_status_e(* vs_secmodule_keypair_get_pubkey_t)(vs_iot_secmodule_slot_e slot, uint8_t *buf, uint16_t buf_sz, uint16_t *key_sz, vs_secmodule_keypair_type_e *keypair_type)
Public key retrieval.
Definition: secmodule.h:193
VS_KEYPAIR_EC_SECP384R1
@ VS_KEYPAIR_EC_SECP384R1
384-bits NIST curve
Definition: secmodule.h:82
vs_secmodule_impl_t::hash_init
vs_secmodule_sw_sha256_init_t hash_init
SHA-256 hash initialize.
Definition: secmodule.h:487
vs_secmodule_impl_t::hash
vs_secmodule_hash_create_t hash
Create hash.
Definition: secmodule.h:490
VS_KEYPAIR_EC_SECP256R1
@ VS_KEYPAIR_EC_SECP256R1
256-bits NIST curve
Definition: secmodule.h:81
VS_KEYPAIR_EC_SECP_MAX
@ VS_KEYPAIR_EC_SECP_MAX
Definition: secmodule.h:87
VS_KDF_INVALID
@ VS_KDF_INVALID
Definition: secmodule.h:103
status_code.h
Status codes and macroses.
vs_secmodule_ecies_decrypt
vs_status_e vs_secmodule_ecies_decrypt(const vs_secmodule_impl_t *secmodule_impl, const uint8_t *recipient_id, size_t recipient_id_sz, const uint8_t *cryptogram, size_t cryptogram_sz, uint8_t *decrypted_data, size_t buf_sz, size_t *decrypted_data_sz)
ECIES decryption for AES-256 based on SHA-384.
vs_secmodule_hmac_t
vs_status_e(* vs_secmodule_hmac_t)(vs_secmodule_hash_type_e hash_type, const uint8_t *key, uint16_t key_sz, const uint8_t *input, uint16_t input_sz, uint8_t *output, uint16_t output_buf_sz, uint16_t *output_sz)
HMAC calculation.
Definition: secmodule.h:250
VS_HASH_SHA_256
@ VS_HASH_SHA_256
SHA-256.
Definition: secmodule.h:96
VS_AES_GCM
@ VS_AES_GCM
AES-GCM.
Definition: secmodule.h:109
secmodule-soft.h
Software Security Module constants.
vs_secmodule_hash_type_e
vs_secmodule_hash_type_e
Hash types.
Definition: secmodule.h:94
VS_KEYPAIR_EC_SECP521R1
@ VS_KEYPAIR_EC_SECP521R1
521-bits NIST curve
Definition: secmodule.h:83
vs_secmodule_aes_decrypt_t
vs_status_e(* vs_secmodule_aes_decrypt_t)(vs_iot_aes_type_e aes_type, const uint8_t *key, uint16_t key_bitlen, const uint8_t *iv, uint16_t iv_len, const uint8_t *add, uint16_t add_len, uint16_t buf_len, const uint8_t *input, uint8_t *output, uint8_t *tag, uint16_t tag_len)
Data decryption by AES algorithm.
Definition: secmodule.h:357
VS_KEYPAIR_EC_SECP256K1
@ VS_KEYPAIR_EC_SECP256K1
256-bits "Koblitz" curve
Definition: secmodule.h:86
VS_KEYPAIR_MAX
@ VS_KEYPAIR_MAX
Definition: secmodule.h:90
vs_iot_secmodule_slot_e
vs_iot_secmodule_slot_e
Slots identifiers.
Definition: secmodule-soft.h:59
vs_secmodule_impl_t::create_keypair
vs_secmodule_keypair_create_t create_keypair
Key pair generation.
Definition: secmodule.h:471
VS_KEYPAIR_EC_SECP224K1
@ VS_KEYPAIR_EC_SECP224K1
224-bits "Koblitz" curve
Definition: secmodule.h:85
VS_AES_CBC
@ VS_AES_CBC
AES-CBC.
Definition: secmodule.h:110
vs_secmodule_keypair_type_e
vs_secmodule_keypair_type_e
Keypair types.
Definition: secmodule.h:76
vs_secmodule_impl_t::deinit
vs_secmodule_deinit_t deinit
Security module destruction.
Definition: secmodule.h:460
vs_secmodule_aes_encrypt_t
vs_status_e(* vs_secmodule_aes_encrypt_t)(vs_iot_aes_type_e aes_type, const uint8_t *key, uint16_t key_bitlen, const uint8_t *iv, uint16_t iv_len, const uint8_t *add, uint16_t add_len, uint16_t buf_len, const uint8_t *input, uint8_t *output, uint8_t *tag, uint16_t tag_len)
Data encryption by AES algorithm.
Definition: secmodule.h:327
VS_KEYPAIR_EC_SECP192R1
@ VS_KEYPAIR_EC_SECP192R1
192-bits NIST curve
Definition: secmodule.h:79
vs_secmodule_kdf_type_e
vs_secmodule_kdf_type_e
KDF type.
Definition: secmodule.h:102
VS_HASH_SHA_384
@ VS_HASH_SHA_384
SHA-384.
Definition: secmodule.h:97
vs_secmodule_impl_t::random
vs_secmodule_random_t random
Get random data.
Definition: secmodule.h:468
vs_secmodule_impl_t::slot_save
vs_secmodule_slot_save_t slot_save
Slot save information.
Definition: secmodule.h:463
vs_secmodule_slot_load_t
vs_status_e(* vs_secmodule_slot_load_t)(vs_iot_secmodule_slot_e slot, uint8_t *data, uint16_t buf_sz, uint16_t *out_sz)
Load information to the slot.
Definition: secmodule.h:139
VS_KEYPAIR_EC_CURVE25519
@ VS_KEYPAIR_EC_CURVE25519
Curve25519.
Definition: secmodule.h:88
vs_secmodule_impl_t::slot_clean
vs_secmodule_slot_delete_t slot_clean
Slot delete.
Definition: secmodule.h:465
vs_secmodule_impl_t::ecdsa_verify
vs_secmodule_ecdsa_verify_t ecdsa_verify
ECDSA verify.
Definition: secmodule.h:476
vs_secmodule_impl_t::hkdf
vs_secmodule_hkdf_t hkdf
HKDF calculate.
Definition: secmodule.h:499
vs_secmodule_impl_t::ecdh
vs_secmodule_ecdh_t ecdh
ECDH.
Definition: secmodule.h:479
vs_secmodule_impl_t::aes_decrypt
vs_secmodule_aes_decrypt_t aes_decrypt
AES decrypt.
Definition: secmodule.h:483
vs_secmodule_impl_t::slot_load
vs_secmodule_slot_load_t slot_load
Slot load information.
Definition: secmodule.h:464
vs_secmodule_kdf_t
vs_status_e(* vs_secmodule_kdf_t)(vs_secmodule_kdf_type_e kdf_type, vs_secmodule_hash_type_e hash_type, const uint8_t *input, uint16_t input_sz, uint8_t *output, uint16_t output_sz)
KDF calculation.
Definition: secmodule.h:270
VS_KEYPAIR_INVALID
@ VS_KEYPAIR_INVALID
Invalid keypair.
Definition: secmodule.h:77
vs_secmodule_random_t
vs_status_e(* vs_secmodule_random_t)(uint8_t *output, uint16_t output_sz)
Random data generation.
Definition: secmodule.h:308