用户空间中的AES内核模块

时间:2014-06-05 14:04:45

标签: c encryption aes

我目前遇到问题

我想在各种设备上测试关于速度的AES内核模块实现。 我修改了代码,以便在用户空间编译 nexn我想对填充了随机数据的缓冲区进行编码和解码。一个包含10个条目的数组。

void randombuffer(int size, uint8_t *buffer)
{
for (int i = 0; i < size; i++)
{buffer[i] = (rand() % 10);}
}

但我在使用主要

中的加密和解密功能时遇到了麻烦
int main(void)
{
    int size = 10;
    uint8_t *buffer = malloc(size * sizeof(uint8_t));
    uint8_t *tempbuffer = malloc(size * sizeof(uint8_t));
    uint8_t *outbuffer = malloc(size * sizeof(uint8_t));
    struct crypto_tfm *tfm = malloc(sizeof(struct crypto_tfm));
    randombuffer(size, buffer);
    aes_encrypt(tfm, tempbuffer, buffer);
    aes_decrypt(tfm, outbuffer, tempbuffer);
}

我实际上不知道如何用所需的上下文填充* tfm,继承结构

struct crypto_tfm
{
uint32_t crt_flags;

union
{
    struct ablkcipher_tfm ablkcipher;
    struct aead_tfm aead;
    struct blkcipher_tfm blkcipher;
    struct cipher_tfm cipher;
    struct hash_tfm hash;
    struct compress_tfm compress;
    struct rng_tfm rng;
} crt_u;

void (*exit)(struct crypto_tfm *tfm);

struct crypto_alg *crt_alg;

void *crt_ctx[] CRYPTO_MINALIGN_ATTR;
};

以及加密功能

static void aes_encrypt(struct crypto_tfm *tfm, uint8_t *out,
        const uint8_t *in)
{
    const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
    const __le32 *src = (const __le32 *)in;
    __le32 *dst = (__le32 *)out;
    uint32_t b0[4], b1[4];
    const uint32_t *kp = ctx->key_enc + 4;
    const int key_len = ctx->key_length;

    b0[0] = le32_to_cpu(src[0]) ^ ctx->key_enc[0];
    b0[1] = le32_to_cpu(src[1]) ^ ctx->key_enc[1];
    b0[2] = le32_to_cpu(src[2]) ^ ctx->key_enc[2];
    b0[3] = le32_to_cpu(src[3]) ^ ctx->key_enc[3];

    if (key_len > 24) {
        f_nround(b1, b0, kp);
        f_nround(b0, b1, kp);
    }        

    if (key_len > 16) {
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    }

    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_lround(b0, b1, kp);

    dst[0] = cpu_to_le32(b0[0]);
    dst[1] = cpu_to_le32(b0[1]);
    dst[2] = cpu_to_le32(b0[2]);
    dst[3] = cpu_to_le32(b0[3]);
    }

最后但并非最不重要的是crypto_aes_ctx struct

struct crypto_aes_ctx {
    uint32_t key_enc[AES_MAX_KEYLENGTH_U32];
    uint32_t key_dec[AES_MAX_KEYLENGTH_U32];
    uint32_t key_length;
};

0 个答案:

没有答案