xref: /openbmc/u-boot/include/uboot_aes.h (revision e8f80a5a)
1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2b80c0b99SStefano Babic /*
3b80c0b99SStefano Babic  * Copyright (c) 2011 The Chromium OS Authors.
4b80c0b99SStefano Babic  * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com>
5b80c0b99SStefano Babic  */
6b80c0b99SStefano Babic 
7b80c0b99SStefano Babic #ifndef _AES_REF_H_
8b80c0b99SStefano Babic #define _AES_REF_H_
9b80c0b99SStefano Babic 
10b80c0b99SStefano Babic #ifdef USE_HOSTCC
11b80c0b99SStefano Babic /* Define compat stuff for use in fw_* tools. */
12b80c0b99SStefano Babic typedef unsigned char u8;
13b80c0b99SStefano Babic typedef unsigned int u32;
14b80c0b99SStefano Babic #define debug(...) do {} while (0)
15b80c0b99SStefano Babic #endif
16b80c0b99SStefano Babic 
17b80c0b99SStefano Babic /*
18b80c0b99SStefano Babic  * AES encryption library, with small code size, supporting only 128-bit AES
19b80c0b99SStefano Babic  *
20b80c0b99SStefano Babic  * AES is a stream cipher which works a block at a time, with each block
21b80c0b99SStefano Babic  * in this case being AES_KEY_LENGTH bytes.
22b80c0b99SStefano Babic  */
23b80c0b99SStefano Babic 
24b80c0b99SStefano Babic enum {
25b80c0b99SStefano Babic 	AES_STATECOLS	= 4,	/* columns in the state & expanded key */
26b80c0b99SStefano Babic 	AES_KEYCOLS	= 4,	/* columns in a key */
27b80c0b99SStefano Babic 	AES_ROUNDS	= 10,	/* rounds in encryption */
28b80c0b99SStefano Babic 
29b80c0b99SStefano Babic 	AES_KEY_LENGTH	= 128 / 8,
30b80c0b99SStefano Babic 	AES_EXPAND_KEY_LENGTH	= 4 * AES_STATECOLS * (AES_ROUNDS + 1),
31b80c0b99SStefano Babic };
32b80c0b99SStefano Babic 
33b80c0b99SStefano Babic /**
34b80c0b99SStefano Babic  * aes_expand_key() - Expand the AES key
35b80c0b99SStefano Babic  *
36b80c0b99SStefano Babic  * Expand a key into a key schedule, which is then used for the other
37b80c0b99SStefano Babic  * operations.
38b80c0b99SStefano Babic  *
39b80c0b99SStefano Babic  * @key		Key, of length AES_KEY_LENGTH bytes
40b80c0b99SStefano Babic  * @expkey	Buffer to place expanded key, AES_EXPAND_KEY_LENGTH
41b80c0b99SStefano Babic  */
42b80c0b99SStefano Babic void aes_expand_key(u8 *key, u8 *expkey);
43b80c0b99SStefano Babic 
44b80c0b99SStefano Babic /**
45b80c0b99SStefano Babic  * aes_encrypt() - Encrypt single block of data with AES 128
46b80c0b99SStefano Babic  *
47b80c0b99SStefano Babic  * @in		Input data
48b80c0b99SStefano Babic  * @expkey	Expanded key to use for encryption (from aes_expand_key())
49b80c0b99SStefano Babic  * @out		Output data
50b80c0b99SStefano Babic  */
51b80c0b99SStefano Babic void aes_encrypt(u8 *in, u8 *expkey, u8 *out);
52b80c0b99SStefano Babic 
53b80c0b99SStefano Babic /**
54b80c0b99SStefano Babic  * aes_decrypt() - Decrypt single block of data with AES 128
55b80c0b99SStefano Babic  *
56b80c0b99SStefano Babic  * @in		Input data
57b80c0b99SStefano Babic  * @expkey	Expanded key to use for decryption (from aes_expand_key())
58b80c0b99SStefano Babic  * @out		Output data
59b80c0b99SStefano Babic  */
60b80c0b99SStefano Babic void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
61b80c0b99SStefano Babic 
62b80c0b99SStefano Babic /**
63b80c0b99SStefano Babic  * Apply chain data to the destination using EOR
64b80c0b99SStefano Babic  *
65b80c0b99SStefano Babic  * Each array is of length AES_KEY_LENGTH.
66b80c0b99SStefano Babic  *
67b80c0b99SStefano Babic  * @cbc_chain_data	Chain data
68b80c0b99SStefano Babic  * @src			Source data
69b80c0b99SStefano Babic  * @dst			Destination data, which is modified here
70b80c0b99SStefano Babic  */
71b80c0b99SStefano Babic void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst);
72b80c0b99SStefano Babic 
73b80c0b99SStefano Babic /**
74b80c0b99SStefano Babic  * aes_cbc_encrypt_blocks() - Encrypt multiple blocks of data with AES CBC.
75b80c0b99SStefano Babic  *
76b80c0b99SStefano Babic  * @key_exp		Expanded key to use
77af09eba6SАндрей Мозжухин  * @iv			Initialization vector
78b80c0b99SStefano Babic  * @src			Source data to encrypt
79b80c0b99SStefano Babic  * @dst			Destination buffer
80b80c0b99SStefano Babic  * @num_aes_blocks	Number of AES blocks to encrypt
81b80c0b99SStefano Babic  */
82af09eba6SАндрей Мозжухин void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
83af09eba6SАндрей Мозжухин 			    u32 num_aes_blocks);
84b80c0b99SStefano Babic 
85b80c0b99SStefano Babic /**
86b80c0b99SStefano Babic  * Decrypt multiple blocks of data with AES CBC.
87b80c0b99SStefano Babic  *
88b80c0b99SStefano Babic  * @key_exp		Expanded key to use
89af09eba6SАндрей Мозжухин  * @iv			Initialization vector
90b80c0b99SStefano Babic  * @src			Source data to decrypt
91b80c0b99SStefano Babic  * @dst			Destination buffer
92b80c0b99SStefano Babic  * @num_aes_blocks	Number of AES blocks to decrypt
93b80c0b99SStefano Babic  */
94af09eba6SАндрей Мозжухин void aes_cbc_decrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
95af09eba6SАндрей Мозжухин 			    u32 num_aes_blocks);
96b80c0b99SStefano Babic 
97b80c0b99SStefano Babic #endif /* _AES_REF_H_ */
98