otp_crypto.c
Include dependency graph for otp_crypto.c:
![digraph {
graph [bgcolor="#00000000"]
node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
edge [color="#1414CE"]
"10" [label="stdbool.h" tooltip="stdbool.h"]
"4" [label="atom.h" tooltip="atom.h"]
"42" [label="trace.h" tooltip="trace.h"]
"13" [label="assert.h" tooltip="assert.h"]
"18" [label="synclist.h" tooltip="synclist.h"]
"31" [label="defaultatoms.h" tooltip="defaultatoms.h"]
"38" [label="mbedtls/sha1.h" tooltip="mbedtls/sha1.h"]
"5" [label="stdint.h" tooltip="stdint.h"]
"6" [label="stdlib.h" tooltip="stdlib.h"]
"30" [label="exportedfunction.h" tooltip="exportedfunction.h"]
"34" [label="mbedtls/ctr_drbg.h" tooltip="mbedtls/ctr_drbg.h"]
"7" [label="context.h" tooltip="context.h"]
"24" [label="utils.h" tooltip="utils.h"]
"16" [label="ets.h" tooltip="ets.h"]
"9" [label="atom_table.h" tooltip="atom_table.h"]
"12" [label="term_typedef.h" tooltip="term_typedef.h"]
"25" [label="stddef.h" tooltip="stddef.h"]
"41" [label="mbedtls/version.h" tooltip="mbedtls/version.h"]
"33" [label="sys_mbedtls.h" tooltip="sys_mbedtls.h"]
"35" [label="mbedtls/entropy.h" tooltip="mbedtls/entropy.h"]
"14" [label="limits.h" tooltip="limits.h"]
"26" [label="refc_binary.h" tooltip="refc_binary.h"]
"22" [label="string.h" tooltip="string.h"]
"28" [label="mailbox.h" tooltip="mailbox.h"]
"21" [label="term.h" tooltip="term.h"]
"11" [label="erl_nif.h" tooltip="erl_nif.h"]
"40" [label="mbedtls/sha512.h" tooltip="mbedtls/sha512.h"]
"32" [label="interop.h" tooltip="interop.h"]
"37" [label="mbedtls/md5.h" tooltip="mbedtls/md5.h"]
"27" [label="resources.h" tooltip="resources.h"]
"1" [label="/home/runner/work/AtomVM/AtomVM/src/libAtomVM/otp_crypto.c" tooltip="/home/runner/work/AtomVM/AtomVM/src/libAtomVM/otp_crypto.c" fillcolor="#BFBFBF"]
"2" [label="otp_crypto.h" tooltip="otp_crypto.h"]
"20" [label="smp.h" tooltip="smp.h"]
"3" [label="nifs.h" tooltip="nifs.h"]
"8" [label="globalcontext.h" tooltip="globalcontext.h"]
"39" [label="mbedtls/sha256.h" tooltip="mbedtls/sha256.h"]
"23" [label="memory.h" tooltip="memory.h"]
"19" [label="stdio.h" tooltip="stdio.h"]
"29" [label="timer_list.h" tooltip="timer_list.h"]
"36" [label="mbedtls/cipher.h" tooltip="mbedtls/cipher.h"]
"17" [label="list.h" tooltip="list.h"]
"15" [label="inttypes.h" tooltip="inttypes.h"]
"4" -> "5" [dir=forward tooltip="include"]
"4" -> "6" [dir=forward tooltip="include"]
"18" -> "19" [dir=forward tooltip="include"]
"18" -> "17" [dir=forward tooltip="include"]
"18" -> "20" [dir=forward tooltip="include"]
"31" -> "8" [dir=forward tooltip="include"]
"30" -> "21" [dir=forward tooltip="include"]
"7" -> "8" [dir=forward tooltip="include"]
"7" -> "17" [dir=forward tooltip="include"]
"7" -> "28" [dir=forward tooltip="include"]
"7" -> "20" [dir=forward tooltip="include"]
"7" -> "21" [dir=forward tooltip="include"]
"7" -> "29" [dir=forward tooltip="include"]
"24" -> "25" [dir=forward tooltip="include"]
"24" -> "19" [dir=forward tooltip="include"]
"24" -> "6" [dir=forward tooltip="include"]
"16" -> "17" [dir=forward tooltip="include"]
"16" -> "18" [dir=forward tooltip="include"]
"16" -> "21" [dir=forward tooltip="include"]
"9" -> "10" [dir=forward tooltip="include"]
"9" -> "4" [dir=forward tooltip="include"]
"12" -> "13" [dir=forward tooltip="include"]
"12" -> "14" [dir=forward tooltip="include"]
"12" -> "15" [dir=forward tooltip="include"]
"12" -> "5" [dir=forward tooltip="include"]
"33" -> "34" [dir=forward tooltip="include"]
"33" -> "35" [dir=forward tooltip="include"]
"26" -> "10" [dir=forward tooltip="include"]
"26" -> "6" [dir=forward tooltip="include"]
"26" -> "17" [dir=forward tooltip="include"]
"26" -> "27" [dir=forward tooltip="include"]
"28" -> "10" [dir=forward tooltip="include"]
"28" -> "17" [dir=forward tooltip="include"]
"28" -> "12" [dir=forward tooltip="include"]
"28" -> "24" [dir=forward tooltip="include"]
"21" -> "10" [dir=forward tooltip="include"]
"21" -> "5" [dir=forward tooltip="include"]
"21" -> "19" [dir=forward tooltip="include"]
"21" -> "6" [dir=forward tooltip="include"]
"21" -> "22" [dir=forward tooltip="include"]
"21" -> "23" [dir=forward tooltip="include"]
"21" -> "26" [dir=forward tooltip="include"]
"21" -> "24" [dir=forward tooltip="include"]
"21" -> "12" [dir=forward tooltip="include"]
"11" -> "12" [dir=forward tooltip="include"]
"32" -> "7" [dir=forward tooltip="include"]
"32" -> "21" [dir=forward tooltip="include"]
"27" -> "6" [dir=forward tooltip="include"]
"27" -> "11" [dir=forward tooltip="include"]
"27" -> "17" [dir=forward tooltip="include"]
"27" -> "23" [dir=forward tooltip="include"]
"1" -> "2" [dir=forward tooltip="include"]
"1" -> "7" [dir=forward tooltip="include"]
"1" -> "31" [dir=forward tooltip="include"]
"1" -> "8" [dir=forward tooltip="include"]
"1" -> "32" [dir=forward tooltip="include"]
"1" -> "3" [dir=forward tooltip="include"]
"1" -> "33" [dir=forward tooltip="include"]
"1" -> "21" [dir=forward tooltip="include"]
"1" -> "12" [dir=forward tooltip="include"]
"1" -> "36" [dir=forward tooltip="include"]
"1" -> "34" [dir=forward tooltip="include"]
"1" -> "35" [dir=forward tooltip="include"]
"1" -> "37" [dir=forward tooltip="include"]
"1" -> "38" [dir=forward tooltip="include"]
"1" -> "39" [dir=forward tooltip="include"]
"1" -> "40" [dir=forward tooltip="include"]
"1" -> "41" [dir=forward tooltip="include"]
"1" -> "42" [dir=forward tooltip="include"]
"2" -> "3" [dir=forward tooltip="include"]
"20" -> "10" [dir=forward tooltip="include"]
"3" -> "4" [dir=forward tooltip="include"]
"3" -> "7" [dir=forward tooltip="include"]
"3" -> "30" [dir=forward tooltip="include"]
"8" -> "5" [dir=forward tooltip="include"]
"8" -> "4" [dir=forward tooltip="include"]
"8" -> "9" [dir=forward tooltip="include"]
"8" -> "11" [dir=forward tooltip="include"]
"8" -> "16" [dir=forward tooltip="include"]
"8" -> "17" [dir=forward tooltip="include"]
"8" -> "28" [dir=forward tooltip="include"]
"8" -> "20" [dir=forward tooltip="include"]
"8" -> "18" [dir=forward tooltip="include"]
"8" -> "21" [dir=forward tooltip="include"]
"8" -> "29" [dir=forward tooltip="include"]
"23" -> "5" [dir=forward tooltip="include"]
"23" -> "6" [dir=forward tooltip="include"]
"23" -> "11" [dir=forward tooltip="include"]
"23" -> "12" [dir=forward tooltip="include"]
"23" -> "24" [dir=forward tooltip="include"]
"29" -> "10" [dir=forward tooltip="include"]
"29" -> "5" [dir=forward tooltip="include"]
"29" -> "17" [dir=forward tooltip="include"]
"17" -> "10" [dir=forward tooltip="include"]
}](../../../_images/graphviz-5f38aa06c6fdaf36a50d35e5d7231814612cad8c.png)
Defines
-
MAX_MD_SIZE 64
-
DEFINE_HASH_FOLD(ALGORITHM, SUFFIX) static InteropFunctionResult ALGORITHM##_hash_fold_fun(term
t, void *accum) \
{ \
mbedtls_##ALGORITHM##_context *md_ctx = (mbedtls_##ALGORITHM##_context *) accum; \
if (
term_is_integer(t)) { \ avm_int64_t tmp = term_maybe_unbox_int64(t); \
if (tmp < 0 || tmp > 255) { \
return
InteropBadArg; \
} \
uint8_t val = (uint8_t) tmp; \
if (
UNLIKELY(mbedtls_##ALGORITHM##_update##SUFFIX(md_ctx, &val, 1) != 0)) { \
return
InteropBadArg; \
} \
} else /*
term_is_binary(t) */ { \
if (
UNLIKELY(mbedtls_##ALGORITHM##_update##SUFFIX(md_ctx, (uint8_t *) term_binary_data(t), term_binary_size(t)) != 0)) { \
return
InteropBadArg; \
} \
} \
return
InteropOk; \
}
-
DEFINE_HASH_FOLD_NORET(ALGORITHM, SUFFIX) static InteropFunctionResult ALGORITHM##_hash_fold_fun(term
t, void *accum) \
{ \
mbedtls_##ALGORITHM##_context *md_ctx = (mbedtls_##ALGORITHM##_context *) accum; \
if (
term_is_integer(t)) { \ avm_int64_t tmp = term_maybe_unbox_int64(t); \
if (tmp < 0 || tmp > 255) { \
return
InteropBadArg; \
} \
uint8_t val = (uint8_t) tmp; \
mbedtls_##ALGORITHM##_update##SUFFIX(md_ctx, &val, 1); \
} else /*
term_is_binary(t) */ { \
mbedtls_##ALGORITHM##_update##SUFFIX(md_ctx, (uint8_t *)
term_binary_data(t), term_binary_size(t)); \
} \
return
InteropOk; \
}
-
DEFINE_DO_HASH(ALGORITHM, SUFFIX) static bool do_##ALGORITHM##_hash(term
data, unsigned char *dst) \
{ \
mbedtls_##ALGORITHM##_context md_ctx; \
\
mbedtls_##ALGORITHM##_init(&md_ctx); \
mbedtls_##ALGORITHM##_starts##SUFFIX(&md_ctx); \
\
InteropFunctionResult result = interop_chardata_fold(data, ALGORITHM##_hash_fold_fun, NULL, (void *) &md_ctx); \
if (
UNLIKELY(result != InteropOk)) { \
return false; \
} \
\
if (
UNLIKELY(mbedtls_##ALGORITHM##_finish##SUFFIX(&md_ctx, dst) != 0)) { \
return false; \
} \
\
return true; \
}
-
DEFINE_DO_HASH_IS_OTHER(ALGORITHM, SUFFIX, IS_OTHER) static bool do_##ALGORITHM##_hash_##IS_OTHER(term
data, unsigned char *dst) \
{ \
mbedtls_##ALGORITHM##_context md_ctx; \
\
mbedtls_##ALGORITHM##_init(&md_ctx); \
mbedtls_##ALGORITHM##_starts##SUFFIX(&md_ctx, IS_OTHER); \
\
InteropFunctionResult result = interop_chardata_fold(data, ALGORITHM##_hash_fold_fun, NULL, (void *) &md_ctx); \
if (
UNLIKELY(result != InteropOk)) { \
return false; \
} \
\
if (
UNLIKELY(mbedtls_##ALGORITHM##_finish##SUFFIX(&md_ctx, dst) != 0)) { \
return false; \
} \
\
return true; \
}
-
DEFINE_DO_HASH_NORET(ALGORITHM, SUFFIX) static bool do_##ALGORITHM##_hash(term
data, unsigned char *dst) \
{ \
mbedtls_##ALGORITHM##_context md_ctx; \
\
mbedtls_##ALGORITHM##_init(&md_ctx); \
mbedtls_##ALGORITHM##_starts##SUFFIX(&md_ctx); \
\
InteropFunctionResult result = interop_chardata_fold(data, ALGORITHM##_hash_fold_fun, NULL, (void *) &md_ctx); \
if (
UNLIKELY(result != InteropOk)) { \
return false; \
} \
\
mbedtls_##ALGORITHM##_finish##SUFFIX(&md_ctx, dst); \
\
return true; \
}
-
DEFINE_DO_HASH_NORET_IS_OTHER(ALGORITHM, SUFFIX, IS_OTHER) static bool do_##ALGORITHM##_hash_##IS_OTHER(term
data, unsigned char *dst) \
{ \
mbedtls_##ALGORITHM##_context md_ctx; \
\
mbedtls_##ALGORITHM##_init(&md_ctx); \
mbedtls_##ALGORITHM##_starts##SUFFIX(&md_ctx, IS_OTHER); \
\
InteropFunctionResult result = interop_chardata_fold(data, ALGORITHM##_hash_fold_fun, NULL, (void *) &md_ctx); \
if (
UNLIKELY(result != InteropOk)) { \
return false; \
} \
\
mbedtls_##ALGORITHM##_finish##SUFFIX(&md_ctx, dst); \
\
return true; \
}
Enums
Functions
-
static InteropFunctionResult md5_hash_fold_fun(term t, void *accum)
-
static bool do_md5_hash(term data, unsigned char *dst)
-
static InteropFunctionResult sha1_hash_fold_fun(term t, void *accum)
-
static bool do_sha1_hash(term data, unsigned char *dst)
-
static InteropFunctionResult sha256_hash_fold_fun(term t, void *accum)
-
static bool do_sha256_hash_true(term data, unsigned char *dst)
-
static bool do_sha256_hash_false(term data, unsigned char *dst)
-
static InteropFunctionResult sha512_hash_fold_fun(term t, void *accum)
-
static bool do_sha512_hash_true(term data, unsigned char *dst)
-
static bool do_sha512_hash_false(term data, unsigned char *dst)
-
static term handle_iodata(term iodata, const void **data, size_t *len, void **allocated_ptr)
-
static bool bool_to_mbedtls_operation(term encrypt_flag, mbedtls_operation_t *operation)
-
const struct Nif *otp_crypto_nif_get_nif(const char *nifname)
Variables
- static const AtomStringIntPair crypto_algorithm_table [] = {{ATOM_STR("\x3", "md5"), CryptoMd5 },{ATOM_STR("\x3", "sha"), CryptoSha1 },{ATOM_STR("\x6", "sha224"), CryptoSha224 },{ATOM_STR("\x6", "sha256"), CryptoSha256 },{ATOM_STR("\x6", "sha384"), CryptoSha384 },{ATOM_STR("\x6", "sha512"), CryptoSha512 },}
- static const AtomStringIntPair cipher_table [] = {{ATOM_STR("\xB", "aes_128_ecb"), MBEDTLS_CIPHER_AES_128_ECB },{ATOM_STR("\xB", "aes_192_ecb"), MBEDTLS_CIPHER_AES_192_ECB },{ATOM_STR("\xB", "aes_256_ecb"), MBEDTLS_CIPHER_AES_256_ECB },{ATOM_STR("\xB", "aes_128_cbc"), MBEDTLS_CIPHER_AES_128_CBC },{ATOM_STR("\xB", "aes_192_cbc"), MBEDTLS_CIPHER_AES_192_CBC },{ATOM_STR("\xB", "aes_256_cbc"), MBEDTLS_CIPHER_AES_256_CBC },{ATOM_STR("\xE", "aes_128_cfb128"), MBEDTLS_CIPHER_AES_128_CFB128 },{ATOM_STR("\xE", "aes_192_cfb128"), MBEDTLS_CIPHER_AES_192_CFB128 },{ATOM_STR("\xE", "aes_256_cfb128"), MBEDTLS_CIPHER_AES_256_CFB128 },{ATOM_STR("\xB", "aes_128_ctr"), MBEDTLS_CIPHER_AES_128_CTR },{ATOM_STR("\xB", "aes_192_ctr"), MBEDTLS_CIPHER_AES_192_CTR },{ATOM_STR("\xB", "aes_256_ctr"), MBEDTLS_CIPHER_AES_256_CTR },}
- static const AtomStringIntPair padding_table [] = {{ATOM_STR("\x4", "none"), MBEDTLS_PADDING_NONE },{ATOM_STR("\xC", "pkcs_padding"), MBEDTLS_PADDING_PKCS7 },}
-
static const struct Nif crypto_hash_nif = {.base.type = NIFFunctionType, .nif_ptr = nif_crypto_hash}
-
static const struct Nif crypto_crypto_one_time_nif = {.base.type = NIFFunctionType, .nif_ptr = nif_crypto_crypto_one_time}
-
static const struct Nif crypto_strong_rand_bytes_nif = {.base.type = NIFFunctionType, .nif_ptr = nif_crypto_strong_rand_bytes}