otp_crypto.c
Include dependency graph for otp_crypto.c:
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}