Repository navigation
Fix AES context alignment in heap allocations #12110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielinux
wants to merge
13
commits into
wolfSSL:master
Choose a base branch
from
danielinux:fix-aes-alloc-align
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−42
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
13c4672
Fix AES context alignment in heap allocations
danielinux 234892d
Fix AES whitebox cleanup for aligned allocations
danielinux 9463c38
Limit AES alignment assertion to non-FIPS builds
danielinux d9ca519
Keep AES allocation helpers available to TI key wrap
danielinux 6541779
Fix CMAC context alignment in heap allocations
danielinux 36acd74
Fix CMAC test cleanup for aligned allocations
danielinux 5871611
Fall back to plain CMAC allocation in FIPS builds
danielinux bf77abb
Use WC_INLINE for CmacAlloc helpers in C89 builds
danielinux 82eaa96
Cast EVP CMAC free arg for C++ builds
danielinux 2c69d22
Consolidate aligned allocation helpers into private header
danielinux 9f49ba7
Make WC_FREE fallback macros statement-safe
danielinux 9bec0b7
Copy alloc_align.h in Arduino library install
danielinux 2ea254f
Void-cast unused params in wc_free_aligned
danielinux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* alloc_align.h | ||
| * | ||
| * Copyright (C) 2006-2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfSSL. | ||
| * | ||
| * wolfSSL is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfSSL is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| /* | ||
| * Private aligned-allocation helpers for heap contexts whose members carry | ||
| * alignment attributes (Aes, Cmac). On 32-bit Arm, malloc may only return an | ||
| * 8-byte-aligned pointer, so over-allocate, align, and keep the original | ||
| * pointer for XFREE. Included only by the files that allocate those contexts | ||
| * on the heap; not part of the public API. | ||
| */ | ||
| #ifndef WOLFSSL_ALLOC_ALIGN_H | ||
| #define WOLFSSL_ALLOC_ALIGN_H | ||
|
|
||
| #include <wolfssl/wolfcrypt/aes.h> | ||
| #include <wolfssl/wolfcrypt/cmac.h> | ||
|
|
||
| #if defined(WOLFSSL_USE_ALIGN) && defined(WC_ALIGNOF) && \ | ||
| !defined(WOLFSSL_TI_CRYPT) | ||
| #define WC_HAVE_ALIGNED_ALLOC | ||
| static WC_INLINE void* wc_alloc_aligned(void* heap, size_t size, | ||
| size_t align, int type) | ||
| { | ||
| byte* raw = (byte*)XMALLOC(size + align - 1 + sizeof(void*), heap, type); | ||
| wc_ptr_t p; | ||
|
|
||
| if (raw == NULL) | ||
| return NULL; | ||
| p = ((wc_ptr_t)(raw + sizeof(void*)) + align - 1) & | ||
| ~((wc_ptr_t)align - 1); | ||
| ((void**)p)[-1] = raw; | ||
| return (void*)p; | ||
| } | ||
|
|
||
| static WC_INLINE void wc_free_aligned(void* p, void* heap, int type) | ||
| { | ||
| (void)heap; | ||
| (void)type; | ||
| if (p != NULL) | ||
| XFREE(((void**)p)[-1], heap, type); | ||
| } | ||
| #endif | ||
|
|
||
| /* Type-specific wrappers: the alignment requirement is the type's, hidden | ||
| * here so call sites stay plain. The TI backend and builds without | ||
| * WC_ALIGNOF use the plain allocation path, which needs no indirection. | ||
| * Each wrapper exists only where its type does. */ | ||
| #ifndef NO_AES | ||
| #ifdef WC_HAVE_ALIGNED_ALLOC | ||
| static WC_INLINE Aes* WC_ALLOC_AES(void* heap) | ||
| { | ||
| return (Aes*)wc_alloc_aligned(heap, sizeof(Aes), WC_ALIGNOF(Aes), | ||
| DYNAMIC_TYPE_AES); | ||
| } | ||
|
|
||
| static WC_INLINE void WC_FREE_AES(Aes* aes, void* heap) | ||
| { | ||
| wc_free_aligned((void*)aes, heap, DYNAMIC_TYPE_AES); | ||
| } | ||
| #else | ||
| #define WC_ALLOC_AES(heap) \ | ||
| ((Aes*)XMALLOC(sizeof(Aes), (heap), DYNAMIC_TYPE_AES)) | ||
| #define WC_FREE_AES(aes, heap) \ | ||
| do {XFREE((aes), (heap), DYNAMIC_TYPE_AES);} while (0) | ||
| #endif | ||
| #endif /* !NO_AES */ | ||
|
|
||
| #ifdef WOLFSSL_CMAC | ||
| #ifdef WC_HAVE_ALIGNED_ALLOC | ||
| static WC_INLINE Cmac* WC_ALLOC_CMAC(void* heap) | ||
| { | ||
| return (Cmac*)wc_alloc_aligned(heap, sizeof(Cmac), WC_ALIGNOF(Cmac), | ||
| DYNAMIC_TYPE_CMAC); | ||
| } | ||
|
|
||
| static WC_INLINE void WC_FREE_CMAC(Cmac* cmac, void* heap) | ||
| { | ||
| wc_free_aligned((void*)cmac, heap, DYNAMIC_TYPE_CMAC); | ||
| } | ||
| #else | ||
| #define WC_ALLOC_CMAC(heap) \ | ||
| ((Cmac*)XMALLOC(sizeof(Cmac), (heap), DYNAMIC_TYPE_CMAC)) | ||
| #define WC_FREE_CMAC(cmac, heap) \ | ||
| do {XFREE((cmac), (heap), DYNAMIC_TYPE_CMAC);} while (0) | ||
| #endif | ||
| #endif /* WOLFSSL_CMAC */ | ||
|
|
||
| #endif /* WOLFSSL_ALLOC_ALIGN_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.