File: aes.c

package info (click to toggle)
optee-os 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,560 kB
  • sloc: ansic: 441,914; asm: 12,903; python: 3,719; makefile: 1,676; sh: 238
file content (60 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (C) 2018, ARM Limited
 * Copyright (C) 2019, Linaro Limited
 */

#include <assert.h>
#include <compiler.h>
#include <crypto/crypto_accel.h>
#include <crypto/crypto.h>
#include <kernel/panic.h>
#include <mbedtls/aes.h>
#include <mbedtls/platform_util.h>
#include <string.h>

#if defined(MBEDTLS_AES_ALT)
void mbedtls_aes_init(mbedtls_aes_context *ctx)
{
	assert(ctx);
	memset(ctx, 0, sizeof(*ctx));
}

void mbedtls_aes_free( mbedtls_aes_context *ctx )
{
	if (ctx)
		mbedtls_platform_zeroize(ctx, sizeof(*ctx));
}

int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
			   unsigned int keybits)
{
	assert(ctx && key);

	if (keybits != 128 && keybits != 192 && keybits != 256)
		return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;

	if (crypto_accel_aes_expand_keys(key, keybits / 8, ctx->key, NULL,
					 sizeof(ctx->key), &ctx->round_count))
		return MBEDTLS_ERR_AES_BAD_INPUT_DATA;

	return 0;
}

int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
			   unsigned int keybits)
{
	uint32_t enc_key[sizeof(ctx->key)] = { 0 };

	assert(ctx && key);

	if (keybits != 128 && keybits != 192 && keybits != 256)
		return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;

	if (crypto_accel_aes_expand_keys(key, keybits / 8, enc_key, ctx->key,
					 sizeof(ctx->key), &ctx->round_count))
		return MBEDTLS_ERR_AES_BAD_INPUT_DATA;

	return 0;
}
#endif /*MBEDTLS_AES_ALT*/