ANDROID: crypto: lib/aes - add vendor hooks for AES library routines

Add vendor hooks that will allow the FIPS140 kernel module to override
the implementations of the AES library routines.  The FIPS 140 versions
are identical to the normal ones, but their code and rodata will have
been integrity checked at module load time.

Original commits:
  android12-5.10:
    9c556792b713 ("ANDROID: crypto: lib/aes - add vendor hooks for AES library routines")
  android14-5.15:
    d4966a820397 ("ANDROID: fips140: remove CONFIG_CRYPTO_FIPS140 option")

Bug: 153614920
Bug: 188620248
Change-Id: I5711fc42eced903565fd3c8d41ca7cdd82641148
Signed-off-by: Ard Biesheuvel <ardb@google.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
This commit is contained in:
Ard Biesheuvel 2021-04-12 13:05:54 +02:00 committed by Eric Biggers
parent 1984e62b10
commit 1c0ab9432e
3 changed files with 51 additions and 2 deletions

View File

@ -121,3 +121,6 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_rwsem_write_finished);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_alter_rwsem_list_add);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_alter_futex_plist_add);
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);

View File

@ -7,12 +7,15 @@
#define _TRACE_HOOK_FIPS140_H
#include <trace/hooks/vendor_hooks.h>
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,
@ -20,6 +23,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 */

View File

@ -7,6 +7,7 @@
#include <linux/crypto.h>
#include <linux/module.h>
#include <asm/unaligned.h>
#include <trace/hooks/fips140.h>
/*
* 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;
#ifndef __DISABLE_EXPORTS
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;
#ifndef __DISABLE_EXPORTS
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;
#ifndef __DISABLE_EXPORTS
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);