Skip to content

Commit a4b6003

Browse files
authored
Replacement for Boot ROM aes_unwrap (#7773)
* Replacement for Boot ROM aes_unwrap * Removed unnecessary test
1 parent dbc5e41 commit a4b6003

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

cores/esp8266/aes_unwrap.cpp

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Replacement for the ROM aes_unwrap() function. It uses the heap instead of
3+
* the static DRAM address at 0x3FFFEA80, which may step on the SYS stack in
4+
* special circumstances such as HWDT Stack Dump.
5+
*
6+
* When not using WPS, the address space 0x3FFFE000 up to 0x40000000 is mostly
7+
* available for the stacks. The one known exception is the ROM AES APIs. When
8+
* `aes_decrypt_init` is called, it uses memory at 0x3FFFEA80 up to 0x3FFFEB30
9+
* for a buffer. At the finish, `aes_decrypt_deinit` zeros out the buffer.
10+
*
11+
* The NONOS SDK appears to have replacements for most of the ROM's AES APIs.
12+
* However, the SDK still calls on the ROM's aes_unwrap function, which uses
13+
* the ROM's AES APIs to operate. These calls can overwrite some of the stack
14+
* space. To resolve the problem, this module replaces `aes_unwrap`.
15+
*
16+
* Final note, so far, I have not seen a problem when using the extra 4K heap
17+
* option without the "debug HWDT". It is when combined with the HWDT Stack
18+
* Dump that a problem shows. This combination adds a Boot ROM stack, which
19+
* pushes up the SYS and CONT stacks into the AES Buffer space. Then the
20+
* problem shows.
21+
*
22+
* While debugging with painted stack space, during WiFi Connect, Reconnect,
23+
* and about every hour, a block of memory 0x3FFFEA80 - 0x3FFFEB30 (176 bytes)
24+
* was zeroed by the Boot ROM function aes_decrypt_init. All other painted
25+
* memory in the area was untouched after starting WiFi.
26+
*/
27+
28+
#if defined(KEEP_ROM_AES_UNWRAP)
29+
// Using the ROM version of aes_unwrap should be fine for the no extra 4K case
30+
// which is usually used in conjunction with WPS.
31+
32+
#else
33+
// This is required for DEBUG_ESP_HWDT.
34+
// The need is unconfirmed for the extra 4K heap case.
35+
#include "umm_malloc/umm_malloc.h"
36+
37+
extern "C" {
38+
39+
// Uses this function from the Boot ROM
40+
void rijndaelKeySetupDec(u32 rk[], const u8 cipherKey[]);
41+
42+
// This replaces the Boot ROM version just for this module
43+
// Uses a malloc-ed buffer instead of the static buffer in stack address space.
44+
static void *aes_decrypt_init(const u8 *key, size_t len) {
45+
if (16u != len) {
46+
return 0;
47+
}
48+
u32 *rk = (u32 *)malloc(16*11);
49+
// u32 *rk = (u32 *)0x3FFFEA80u; // This is what the ROM would have used.
50+
if (rk) {
51+
rijndaelKeySetupDec(rk, key);
52+
}
53+
return (void *)rk;
54+
}
55+
56+
// This replaces the Boot ROM version just for this module
57+
static void aes_decrypt_deinit(void *ctx) {
58+
if (ctx) {
59+
ets_memset(ctx, 0, 16*11);
60+
free(ctx);
61+
}
62+
return;
63+
}
64+
65+
/*
66+
* The NONOS SDK has an override on this function. To replace the aes_unwrap
67+
* without changing its behavior too much. We need access to the ROM version of
68+
* the AES APIs to make our aes_unwrap functionally equal to the current
69+
* environment except for the AES Buffer.
70+
*/
71+
#ifndef ROM_aes_decrypt
72+
#define ROM_aes_decrypt 0x400092d4
73+
#endif
74+
75+
typedef void (*fp_aes_decrypt_t)(void *ctx, const u8 *crypt, u8 *plain);
76+
#define AES_DECRYPT (reinterpret_cast<fp_aes_decrypt_t>(ROM_aes_decrypt))
77+
78+
///////////////////////////////////////////////////////////////////////////////
79+
///////////////////////////////////////////////////////////////////////////////
80+
///////////////////////////////////////////////////////////////////////////////
81+
/*
82+
* This aes_unwrap() function overrides/replaces the Boot ROM version.
83+
*
84+
* It was adapted from aes_unwrap() found in the ESP8266 RTOS SDK
85+
* .../components/wap_supplicant/src/crypto/aes-unwrap.c
86+
*
87+
*/
88+
///////////////////////////////////////////////////////////////////////////////
89+
/*
90+
* AES key unwrap (128-bit KEK, RFC3394)
91+
*
92+
* Copyright (c) 2003-2007, Jouni Malinen <[email protected]>
93+
*
94+
* This program is free software; you can redistribute it and/or modify
95+
* it under the terms of the GNU General Public License version 2 as
96+
* published by the Free Software Foundation.
97+
*
98+
* Alternatively, this software may be distributed under the terms of BSD
99+
* license.
100+
*
101+
* See README and COPYING for more details.
102+
*/
103+
104+
/** based on RTOS SDK
105+
* aes_unwrap - Unwrap key with AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
106+
* @kek: Key encryption key (KEK)
107+
* @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
108+
* bytes
109+
* @cipher: Wrapped key to be unwrapped, (n + 1) * 64 bits
110+
* @plain: Plaintext key, n * 64 bits
111+
* Returns: 0 on success, -1 on failure (e.g., integrity verification failed)
112+
*/
113+
int aes_unwrap(const u8 *kek, int n, const u8 *cipher, u8 *plain)
114+
{
115+
u8 a[8], *r, b[16];
116+
int i, j;
117+
void *ctx;
118+
119+
/* 1) Initialize variables. */
120+
ets_memcpy(a, cipher, 8);
121+
r = plain;
122+
ets_memcpy(r, cipher + 8, 8 * n);
123+
124+
ctx = aes_decrypt_init(kek, 16);
125+
if (ctx == NULL)
126+
return -1;
127+
128+
/* 2) Compute intermediate values.
129+
* For j = 5 to 0
130+
* For i = n to 1
131+
* B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
132+
* A = MSB(64, B)
133+
* R[i] = LSB(64, B)
134+
*/
135+
for (j = 5; j >= 0; j--) {
136+
r = plain + (n - 1) * 8;
137+
for (i = n; i >= 1; i--) {
138+
ets_memcpy(b, a, 8);
139+
b[7] ^= n * j + i;
140+
141+
ets_memcpy(b + 8, r, 8);
142+
AES_DECRYPT(ctx, b, b);
143+
ets_memcpy(a, b, 8);
144+
ets_memcpy(r, b + 8, 8);
145+
r -= 8;
146+
}
147+
}
148+
aes_decrypt_deinit(ctx);
149+
150+
/* 3) Output results.
151+
*
152+
* These are already in @plain due to the location of temporary
153+
* variables. Just verify that the IV matches with the expected value.
154+
*/
155+
for (i = 0; i < 8; i++) {
156+
if (a[i] != 0xa6)
157+
return -1;
158+
}
159+
160+
return 0;
161+
}
162+
};
163+
#endif

0 commit comments

Comments
 (0)