Skip to contentSkip to conversation

aes: Fix kerneldoc for aes.h

PATCH New Marek Vasut 3 participants 9 messages Mar 5, 2014 – Apr 18, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 aes: Fix kerneldoc for aes.h

Fix the function annotations in aes.h so they're compatible with kerneldoc.

---
 include/aes.h | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
include/aes.h +12 -10
index ea06308..c70eda6 100644
@@ -25,29 +25,31 @@ enum {
 };
 
 /**
+ * aes_expand_key() - Expand the AES key
+ *
  * Expand a key into a key schedule, which is then used for the other
  * operations.
  *
- * \param key		Key, of length AES_KEY_LENGTH bytes
- * \param expkey	Buffer to place expanded key, AES_EXPAND_KEY_LENGTH
+ * @key		Key, of length AES_KEY_LENGTH bytes
+ * @expkey	Buffer to place expanded key, AES_EXPAND_KEY_LENGTH
  */
 void aes_expand_key(u8 *key, u8 *expkey);
 
 /**
- * Encrypt a single block of data
+ * aes_encrypt() - Encrypt single block of data with AES 128
  *
- * in		Input data
- * expkey	Expanded key to use for encryption (from aes_expand_key())
- * out		Output data
+ * @in		Input data
+ * @expkey	Expanded key to use for encryption (from aes_expand_key())
+ * @out		Output data
  */
 void aes_encrypt(u8 *in, u8 *expkey, u8 *out);
 
 /**
- * Decrypt a single block of data
+ * aes_decrypt() - Decrypt single block of data with AES 128
  *
- * in		Input data
- * expkey	Expanded key to use for decryption (from aes_expand_key())
- * out		Output data
+ * @in		Input data
+ * @expkey	Expanded key to use for decryption (from aes_expand_key())
+ * @out		Output data
  */
 void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
 
Signature · 1 line
1.8.5.2
#1 lore raw

PATCH v2 2/4 aes: Move the AES-128-CBC encryption function to common code

Move the AES-128-CBC encryption function implemented in tegra20-common/crypto.c into lib/aes.c . This is well re-usable common code. Moreover, clean the code up a bit and fix the kerneldoc-style annotations.

---
 arch/arm/cpu/tegra20-common/crypto.c | 72 +-----------------------------------
 include/aes.h                        | 10 +++++
 lib/aes.c                            | 52 ++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 70 deletions(-)

V2: Replace AES_AES_KEY_LENGTH with AES_KEY_LENGTH
Use print_buffer() instead of ad-hoc implementation

arch/arm/cpu/tegra20-common/crypto.c +2 -70
index 8209f76..b18e67c 100644
@@ -19,74 +19,6 @@ enum security_op {
 	SECURITY_ENCRYPT	= 1 << 1,	/* Encrypt the data */
 };
 
-static void debug_print_vector(char *name, u32 num_bytes, u8 *data)
-{
-	u32 i;
-
-	debug("%s [%d] @0x%08x", name, num_bytes, (u32)data);
-	for (i = 0; i < num_bytes; i++) {
-		if (i % 16 == 0)
-			debug(" = ");
-		debug("%02x", data[i]);
-		if ((i+1) % 16 != 0)
-			debug(" ");
-	}
-	debug("\n");
-}
-
-/**
- * Apply chain data to the destination using EOR
- *
- * Each array is of length AES_AES_KEY_LENGTH.
- *
- * \param cbc_chain_data	Chain data
- * \param src			Source data
- * \param dst			Destination data, which is modified here
- */
-static void apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst)
-{
-	int i;
-
-	for (i = 0; i < 16; i++)
-		*dst++ = *src++ ^ *cbc_chain_data++;
-}
-
-/**
- * Encrypt some data with AES.
- *
- * \param key_schedule		Expanded key to use
- * \param src			Source data to encrypt
- * \param dst			Destination buffer
- * \param num_aes_blocks	Number of AES blocks to encrypt
- */
-static void encrypt_object(u8 *key_schedule, u8 *src, u8 *dst,
-			   u32 num_aes_blocks)
-{
-	u8 tmp_data[AES_KEY_LENGTH];
-	u8 *cbc_chain_data;
-	u32 i;
-
-	cbc_chain_data = zero_key;	/* Convenient array of 0's for IV */
-
-	for (i = 0; i < num_aes_blocks; i++) {
-		debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
-		debug_print_vector("AES Src", AES_KEY_LENGTH, src);
-
-		/* Apply the chain data */
-		apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
-		debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
-
-		/* encrypt the AES block */
-		aes_encrypt(tmp_data, key_schedule, dst);
-		debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
-
-		/* Update pointers for next loop. */
-		cbc_chain_data = dst;
-		src += AES_KEY_LENGTH;
-		dst += AES_KEY_LENGTH;
-	}
-}
-
 /**
  * Shift a vector left by one bit
  *
@@ -129,7 +61,7 @@ static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst,
 	for (i = 0; i < AES_KEY_LENGTH; i++)
 		tmp_data[i] = 0;
 
-	encrypt_object(key_schedule, tmp_data, left, 1);
+	aes_cbc_encrypt_blocks(key_schedule, tmp_data, left, 1);
 	debug_print_vector("AES(key, nonce)", AES_KEY_LENGTH, left);
 
 	left_shift_vector(left, k1, sizeof(left));
@@ -193,7 +125,7 @@ static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src,
 	if (oper & SECURITY_ENCRYPT) {
 		/* Perform this in place, resulting in src being encrypted. */
 		debug("encrypt_and_sign: begin encryption\n");
-		encrypt_object(key_schedule, src, src, num_aes_blocks);
+		aes_cbc_encrypt_blocks(key_schedule, src, src, num_aes_blocks);
 		debug("encrypt_and_sign: end encryption\n");
 	}
 
include/aes.h +10 -0
index c70eda6..d9bb387 100644
@@ -53,4 +53,14 @@ void aes_encrypt(u8 *in, u8 *expkey, u8 *out);
  */
 void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
 
+/**
+ * aes_cbc_encrypt_blocks() - Encrypt multiple blocks of data with AES CBC.
+ *
+ * @key_exp		Expanded key to use
+ * @src			Source data to encrypt
+ * @dst			Destination buffer
+ * @num_aes_blocks	Number of AES blocks to encrypt
+ */
+void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks);
+
 #endif /* _AES_REF_H_ */
lib/aes.c +52 -0

55 lines not on this page · raw message

Signature · 1 line
1.8.5.2
#2 lore raw

PATCH v2 2/4 aes: Move the AES-128-CBC encryption function to common code

Quoted 5 lines · Marek Vasut

Move the AES-128-CBC encryption function implemented in tegra20-common/crypto.c into lib/aes.c . This is well re-usable common code. Moreover, clean the code up a bit and fix the kerneldoc-style annotations.

Applied to u-boot/next (with a reword to rewrap), thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140327/0bbd004a/attachment.pgp>
#3 lore raw

PATCH v2 2/4 aes: Move the AES-128-CBC encryption function to common code

Quoted 9 lines · Tom Rini
Quoted 5 lines · Marek Vasut

Move the AES-128-CBC encryption function implemented in tegra20-common/crypto.c into lib/aes.c . This is well re-usable common code. Moreover, clean the code up a bit and fix the kerneldoc-style annotations.

Applied to u-boot/next (with a reword to rewrap), thanks!

BTW, this breaks compilation on NVIDIA Tegra Seaboard.

Tom R fixed most of the issue with "ARM:tegra20: Remove aes debug prints", but the following still remains:

Quoted 7 lines

arch/arm/cpu/tegra20-common/crypto.c: In function ?sign_object?:
arch/arm/cpu/tegra20-common/crypto.c:74:3: warning: implicit declaration of function ?apply_cbc_chain_data? [-Wimplicit-function-declaration] arch/arm/cpu/tegra20-common/crypto.c: In function ?sign_object?:
arch/arm/cpu/tegra20-common/crypto.c:74:3: warning: implicit declaration of function ?apply_cbc_chain_data? [-Wimplicit-function-declaration] arch/arm/cpu/built-in.o: In function `sign_object':
.../arch/arm/cpu/tegra20-common/crypto.c:74: undefined reference to apply_cbc_chain_data' .../arch/arm/cpu/tegra20-common/crypto.c:78: undefined reference to apply_cbc_chain_data'

#4 lore raw

PATCH 3/4 aes: Implement AES-128-CBC decryption function

In reply to Marek Vasut

Implement a compatible AES-128-CBC decryption function as a counterpart of the encryption function pulled from tegra20-common/crypto.c .

---
 include/aes.h | 10 ++++++++++
 lib/aes.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
include/aes.h +10 -0
index d9bb387..4897e6f 100644
@@ -63,4 +63,14 @@ void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
  */
 void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks);
 
+/**
+ * Decrypt multiple blocks of data with AES CBC.
+ *
+ * @key_exp		Expanded key to use
+ * @src			Source data to decrypt
+ * @dst			Destination buffer
+ * @num_aes_blocks	Number of AES blocks to decrypt
+ */
+void aes_cbc_decrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks);
+
 #endif /* _AES_REF_H_ */
lib/aes.c +28 -0
index a6648f9..9dadb22 100644
@@ -632,3 +632,31 @@ void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks)
 		dst += AES_KEY_LENGTH;
 	}
 }
+
+void aes_cbc_decrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks)
+{
+	u8 tmp_data[AES_KEY_LENGTH], tmp_block[AES_KEY_LENGTH];
+	/* Convenient array of 0's for IV */
+	u8 cbc_chain_data[AES_KEY_LENGTH] = { 0 };
+	u32 i;
+
+	for (i = 0; i < num_aes_blocks; i++) {
+		debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
+		debug_print_vector("AES Src", AES_KEY_LENGTH, src);
+
+		memcpy(tmp_block, src, AES_KEY_LENGTH);
+
+		/* Decrypt the AES block */
+		aes_decrypt(src, key_exp, tmp_data);
+		debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
+
+		/* Apply the chain data */
+		apply_cbc_chain_data(cbc_chain_data, tmp_data, dst);
+		debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
+
+		/* Update pointers for next loop. */
+		memcpy(cbc_chain_data, tmp_block, AES_KEY_LENGTH);
+		src += AES_KEY_LENGTH;
+		dst += AES_KEY_LENGTH;
+	}
+}
Signature · 1 line
1.8.5.2
#5 lore raw

PATCH 3/4 aes: Implement AES-128-CBC decryption function

Quoted 4 lines · Marek Vasut

Implement a compatible AES-128-CBC decryption function as a counterpart of the encryption function pulled from tegra20-common/crypto.c .

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140327/5f3a92f5/attachment.pgp>
#6 lore raw

PATCH 4/4 aes: Add 'aes' command to access AES-128-CBC

In reply to Marek Vasut

Add simple 'aes' command, which allows using the AES-128-CBC encryption and decryption functions from U-Boot command line.

---
 README           |  1 +
 common/Makefile  |  1 +
 common/cmd_aes.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 common/cmd_aes.c
README +1 -0
index 216f0c7..fd717bf 100644
@@ -910,6 +910,7 @@ The following options need to be configured:
 		The default command configuration includes all commands
 		except those marked below with a "*".
 
+		CONFIG_CMD_AES		  AES 128 CBC encrypt/decrypt
 		CONFIG_CMD_ASKENV	* ask for env variable
 		CONFIG_CMD_BDI		  bdinfo
 		CONFIG_CMD_BEDBUG	* Include BedBug Debugger
common/Makefile +1 -0
index 04e9cdd..622f7bb 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_ENV_IS_IN_UBI) += env_ubi.o
 obj-$(CONFIG_ENV_IS_NOWHERE) += env_nowhere.o
 
 # command
+obj-$(CONFIG_CMD_AES) += cmd_aes.o
 obj-$(CONFIG_CMD_AMBAPP) += cmd_ambapp.o
 obj-$(CONFIG_SOURCE) += cmd_source.o
 obj-$(CONFIG_CMD_SOURCE) += cmd_source.o
common/cmd_aes.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
1.8.5.2
#7 lore raw

PATCH 4/4 aes: Add 'aes' command to access AES-128-CBC

Quoted 4 lines · Marek Vasut

Add simple 'aes' command, which allows using the AES-128-CBC encryption and decryption functions from U-Boot command line.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140327/4f762456/attachment.pgp>
#8 lore raw

PATCH 1/4 aes: Fix kerneldoc for aes.h

In reply to Marek Vasut
Quoted 3 lines · Marek Vasut

Fix the function annotations in aes.h so they're compatible with kerneldoc.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140327/792bcc27/attachment.pgp>