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