diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 44397e97ff23..9829d2b783f5 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -323,3 +323,6 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_balance_anon_file_reclaim); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cpuidle_psci_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cpuidle_psci_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_sha256); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_aes_expandkey); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_aes_encrypt); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_aes_decrypt); diff --git a/include/trace/hooks/fips140.h b/include/trace/hooks/fips140.h index 10fe4bbaee08..3aebf6168791 100644 --- a/include/trace/hooks/fips140.h +++ b/include/trace/hooks/fips140.h @@ -8,12 +8,15 @@ #include #include +struct crypto_aes_ctx; + /* - * This hook exists only for the benefit of the FIPS140 crypto module, which - * uses it to swap out the underlying implementation with one that is integrity + * These hooks exist only for the benefit of the FIPS140 crypto module, which + * uses them to swap out the underlying implementation with one that is integrity * checked as per FIPS 140 requirements. No other uses are allowed or * supported. */ + DECLARE_HOOK(android_vh_sha256, TP_PROTO(const u8 *data, unsigned int len, @@ -21,6 +24,27 @@ DECLARE_HOOK(android_vh_sha256, int *hook_inuse), TP_ARGS(data, len, out, hook_inuse)); +DECLARE_HOOK(android_vh_aes_expandkey, + TP_PROTO(struct crypto_aes_ctx *ctx, + const u8 *in_key, + unsigned int key_len, + int *err), + TP_ARGS(ctx, in_key, key_len, err)); + +DECLARE_HOOK(android_vh_aes_encrypt, + TP_PROTO(const struct crypto_aes_ctx *ctx, + u8 *out, + const u8 *in, + int *hook_inuse), + TP_ARGS(ctx, out, in, hook_inuse)); + +DECLARE_HOOK(android_vh_aes_decrypt, + TP_PROTO(const struct crypto_aes_ctx *ctx, + u8 *out, + const u8 *in, + int *hook_inuse), + TP_ARGS(ctx, out, in, hook_inuse)); + #endif /* _TRACE_HOOK_FIPS140_H */ /* This part must be outside protection */ diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c index 827fe89922ff..6a8b70a12365 100644 --- a/lib/crypto/aes.c +++ b/lib/crypto/aes.c @@ -7,6 +7,7 @@ #include #include #include +#include /* * Emit the sbox as volatile const to prevent the compiler from doing @@ -189,6 +190,13 @@ int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, u32 rc, i, j; int err; +#if defined(CONFIG_CRYPTO_FIPS140) && !defined(BUILD_FIPS140_KO) + err = -(MAX_ERRNO + 1); + trace_android_vh_aes_expandkey(ctx, in_key, key_len, &err); + if (err != -(MAX_ERRNO + 1)) + return err; +#endif + err = aes_check_keylen(key_len); if (err) return err; @@ -261,6 +269,13 @@ void aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in) int rounds = 6 + ctx->key_length / 4; u32 st0[4], st1[4]; int round; +#if defined(CONFIG_CRYPTO_FIPS140) && !defined(BUILD_FIPS140_KO) + int hook_inuse = 0; + + trace_android_vh_aes_encrypt(ctx, out, in, &hook_inuse); + if (hook_inuse) + return; +#endif st0[0] = ctx->key_enc[0] ^ get_unaligned_le32(in); st0[1] = ctx->key_enc[1] ^ get_unaligned_le32(in + 4); @@ -312,6 +327,13 @@ void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in) int rounds = 6 + ctx->key_length / 4; u32 st0[4], st1[4]; int round; +#if defined(CONFIG_CRYPTO_FIPS140) && !defined(BUILD_FIPS140_KO) + int hook_inuse = 0; + + trace_android_vh_aes_decrypt(ctx, out, in, &hook_inuse); + if (hook_inuse) + return; +#endif st0[0] = ctx->key_dec[0] ^ get_unaligned_le32(in); st0[1] = ctx->key_dec[1] ^ get_unaligned_le32(in + 4);