mirror of
https://github.com/shchmue/Lockpick.git
synced 2025-02-09 22:33:06 +01:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b150d20957 | ||
|
d91923accc | ||
|
9bbef1cc69 | ||
|
b85a2e6631 | ||
|
09f9e4242c | ||
|
b2ca9c881d | ||
|
0c6bb239f0 | ||
|
b65668144f | ||
|
31a882496f | ||
|
90563f6e63 | ||
|
f7dc8f027b | ||
|
8b42b1b184 | ||
|
867d2d6520 | ||
|
986468a32b | ||
|
9deeefee9d | ||
|
d4b4f868b1 | ||
|
352c39ae8a | ||
|
49c6fd8dc7 | ||
|
4d460e9509 | ||
|
922cf3f4c4 | ||
|
41c2604d9a | ||
|
c866b04a74 | ||
|
3eefba895d |
55
.gitignore
vendored
Normal file
55
.gitignore
vendored
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Object files
|
||||||
|
*.o
|
||||||
|
*.ko
|
||||||
|
*.obj
|
||||||
|
*.elf
|
||||||
|
|
||||||
|
# Linker output
|
||||||
|
*.ilk
|
||||||
|
*.map
|
||||||
|
*.exp
|
||||||
|
*.lst
|
||||||
|
main
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Libraries
|
||||||
|
*.lib
|
||||||
|
*.a
|
||||||
|
*.la
|
||||||
|
*.lo
|
||||||
|
|
||||||
|
# Shared objects (inc. Windows DLLs)
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
*.i*86
|
||||||
|
*.x86_64
|
||||||
|
*.hex
|
||||||
|
*.nso
|
||||||
|
*.pfs0
|
||||||
|
*.nro
|
||||||
|
*.nacp
|
||||||
|
*.pfs0
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
*.dSYM/
|
||||||
|
*.su
|
||||||
|
*.idb
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
.vscode
|
||||||
|
build
|
||||||
|
out
|
||||||
|
research
|
10
Makefile
10
Makefile
@ -32,12 +32,12 @@ include $(DEVKITPRO)/libnx/switch_rules
|
|||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
APP_TITLE := Lockpick
|
APP_TITLE := Lockpick
|
||||||
APP_AUTHOR := shchmue
|
APP_AUTHOR := shchmue
|
||||||
APP_VERSION := 1.0
|
APP_VERSION := 1.2.6
|
||||||
|
|
||||||
TARGET := $(subst $e ,_,$(notdir $(APP_TITLE)))
|
TARGET := $(subst $e ,_,$(notdir $(APP_TITLE)))
|
||||||
BUILD := build
|
BUILD := build
|
||||||
OUTDIR := out
|
OUTDIR := out
|
||||||
SOURCES := source
|
SOURCES := source source/fatfs source/nx
|
||||||
DATA := data
|
DATA := data
|
||||||
INCLUDES := include
|
INCLUDES := include
|
||||||
EXEFS_SRC := exefs_src
|
EXEFS_SRC := exefs_src
|
||||||
@ -58,13 +58,13 @@ CXXFLAGS := $(CFLAGS) -std=gnu++17 -fno-rtti -fno-exceptions
|
|||||||
ASFLAGS := -g $(ARCH)
|
ASFLAGS := -g $(ARCH)
|
||||||
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
||||||
|
|
||||||
LIBS := -lnx -lmbedcrypto -lstdc++fs `freetype-config --libs`
|
LIBS := -lnx -lstdc++fs `freetype-config --libs`
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
# list of directories containing libraries, this must be the top level containing
|
# list of directories containing libraries, this must be the top level containing
|
||||||
# include and lib
|
# include and lib
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
LIBDIRS := $(PORTLIBS) $(LIBNX) $(TOPDIR)/source/mbedtls
|
LIBDIRS := $(PORTLIBS) $(LIBNX)
|
||||||
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
@ -150,13 +150,11 @@ all: $(BUILD)
|
|||||||
|
|
||||||
$(BUILD):
|
$(BUILD):
|
||||||
@[ -d $@ ] || mkdir -p $@ $(BUILD) $(OUTDIR)
|
@[ -d $@ ] || mkdir -p $@ $(BUILD) $(OUTDIR)
|
||||||
$(MAKE) -C source/mbedtls/lib all
|
|
||||||
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
clean:
|
clean:
|
||||||
@echo clean ...
|
@echo clean ...
|
||||||
$(MAKE) -C source/mbedtls/lib clean
|
|
||||||
@rm -fr $(BUILD) $(OUTDIR) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf
|
@rm -fr $(BUILD) $(OUTDIR) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf
|
||||||
|
|
||||||
|
|
||||||
|
39
README.md
39
README.md
@ -1,31 +1,42 @@
|
|||||||
Lockpick
|
Lockpick
|
||||||
=
|
=
|
||||||
This is a ground-up C++17 rewrite of homebrew key derivation software, namely [kezplez-nx](https://github.com/tesnos/kezplez-nx). It also dumps titlekeys. This will dump all keys through `*_key_05` on firmwares below 6.2.0 and through `*_key_06` on 6.2.0 and above.
|
Lockpick is a ground-up C++17 rewrite of homebrew key derivation software, namely [kezplez-nx](https://github.com/tesnos/kezplez-nx). It also dumps titlekeys. This will dump all keys through `*_key_05` on firmwares below `6.2.0` and through `*_key_06` on `6.2.0`.
|
||||||
|
|
||||||
|
Due to key generation changes introduced in `7.0.0`, Lockpick is not able to dump keys ending in 07 at all. Furthermore, unfortunately the public method to dump `tsec_root_key` is only available on firmware `6.2.0` so `7.x` consoles can only dump through keys ending in 05.
|
||||||
|
|
||||||
What this software does differently
|
What this software does differently
|
||||||
=
|
=
|
||||||
* Dumps titlekeys
|
* Dumps `titlekeys` and SD seed
|
||||||
|
* Dumps all keys through `6.2.0`
|
||||||
* Uses the superfast `xxHash` instead of `sha256` when searching exefs for keys for a ~5x speed improvement
|
* Uses the superfast `xxHash` instead of `sha256` when searching exefs for keys for a ~5x speed improvement
|
||||||
* Gets all possible keys from running process memory - this means no need to decrypt `Package2` at all, let alone decompress `KIP`s
|
* Gets all possible keys from running process memory - this means no need to decrypt `Package2` at all, let alone decompress `KIP`s
|
||||||
* Gets `header_key` without `tsec`, `sbk`, `master_key_00` or `aes` sources - which may or may not be the same way `ChoiDujourNX` does it :eyes: (and I'm gonna issue a challenge to homebrew title installers to implement similar code so you don't need your users to use separate software like this :stuck_out_tongue_winking_eye: it's up to you to figure out if the same can be done for `key_area_keys` if needed)
|
* Gets bis keys and `header_key` without `tsec`, `sbk`, `master_key_00` or `aes` sources. Shoutout to exelix11 for using this method in [SwitchThemeInjector](https://github.com/exelix11/SwitchThemeInjector)! Homebrew devs should be doing this instead of requiring users to provide key files!
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
=
|
=
|
||||||
1. Use [Hekate](https://github.com/CTCaer/hekate/releases) to dump TSEC and fuses:
|
1. Use [Hekate v4.5+](https://github.com/CTCaer/hekate/releases) to dump TSEC and fuses:
|
||||||
1. Push hekate payload bin using [TegraRCMSmash](https://github.com/rajkosto/TegraRcmSmash)/[TegraRCMGUI](https://github.com/eliboa/TegraRcmGUI)/modchip/injector
|
1. Push hekate payload bin using [TegraRCMSmash](https://github.com/rajkosto/TegraRcmSmash)/[TegraRCMGUI](https://github.com/eliboa/TegraRcmGUI)/modchip/injector
|
||||||
2. Using the `VOL` and `Power` buttons to navigate, select `Console info...`
|
2. Using the `VOL` and `Power` buttons to navigate, select `Console info...`
|
||||||
3. Select `Print fuse info`
|
3. Select `Print fuse info` (_not_ `kfuse info`)
|
||||||
4. Press `Power` to save fuses to SD card
|
4. Press `Power` to save fuse info to SD card
|
||||||
5. Select `Print TSEC keys`
|
5. Select `Print TSEC keys`
|
||||||
6. Press `Power` to save TSEC to SD card
|
6. Press `Power` to save TSEC keys to SD card
|
||||||
2. Launch CFW of choice
|
2. Launch CFW of choice
|
||||||
3. Open `Homebrew Menu`
|
3. Open `Homebrew Menu`
|
||||||
4. Run `Lockpick`
|
4. Run `Lockpick`
|
||||||
5. Use the resulting `prod.keys` file as needed and rename if required
|
5. Use the resulting `/switch/prod.keys` file as needed and rename if required by any software you're using
|
||||||
|
|
||||||
|
You may instead use [biskeydump](https://github.com/rajkosto/biskeydump) and dump to SD to get all keys prior to the 6.2.0 generation - all keys up to those ending in 05. Lockpick will dump all keys up to that point regardless which firmware it's run on.
|
||||||
|
|
||||||
|
Notes
|
||||||
|
=
|
||||||
|
* To get keys ending in 06, you must have firmware `6.2.0` installed
|
||||||
|
* No one knows `package1_key_06`, it's derived and erased fully within the encrypted TSEC payload. While there's a way to extricate `tsec_root_key` due to the way it's used, this is unfortunately not true of the `package1` key
|
||||||
|
* If for some reason you dump TSEC keys on `6.2.0` and not fuses (`secure_boot_key`) you will still get everything except any of the `package1` or keyblob keys (without `secure_boot_key`, you can't decrypt keyblobs and that's where `package1` keys live)
|
||||||
|
|
||||||
Building
|
Building
|
||||||
=
|
=
|
||||||
Release built with `libnx v1.6.0`.
|
Release built with [libnx release v2.4.0](https://github.com/switchbrew/libnx).
|
||||||
|
|
||||||
Uses `freetype` which comes with `switch-portlibs` via `devkitPro pacman`:
|
Uses `freetype` which comes with `switch-portlibs` via `devkitPro pacman`:
|
||||||
```
|
```
|
||||||
@ -45,14 +56,16 @@ Special Thanks
|
|||||||
* The folks in the [ReSwitched](https://reswitched.team/) Discord server for answering my innumerable questions while researching this (and having such a useful chat backlog!)
|
* The folks in the [ReSwitched](https://reswitched.team/) Discord server for answering my innumerable questions while researching this (and having such a useful chat backlog!)
|
||||||
* The memory reading code from jakibaki's [sys-netcheat](https://github.com/jakibaki/sys-netcheat) was super useful for getting keys out of running process memory
|
* The memory reading code from jakibaki's [sys-netcheat](https://github.com/jakibaki/sys-netcheat) was super useful for getting keys out of running process memory
|
||||||
* The System Save dumping methodology from Adubbz' [Compelled Disclosure](https://github.com/Adubbz/Compelled-Disclosure)
|
* The System Save dumping methodology from Adubbz' [Compelled Disclosure](https://github.com/Adubbz/Compelled-Disclosure)
|
||||||
* Shouts out to fellow key derivers: shadowninja108 for [HACGUI](https://github.com/shadowninja108/HACGUI), Thealexblarney for [Libhac](https://github.com/Thealexbarney/LibHac), and [rajkosto](https://github.com/rajkosto/) :eyes:
|
* Shouts out to fellow key derivers: shadowninja108 for [HACGUI](https://github.com/shadowninja108/HACGUI), Thealexbarney for [Libhac](https://github.com/Thealexbarney/LibHac), and [rajkosto](https://github.com/rajkosto/) :eyes:
|
||||||
|
* [misson2000](https://github.com/misson20000) for help with `std::invoke` to get the function timer working
|
||||||
|
* [Simon](https://github.com/simontime) for the `eticket_rsa_kek` derivation method and for suggesting invoking `spl` for faster titlekey derivation
|
||||||
|
* [SciresM](https://github.com/SciresM) for the libnx aes library
|
||||||
* The constantly-improving docs on [Switchbrew wiki](https://switchbrew.org/wiki/) and [libnx](https://switchbrew.github.io/libnx/files.html)
|
* The constantly-improving docs on [Switchbrew wiki](https://switchbrew.org/wiki/) and [libnx](https://switchbrew.github.io/libnx/files.html)
|
||||||
* [mission2000](https://github.com/misson20000) for help with `std::invoke` to get the function timer working
|
|
||||||
* Literally the friends I made along the way! I came to the scene late and I've still managed to meet some wonderful people :) Thanks for all the help testing, making suggestions, and cheerleading!
|
* Literally the friends I made along the way! I came to the scene late and I've still managed to meet some wonderful people :) Thanks for all the help testing, making suggestions, and cheerleading!
|
||||||
|
|
||||||
Licenses
|
Licenses
|
||||||
=
|
=
|
||||||
* `AES` functions are from [mbedtls](https://tls.mbed.org/) licensed under [GPLv2](https://www.gnu.org/licenses/old-licenses/gpl-2.0.html))
|
* `es` ipc code is from [Tinfoil](https://github.com/Adubbz/Tinfoil) licensed under [MIT](https://github.com/Adubbz/Tinfoil/blob/master/LICENSE)
|
||||||
* `creport_debug_types` and fast `sha256` implementation are from [Atmosphère](https://github.com/atmosphere-NX/Atmosphere) licensed under [GPLv2](https://github.com/Atmosphere-NX/Atmosphere/blob/master/LICENSE)
|
* `FatFs R0.13c` is located [here](http://elm-chan.org/fsw/ff/00index_e.html) and is licensed under its own [BSD-style license](source/fatfs/LICENSE.txt)
|
||||||
* Simple `xxHash` implementation is from [stbrumme](https://github.com/stbrumme/xxhash) licensed under [MIT](https://github.com/stbrumme/xxhash/blob/master/LICENSE)
|
* Simple `xxHash` implementation is from [stbrumme](https://github.com/stbrumme/xxhash) licensed under [MIT](https://github.com/stbrumme/xxhash/blob/master/LICENSE)
|
||||||
* Padlock icon is from [Icons8](https://icons8.com/) licensed under [Creative Commons Attribution-NoDerivs 3.0 Unported](https://creativecommons.org/licenses/by-nd/3.0/)
|
* Padlock icon is from [Icons8](https://icons8.com/) licensed under [Creative Commons Attribution-NoDerivs 3.0 Unported](https://creativecommons.org/licenses/by-nd/3.0/)
|
50
changelog.md
Normal file
50
changelog.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# Changelog
|
||||||
|
## Version 1.2.6
|
||||||
|
* Fix bis key generation on newer hardware
|
||||||
|
|
||||||
|
## Version 1.2.5
|
||||||
|
* Support Hekate v5 fuse dump format
|
||||||
|
* Make names consistent with libnx v2.2.0
|
||||||
|
* Adjust text alignment and coloring in Lockpick_RCM note
|
||||||
|
|
||||||
|
## Version 1.2.4
|
||||||
|
* Support new emunand FS memory layout
|
||||||
|
* No longer save header_key if empty
|
||||||
|
|
||||||
|
## Version 1.2.3
|
||||||
|
* Remove mbedtls dependency in favor of new libnx crypto library
|
||||||
|
* Remove libnx 1.6.0 support since crypto requires later commit
|
||||||
|
* Skip contradictory messaging if skipping keyfile save
|
||||||
|
|
||||||
|
## Version 1.2.2
|
||||||
|
* Do not overwrite existing keyfile that contains master_key_07
|
||||||
|
* Read eticket_rsa_kek from existing keyfile in case user is only running this for titlekeys
|
||||||
|
* Create /switch folder if needed
|
||||||
|
|
||||||
|
## Version 1.2.1
|
||||||
|
* Generate bis keys without master keys
|
||||||
|
* Update file size check to support Hekate v4.8 TSEC dump
|
||||||
|
* Fixed prod.keys alphabetization error
|
||||||
|
* Fixed build warning for ff.c
|
||||||
|
* Added in-app disclaimer about which keys can be dumped
|
||||||
|
|
||||||
|
## Version 1.2
|
||||||
|
* Update for libnx v2.0.0 compatibility and still runs when built with v1.6.0
|
||||||
|
* The binary got even smaller!
|
||||||
|
* Accelerate finding FS keys
|
||||||
|
* No longer find BIS sources as they're hardcoded (whoops)
|
||||||
|
* Find all keys on first pass hashing FS instead of hashing the whole thing from the beginning repeatedly (__*whoops*__)
|
||||||
|
|
||||||
|
## Version 1.1.1
|
||||||
|
* No longer try to dump SD seed and ES keys on 1.0.0 as they're not available until 2.0.0
|
||||||
|
|
||||||
|
## Version 1.1
|
||||||
|
* Changed titlekey dump methodology
|
||||||
|
* No longer crashes sysmodule, reboot no longer needed
|
||||||
|
* Queries ES to verify ticket list is accurate
|
||||||
|
* May take slightly longer than before on systems with hundreds of tickets
|
||||||
|
* Now dumps SD seed
|
||||||
|
* Reorganized and clarified UI text
|
||||||
|
* Now indicates if no titles are installed to dump titlekeys from
|
||||||
|
* Swapped C++ stream functions for C I/O to reclaim some speed and binary size
|
||||||
|
* Tightened up dependencies
|
@ -20,19 +20,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Common.hpp"
|
#include "Common.hpp"
|
||||||
|
#include "Key.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
#include <machine/endian.h>
|
#include <machine/endian.h>
|
||||||
}
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "sha256.h"
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
static u32 framebuf_width = 0;
|
static u32 framebuf_width = 0;
|
||||||
|
static Framebuffer fb;
|
||||||
|
static u32 stride;
|
||||||
static u32 *framebuf;
|
static u32 *framebuf;
|
||||||
// FreeType vars
|
// FreeType vars
|
||||||
static FT_Library library;
|
static FT_Library library;
|
||||||
@ -108,26 +111,31 @@ namespace Common {
|
|||||||
|
|
||||||
PlFontData font;
|
PlFontData font;
|
||||||
|
|
||||||
consoleInit(NULL);
|
|
||||||
|
|
||||||
plGetSharedFontByType(&font, PlSharedFontType_Standard);
|
plGetSharedFontByType(&font, PlSharedFontType_Standard);
|
||||||
|
|
||||||
FT_Init_FreeType(&library);
|
FT_Init_FreeType(&library);
|
||||||
FT_New_Memory_Face(library, static_cast<FT_Byte *>(font.address), font.size, 0, &face);
|
FT_New_Memory_Face(library, static_cast<FT_Byte *>(font.address), font.size, 0, &face);
|
||||||
FT_Set_Char_Size(face, 0, 6*64, 300, 300);
|
FT_Set_Char_Size(face, 0, 6*64, 300, 300);
|
||||||
|
|
||||||
gfxSetMode(GfxMode_LinearDouble);
|
framebufferCreate(&fb, nwindowGetDefault(), FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
|
||||||
framebuf = (u32 *)gfxGetFramebuffer(&framebuf_width, NULL);
|
framebufferMakeLinear(&fb);
|
||||||
memset(framebuf, 0, gfxGetFramebufferSize());
|
framebuf = (u32 *)framebufferBegin(&fb, &stride);
|
||||||
|
framebuf_width = stride / sizeof(u32);
|
||||||
|
memset(framebuf, 0, stride*FB_HEIGHT);
|
||||||
|
framebufferEnd(&fb);
|
||||||
|
|
||||||
draw_text(0x10, 0x020, YELLOW, "Lockpick! by shchmue");
|
draw_text(0x010, 0x020, YELLOW, "Lockpick! by shchmue");
|
||||||
|
draw_text(0x190, 0x020, YELLOW, "Note:");
|
||||||
|
draw_text(0x1e0, 0x020, YELLOW, "Lockpick can only dump keys 00-05 (or 00-06 on 6.2.0)");
|
||||||
|
draw_text(0x1e0, 0x040, CYAN, "Lockpick_RCM");
|
||||||
|
draw_text(0x2a0, 0x040, YELLOW, "can get newer keys on firmware 7.0.0+!");
|
||||||
|
|
||||||
draw_set_rect(814, 452 + 42 * 0, 450, 42, RGBA8_MAXALPHA(0xe7, 0x00, 0x00));
|
draw_set_rect(814, 452 + 42 * 0, 450, 42, FLAG_RED);
|
||||||
draw_set_rect(814, 452 + 42 * 1, 450, 42, RGBA8_MAXALPHA(0xff, 0x8c, 0x00));
|
draw_set_rect(814, 452 + 42 * 1, 450, 42, FLAG_ORANGE);
|
||||||
draw_set_rect(814, 452 + 42 * 2, 450, 42, RGBA8_MAXALPHA(0xff, 0xef, 0x00));
|
draw_set_rect(814, 452 + 42 * 2, 450, 42, FLAG_YELLOW);
|
||||||
draw_set_rect(814, 452 + 42 * 3, 450, 42, RGBA8_MAXALPHA(0x00, 0x81, 0x1f));
|
draw_set_rect(814, 452 + 42 * 3, 450, 42, FLAG_GREEN);
|
||||||
draw_set_rect(814, 452 + 42 * 4, 450, 42, RGBA8_MAXALPHA(0x00, 0x44, 0xff));
|
draw_set_rect(814, 452 + 42 * 4, 450, 42, FLAG_BLUE);
|
||||||
draw_set_rect(814, 452 + 42 * 5, 450, 42, RGBA8_MAXALPHA(0x76, 0x00, 0x89));
|
draw_set_rect(814, 452 + 42 * 5, 450, 42, FLAG_VIOLET);
|
||||||
|
|
||||||
if ( !(envIsSyscallHinted(0x60) && // svcDebugActiveProcess
|
if ( !(envIsSyscallHinted(0x60) && // svcDebugActiveProcess
|
||||||
envIsSyscallHinted(0x63) && // svcGetDebugEvent
|
envIsSyscallHinted(0x63) && // svcGetDebugEvent
|
||||||
@ -135,103 +143,94 @@ namespace Common {
|
|||||||
envIsSyscallHinted(0x69) && // svcQueryDebugProcessMemory
|
envIsSyscallHinted(0x69) && // svcQueryDebugProcessMemory
|
||||||
envIsSyscallHinted(0x6a))) { // svcReadDebugProcessMemory
|
envIsSyscallHinted(0x6a))) { // svcReadDebugProcessMemory
|
||||||
draw_text(0x190, 0x20, RED, "Error: Please run with debug svc permissions!");
|
draw_text(0x190, 0x20, RED, "Error: Please run with debug svc permissions!");
|
||||||
wait_to_exit(Status_fail);
|
wait_to_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_text(0x10, 0x080, CYAN, "Get Tegra keys...");
|
draw_text(0x10, 0x060, CYAN, "Get Tegra keys...");
|
||||||
draw_text(0x10, 0x0a0, CYAN, "Get keys from memory...");
|
draw_text(0x10, 0x080, CYAN, "Get keys from memory...");
|
||||||
draw_text(0x10, 0x0c0, CYAN, "Get master keys...");
|
draw_text(0x10, 0x0a0, CYAN, "Get master keys...");
|
||||||
draw_text(0x10, 0x0e0, CYAN, "Derive remaining keys...");
|
draw_text(0x10, 0x0c0, CYAN, "Derive remaining keys...");
|
||||||
draw_text(0x10, 0x100, CYAN, "Saving keys to keyfile...");
|
draw_text(0x10, 0x0e0, CYAN, "Saving keys to keyfile...");
|
||||||
draw_text(0x10, 0x130, CYAN, "Total time elapsed:");
|
draw_text(0x10, 0x110, CYAN, "Total time elapsed:");
|
||||||
|
|
||||||
consoleUpdate(NULL);
|
update_display();
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_tegra_keys(Key &sbk, Key &tsec, Key &tsec_root) {
|
void get_tegra_keys(Key &sbk, Key &tsec, Key &tsec_root) {
|
||||||
// support hekate dump
|
// support Hekate dump
|
||||||
if (!std::filesystem::exists("/backup"))
|
if (std::filesystem::exists("/backup")) {
|
||||||
return;
|
|
||||||
for (auto &p : std::filesystem::recursive_directory_iterator("/backup")) {
|
for (auto &p : std::filesystem::recursive_directory_iterator("/backup")) {
|
||||||
|
if (p.is_regular_file()) {
|
||||||
|
if (!sbk.found() && (p.file_size() == 0x2fc || p.file_size() == 0x300) &&
|
||||||
|
((p.path().filename().string().substr(0, 5).compare("fuses") == 0) ||
|
||||||
|
(p.path().filename().string().substr(0, 11).compare("fuse_cached") == 0)))
|
||||||
|
{
|
||||||
|
FILE *fuse_file = fopen(p.path().c_str(), "rb");
|
||||||
|
if (!fuse_file) continue;
|
||||||
|
byte_vector temp_key(0x10);
|
||||||
|
fseek(fuse_file, 0xa4, SEEK_SET);
|
||||||
|
fread(temp_key.data(), 0x10, 1, fuse_file);
|
||||||
|
sbk = Key("secure_boot_key", 0x10, temp_key);
|
||||||
|
fclose(fuse_file);
|
||||||
|
}
|
||||||
|
else if (!tsec.found() && (p.file_size() == 0x20 || p.file_size() == 0x30) &&
|
||||||
|
(p.path().filename().string().substr(0, 4).compare("tsec") == 0))
|
||||||
|
{
|
||||||
|
FILE *tsec_file = fopen(p.path().c_str(), "rb");
|
||||||
|
if (!tsec_file) continue;
|
||||||
|
byte_vector temp_key(0x10);
|
||||||
|
fread(temp_key.data(), 0x10, 1, tsec_file);
|
||||||
|
tsec = Key("tsec_key", 0x10, temp_key);
|
||||||
|
fread(temp_key.data(), 0x10, 1, tsec_file);
|
||||||
|
tsec_root.find_key(temp_key);
|
||||||
|
fclose(tsec_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (sbk.found() && tsec.found())
|
if (sbk.found() && tsec.found())
|
||||||
return;
|
return;
|
||||||
if (p.is_regular_file()) {
|
|
||||||
if (!sbk.found() && (p.file_size() == 0x2fc) &&
|
|
||||||
(std::string("fuse").compare(std::string(p.path().filename()).substr(0, 4)) == 0))
|
|
||||||
{
|
|
||||||
std::ifstream fuse_file(p.path(), std::ios::binary);
|
|
||||||
byte_vector temp_key(0x10);
|
|
||||||
fuse_file.seekg(0xa4);
|
|
||||||
fuse_file.read(reinterpret_cast<char *>(temp_key.data()), 0x10);
|
|
||||||
sbk = Key("secure_boot_key", 0x10, temp_key);
|
|
||||||
}
|
|
||||||
else if (!tsec.found() && (p.file_size() == 0x30) &&
|
|
||||||
(std::string("tsec").compare(std::string(p.path().filename()).substr(0, 4)) == 0))
|
|
||||||
{
|
|
||||||
std::ifstream tsec_file(p.path(), std::ios::binary);
|
|
||||||
byte_vector temp_key(0x10);
|
|
||||||
tsec_file.read(reinterpret_cast<char *>(temp_key.data()), 0x10);
|
|
||||||
tsec = Key("tsec_key", 0x10, temp_key);
|
|
||||||
tsec_file.read(reinterpret_cast<char *>(temp_key.data()), 0x10);
|
|
||||||
tsec_root.find_key(temp_key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// support biskeydump v7 dump
|
// support biskeydump v7 dump
|
||||||
if (std::filesystem::exists("/device.keys")) {
|
if (std::filesystem::exists("/device.keys")) {
|
||||||
std::ifstream key_file("/device.keys");
|
FILE *key_file = fopen("/device.keys", "r");
|
||||||
for (std::string line; std::getline(key_file, line); !sbk.found() && !tsec.found()) {
|
char line[0x100];
|
||||||
line.erase(std::remove_if(
|
while (fgets(line, sizeof(line), key_file) && !(sbk.found() && tsec.found())) {
|
||||||
line.begin(), line.end(),
|
if (strncmp("secure_boot_key", line, 15) == 0)
|
||||||
[l = std::locale{}](auto ch) { return std::isspace(ch, l); }
|
|
||||||
), line.end());
|
|
||||||
if (line.substr(0, 15).compare("secure_boot_key") == 0)
|
|
||||||
sbk = Key("secure_boot_key", 0x10, key_string_to_byte_vector(line));
|
sbk = Key("secure_boot_key", 0x10, key_string_to_byte_vector(line));
|
||||||
else if (line.substr(0, 8).compare("tsec_key") == 0)
|
else if (strncmp("tsec_key", line, 8) == 0)
|
||||||
tsec = Key("tsec_key", 0x10, key_string_to_byte_vector(line));
|
tsec = Key("tsec_key", 0x10, key_string_to_byte_vector(line));
|
||||||
}
|
}
|
||||||
|
fclose(key_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wait_to_exit(int status) {
|
void wait_to_exit() {
|
||||||
if (status == Status_fail)
|
draw_text(0x10b, 0x24b, YELLOW, ">> Press + to exit <<");
|
||||||
draw_text(0x1f4, 0x080, RED, ">> Press + to exit <<");
|
|
||||||
else if (status == Status_success_no_titlekeys)
|
|
||||||
draw_text(0x1f4, 0x1a0, GREEN, ">> Press + to exit <<");
|
|
||||||
else if (status == Status_success_titlekeys)
|
|
||||||
draw_text(0x1f4, 0x1f0, GREEN, ">> Press + to exit <<");
|
|
||||||
else if (status == Status_success_titlekeys_failed)
|
|
||||||
draw_text(0x1f4, 0x1f0, RED, ">> Press + to exit <<");
|
|
||||||
|
|
||||||
while(appletMainLoop() & (status != Status_success_no_titlekeys)) {
|
while(appletMainLoop()) {
|
||||||
hidScanInput();
|
hidScanInput();
|
||||||
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
|
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
|
||||||
if (kDown & KEY_PLUS) break;
|
if (kDown & KEY_PLUS) break;
|
||||||
|
|
||||||
consoleUpdate(NULL);
|
update_display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
framebufferClose(&fb);
|
||||||
FT_Done_Face(face);
|
FT_Done_Face(face);
|
||||||
FT_Done_FreeType(library);
|
FT_Done_FreeType(library);
|
||||||
|
|
||||||
consoleExit(NULL);
|
|
||||||
|
|
||||||
appletUnlockExit();
|
appletUnlockExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha256(const u8 *data, u8 *hash, size_t length) {
|
void update_display() {
|
||||||
struct sha256_state ctx;
|
framebufferBegin(&fb, &stride);
|
||||||
sha256_init(&ctx);
|
framebufferEnd(&fb);
|
||||||
sha256_update(&ctx, data, length);
|
|
||||||
sha256_finalize(&ctx);
|
|
||||||
sha256_finish(&ctx, hash);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_vector key_string_to_byte_vector(std::string key_string) {
|
byte_vector key_string_to_byte_vector(std::string key_string) {
|
||||||
key_string = key_string.substr(key_string.find('=') + 1);
|
key_string = key_string.substr(key_string.find('=') + 2);
|
||||||
byte_vector temp_key(key_string.size() / 2);
|
byte_vector temp_key((key_string.size() - 1) / 2);
|
||||||
for (size_t i = 0; i < temp_key.size(); i += 8)
|
for (size_t i = 0; i < temp_key.size() - 1; i += 8)
|
||||||
*reinterpret_cast<u64 *>(temp_key.data() + i) = __bswap64(strtoul(key_string.substr(i * 2, 0x10).c_str(), NULL, 16));
|
*reinterpret_cast<u64 *>(temp_key.data() + i) = __bswap64(strtoul(key_string.substr(i * 2, 0x10).c_str(), NULL, 16));
|
||||||
return temp_key;
|
return temp_key;
|
||||||
}
|
}
|
||||||
|
@ -16,26 +16,33 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Key.hpp"
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <ft2build.h>
|
#include <ft2build.h>
|
||||||
#include <switch.h>
|
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
|
|
||||||
|
#include <switch/types.h>
|
||||||
|
|
||||||
|
#define FB_WIDTH 1280
|
||||||
|
#define FB_HEIGHT 720
|
||||||
|
|
||||||
#define GREEN RGBA8_MAXALPHA(0, 0xff, 0)
|
#define GREEN RGBA8_MAXALPHA(0, 0xff, 0)
|
||||||
#define RED RGBA8_MAXALPHA(0xff, 0, 0)
|
#define RED RGBA8_MAXALPHA(0xff, 0, 0)
|
||||||
#define CYAN RGBA8_MAXALPHA(0, 0xff, 0xff)
|
#define CYAN RGBA8_MAXALPHA(0, 0xff, 0xff)
|
||||||
#define YELLOW RGBA8_MAXALPHA(0xff, 0xff, 0)
|
#define YELLOW RGBA8_MAXALPHA(0xff, 0xff, 0)
|
||||||
|
|
||||||
enum {
|
#define FLAG_RED RGBA8_MAXALPHA(0xe7, 0x00, 0x00)
|
||||||
Status_fail = 0,
|
#define FLAG_ORANGE RGBA8_MAXALPHA(0xff, 0x8c, 0x00)
|
||||||
Status_success_no_titlekeys,
|
#define FLAG_YELLOW RGBA8_MAXALPHA(0xff, 0xef, 0x00)
|
||||||
Status_success_titlekeys,
|
#define FLAG_GREEN RGBA8_MAXALPHA(0x00, 0x81, 0x1f)
|
||||||
Status_success_titlekeys_failed
|
#define FLAG_BLUE RGBA8_MAXALPHA(0x00, 0x44, 0xff)
|
||||||
};
|
#define FLAG_VIOLET RGBA8_MAXALPHA(0x76, 0x00, 0x89)
|
||||||
|
|
||||||
class Key;
|
class Key;
|
||||||
|
|
||||||
|
typedef std::vector<u8> byte_vector;
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
// draw letter, called by draw_text
|
// draw letter, called by draw_text
|
||||||
void draw_glyph(FT_Bitmap *bitmap, u32 x, u32 y, u32 color);
|
void draw_glyph(FT_Bitmap *bitmap, u32 x, u32 y, u32 color);
|
||||||
@ -54,10 +61,12 @@ namespace Common {
|
|||||||
void intro();
|
void intro();
|
||||||
// get tegra keys from payload dump
|
// get tegra keys from payload dump
|
||||||
void get_tegra_keys(Key &sbk, Key &tsec, Key &tsec_root);
|
void get_tegra_keys(Key &sbk, Key &tsec, Key &tsec_root);
|
||||||
// print exit in color green or red (fail==true)
|
// print exit
|
||||||
void wait_to_exit(int status);
|
void wait_to_exit();
|
||||||
|
|
||||||
|
// refresh display
|
||||||
|
void update_display();
|
||||||
|
|
||||||
void sha256(const u8 *data, u8 *hash, size_t length);
|
|
||||||
// reads "<keyname> = <hexkey>" and returns byte vector
|
// reads "<keyname> = <hexkey>" and returns byte vector
|
||||||
std::vector<u8> key_string_to_byte_vector(std::string key_string);
|
byte_vector key_string_to_byte_vector(std::string key_string);
|
||||||
}
|
}
|
@ -17,14 +17,12 @@
|
|||||||
#include "Key.hpp"
|
#include "Key.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iomanip>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <mbedtls/aes.h>
|
|
||||||
#include <mbedtls/cmac.h>
|
|
||||||
|
|
||||||
#include "xxhash64.h"
|
#include "xxhash64.h"
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
size_t Key::saved_key_count = 0;
|
size_t Key::saved_key_count = 0;
|
||||||
|
|
||||||
Key::Key(std::string name, u64 xx_hash, byte_vector hash, u8 length, byte_vector key) :
|
Key::Key(std::string name, u64 xx_hash, byte_vector hash, u8 length, byte_vector key) :
|
||||||
@ -68,19 +66,15 @@ Key::Key() :
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Key::save_key(std::ofstream &file) {
|
void Key::save_key(FILE *file) {
|
||||||
if (!found())
|
if (!found())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// format: <keyname> = <hex key> for hactool and similar tools
|
// format: <keyname> = <hex key> for hactool and similar tools
|
||||||
char key_chars[3] = "00";
|
fprintf(file, "%s = ", name.c_str());
|
||||||
file.write(name.c_str(), name.size());
|
for (auto n : key)
|
||||||
file.write(" = ", 3);
|
fprintf(file, "%02x", n);
|
||||||
for (u8 c : key) {
|
fprintf(file, "\n");
|
||||||
sprintf(key_chars, "%02x", c);
|
|
||||||
file.write(key_chars, 2);
|
|
||||||
}
|
|
||||||
file.write("\n", 1);
|
|
||||||
|
|
||||||
saved_key_count++;
|
saved_key_count++;
|
||||||
}
|
}
|
||||||
@ -90,16 +84,9 @@ byte_vector Key::aes_decrypt_ctr(const byte_vector &data, byte_vector iv) {
|
|||||||
if (!found())
|
if (!found())
|
||||||
return dest;
|
return dest;
|
||||||
|
|
||||||
// used internally
|
Aes128CtrContext con;
|
||||||
size_t nc_off = 0;
|
aes128CtrContextCreate(&con, key.data(), iv.data());
|
||||||
u8 stream_block[0x10];
|
aes128CtrCrypt(&con, dest.data(), data.data(), data.size());
|
||||||
|
|
||||||
mbedtls_aes_context dec;
|
|
||||||
mbedtls_aes_init(&dec);
|
|
||||||
mbedtls_aes_setkey_enc(&dec, key.data(), length * 8);
|
|
||||||
mbedtls_aes_crypt_ctr(&dec, data.size(), &nc_off, iv.data(), stream_block, data.data(), dest.data());
|
|
||||||
mbedtls_aes_free(&dec);
|
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,13 +95,10 @@ byte_vector Key::aes_decrypt_ecb(const byte_vector &data) {
|
|||||||
if (!found())
|
if (!found())
|
||||||
return dest;
|
return dest;
|
||||||
|
|
||||||
mbedtls_aes_context dec;
|
Aes128Context con;
|
||||||
mbedtls_aes_init(&dec);
|
aes128ContextCreate(&con, key.data(), false);
|
||||||
mbedtls_aes_setkey_dec(&dec, key.data(), length * 8);
|
|
||||||
for (size_t offset = 0; offset < data.size(); offset += 0x10)
|
for (size_t offset = 0; offset < data.size(); offset += 0x10)
|
||||||
mbedtls_aes_crypt_ecb(&dec, MBEDTLS_AES_DECRYPT, data.data() + offset, dest.data() + offset);
|
aes128DecryptBlock(&con, dest.data() + offset, data.data() + offset);
|
||||||
mbedtls_aes_free(&dec);
|
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,30 +107,30 @@ byte_vector Key::cmac(byte_vector data) {
|
|||||||
if (!found())
|
if (!found())
|
||||||
return dest;
|
return dest;
|
||||||
|
|
||||||
mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), key.data(), length * 8, data.data(), data.size(), dest.data());
|
cmacAes128CalculateMac(dest.data(), key.data(), data.data(), data.size());
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Key::find_key(const byte_vector &buffer) {
|
void Key::find_key(const byte_vector &buffer, size_t start) {
|
||||||
if ((buffer.size() == 0) || (found()))
|
if ((buffer.size() == 0) || (found()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
u8 temp_hash[0x20];
|
u8 temp_hash[0x20];
|
||||||
|
|
||||||
if (buffer.size() == length) {
|
if (buffer.size() == length) {
|
||||||
Common::sha256(buffer.data(), temp_hash, length);
|
sha256CalculateHash(temp_hash, buffer.data(), length);
|
||||||
if (!std::equal(hash.begin(), hash.end(), temp_hash))
|
if (!std::equal(hash.begin(), hash.end(), temp_hash))
|
||||||
return;
|
return;
|
||||||
std::copy(buffer.begin(), buffer.begin() + length, std::back_inserter(key));
|
std::copy(buffer.begin(), buffer.begin() + length, std::back_inserter(key));
|
||||||
is_found = true;
|
is_found = true;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash every length-sized byte chunk in buffer until it matches member hash
|
// hash every length-sized byte chunk in buffer until it matches member hash
|
||||||
for (size_t i = 0; i < buffer.size() - length; i++) {
|
for (size_t i = start; i < buffer.size() - length; i++) {
|
||||||
if (xx_hash == XXHash64::hash(buffer.data() + i, length, 0)) {
|
if (xx_hash == XXHash64::hash(buffer.data() + i, length, 0)) {
|
||||||
// double-check sha256 since xxhash64 isn't as collision-safe
|
// double-check sha256 since xxhash64 isn't as collision-safe
|
||||||
Common::sha256(buffer.data() + i, temp_hash, length);
|
sha256CalculateHash(temp_hash, buffer.data() + i, length);
|
||||||
if (!std::equal(hash.begin(), hash.end(), temp_hash))
|
if (!std::equal(hash.begin(), hash.end(), temp_hash))
|
||||||
continue;
|
continue;
|
||||||
std::copy(buffer.begin() + i, buffer.begin() + i + length, std::back_inserter(key));
|
std::copy(buffer.begin() + i, buffer.begin() + i + length, std::back_inserter(key));
|
||||||
|
@ -16,13 +16,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch/types.h>
|
||||||
|
|
||||||
#include "Common.hpp"
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef std::vector<u8> byte_vector;
|
typedef std::vector<u8> byte_vector;
|
||||||
|
|
||||||
@ -44,7 +43,7 @@ public:
|
|||||||
void set_found() { is_found = true; }
|
void set_found() { is_found = true; }
|
||||||
|
|
||||||
// write key to file
|
// write key to file
|
||||||
void save_key(std::ofstream &file);
|
void save_key(FILE *file);
|
||||||
|
|
||||||
static const size_t get_saved_key_count() { return saved_key_count; }
|
static const size_t get_saved_key_count() { return saved_key_count; }
|
||||||
|
|
||||||
@ -55,18 +54,17 @@ public:
|
|||||||
// return CMAC of data
|
// return CMAC of data
|
||||||
byte_vector cmac(byte_vector data);
|
byte_vector cmac(byte_vector data);
|
||||||
// find key in buffer by hash, optionally specify start offset
|
// find key in buffer by hash, optionally specify start offset
|
||||||
void find_key(const byte_vector &buffer);
|
void find_key(const byte_vector &buffer, size_t start = 0);
|
||||||
// get key encryption key
|
// get key encryption key
|
||||||
byte_vector generate_kek(Key &master_key, const Key &kek_seed, const Key &key_seed);
|
byte_vector generate_kek(Key &master_key, const Key &kek_seed, const Key &key_seed);
|
||||||
|
|
||||||
byte_vector key;
|
byte_vector key;
|
||||||
|
|
||||||
private:
|
|
||||||
std::string name;
|
std::string name;
|
||||||
u64 xx_hash;
|
u64 xx_hash;
|
||||||
byte_vector hash;
|
byte_vector hash;
|
||||||
u8 length;
|
u8 length;
|
||||||
bool is_found = false;
|
bool is_found = false;
|
||||||
|
|
||||||
|
private:
|
||||||
static size_t saved_key_count;
|
static size_t saved_key_count;
|
||||||
};
|
};
|
@ -17,29 +17,40 @@
|
|||||||
#include "KeyCollection.hpp"
|
#include "KeyCollection.hpp"
|
||||||
|
|
||||||
#include "Common.hpp"
|
#include "Common.hpp"
|
||||||
#include "creport_debug_types.hpp"
|
|
||||||
#include "Stopwatch.hpp"
|
#include "Stopwatch.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iomanip>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
|
#include "fatfs/ff.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "set_ext.h"
|
#include "nx/es.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
const u8 KeyCollection::null_hash[0x20] = { // hash of empty string
|
#define TITLEKEY_BUFFER_SIZE 0x40000
|
||||||
|
|
||||||
|
// hash of empty string
|
||||||
|
const u8 KeyCollection::null_hash[0x20] = {
|
||||||
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
|
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
|
||||||
0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
|
0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
|
||||||
|
|
||||||
|
FsStorage storage;
|
||||||
|
|
||||||
// function timer
|
// function timer
|
||||||
template<typename Duration = std::chrono::microseconds, typename FT, typename ... Args>
|
template<typename Duration = std::chrono::microseconds, typename FT, typename ... Args>
|
||||||
typename Duration::rep profile(FT&& fun, Args&&... args) {
|
typename Duration::rep profile(FT&& fun, Args&&... args) {
|
||||||
const auto beg = std::chrono::high_resolution_clock::now();
|
const auto beg = std::chrono::high_resolution_clock::now();
|
||||||
std::invoke(fun, std::forward<Args>(args)...);//std::forward<FT>(fun)(std::forward<Args>(args)...);
|
std::invoke(fun, std::forward<Args>(args)...);
|
||||||
const auto end = std::chrono::high_resolution_clock::now();
|
const auto end = std::chrono::high_resolution_clock::now();
|
||||||
return std::chrono::duration_cast<Duration>(end - beg).count();
|
return std::chrono::duration_cast<Duration>(end - beg).count();
|
||||||
}
|
}
|
||||||
@ -82,7 +93,7 @@ KeyCollection::KeyCollection() {
|
|||||||
0x37, 0x4B, 0x77, 0x29, 0x59, 0xB4, 0x04, 0x30, 0x81, 0xF6, 0xE5, 0x8C, 0x6D, 0x36, 0x17, 0x9A}});
|
0x37, 0x4B, 0x77, 0x29, 0x59, 0xB4, 0x04, 0x30, 0x81, 0xF6, 0xE5, 0x8C, 0x6D, 0x36, 0x17, 0x9A}});
|
||||||
|
|
||||||
//======================================Keys======================================//
|
//======================================Keys======================================//
|
||||||
// from Package1 -> TrustZone (Secure Monitor)
|
// from Package1 -> Secure_Monitor
|
||||||
aes_kek_generation_source = {"aes_kek_generation_source", 0x10, {
|
aes_kek_generation_source = {"aes_kek_generation_source", 0x10, {
|
||||||
0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9}};
|
0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9}};
|
||||||
aes_kek_seed_01 = {"aes_kek_seed_01", 0x10, {
|
aes_kek_seed_01 = {"aes_kek_seed_01", 0x10, {
|
||||||
@ -96,7 +107,7 @@ KeyCollection::KeyCollection() {
|
|||||||
retail_specific_aes_key_source = {"retail_specific_aes_key_source", 0x10, {
|
retail_specific_aes_key_source = {"retail_specific_aes_key_source", 0x10, {
|
||||||
0xE2, 0xD6, 0xB8, 0x7A, 0x11, 0x9C, 0xB8, 0x80, 0xE8, 0x22, 0x88, 0x8A, 0x46, 0xFB, 0xA1, 0x95}};
|
0xE2, 0xD6, 0xB8, 0x7A, 0x11, 0x9C, 0xB8, 0x80, 0xE8, 0x22, 0x88, 0x8A, 0x46, 0xFB, 0xA1, 0x95}};
|
||||||
|
|
||||||
// from Package1ldr
|
// from Package1ldr (or Secure_Monitor on 6.2.0)
|
||||||
keyblob_mac_key_source = {"keyblob_mac_key_source", 0x10, {
|
keyblob_mac_key_source = {"keyblob_mac_key_source", 0x10, {
|
||||||
0x59, 0xC7, 0xFB, 0x6F, 0xBE, 0x9B, 0xBE, 0x87, 0x65, 0x6B, 0x15, 0xC0, 0x53, 0x73, 0x36, 0xA5}};
|
0x59, 0xC7, 0xFB, 0x6F, 0xBE, 0x9B, 0xBE, 0x87, 0x65, 0x6B, 0x15, 0xC0, 0x53, 0x73, 0x36, 0xA5}};
|
||||||
master_key_source = {"master_key_source", 0x10, {
|
master_key_source = {"master_key_source", 0x10, {
|
||||||
@ -147,10 +158,10 @@ KeyCollection::KeyCollection() {
|
|||||||
sd_card_kek_source = {"sd_card_kek_source", 0xc408d710a3b821eb, {
|
sd_card_kek_source = {"sd_card_kek_source", 0xc408d710a3b821eb, {
|
||||||
0x6B, 0x2E, 0xD8, 0x77, 0xC2, 0xC5, 0x23, 0x34, 0xAC, 0x51, 0xE5, 0x9A, 0xBF, 0xA7, 0xEC, 0x45,
|
0x6B, 0x2E, 0xD8, 0x77, 0xC2, 0xC5, 0x23, 0x34, 0xAC, 0x51, 0xE5, 0x9A, 0xBF, 0xA7, 0xEC, 0x45,
|
||||||
0x7F, 0x4A, 0x7D, 0x01, 0xE4, 0x62, 0x91, 0xE9, 0xF2, 0xEA, 0xA4, 0x5F, 0x01, 0x1D, 0x24, 0xB7}, 0x10};
|
0x7F, 0x4A, 0x7D, 0x01, 0xE4, 0x62, 0x91, 0xE9, 0xF2, 0xEA, 0xA4, 0x5F, 0x01, 0x1D, 0x24, 0xB7}, 0x10};
|
||||||
sd_card_nca_key_source = {"sd_card_nca_key_source", 0xbea347c9f8472947, {
|
sd_card_nca_key_source = {"sd_card_nca_key_source", 0xb026106d9699fec0, { // xxhash of first 0x10 bytes
|
||||||
0x2E, 0x75, 0x1C, 0xEC, 0xF7, 0xD9, 0x3A, 0x2B, 0x95, 0x7B, 0xD5, 0xFF, 0xCB, 0x08, 0x2F, 0xD0,
|
0x2E, 0x75, 0x1C, 0xEC, 0xF7, 0xD9, 0x3A, 0x2B, 0x95, 0x7B, 0xD5, 0xFF, 0xCB, 0x08, 0x2F, 0xD0,
|
||||||
0x38, 0xCC, 0x28, 0x53, 0x21, 0x9D, 0xD3, 0x09, 0x2C, 0x6D, 0xAB, 0x98, 0x38, 0xF5, 0xA7, 0xCC}, 0x20};
|
0x38, 0xCC, 0x28, 0x53, 0x21, 0x9D, 0xD3, 0x09, 0x2C, 0x6D, 0xAB, 0x98, 0x38, 0xF5, 0xA7, 0xCC}, 0x20};
|
||||||
sd_card_save_key_source = {"sd_card_save_key_source", 0xf87fe8c3688c3022, {
|
sd_card_save_key_source = {"sd_card_save_key_source", 0x9697ba2fec3d3ed1, { // xxhash of first 0x10 bytes
|
||||||
0xD4, 0x82, 0x74, 0x35, 0x63, 0xD3, 0xEA, 0x5D, 0xCD, 0xC3, 0xB7, 0x4E, 0x97, 0xC9, 0xAC, 0x8A,
|
0xD4, 0x82, 0x74, 0x35, 0x63, 0xD3, 0xEA, 0x5D, 0xCD, 0xC3, 0xB7, 0x4E, 0x97, 0xC9, 0xAC, 0x8A,
|
||||||
0x34, 0x21, 0x64, 0xFA, 0x04, 0x1A, 0x1D, 0xC8, 0x0F, 0x17, 0xF6, 0xD3, 0x1E, 0x4B, 0xC0, 0x1C}, 0x20};
|
0x34, 0x21, 0x64, 0xFA, 0x04, 0x1A, 0x1D, 0xC8, 0x0F, 0x17, 0xF6, 0xD3, 0x1E, 0x4B, 0xC0, 0x1C}, 0x20};
|
||||||
|
|
||||||
@ -184,10 +195,6 @@ KeyCollection::KeyCollection() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs_rodata_keys = {
|
fs_rodata_keys = {
|
||||||
&bis_kek_source,
|
|
||||||
&bis_key_source_00,
|
|
||||||
&bis_key_source_01,
|
|
||||||
&bis_key_source_02,
|
|
||||||
&header_kek_source,
|
&header_kek_source,
|
||||||
&key_area_key_application_source,
|
&key_area_key_application_source,
|
||||||
&key_area_key_ocean_source,
|
&key_area_key_ocean_source,
|
||||||
@ -205,88 +212,82 @@ KeyCollection::KeyCollection() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
package1ldr_keys = {
|
|
||||||
&keyblob_mac_key_source,
|
|
||||||
&master_key_source,
|
|
||||||
&per_console_key_source
|
|
||||||
};
|
|
||||||
|
|
||||||
ssl_keys = {
|
ssl_keys = {
|
||||||
&ssl_rsa_kek_source_x,
|
&ssl_rsa_kek_source_x,
|
||||||
&ssl_rsa_kek_source_y
|
&ssl_rsa_kek_source_y
|
||||||
};
|
};
|
||||||
|
|
||||||
tz_keys = {
|
|
||||||
&aes_kek_generation_source,
|
|
||||||
&package2_key_source,
|
|
||||||
&titlekek_source,
|
|
||||||
&retail_specific_aes_key_source,
|
|
||||||
&aes_kek_seed_01,
|
|
||||||
&aes_kek_seed_03
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int KeyCollection::get_keys() {
|
void KeyCollection::get_keys() {
|
||||||
Stopwatch total_time;
|
Stopwatch total_time;
|
||||||
total_time.start();
|
total_time.start();
|
||||||
|
|
||||||
int64_t profiler_time = profile(Common::get_tegra_keys, sbk, tsec, tsec_root_key);
|
int64_t profiler_time = profile(Common::get_tegra_keys, sbk, tsec, tsec_root_key);
|
||||||
if ((sbk.found() && tsec.found()) || tsec_root_key.found()) {
|
if ((sbk.found() && tsec.found()) || tsec_root_key.found()) {
|
||||||
Common::draw_text_with_time(0x10, 0x80, GREEN, "Get Tegra keys...", profiler_time);
|
Common::draw_text_with_time(0x10, 0x60, GREEN, "Get Tegra keys...", profiler_time);
|
||||||
} else {
|
} else {
|
||||||
Common::draw_text(0x010, 0x80, RED, "Get Tegra keys...");
|
Common::draw_text(0x010, 0x60, RED, "Get Tegra keys...");
|
||||||
Common::draw_text(0x190, 0x80, RED, "Failed");
|
Common::draw_text(0x190, 0x60, RED, "Failed");
|
||||||
Common::draw_text(0x190, 0x20, RED, "Warning: Saving limited keyset.");
|
Common::draw_text(0x2a0, 0x60, RED, "Warning: Saving limited keyset.");
|
||||||
Common::draw_text(0x190, 0x40, RED, "Dump Tegra keys with payload and run again to get all keys.");
|
Common::draw_text(0x2a0, 0x80, RED, "Dump TSEC and Fuses with Hekate.");
|
||||||
}
|
}
|
||||||
|
|
||||||
profiler_time = profile(&KeyCollection::get_memory_keys, *this);
|
profiler_time = profile(&KeyCollection::get_memory_keys, *this);
|
||||||
Common::draw_text_with_time(0x10, 0x0a0, GREEN, "Get keys from memory...", profiler_time);
|
Common::draw_text_with_time(0x10, 0x080, GREEN, "Get keys from memory...", profiler_time);
|
||||||
|
|
||||||
profiler_time = profile(&KeyCollection::get_master_keys, *this);
|
profiler_time = profile(&KeyCollection::get_master_keys, *this);
|
||||||
Common::draw_text_with_time(0x10, 0x0c0, GREEN, "Get master keys...", profiler_time);
|
Common::draw_text_with_time(0x10, 0x0a0, GREEN, "Get master keys...", profiler_time);
|
||||||
|
|
||||||
profiler_time = profile(&KeyCollection::derive_keys, *this);
|
profiler_time = profile(&KeyCollection::derive_keys, *this);
|
||||||
Common::draw_text_with_time(0x10, 0x0e0, GREEN, "Derive remaining keys...", profiler_time);
|
Common::draw_text_with_time(0x10, 0x0c0, GREEN, "Derive remaining keys...", profiler_time);
|
||||||
|
|
||||||
|
// avoid crash on CFWs that don't use /switch folder
|
||||||
|
if (!std::filesystem::exists("/switch"))
|
||||||
|
std::filesystem::create_directory("/switch");
|
||||||
|
// since Lockpick_RCM can dump newer keys, check for existing keyfile
|
||||||
|
bool Lockpick_RCM_file_found = false;
|
||||||
|
if (std::filesystem::exists("/switch/prod.keys")) {
|
||||||
|
FILE *key_file = fopen("/switch/prod.keys", "r");
|
||||||
|
char line[0x200];
|
||||||
|
while (fgets(line, sizeof(line), key_file)) {
|
||||||
|
if (strncmp("master_key_07", line, 13) == 0) {
|
||||||
|
Lockpick_RCM_file_found = true;
|
||||||
|
} else if (!eticket_rsa_kek.found() && (strncmp("eticket_rsa_kek", line, 15)) == 0) {
|
||||||
|
// grab eticket_rsa_kek from existing file to make sure we can dump titlekeys
|
||||||
|
eticket_rsa_kek = Key("eticket_rsa_kek", 0x10, Common::key_string_to_byte_vector(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(key_file);
|
||||||
|
}
|
||||||
|
if (!Lockpick_RCM_file_found) {
|
||||||
profiler_time = profile(&KeyCollection::save_keys, *this);
|
profiler_time = profile(&KeyCollection::save_keys, *this);
|
||||||
Common::draw_text_with_time(0x10, 0x100, GREEN, "Saving keys to keyfile...", profiler_time);
|
Common::draw_text_with_time(0x10, 0x0e0, GREEN, "Saving keys to keyfile...", profiler_time);
|
||||||
|
} else {
|
||||||
|
Common::draw_text(0x10, 0x0e0, YELLOW, "Saving keys to keyfile...");
|
||||||
|
Common::draw_text(0x190, 0x0e0, YELLOW, "Newer keyfile found. Skipped overwriting keys");
|
||||||
|
}
|
||||||
|
|
||||||
total_time.stop();
|
total_time.stop();
|
||||||
Common::draw_line(0x8, 0x110, 0x280, GREEN);
|
Common::draw_line(0x8, 0xf0, 0x280, GREEN);
|
||||||
Common::draw_text_with_time(0x10, 0x130, GREEN, "Total time elapsed:", total_time.get_elapsed());
|
Common::draw_text_with_time(0x10, 0x110, GREEN, "Total time elapsed:", total_time.get_elapsed());
|
||||||
|
|
||||||
char keys_str[32];
|
char keys_str[32];
|
||||||
|
if (!Lockpick_RCM_file_found) {
|
||||||
sprintf(keys_str, "Total keys found: %lu", Key::get_saved_key_count());
|
sprintf(keys_str, "Total keys found: %lu", Key::get_saved_key_count());
|
||||||
Common::draw_text(0x2a0, 0x130, CYAN, keys_str);
|
Common::draw_text(0x2a0, 0x110, CYAN, keys_str);
|
||||||
|
Common::draw_text(0x80, 0x140, YELLOW, "Keys saved to \"/switch/prod.keys\"!");
|
||||||
Common::draw_text(0x30, 0x160, YELLOW, "WARNING: dumping titlekeys may crash homebrew or games UNLESS you reboot afterwards");
|
|
||||||
Common::draw_text(0x160, 0x180, CYAN, ">> Press A to dump titlekeys or + to exit <<");
|
|
||||||
|
|
||||||
for(;;) {
|
|
||||||
hidScanInput();
|
|
||||||
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
|
|
||||||
if (kDown & KEY_PLUS)
|
|
||||||
return Status_success_no_titlekeys;
|
|
||||||
else if (kDown & KEY_A)
|
|
||||||
break;
|
|
||||||
|
|
||||||
consoleUpdate(NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::draw_text(0x10, 0x1b0, CYAN, "Dumping titlekeys...");
|
Common::draw_text(0x10, 0x170, CYAN, "Dumping titlekeys...");
|
||||||
consoleUpdate(NULL);
|
Common::update_display();
|
||||||
profiler_time = profile(&KeyCollection::get_titlekeys, *this);
|
profiler_time = profile(&KeyCollection::get_titlekeys, *this);
|
||||||
if (titlekeys_dumped > 0) {
|
Common::draw_text_with_time(0x10, 0x170, GREEN, "Dumping titlekeys...", profiler_time);
|
||||||
Common::draw_text_with_time(0x10, 0x1b0, GREEN, "Dumping titlekeys...", profiler_time);
|
|
||||||
sprintf(keys_str, "Titlekeys found: %lu", titlekeys_dumped);
|
sprintf(keys_str, "Titlekeys found: %lu", titlekeys_dumped);
|
||||||
Common::draw_text(0x2a0, 0x1b0, CYAN, keys_str);
|
Common::draw_text(0x2a0, 0x170, CYAN, keys_str);
|
||||||
return Status_success_titlekeys;
|
if (titlekeys_dumped > 0)
|
||||||
} else {
|
Common::draw_text(0x80, 0x1a0, YELLOW, "Titlekeys saved to \"/switch/title.keys\"!");
|
||||||
Common::draw_text(0x010, 0x1b0, RED, "Dumping titlekeys...");
|
else
|
||||||
Common::draw_text(0x190, 0x1b0, RED, "Failed. Reboot and try again!");
|
Common::draw_text(0x80, 0x1a0, GREEN, "No titlekeys found. Either you've never played or installed a game or dump failed.");
|
||||||
return Status_success_titlekeys_failed;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCollection::get_master_keys() {
|
void KeyCollection::get_master_keys() {
|
||||||
@ -366,28 +367,70 @@ void KeyCollection::get_memory_keys() {
|
|||||||
FSRodata.get_from_memory(FS_TID, SEG_RODATA);
|
FSRodata.get_from_memory(FS_TID, SEG_RODATA);
|
||||||
FSData.get_from_memory(FS_TID, SEG_DATA);
|
FSData.get_from_memory(FS_TID, SEG_DATA);
|
||||||
|
|
||||||
for (auto k : fs_rodata_keys)
|
FSRodata.find_keys(fs_rodata_keys);
|
||||||
k->find_key(FSRodata.data);
|
|
||||||
|
|
||||||
header_key_source.find_key(FSData.data);
|
size_t i = 0;
|
||||||
|
/*for ( ; i < FSData.data.size(); i++) {
|
||||||
|
// speeds things up but i'm not 100% sure this is always here
|
||||||
|
if (*reinterpret_cast<u128 *>(FSData.data.data() + i) == 0x10001)
|
||||||
|
break;
|
||||||
|
}*/
|
||||||
|
header_key_source.find_key(FSData.data, i);
|
||||||
|
|
||||||
|
SSLRodata.get_from_memory(SSL_TID, SEG_RODATA);
|
||||||
|
// using find_keys on these is actually slower
|
||||||
|
for (auto k : ssl_keys)
|
||||||
|
k->find_key(SSLRodata.data);
|
||||||
|
|
||||||
|
// firmware 1.0.0 doesn't have the ES keys
|
||||||
|
if (!kernelAbove200())
|
||||||
|
return;
|
||||||
ESRodata.get_from_memory(ES_TID, SEG_RODATA);
|
ESRodata.get_from_memory(ES_TID, SEG_RODATA);
|
||||||
for (auto k : es_keys)
|
for (auto k : es_keys)
|
||||||
k->find_key(ESRodata.data);
|
k->find_key(ESRodata.data);
|
||||||
|
|
||||||
SSLRodata.get_from_memory(SSL_TID, SEG_RODATA);
|
|
||||||
for (auto k : ssl_keys)
|
|
||||||
k->find_key(SSLRodata.data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCollection::derive_keys() {
|
void KeyCollection::derive_keys() {
|
||||||
header_key = {"header_key", 0x20, {}};
|
|
||||||
if (header_kek_source.found() && header_key_source.found()) {
|
if (header_kek_source.found() && header_key_source.found()) {
|
||||||
u8 tempheaderkek[0x10], tempheaderkey[0x20];
|
u8 tempheaderkek[0x10], tempheaderkey[0x20];
|
||||||
|
splCryptoInitialize();
|
||||||
splCryptoGenerateAesKek(header_kek_source.key.data(), 0, 0, tempheaderkek);
|
splCryptoGenerateAesKek(header_kek_source.key.data(), 0, 0, tempheaderkek);
|
||||||
splCryptoGenerateAesKey(tempheaderkek, header_key_source.key.data(), tempheaderkey);
|
splCryptoGenerateAesKey(tempheaderkek, header_key_source.key.data() + 0x00, tempheaderkey + 0x00);
|
||||||
splCryptoGenerateAesKey(tempheaderkek, header_key_source.key.data() + 0x10, tempheaderkey + 0x10);
|
splCryptoGenerateAesKey(tempheaderkek, header_key_source.key.data() + 0x10, tempheaderkey + 0x10);
|
||||||
header_key = {"header_key", 0x20, byte_vector(&tempheaderkey[0], &tempheaderkey[0x20])};
|
header_key = {"header_key", 0x20, byte_vector(tempheaderkey, tempheaderkey + 0x20)};
|
||||||
|
splCryptoExit();
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 key_generation = 0;
|
||||||
|
SetSysFirmwareVersion ver;
|
||||||
|
|
||||||
|
setsysInitialize();
|
||||||
|
setsysGetFirmwareVersion(&ver);
|
||||||
|
setsysExit();
|
||||||
|
|
||||||
|
Result rc = 0;
|
||||||
|
if (ver.major >= 5) {
|
||||||
|
rc = splGetConfig(SplConfigItem_NewKeyGeneration, &key_generation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && bis_key_source_00.found() && bis_key_source_01.found() && bis_key_source_02.found()) {
|
||||||
|
u8 tempbiskek[0x10], tempbiskey[0x20];
|
||||||
|
splFsInitialize();
|
||||||
|
splFsGenerateSpecificAesKey(bis_key_source_00.key.data() + 0x00, key_generation, 0, tempbiskey + 0x00);
|
||||||
|
splFsGenerateSpecificAesKey(bis_key_source_00.key.data() + 0x10, key_generation, 0, tempbiskey + 0x10);
|
||||||
|
bis_key.push_back(Key {"bis_key_00", 0x20, byte_vector(tempbiskey, tempbiskey + 0x20)});
|
||||||
|
splFsExit();
|
||||||
|
|
||||||
|
splCryptoInitialize();
|
||||||
|
splCryptoGenerateAesKek(bis_kek_source.key.data(), key_generation, 1, tempbiskek);
|
||||||
|
splCryptoGenerateAesKey(tempbiskek, bis_key_source_01.key.data() + 0x00, tempbiskey + 0x00);
|
||||||
|
splCryptoGenerateAesKey(tempbiskek, bis_key_source_01.key.data() + 0x10, tempbiskey + 0x10);
|
||||||
|
bis_key.push_back(Key {"bis_key_01", 0x20, byte_vector(tempbiskey, tempbiskey + 0x20)});
|
||||||
|
splCryptoGenerateAesKey(tempbiskek, bis_key_source_02.key.data() + 0x00, tempbiskey + 0x00);
|
||||||
|
splCryptoGenerateAesKey(tempbiskek, bis_key_source_02.key.data() + 0x10, tempbiskey + 0x10);
|
||||||
|
bis_key.push_back(Key {"bis_key_02", 0x20, byte_vector(tempbiskey, tempbiskey + 0x20)});
|
||||||
|
bis_key.push_back(Key {"bis_key_03", 0x20, bis_key[2].key});
|
||||||
|
splCryptoExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (u8 i = 0; i < aes_kek_generation_source.key.size(); i++) {
|
for (u8 i = 0; i < aes_kek_generation_source.key.size(); i++) {
|
||||||
@ -405,15 +448,6 @@ void KeyCollection::derive_keys() {
|
|||||||
save_mac_key = Key {"save_mac_key", 0x10, kek.aes_decrypt_ecb(save_mac_key_source.key)};
|
save_mac_key = Key {"save_mac_key", 0x10, kek.aes_decrypt_ecb(save_mac_key_source.key)};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device_key.found()) {
|
|
||||||
Key kek = {device_key.aes_decrypt_ecb(retail_specific_aes_key_source.key), 0x10};
|
|
||||||
bis_key.push_back(Key {"bis_key_00", 0x20, kek.aes_decrypt_ecb(bis_key_source_00.key)});
|
|
||||||
kek = Key {bis_kek_source.generate_kek(device_key, aes_kek_generation_source, aes_key_generation_source), 0x10};
|
|
||||||
bis_key.push_back(Key {"bis_key_01", 0x20, kek.aes_decrypt_ecb(bis_key_source_01.key)});
|
|
||||||
bis_key.push_back(Key {"bis_key_02", 0x20, kek.aes_decrypt_ecb(bis_key_source_02.key)});
|
|
||||||
bis_key.push_back(Key {"bis_key_03", 0x20, bis_key[2].key});\
|
|
||||||
}
|
|
||||||
|
|
||||||
char keynum[] = "00";
|
char keynum[] = "00";
|
||||||
for (u8 i = 0; i < master_key.size(); i++) {
|
for (u8 i = 0; i < master_key.size(); i++) {
|
||||||
if (!master_key[i].found())
|
if (!master_key[i].found())
|
||||||
@ -435,19 +469,58 @@ void KeyCollection::derive_keys() {
|
|||||||
if (ssl_rsa_kek_source_x.found() && ssl_rsa_kek_source_y.found() && !master_key.empty())
|
if (ssl_rsa_kek_source_x.found() && ssl_rsa_kek_source_y.found() && !master_key.empty())
|
||||||
ssl_rsa_kek = Key {"ssl_rsa_kek", 0x10,
|
ssl_rsa_kek = Key {"ssl_rsa_kek", 0x10,
|
||||||
ssl_rsa_kek_source_x.generate_kek(master_key[0], rsa_private_kek_generation_source, ssl_rsa_kek_source_y)};
|
ssl_rsa_kek_source_x.generate_kek(master_key[0], rsa_private_kek_generation_source, ssl_rsa_kek_source_y)};
|
||||||
|
|
||||||
|
char seed_vector[0x10], seed[0x10], buffer[0x10];
|
||||||
|
u32 bytes_read, file_pos = 0;
|
||||||
|
|
||||||
|
// dump sd seed
|
||||||
|
if (!kernelAbove200())
|
||||||
|
return;
|
||||||
|
FILE *sd_private = fopen("/Nintendo/Contents/private", "rb");
|
||||||
|
if (!sd_private) return;
|
||||||
|
fread(seed_vector, 0x10, 1, sd_private);
|
||||||
|
fclose(sd_private);
|
||||||
|
|
||||||
|
FATFS fs;
|
||||||
|
FRESULT fr;
|
||||||
|
FIL save_file;
|
||||||
|
|
||||||
|
fsOpenBisStorage(&storage, FsBisPartitionId_System);
|
||||||
|
if (f_mount(&fs, "", 1) ||
|
||||||
|
f_chdir("/save") ||
|
||||||
|
f_open(&save_file, "8000000000000043", FA_READ | FA_OPEN_EXISTING))
|
||||||
|
{
|
||||||
|
fsStorageClose(&storage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
fr = f_read(&save_file, buffer, 0x10, &bytes_read);
|
||||||
|
if (fr || (bytes_read == 0)) break;
|
||||||
|
if (std::equal(seed_vector, seed_vector + 0x10, buffer)) {
|
||||||
|
f_read(&save_file, seed, 0x10, &bytes_read);
|
||||||
|
sd_seed = Key {"sd_seed", 0x10, byte_vector(seed, seed + 0x10)};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
file_pos += 0x4000;
|
||||||
|
if (f_lseek(&save_file, file_pos)) break;
|
||||||
|
}
|
||||||
|
f_close(&save_file);
|
||||||
|
fsStorageClose(&storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCollection::save_keys() {
|
void KeyCollection::save_keys() {
|
||||||
std::ofstream key_file("/switch/prod.keys");
|
FILE *key_file = fopen("/switch/prod.keys", "w");
|
||||||
|
if (!key_file) return;
|
||||||
|
|
||||||
aes_kek_generation_source.save_key(key_file);
|
aes_kek_generation_source.save_key(key_file);
|
||||||
aes_key_generation_source.save_key(key_file);
|
aes_key_generation_source.save_key(key_file);
|
||||||
bis_kek_source.save_key(key_file);
|
bis_kek_source.save_key(key_file);
|
||||||
|
for (auto k : bis_key)
|
||||||
|
k.save_key(key_file);
|
||||||
bis_key_source_00.save_key(key_file);
|
bis_key_source_00.save_key(key_file);
|
||||||
bis_key_source_01.save_key(key_file);
|
bis_key_source_01.save_key(key_file);
|
||||||
bis_key_source_02.save_key(key_file);
|
bis_key_source_02.save_key(key_file);
|
||||||
for (auto k : bis_key)
|
|
||||||
k.save_key(key_file);
|
|
||||||
device_key.save_key(key_file);
|
device_key.save_key(key_file);
|
||||||
eticket_rsa_kek.save_key(key_file);
|
eticket_rsa_kek.save_key(key_file);
|
||||||
for (auto k : es_keys)
|
for (auto k : es_keys)
|
||||||
@ -492,10 +565,11 @@ void KeyCollection::save_keys() {
|
|||||||
save_mac_kek_source.save_key(key_file);
|
save_mac_kek_source.save_key(key_file);
|
||||||
save_mac_key.save_key(key_file);
|
save_mac_key.save_key(key_file);
|
||||||
save_mac_key_source.save_key(key_file);
|
save_mac_key_source.save_key(key_file);
|
||||||
sbk.save_key(key_file);
|
|
||||||
sd_card_kek_source.save_key(key_file);
|
sd_card_kek_source.save_key(key_file);
|
||||||
sd_card_nca_key_source.save_key(key_file);
|
sd_card_nca_key_source.save_key(key_file);
|
||||||
sd_card_save_key_source.save_key(key_file);
|
sd_card_save_key_source.save_key(key_file);
|
||||||
|
sd_seed.save_key(key_file);
|
||||||
|
sbk.save_key(key_file);
|
||||||
ssl_rsa_kek.save_key(key_file);
|
ssl_rsa_kek.save_key(key_file);
|
||||||
for (auto k : ssl_keys)
|
for (auto k : ssl_keys)
|
||||||
k->save_key(key_file);
|
k->save_key(key_file);
|
||||||
@ -504,26 +578,60 @@ void KeyCollection::save_keys() {
|
|||||||
titlekek_source.save_key(key_file);
|
titlekek_source.save_key(key_file);
|
||||||
tsec.save_key(key_file);
|
tsec.save_key(key_file);
|
||||||
tsec_root_key.save_key(key_file);
|
tsec_root_key.save_key(key_file);
|
||||||
|
|
||||||
|
fclose(key_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCollection::get_titlekeys() {
|
void KeyCollection::get_titlekeys() {
|
||||||
if (!kernelAbove200() || !eticket_rsa_kek.found())
|
if (!kernelAbove200() || !eticket_rsa_kek.found())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
u32 common_count, personalized_count, bytes_read, ids_written;
|
||||||
|
|
||||||
|
esInitialize();
|
||||||
|
esCountCommonTicket(&common_count);
|
||||||
|
esCountPersonalizedTicket(&personalized_count);
|
||||||
|
RightsId common_rights_ids[common_count], personalized_rights_ids[personalized_count];
|
||||||
|
esListCommonTicket(&ids_written, common_rights_ids, sizeof(common_rights_ids));
|
||||||
|
esListPersonalizedTicket(&ids_written, personalized_rights_ids, sizeof(personalized_rights_ids));
|
||||||
|
esExit();
|
||||||
|
if (common_count + personalized_count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
catalog all currently installed rights ids
|
||||||
|
since we are crawling the whole save file, we might accidentally find previously deleted tickets
|
||||||
|
this would be fine, except we have to match the exact list so we don't stop too early
|
||||||
|
*/
|
||||||
|
char titlekey_block[0x100], buffer[TITLEKEY_BUFFER_SIZE], rights_id_string[0x21], titlekey_string[0x21];
|
||||||
|
std::unordered_set<std::string> rights_ids;
|
||||||
|
for (size_t i = 0; i < common_count; i++) {
|
||||||
|
for (size_t j = 0; j < 0x10; j++) {
|
||||||
|
sprintf(&rights_id_string[j*2], "%02x", common_rights_ids[i].c[j]);
|
||||||
|
}
|
||||||
|
rights_ids.insert(rights_id_string);
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < personalized_count; i++) {
|
||||||
|
for (size_t j = 0; j < 0x10; j++) {
|
||||||
|
sprintf(&rights_id_string[j*2], "%02x", personalized_rights_ids[i].c[j]);
|
||||||
|
}
|
||||||
|
rights_ids.insert(rights_id_string);
|
||||||
|
}
|
||||||
|
|
||||||
// get extended eticket RSA key from PRODINFO
|
// get extended eticket RSA key from PRODINFO
|
||||||
u8 eticket_data[0x244] = {};
|
SetCalRsa2048DeviceKey eticket_data = {};
|
||||||
|
|
||||||
setcalInitialize();
|
setcalInitialize();
|
||||||
setcalGetEticketDeviceKey(eticket_data);
|
setcalGetEticketDeviceKey(&eticket_data);
|
||||||
setcalExit();
|
setcalExit();
|
||||||
|
|
||||||
byte_vector dec_keypair = eticket_rsa_kek.aes_decrypt_ctr(
|
byte_vector dec_keypair = eticket_rsa_kek.aes_decrypt_ctr(
|
||||||
byte_vector(eticket_data + 0x14, eticket_data + 0x244),
|
byte_vector(eticket_data.key + 0x10, eticket_data.key + 0x240),
|
||||||
byte_vector(eticket_data + 4, eticket_data + 0x14)
|
byte_vector(eticket_data.key, eticket_data.key + 0x10)
|
||||||
);
|
);
|
||||||
|
|
||||||
// public exponent must be 65537 == 0x10001
|
// public exponent must be 65537 == 0x10001 (big endian)
|
||||||
if (!(dec_keypair[0x201] == 1) || !(dec_keypair[0x203] == 1))
|
if (!(dec_keypair[0x200] == 0) || !(dec_keypair[0x201] == 1) || !(dec_keypair[0x202] == 0) || !(dec_keypair[0x203] == 1))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
u8 *D = &dec_keypair[0], *N = &dec_keypair[0x100], *E = &dec_keypair[0x200];
|
u8 *D = &dec_keypair[0], *N = &dec_keypair[0x100], *E = &dec_keypair[0x200];
|
||||||
@ -531,105 +639,115 @@ void KeyCollection::get_titlekeys() {
|
|||||||
if (!test_key_pair(E, D, N))
|
if (!test_key_pair(E, D, N))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
FsFileSystem save_fs;
|
FATFS fs;
|
||||||
Result rc;
|
FRESULT fr;
|
||||||
|
FIL save_file;
|
||||||
|
// map of all found rights ids and corresponding titlekeys
|
||||||
|
std::unordered_map<std::string, std::string> titlekeys;
|
||||||
|
|
||||||
for(size_t attempts = 0; attempts < 100; attempts++) {
|
fsOpenBisStorage(&storage, FsBisPartitionId_System);
|
||||||
pmshellTerminateProcessByTitleId(ES_TID);
|
if (f_mount(&fs, "", 1) || f_chdir("/save")) return;
|
||||||
|
if (f_open(&save_file, "80000000000000e1", FA_READ | FA_OPEN_EXISTING)) return;
|
||||||
|
while ((common_count != 0) && (titlekeys_dumped < common_count)) {
|
||||||
|
fr = f_read(&save_file, buffer, TITLEKEY_BUFFER_SIZE, &bytes_read);
|
||||||
|
if (fr || (bytes_read == 0)) break;
|
||||||
|
for (size_t i = 0; i < bytes_read; i += 0x4000) {
|
||||||
|
for (size_t j = i; j < i + 0x4000; j += 0x400) {
|
||||||
|
if (*reinterpret_cast<u32 *>(&buffer[j]) == 0x10004) {
|
||||||
|
for (size_t k = 0; k < 0x10; k++)
|
||||||
|
sprintf(&rights_id_string[k*2], "%02x", buffer[j + 0x2a0 + k]);
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc = fsMount_SystemSaveData(&save_fs, ES_COMMON_SAVE_ID)))
|
// skip if rights id not reported by es
|
||||||
break;
|
if (rights_ids.find(rights_id_string) == rights_ids.end())
|
||||||
}
|
continue;
|
||||||
if (R_FAILED(rc))
|
// skip if rights id already in map
|
||||||
return;
|
if (titlekeys.find(rights_id_string) != titlekeys.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
if (fsdevMountDevice("save", save_fs) == -1)
|
for (size_t k = 0; k < 0x10; k++)
|
||||||
return;
|
sprintf(&titlekey_string[k*2], "%02x", buffer[j + 0x180 + k]);
|
||||||
|
titlekeys[rights_id_string] = titlekey_string;
|
||||||
char ca_issuer[4], titlekey_block[0x100], rights_id[0x10], write_string[0x20];
|
|
||||||
|
|
||||||
std::ofstream titlekey_file("/switch/title.keys");
|
|
||||||
std::ifstream common_ticket_bin("save:/ticket.bin", std::ios::binary);
|
|
||||||
|
|
||||||
for (size_t i = 0; ; i += 0x400) {
|
|
||||||
common_ticket_bin.seekg(i + 0x140);
|
|
||||||
common_ticket_bin.read(ca_issuer, 4);
|
|
||||||
if (!std::equal(ca_issuer, ca_issuer + 4, "Root"))
|
|
||||||
break;
|
|
||||||
common_ticket_bin.seekg(i + 0x180);
|
|
||||||
common_ticket_bin.read(titlekey_block, 0x10);
|
|
||||||
common_ticket_bin.seekg(i + 0x2a0);
|
|
||||||
common_ticket_bin.read(rights_id, 0x10);
|
|
||||||
for (size_t j = 0; j < 0x10; j++)
|
|
||||||
sprintf(&write_string[j*2], "%02x", rights_id[j]);
|
|
||||||
titlekey_file.write(write_string, 0x20);
|
|
||||||
titlekey_file.write(" = ", 3);
|
|
||||||
for (size_t j = 0; j < 0x10; j++)
|
|
||||||
sprintf(&write_string[j*2], "%02x", titlekey_block[j]);
|
|
||||||
titlekey_file.write(write_string, 0x20);
|
|
||||||
titlekey_file.write("\n", 1);
|
|
||||||
titlekeys_dumped++;
|
titlekeys_dumped++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
fsdevUnmountDevice("save");
|
}
|
||||||
|
}
|
||||||
if (R_FAILED(fsMount_SystemSaveData(&save_fs, ES_PERSONALIZED_SAVE_ID)) ||
|
}
|
||||||
(fsdevMountDevice("save", save_fs) == -1))
|
f_close(&save_file);
|
||||||
return;
|
|
||||||
|
|
||||||
std::ifstream personalized_ticket_bin("save:/ticket.bin", std::ios::binary);
|
|
||||||
|
|
||||||
u8 M[0x100];
|
u8 M[0x100];
|
||||||
for (size_t i = 0; ; i += 0x400) {
|
|
||||||
personalized_ticket_bin.seekg(i + 0x140);
|
if (f_open(&save_file, "80000000000000e2", FA_READ | FA_OPEN_EXISTING)) return;
|
||||||
personalized_ticket_bin.read(ca_issuer, 4);
|
while ((personalized_count != 0) && (titlekeys_dumped < common_count + personalized_count)) {
|
||||||
if (!std::equal(ca_issuer, ca_issuer + 4, "Root"))
|
fr = f_read(&save_file, buffer, TITLEKEY_BUFFER_SIZE, &bytes_read);
|
||||||
break;
|
if (fr || (bytes_read == 0)) break;
|
||||||
personalized_ticket_bin.seekg(i + 0x180);
|
for (size_t i = 0; i < bytes_read; i += 0x4000) {
|
||||||
personalized_ticket_bin.read(titlekey_block, 0x100);
|
for (size_t j = i; j < i + 0x4000; j += 0x400) {
|
||||||
|
if (*reinterpret_cast<u32 *>(&buffer[j]) == 0x10004) {
|
||||||
|
for (size_t k = 0; k < 0x10; k++)
|
||||||
|
sprintf(&rights_id_string[k*2], "%02x", buffer[j + 0x2a0 + k]);
|
||||||
|
|
||||||
|
// skip if rights id not reported by es
|
||||||
|
if (rights_ids.find(rights_id_string) == rights_ids.end())
|
||||||
|
continue;
|
||||||
|
// skip if rights id already in map
|
||||||
|
if (titlekeys.find(rights_id_string) != titlekeys.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::copy(buffer + j + 0x180, buffer + j + 0x280, titlekey_block);
|
||||||
|
|
||||||
splUserExpMod(titlekey_block, N, D, 0x100, M);
|
splUserExpMod(titlekey_block, N, D, 0x100, M);
|
||||||
|
|
||||||
// decrypts the titlekey from personalized ticket
|
// decrypts the titlekey from personalized ticket
|
||||||
u8 salt[0x20], db[0xdf];
|
u8 salt[0x20], db[0xdf];
|
||||||
mgf1(M + 0x21, 0xdf, salt, 0x20);
|
mgf1(M + 0x21, 0xdf, salt, 0x20);
|
||||||
for (size_t j = 0; j < 0x20; j++)
|
for (size_t k = 0; k < 0x20; k++)
|
||||||
salt[j] ^= M[j + 1];
|
salt[k] ^= M[k + 1];
|
||||||
|
|
||||||
mgf1(salt, 0x20, db, 0xdf);
|
mgf1(salt, 0x20, db, 0xdf);
|
||||||
for (size_t j = 0; j < 0xdf; j++)
|
for (size_t k = 0; k < 0xdf; k++)
|
||||||
db[j] ^= M[j + 0x21];
|
db[k] ^= M[k + 0x21];
|
||||||
|
|
||||||
// verify it starts with hash of null string
|
// verify it starts with hash of null string
|
||||||
if (!std::equal(db, db + 0x20, null_hash))
|
if (!std::equal(db, db + 0x20, null_hash))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
personalized_ticket_bin.seekg(i + 0x2a0);
|
for (size_t k = 0; k < 0x10; k++)
|
||||||
personalized_ticket_bin.read(rights_id, 0x10);
|
sprintf(&titlekey_string[k*2], "%02x", db[k + 0xcf]);
|
||||||
for (size_t j = 0; j < 0x10; j++)
|
titlekeys[rights_id_string] = titlekey_string;
|
||||||
sprintf(&write_string[j*2], "%02x", rights_id[j]);
|
|
||||||
titlekey_file.write(write_string, 0x20);
|
|
||||||
titlekey_file.write(" = ", 3);
|
|
||||||
for (size_t j = 0; j < 0x10; j++)
|
|
||||||
sprintf(&write_string[j*2], "%02x", db[j + 0xcf]);
|
|
||||||
titlekey_file.write(write_string, 0x20);
|
|
||||||
titlekey_file.write("\n", 1);
|
|
||||||
titlekeys_dumped++;
|
titlekeys_dumped++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
fsdevUnmountDevice("save");
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f_close(&save_file);
|
||||||
|
fsStorageClose(&storage);
|
||||||
|
|
||||||
|
if (titlekeys.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
FILE *titlekey_file = fopen("/switch/title.keys", "wb");
|
||||||
|
if (!titlekey_file) return;
|
||||||
|
for (auto k : titlekeys)
|
||||||
|
fprintf(titlekey_file, "%s = %s\n", k.first.c_str(), k.second.c_str());
|
||||||
|
fclose(titlekey_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCollection::mgf1(const u8 *data, size_t data_length, u8 *mask, size_t mask_length) {
|
void KeyCollection::mgf1(const u8 *data, size_t data_length, u8 *mask, size_t mask_length) {
|
||||||
u8 data_counter[data_length + 4] = {};
|
u8 data_counter[data_length + 4] = {};
|
||||||
std::copy(data, data + data_length, data_counter);
|
std::copy(data, data + data_length, data_counter);
|
||||||
Common::sha256(data_counter, mask, data_length + 4);
|
sha256CalculateHash(mask, data_counter, data_length + 4);
|
||||||
for (u32 i = 1; i < (mask_length / 0x20) + 1; i++) {
|
for (u32 i = 1; i < (mask_length / 0x20) + 1; i++) {
|
||||||
for (size_t j = 0; j < 4; j++)
|
for (size_t j = 0; j < 4; j++)
|
||||||
data_counter[data_length + 3 - j] = (i >> (8 * j)) & 0xff;
|
data_counter[data_length + 3 - j] = (i >> (8 * j)) & 0xff;
|
||||||
if (i * 0x20 <= mask_length)
|
if (i * 0x20 <= mask_length)
|
||||||
Common::sha256(data_counter, mask + (i * 0x20), data_length + 4);
|
sha256CalculateHash(mask + (i * 0x20), data_counter, data_length + 4);
|
||||||
else {
|
else {
|
||||||
u8 temp_mask[0x20];
|
u8 temp_mask[0x20];
|
||||||
Common::sha256(data_counter, temp_mask, data_length + 4);
|
sha256CalculateHash(temp_mask, data_counter, data_length + 4);
|
||||||
std::copy(temp_mask, temp_mask + mask_length - (i * 0x20), mask + (i * 0x20));
|
std::copy(temp_mask, temp_mask + mask_length - (i * 0x20), mask + (i * 0x20));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,14 @@
|
|||||||
#include "Key.hpp"
|
#include "Key.hpp"
|
||||||
#include "KeyLocation.hpp"
|
#include "KeyLocation.hpp"
|
||||||
|
|
||||||
|
#include <switch/types.h>
|
||||||
|
|
||||||
class KeyCollection {
|
class KeyCollection {
|
||||||
public:
|
public:
|
||||||
KeyCollection();
|
KeyCollection();
|
||||||
|
|
||||||
// get KeyLocations and find keys in them
|
// get KeyLocations and find keys in them
|
||||||
int get_keys();
|
void get_keys();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// utility functions called by get_keys
|
// utility functions called by get_keys
|
||||||
@ -87,7 +89,9 @@ private:
|
|||||||
rsa_oaep_kek_generation_source,
|
rsa_oaep_kek_generation_source,
|
||||||
rsa_private_kek_generation_source,
|
rsa_private_kek_generation_source,
|
||||||
save_mac_key,
|
save_mac_key,
|
||||||
ssl_rsa_kek;
|
ssl_rsa_kek,
|
||||||
|
// other
|
||||||
|
sd_seed;
|
||||||
|
|
||||||
// key families
|
// key families
|
||||||
std::vector<Key>
|
std::vector<Key>
|
||||||
@ -109,7 +113,7 @@ private:
|
|||||||
titlekek;
|
titlekek;
|
||||||
|
|
||||||
std::vector<Key *>
|
std::vector<Key *>
|
||||||
es_keys, fs_rodata_keys, package1ldr_keys, ssl_keys, tz_keys;
|
es_keys, fs_rodata_keys, ssl_keys;
|
||||||
|
|
||||||
// hash of empty string used to verify titlekeys for personalized tickets
|
// hash of empty string used to verify titlekeys for personalized tickets
|
||||||
static const u8 null_hash[0x20];
|
static const u8 null_hash[0x20];
|
||||||
|
@ -16,19 +16,21 @@
|
|||||||
|
|
||||||
#include "KeyLocation.hpp"
|
#include "KeyLocation.hpp"
|
||||||
|
|
||||||
#include <filesystem>
|
#include "xxhash64.h"
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include "creport_debug_types.hpp"
|
#include <algorithm>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
void KeyLocation::get_from_memory(u64 tid, u8 segMask) {
|
#include <switch.h>
|
||||||
|
|
||||||
|
void KeyLocation::get_from_memory(u64 tid, u8 seg_mask) {
|
||||||
Handle debug_handle = INVALID_HANDLE;
|
Handle debug_handle = INVALID_HANDLE;
|
||||||
DebugEventInfo d;
|
u64 d[8];
|
||||||
|
|
||||||
// if not a kernel process, get pid from pm:dmnt
|
// if not a kernel process, get pid from pm:dmnt
|
||||||
if ((tid > 0x0100000000000005) && (tid != 0x0100000000000028)) {
|
if ((tid > 0x0100000000000005) && (tid != 0x0100000000000028)) {
|
||||||
u64 pid;
|
u64 pid;
|
||||||
pmdmntGetTitlePid(&pid, tid);
|
pmdmntGetProcessId(&pid, tid);
|
||||||
|
|
||||||
if (R_FAILED(svcDebugActiveProcess(&debug_handle, pid)) ||
|
if (R_FAILED(svcDebugActiveProcess(&debug_handle, pid)) ||
|
||||||
R_FAILED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle)))
|
R_FAILED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle)))
|
||||||
@ -44,7 +46,7 @@ void KeyLocation::get_from_memory(u64 tid, u8 segMask) {
|
|||||||
for (i = 0; i < num_processes - 1; i++) {
|
for (i = 0; i < num_processes - 1; i++) {
|
||||||
if (R_SUCCEEDED(svcDebugActiveProcess(&debug_handle, pids[i])) &&
|
if (R_SUCCEEDED(svcDebugActiveProcess(&debug_handle, pids[i])) &&
|
||||||
R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle)) &&
|
R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle)) &&
|
||||||
(d.info.attach_process.title_id == tid))
|
(d[2] == tid))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -60,7 +62,22 @@ void KeyLocation::get_from_memory(u64 tid, u8 segMask) {
|
|||||||
|
|
||||||
u32 page_info;
|
u32 page_info;
|
||||||
u64 addr = 0;
|
u64 addr = 0;
|
||||||
|
u64 last_text_addr = 0;
|
||||||
|
|
||||||
|
// locate "real" .text segment as Atmosphere emuNAND has two
|
||||||
|
for (;;) {
|
||||||
|
svcQueryDebugProcessMemory(&mem_info, &page_info, debug_handle, addr);
|
||||||
|
if ((mem_info.perm & Perm_X) &&
|
||||||
|
((mem_info.type & 0xff) >= MemType_CodeStatic) &&
|
||||||
|
((mem_info.type & 0xff) < MemType_Heap))
|
||||||
|
{
|
||||||
|
last_text_addr = mem_info.addr;
|
||||||
|
}
|
||||||
|
addr = mem_info.addr + mem_info.size;
|
||||||
|
if (addr == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
addr = last_text_addr;
|
||||||
for (u8 segment = 1; segment < BIT(3); )
|
for (u8 segment = 1; segment < BIT(3); )
|
||||||
{
|
{
|
||||||
svcQueryDebugProcessMemory(&mem_info, &page_info, debug_handle, addr);
|
svcQueryDebugProcessMemory(&mem_info, &page_info, debug_handle, addr);
|
||||||
@ -68,7 +85,7 @@ void KeyLocation::get_from_memory(u64 tid, u8 segMask) {
|
|||||||
if ((mem_info.perm & Perm_R) &&
|
if ((mem_info.perm & Perm_R) &&
|
||||||
((mem_info.type & 0xff) >= MemType_CodeStatic) &&
|
((mem_info.type & 0xff) >= MemType_CodeStatic) &&
|
||||||
((mem_info.type & 0xff) < MemType_Heap) &&
|
((mem_info.type & 0xff) < MemType_Heap) &&
|
||||||
((segment <<= 1) >> 1 & segMask) > 0)
|
((segment <<= 1) >> 1 & seg_mask) > 0)
|
||||||
{
|
{
|
||||||
data.resize(data.size() + mem_info.size);
|
data.resize(data.size() + mem_info.size);
|
||||||
if(R_FAILED(svcReadDebugProcessMemory(data.data() + data.size() - mem_info.size, debug_handle, mem_info.addr, mem_info.size))) {
|
if(R_FAILED(svcReadDebugProcessMemory(data.data() + data.size() - mem_info.size, debug_handle, mem_info.addr, mem_info.size))) {
|
||||||
@ -79,14 +96,47 @@ void KeyLocation::get_from_memory(u64 tid, u8 segMask) {
|
|||||||
addr = mem_info.addr + mem_info.size;
|
addr = mem_info.addr + mem_info.size;
|
||||||
if (addr == 0) break;
|
if (addr == 0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
svcCloseHandle(debug_handle);
|
svcCloseHandle(debug_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyLocation::get_keyblobs() {
|
void KeyLocation::get_keyblobs() {
|
||||||
FsStorage boot0;
|
FsStorage boot0;
|
||||||
fsOpenBisStorage(&boot0, 0);
|
fsOpenBisStorage(&boot0, FsBisPartitionId_BootPartition1Root);
|
||||||
data.resize(0x200 * KNOWN_KEYBLOBS);
|
data.resize(0x200 * KNOWN_KEYBLOBS);
|
||||||
fsStorageRead(&boot0, KEYBLOB_OFFSET, data.data(), data.size());
|
fsStorageRead(&boot0, KEYBLOB_OFFSET, data.data(), data.size());
|
||||||
fsStorageClose(&boot0);
|
fsStorageClose(&boot0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KeyLocation::find_keys(std::vector<Key *> &keys) {
|
||||||
|
if (data.size() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
u8 temp_hash[0x20];
|
||||||
|
size_t key_indices_left = keys.size();
|
||||||
|
u64 hash = 0;
|
||||||
|
std::unordered_map<u64, size_t> hash_index;
|
||||||
|
for (size_t i = 0; i < keys.size(); i++)
|
||||||
|
hash_index[keys[i]->xx_hash] = i;
|
||||||
|
|
||||||
|
// hash every length-sized byte chunk in data until it matches a key hash
|
||||||
|
for (size_t i = 0; i < data.size() - 0x10; i++) {
|
||||||
|
hash = XXHash64::hash(data.data() + i, 0x10, 0);
|
||||||
|
auto search = hash_index.find(hash);
|
||||||
|
if (search == hash_index.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
size_t key_index = hash_index[hash];
|
||||||
|
u8 key_length = keys[key_index]->length;
|
||||||
|
// double-check sha256 since xxhash64 isn't as collision-safe
|
||||||
|
sha256CalculateHash(temp_hash, data.data() + i, key_length);
|
||||||
|
if (!std::equal(keys[key_index]->hash.begin(), keys[key_index]->hash.end(), temp_hash))
|
||||||
|
continue;
|
||||||
|
std::copy(data.begin() + i, data.begin() + i + key_length, std::back_inserter(keys[key_index]->key));
|
||||||
|
keys[key_index]->is_found = true;
|
||||||
|
key_indices_left--;
|
||||||
|
if (key_indices_left == 0)
|
||||||
|
return;
|
||||||
|
hash_index.erase(hash);
|
||||||
|
i += key_length - 1;
|
||||||
|
}
|
||||||
|
}
|
@ -16,11 +16,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Key.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch/types.h>
|
||||||
|
|
||||||
#include "Common.hpp"
|
|
||||||
|
|
||||||
#define FS_TID 0x0100000000000000
|
#define FS_TID 0x0100000000000000
|
||||||
#define SSL_TID 0x0100000000000024
|
#define SSL_TID 0x0100000000000024
|
||||||
@ -41,12 +41,16 @@
|
|||||||
|
|
||||||
#define KEYBLOB_OFFSET 0x180000
|
#define KEYBLOB_OFFSET 0x180000
|
||||||
|
|
||||||
|
typedef std::vector<u8> byte_vector;
|
||||||
|
|
||||||
class KeyLocation {
|
class KeyLocation {
|
||||||
public:
|
public:
|
||||||
// get memory in requested segments from running title
|
// get memory in requested segments from running title
|
||||||
void get_from_memory(u64 tid, u8 segMask);
|
void get_from_memory(u64 tid, u8 seg_mask);
|
||||||
// get keyblobs from BOOT0
|
// get keyblobs from BOOT0
|
||||||
void get_keyblobs();
|
void get_keyblobs();
|
||||||
|
// locate keys in data
|
||||||
|
void find_keys(std::vector<Key *> &keys);
|
||||||
|
|
||||||
// data found by get functions
|
// data found by get functions
|
||||||
byte_vector data;
|
byte_vector data;
|
||||||
|
@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
#include "Stopwatch.hpp"
|
#include "Stopwatch.hpp"
|
||||||
|
|
||||||
#include "Common.hpp"
|
|
||||||
|
|
||||||
void Stopwatch::start() {
|
void Stopwatch::start() {
|
||||||
clock_gettime(CLOCK_REALTIME, &start_time);
|
clock_gettime(CLOCK_REALTIME, &start_time);
|
||||||
}
|
}
|
||||||
|
@ -1,116 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018 Atmosphère-NX
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms and conditions of the GNU General Public License,
|
|
||||||
* version 2, as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
// modified 13 nov 2018 by shchmue // -> deleted everything that isn't the struct defines
|
|
||||||
/////////////////////////////////////
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <switch.h>
|
|
||||||
|
|
||||||
struct StackFrame {
|
|
||||||
u64 fp;
|
|
||||||
u64 lr;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AttachProcessInfo {
|
|
||||||
u64 title_id;
|
|
||||||
u64 process_id;
|
|
||||||
char name[0xC];
|
|
||||||
u32 flags;
|
|
||||||
u64 user_exception_context_address; /* 5.0.0+ */
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AttachThreadInfo {
|
|
||||||
u64 thread_id;
|
|
||||||
u64 tls_address;
|
|
||||||
u64 entrypoint;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class DebugExceptionType : u32 {
|
|
||||||
UndefinedInstruction = 0,
|
|
||||||
InstructionAbort = 1,
|
|
||||||
DataAbort = 2,
|
|
||||||
AlignmentFault = 3,
|
|
||||||
DebuggerAttached = 4,
|
|
||||||
BreakPoint = 5,
|
|
||||||
UserBreak = 6,
|
|
||||||
DebuggerBreak = 7,
|
|
||||||
BadSvc = 8,
|
|
||||||
UnknownNine = 9,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UndefinedInstructionInfo {
|
|
||||||
u32 insn;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DataAbortInfo {
|
|
||||||
u64 address;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AlignmentFaultInfo {
|
|
||||||
u64 address;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UserBreakInfo {
|
|
||||||
u64 break_reason;
|
|
||||||
u64 address;
|
|
||||||
u64 size;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BadSvcInfo {
|
|
||||||
u32 id;
|
|
||||||
};
|
|
||||||
|
|
||||||
union SpecificExceptionInfo {
|
|
||||||
UndefinedInstructionInfo undefined_instruction;
|
|
||||||
DataAbortInfo data_abort;
|
|
||||||
AlignmentFaultInfo alignment_fault;
|
|
||||||
UserBreakInfo user_break;
|
|
||||||
BadSvcInfo bad_svc;
|
|
||||||
u64 raw;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ExceptionInfo {
|
|
||||||
DebugExceptionType type;
|
|
||||||
u64 address;
|
|
||||||
SpecificExceptionInfo specific;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
enum class DebugEventType : u32 {
|
|
||||||
AttachProcess = 0,
|
|
||||||
AttachThread = 1,
|
|
||||||
ExitProcess = 2,
|
|
||||||
ExitThread = 3,
|
|
||||||
Exception = 4
|
|
||||||
};
|
|
||||||
|
|
||||||
union DebugInfo {
|
|
||||||
AttachProcessInfo attach_process;
|
|
||||||
AttachThreadInfo attach_thread;
|
|
||||||
ExceptionInfo exception;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DebugEventInfo {
|
|
||||||
DebugEventType type;
|
|
||||||
u32 flags;
|
|
||||||
u64 thread_id;
|
|
||||||
union {
|
|
||||||
DebugInfo info;
|
|
||||||
u64 _[0x40/sizeof(u64)];
|
|
||||||
};
|
|
||||||
};
|
|
24
source/fatfs/LICENSE.txt
Normal file
24
source/fatfs/LICENSE.txt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
FatFs License
|
||||||
|
|
||||||
|
FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files.
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------/
|
||||||
|
/ FatFs - Generic FAT Filesystem Module Rx.xx /
|
||||||
|
/-----------------------------------------------------------------------------/
|
||||||
|
/
|
||||||
|
/ Copyright (C) 20xx, ChaN, all right reserved.
|
||||||
|
/
|
||||||
|
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||||
|
/ source and binary forms, with or without modification, are permitted provided
|
||||||
|
/ that the following condition is met:
|
||||||
|
/
|
||||||
|
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
/ this condition and the following disclaimer.
|
||||||
|
/
|
||||||
|
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||||
|
/ and any warranties related to this software are DISCLAIMED.
|
||||||
|
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||||
|
/ by use of this software.
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses including GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.
|
92
source/fatfs/diskio.c
Normal file
92
source/fatfs/diskio.c
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* If a working storage control module is available, it should be */
|
||||||
|
/* attached to the FatFs via a glue function rather than modifying it. */
|
||||||
|
/* This is an example of glue functions to attach various exsisting */
|
||||||
|
/* storage control modules to the FatFs module with a defined API. */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include "ff.h" /* Obtains integer types */
|
||||||
|
#include "diskio.h" /* Declarations of disk functions */
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
|
extern FsStorage storage;
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Get Drive Status */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DSTATUS disk_status (
|
||||||
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Inidialize a Drive */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DSTATUS disk_initialize (
|
||||||
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Read Sector(s) */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DRESULT disk_read (
|
||||||
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
||||||
|
BYTE *buff, /* Data buffer to store read data */
|
||||||
|
DWORD sector, /* Start sector in LBA */
|
||||||
|
UINT count /* Number of sectors to read */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (R_SUCCEEDED(fsStorageRead(&storage, FF_MAX_SS * sector, buff, FF_MAX_SS * count)))
|
||||||
|
return RES_OK;
|
||||||
|
return RES_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Write Sector(s) */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#if FF_FS_READONLY == 0
|
||||||
|
|
||||||
|
DRESULT disk_write (
|
||||||
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
||||||
|
const BYTE *buff, /* Data to be written */
|
||||||
|
DWORD sector, /* Start sector in LBA */
|
||||||
|
UINT count /* Number of sectors to write */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return RES_PARERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Miscellaneous Functions */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DRESULT disk_ioctl (
|
||||||
|
BYTE pdrv, /* Physical drive nmuber (0..) */
|
||||||
|
BYTE cmd, /* Control code */
|
||||||
|
void *buff /* Buffer to send/receive control data */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return RES_OK;
|
||||||
|
}
|
||||||
|
|
77
source/fatfs/diskio.h
Normal file
77
source/fatfs/diskio.h
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/*-----------------------------------------------------------------------/
|
||||||
|
/ Low level disk interface modlue include file (C)ChaN, 2014 /
|
||||||
|
/-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef _DISKIO_DEFINED
|
||||||
|
#define _DISKIO_DEFINED
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Status of Disk Functions */
|
||||||
|
typedef BYTE DSTATUS;
|
||||||
|
|
||||||
|
/* Results of Disk Functions */
|
||||||
|
typedef enum {
|
||||||
|
RES_OK = 0, /* 0: Successful */
|
||||||
|
RES_ERROR, /* 1: R/W Error */
|
||||||
|
RES_WRPRT, /* 2: Write Protected */
|
||||||
|
RES_NOTRDY, /* 3: Not Ready */
|
||||||
|
RES_PARERR /* 4: Invalid Parameter */
|
||||||
|
} DRESULT;
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------*/
|
||||||
|
/* Prototypes for disk control functions */
|
||||||
|
|
||||||
|
|
||||||
|
DSTATUS disk_initialize (BYTE pdrv);
|
||||||
|
DSTATUS disk_status (BYTE pdrv);
|
||||||
|
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
|
||||||
|
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
|
||||||
|
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
|
||||||
|
|
||||||
|
|
||||||
|
/* Disk Status Bits (DSTATUS) */
|
||||||
|
|
||||||
|
#define STA_NOINIT 0x01 /* Drive not initialized */
|
||||||
|
#define STA_NODISK 0x02 /* No medium in the drive */
|
||||||
|
#define STA_PROTECT 0x04 /* Write protected */
|
||||||
|
|
||||||
|
|
||||||
|
/* Command code for disk_ioctrl fucntion */
|
||||||
|
|
||||||
|
/* Generic command (Used by FatFs) */
|
||||||
|
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
|
||||||
|
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
|
||||||
|
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
|
||||||
|
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
|
||||||
|
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */
|
||||||
|
|
||||||
|
/* Generic command (Not used by FatFs) */
|
||||||
|
#define CTRL_POWER 5 /* Get/Set power status */
|
||||||
|
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
|
||||||
|
#define CTRL_EJECT 7 /* Eject media */
|
||||||
|
#define CTRL_FORMAT 8 /* Create physical format on the media */
|
||||||
|
|
||||||
|
/* MMC/SDC specific ioctl command */
|
||||||
|
#define MMC_GET_TYPE 10 /* Get card type */
|
||||||
|
#define MMC_GET_CSD 11 /* Get CSD */
|
||||||
|
#define MMC_GET_CID 12 /* Get CID */
|
||||||
|
#define MMC_GET_OCR 13 /* Get OCR */
|
||||||
|
#define MMC_GET_SDSTAT 14 /* Get SD status */
|
||||||
|
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
|
||||||
|
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
|
||||||
|
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
|
||||||
|
|
||||||
|
/* ATA/CF specific ioctl command */
|
||||||
|
#define ATA_GET_REV 20 /* Get F/W revision */
|
||||||
|
#define ATA_GET_MODEL 21 /* Get model name */
|
||||||
|
#define ATA_GET_SN 22 /* Get serial number */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
6554
source/fatfs/ff.c
Normal file
6554
source/fatfs/ff.c
Normal file
File diff suppressed because it is too large
Load Diff
405
source/fatfs/ff.h
Normal file
405
source/fatfs/ff.h
Normal file
@ -0,0 +1,405 @@
|
|||||||
|
/*----------------------------------------------------------------------------/
|
||||||
|
/ FatFs - Generic FAT Filesystem module R0.13c /
|
||||||
|
/-----------------------------------------------------------------------------/
|
||||||
|
/
|
||||||
|
/ Copyright (C) 2018, ChaN, all right reserved.
|
||||||
|
/
|
||||||
|
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||||
|
/ source and binary forms, with or without modification, are permitted provided
|
||||||
|
/ that the following condition is met:
|
||||||
|
|
||||||
|
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
/ this condition and the following disclaimer.
|
||||||
|
/
|
||||||
|
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||||
|
/ and any warranties related to this software are DISCLAIMED.
|
||||||
|
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||||
|
/ by use of this software.
|
||||||
|
/
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef FF_DEFINED
|
||||||
|
#define FF_DEFINED 86604 /* Revision ID */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "ffconf.h" /* FatFs configuration options */
|
||||||
|
|
||||||
|
#if FF_DEFINED != FFCONF_DEF
|
||||||
|
#error Wrong configuration file (ffconf.h).
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* Integer types used for FatFs API */
|
||||||
|
|
||||||
|
#if defined(_WIN32) /* Main development platform */
|
||||||
|
#define FF_INTDEF 2
|
||||||
|
#include <windows.h>
|
||||||
|
typedef unsigned __int64 QWORD;
|
||||||
|
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
|
||||||
|
#define FF_INTDEF 2
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
|
||||||
|
typedef unsigned char BYTE; /* char must be 8-bit */
|
||||||
|
typedef uint16_t WORD; /* 16-bit unsigned integer */
|
||||||
|
typedef uint16_t WCHAR; /* 16-bit unsigned integer */
|
||||||
|
typedef uint32_t DWORD; /* 32-bit unsigned integer */
|
||||||
|
typedef uint64_t QWORD; /* 64-bit unsigned integer */
|
||||||
|
#else /* Earlier than C99 */
|
||||||
|
#define FF_INTDEF 1
|
||||||
|
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
|
||||||
|
typedef unsigned char BYTE; /* char must be 8-bit */
|
||||||
|
typedef unsigned short WORD; /* 16-bit unsigned integer */
|
||||||
|
typedef unsigned short WCHAR; /* 16-bit unsigned integer */
|
||||||
|
typedef unsigned long DWORD; /* 32-bit unsigned integer */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* Definitions of volume management */
|
||||||
|
|
||||||
|
#if FF_MULTI_PARTITION /* Multiple partition configuration */
|
||||||
|
typedef struct {
|
||||||
|
BYTE pd; /* Physical drive number */
|
||||||
|
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||||
|
} PARTITION;
|
||||||
|
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if FF_STR_VOLUME_ID
|
||||||
|
#ifndef FF_VOLUME_STRS
|
||||||
|
extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Type of path name strings on FatFs API */
|
||||||
|
|
||||||
|
#ifndef _INC_TCHAR
|
||||||
|
#define _INC_TCHAR
|
||||||
|
|
||||||
|
#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
|
||||||
|
typedef WCHAR TCHAR;
|
||||||
|
#define _T(x) L ## x
|
||||||
|
#define _TEXT(x) L ## x
|
||||||
|
#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
|
||||||
|
typedef char TCHAR;
|
||||||
|
#define _T(x) u8 ## x
|
||||||
|
#define _TEXT(x) u8 ## x
|
||||||
|
#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
|
||||||
|
typedef DWORD TCHAR;
|
||||||
|
#define _T(x) U ## x
|
||||||
|
#define _TEXT(x) U ## x
|
||||||
|
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
|
||||||
|
#error Wrong FF_LFN_UNICODE setting
|
||||||
|
#else /* ANSI/OEM code in SBCS/DBCS */
|
||||||
|
typedef char TCHAR;
|
||||||
|
#define _T(x) x
|
||||||
|
#define _TEXT(x) x
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Type of file size variables */
|
||||||
|
|
||||||
|
#if FF_FS_EXFAT
|
||||||
|
#if FF_INTDEF != 2
|
||||||
|
#error exFAT feature wants C99 or later
|
||||||
|
#endif
|
||||||
|
typedef QWORD FSIZE_t;
|
||||||
|
#else
|
||||||
|
typedef DWORD FSIZE_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Filesystem object structure (FATFS) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
BYTE fs_type; /* Filesystem type (0:not mounted) */
|
||||||
|
BYTE pdrv; /* Associated physical drive */
|
||||||
|
BYTE n_fats; /* Number of FATs (1 or 2) */
|
||||||
|
BYTE wflag; /* win[] flag (b0:dirty) */
|
||||||
|
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
|
||||||
|
WORD id; /* Volume mount ID */
|
||||||
|
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
|
||||||
|
WORD csize; /* Cluster size [sectors] */
|
||||||
|
#if FF_MAX_SS != FF_MIN_SS
|
||||||
|
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
|
||||||
|
#endif
|
||||||
|
#if FF_USE_LFN
|
||||||
|
WCHAR* lfnbuf; /* LFN working buffer */
|
||||||
|
#endif
|
||||||
|
#if FF_FS_EXFAT
|
||||||
|
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
|
||||||
|
#endif
|
||||||
|
#if FF_FS_REENTRANT
|
||||||
|
FF_SYNC_t sobj; /* Identifier of sync object */
|
||||||
|
#endif
|
||||||
|
#if !FF_FS_READONLY
|
||||||
|
DWORD last_clst; /* Last allocated cluster */
|
||||||
|
DWORD free_clst; /* Number of free clusters */
|
||||||
|
#endif
|
||||||
|
#if FF_FS_RPATH
|
||||||
|
DWORD cdir; /* Current directory start cluster (0:root) */
|
||||||
|
#if FF_FS_EXFAT
|
||||||
|
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
|
||||||
|
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
|
||||||
|
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
|
||||||
|
DWORD fsize; /* Size of an FAT [sectors] */
|
||||||
|
DWORD volbase; /* Volume base sector */
|
||||||
|
DWORD fatbase; /* FAT base sector */
|
||||||
|
DWORD dirbase; /* Root directory base sector/cluster */
|
||||||
|
DWORD database; /* Data base sector */
|
||||||
|
#if FF_FS_EXFAT
|
||||||
|
DWORD bitbase; /* Allocation bitmap base sector */
|
||||||
|
#endif
|
||||||
|
DWORD winsect; /* Current sector appearing in the win[] */
|
||||||
|
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
|
||||||
|
} FATFS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Object ID and allocation information (FFOBJID) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FATFS* fs; /* Pointer to the hosting volume of this object */
|
||||||
|
WORD id; /* Hosting volume mount ID */
|
||||||
|
BYTE attr; /* Object attribute */
|
||||||
|
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
|
||||||
|
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
|
||||||
|
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
|
||||||
|
#if FF_FS_EXFAT
|
||||||
|
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
|
||||||
|
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
|
||||||
|
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
|
||||||
|
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
|
||||||
|
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
|
||||||
|
#endif
|
||||||
|
#if FF_FS_LOCK
|
||||||
|
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
|
||||||
|
#endif
|
||||||
|
} FFOBJID;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File object structure (FIL) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
|
||||||
|
BYTE flag; /* File status flags */
|
||||||
|
BYTE err; /* Abort flag (error code) */
|
||||||
|
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
|
||||||
|
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
|
||||||
|
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
|
||||||
|
#if !FF_FS_READONLY
|
||||||
|
DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
|
||||||
|
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
|
||||||
|
#endif
|
||||||
|
#if FF_USE_FASTSEEK
|
||||||
|
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
|
||||||
|
#endif
|
||||||
|
#if !FF_FS_TINY
|
||||||
|
BYTE buf[FF_MAX_SS]; /* File private data read/write window */
|
||||||
|
#endif
|
||||||
|
} FIL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Directory object structure (DIR) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FFOBJID obj; /* Object identifier */
|
||||||
|
DWORD dptr; /* Current read/write offset */
|
||||||
|
DWORD clust; /* Current cluster */
|
||||||
|
DWORD sect; /* Current sector (0:Read operation has terminated) */
|
||||||
|
BYTE* dir; /* Pointer to the directory item in the win[] */
|
||||||
|
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
|
||||||
|
#if FF_USE_LFN
|
||||||
|
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
|
||||||
|
#endif
|
||||||
|
#if FF_USE_FIND
|
||||||
|
const TCHAR* pat; /* Pointer to the name matching pattern */
|
||||||
|
#endif
|
||||||
|
} DIR;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File information structure (FILINFO) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FSIZE_t fsize; /* File size */
|
||||||
|
WORD fdate; /* Modified date */
|
||||||
|
WORD ftime; /* Modified time */
|
||||||
|
BYTE fattrib; /* File attribute */
|
||||||
|
#if FF_USE_LFN
|
||||||
|
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
|
||||||
|
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
|
||||||
|
#else
|
||||||
|
TCHAR fname[12 + 1]; /* File name */
|
||||||
|
#endif
|
||||||
|
} FILINFO;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File function return code (FRESULT) */
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
FR_OK = 0, /* (0) Succeeded */
|
||||||
|
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
|
||||||
|
FR_INT_ERR, /* (2) Assertion failed */
|
||||||
|
FR_NOT_READY, /* (3) The physical drive cannot work */
|
||||||
|
FR_NO_FILE, /* (4) Could not find the file */
|
||||||
|
FR_NO_PATH, /* (5) Could not find the path */
|
||||||
|
FR_INVALID_NAME, /* (6) The path name format is invalid */
|
||||||
|
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
|
||||||
|
FR_EXIST, /* (8) Access denied due to prohibited access */
|
||||||
|
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
|
||||||
|
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
|
||||||
|
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
|
||||||
|
FR_NOT_ENABLED, /* (12) The volume has no work area */
|
||||||
|
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
|
||||||
|
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
|
||||||
|
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
|
||||||
|
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
|
||||||
|
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
|
||||||
|
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
|
||||||
|
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
|
||||||
|
} FRESULT;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* FatFs module application interface */
|
||||||
|
|
||||||
|
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
|
||||||
|
FRESULT f_close (FIL* fp); /* Close an open file object */
|
||||||
|
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
|
||||||
|
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
|
||||||
|
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
|
||||||
|
FRESULT f_truncate (FIL* fp); /* Truncate the file */
|
||||||
|
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
|
||||||
|
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
|
||||||
|
FRESULT f_closedir (DIR* dp); /* Close an open directory */
|
||||||
|
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
|
||||||
|
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
|
||||||
|
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
|
||||||
|
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
|
||||||
|
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
|
||||||
|
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
|
||||||
|
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
|
||||||
|
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
|
||||||
|
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
|
||||||
|
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
|
||||||
|
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
|
||||||
|
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
|
||||||
|
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
|
||||||
|
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
|
||||||
|
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
|
||||||
|
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
|
||||||
|
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */
|
||||||
|
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
|
||||||
|
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */
|
||||||
|
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */
|
||||||
|
FRESULT f_setcp (WORD cp); /* Set current code page */
|
||||||
|
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
|
||||||
|
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
|
||||||
|
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
|
||||||
|
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
|
||||||
|
|
||||||
|
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
|
||||||
|
#define f_error(fp) ((fp)->err)
|
||||||
|
#define f_tell(fp) ((fp)->fptr)
|
||||||
|
#define f_size(fp) ((fp)->obj.objsize)
|
||||||
|
#define f_rewind(fp) f_lseek((fp), 0)
|
||||||
|
#define f_rewinddir(dp) f_readdir((dp), 0)
|
||||||
|
#define f_rmdir(path) f_unlink(path)
|
||||||
|
#define f_unmount(path) f_mount(0, path, 0)
|
||||||
|
|
||||||
|
#ifndef EOF
|
||||||
|
#define EOF (-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* Additional user defined functions */
|
||||||
|
|
||||||
|
/* RTC function */
|
||||||
|
#if !FF_FS_READONLY && !FF_FS_NORTC
|
||||||
|
DWORD get_fattime (void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* LFN support functions */
|
||||||
|
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
|
||||||
|
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
|
||||||
|
WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
|
||||||
|
DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
|
||||||
|
#endif
|
||||||
|
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
|
||||||
|
void* ff_memalloc (UINT msize); /* Allocate memory block */
|
||||||
|
void ff_memfree (void* mblock); /* Free memory block */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Sync functions */
|
||||||
|
#if FF_FS_REENTRANT
|
||||||
|
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
|
||||||
|
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
|
||||||
|
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
|
||||||
|
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* Flags and offset address */
|
||||||
|
|
||||||
|
|
||||||
|
/* File access mode and open method flags (3rd argument of f_open) */
|
||||||
|
#define FA_READ 0x01
|
||||||
|
#define FA_WRITE 0x02
|
||||||
|
#define FA_OPEN_EXISTING 0x00
|
||||||
|
#define FA_CREATE_NEW 0x04
|
||||||
|
#define FA_CREATE_ALWAYS 0x08
|
||||||
|
#define FA_OPEN_ALWAYS 0x10
|
||||||
|
#define FA_OPEN_APPEND 0x30
|
||||||
|
|
||||||
|
/* Fast seek controls (2nd argument of f_lseek) */
|
||||||
|
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
|
||||||
|
|
||||||
|
/* Format options (2nd argument of f_mkfs) */
|
||||||
|
#define FM_FAT 0x01
|
||||||
|
#define FM_FAT32 0x02
|
||||||
|
#define FM_EXFAT 0x04
|
||||||
|
#define FM_ANY 0x07
|
||||||
|
#define FM_SFD 0x08
|
||||||
|
|
||||||
|
/* Filesystem type (FATFS.fs_type) */
|
||||||
|
#define FS_FAT12 1
|
||||||
|
#define FS_FAT16 2
|
||||||
|
#define FS_FAT32 3
|
||||||
|
#define FS_EXFAT 4
|
||||||
|
|
||||||
|
/* File attribute bits for directory entry (FILINFO.fattrib) */
|
||||||
|
#define AM_RDO 0x01 /* Read only */
|
||||||
|
#define AM_HID 0x02 /* Hidden */
|
||||||
|
#define AM_SYS 0x04 /* System */
|
||||||
|
#define AM_DIR 0x10 /* Directory */
|
||||||
|
#define AM_ARC 0x20 /* Archive */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* FF_DEFINED */
|
288
source/fatfs/ffconf.h
Normal file
288
source/fatfs/ffconf.h
Normal file
@ -0,0 +1,288 @@
|
|||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ FatFs Functional Configurations
|
||||||
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define FFCONF_DEF 86604 /* Revision ID */
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Function Configurations
|
||||||
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define FF_FS_READONLY 1
|
||||||
|
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
|
||||||
|
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
|
||||||
|
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
|
||||||
|
/ and optional writing functions as well. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_MINIMIZE 1
|
||||||
|
/* This option defines minimization level to remove some basic API functions.
|
||||||
|
/
|
||||||
|
/ 0: Basic functions are fully enabled.
|
||||||
|
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
|
||||||
|
/ are removed.
|
||||||
|
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
|
||||||
|
/ 3: f_lseek() function is removed in addition to 2. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_STRFUNC 0
|
||||||
|
/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
|
||||||
|
/
|
||||||
|
/ 0: Disable string functions.
|
||||||
|
/ 1: Enable without LF-CRLF conversion.
|
||||||
|
/ 2: Enable with LF-CRLF conversion. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_FIND 2
|
||||||
|
/* This option switches filtered directory read functions, f_findfirst() and
|
||||||
|
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_MKFS 0
|
||||||
|
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_FASTSEEK 0
|
||||||
|
/* This option switches fast seek function. (0:Disable or 1:Enable) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_EXPAND 0
|
||||||
|
/* This option switches f_expand function. (0:Disable or 1:Enable) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_CHMOD 0
|
||||||
|
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
|
||||||
|
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_LABEL 0
|
||||||
|
/* This option switches volume label functions, f_getlabel() and f_setlabel().
|
||||||
|
/ (0:Disable or 1:Enable) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_FORWARD 0
|
||||||
|
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Locale and Namespace Configurations
|
||||||
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define FF_CODE_PAGE 932
|
||||||
|
/* This option specifies the OEM code page to be used on the target system.
|
||||||
|
/ Incorrect code page setting can cause a file open failure.
|
||||||
|
/
|
||||||
|
/ 437 - U.S.
|
||||||
|
/ 720 - Arabic
|
||||||
|
/ 737 - Greek
|
||||||
|
/ 771 - KBL
|
||||||
|
/ 775 - Baltic
|
||||||
|
/ 850 - Latin 1
|
||||||
|
/ 852 - Latin 2
|
||||||
|
/ 855 - Cyrillic
|
||||||
|
/ 857 - Turkish
|
||||||
|
/ 860 - Portuguese
|
||||||
|
/ 861 - Icelandic
|
||||||
|
/ 862 - Hebrew
|
||||||
|
/ 863 - Canadian French
|
||||||
|
/ 864 - Arabic
|
||||||
|
/ 865 - Nordic
|
||||||
|
/ 866 - Russian
|
||||||
|
/ 869 - Greek 2
|
||||||
|
/ 932 - Japanese (DBCS)
|
||||||
|
/ 936 - Simplified Chinese (DBCS)
|
||||||
|
/ 949 - Korean (DBCS)
|
||||||
|
/ 950 - Traditional Chinese (DBCS)
|
||||||
|
/ 0 - Include all code pages above and configured by f_setcp()
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_LFN 1
|
||||||
|
#define FF_MAX_LFN 255
|
||||||
|
/* The FF_USE_LFN switches the support for LFN (long file name).
|
||||||
|
/
|
||||||
|
/ 0: Disable LFN. FF_MAX_LFN has no effect.
|
||||||
|
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
|
||||||
|
/ 2: Enable LFN with dynamic working buffer on the STACK.
|
||||||
|
/ 3: Enable LFN with dynamic working buffer on the HEAP.
|
||||||
|
/
|
||||||
|
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
|
||||||
|
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
|
||||||
|
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
|
||||||
|
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
|
||||||
|
/ be in range of 12 to 255. It is recommended to be set 255 to fully support LFN
|
||||||
|
/ specification.
|
||||||
|
/ When use stack for the working buffer, take care on stack overflow. When use heap
|
||||||
|
/ memory for the working buffer, memory management functions, ff_memalloc() and
|
||||||
|
/ ff_memfree() in ffsystem.c, need to be added to the project. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_LFN_UNICODE 0
|
||||||
|
/* This option switches the character encoding on the API when LFN is enabled.
|
||||||
|
/
|
||||||
|
/ 0: ANSI/OEM in current CP (TCHAR = char)
|
||||||
|
/ 1: Unicode in UTF-16 (TCHAR = WCHAR)
|
||||||
|
/ 2: Unicode in UTF-8 (TCHAR = char)
|
||||||
|
/ 3: Unicode in UTF-32 (TCHAR = DWORD)
|
||||||
|
/
|
||||||
|
/ Also behavior of string I/O functions will be affected by this option.
|
||||||
|
/ When LFN is not enabled, this option has no effect. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_LFN_BUF 255
|
||||||
|
#define FF_SFN_BUF 12
|
||||||
|
/* This set of options defines size of file name members in the FILINFO structure
|
||||||
|
/ which is used to read out directory items. These values should be suffcient for
|
||||||
|
/ the file names to read. The maximum possible length of the read file name depends
|
||||||
|
/ on character encoding. When LFN is not enabled, these options have no effect. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_STRF_ENCODE 3
|
||||||
|
/* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
|
||||||
|
/ f_putc(), f_puts and f_printf() convert the character encoding in it.
|
||||||
|
/ This option selects assumption of character encoding ON THE FILE to be
|
||||||
|
/ read/written via those functions.
|
||||||
|
/
|
||||||
|
/ 0: ANSI/OEM in current CP
|
||||||
|
/ 1: Unicode in UTF-16LE
|
||||||
|
/ 2: Unicode in UTF-16BE
|
||||||
|
/ 3: Unicode in UTF-8
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_RPATH 1
|
||||||
|
/* This option configures support for relative path.
|
||||||
|
/
|
||||||
|
/ 0: Disable relative path and remove related functions.
|
||||||
|
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
|
||||||
|
/ 2: f_getcwd() function is available in addition to 1.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Drive/Volume Configurations
|
||||||
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define FF_VOLUMES 1
|
||||||
|
/* Number of volumes (logical drives) to be used. (1-10) */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_STR_VOLUME_ID 0
|
||||||
|
#define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
|
||||||
|
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
|
||||||
|
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
|
||||||
|
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
|
||||||
|
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid
|
||||||
|
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
|
||||||
|
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
|
||||||
|
/ not defined, a user defined volume string table needs to be defined as:
|
||||||
|
/
|
||||||
|
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_MULTI_PARTITION 0
|
||||||
|
/* This option switches support for multiple volumes on the physical drive.
|
||||||
|
/ By default (0), each logical drive number is bound to the same physical drive
|
||||||
|
/ number and only an FAT volume found on the physical drive will be mounted.
|
||||||
|
/ When this function is enabled (1), each logical drive number can be bound to
|
||||||
|
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
|
||||||
|
/ funciton will be available. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_MIN_SS 512
|
||||||
|
#define FF_MAX_SS 512
|
||||||
|
/* This set of options configures the range of sector size to be supported. (512,
|
||||||
|
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
|
||||||
|
/ harddisk. But a larger value may be required for on-board flash memory and some
|
||||||
|
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
|
||||||
|
/ for variable sector size mode and disk_ioctl() function needs to implement
|
||||||
|
/ GET_SECTOR_SIZE command. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_USE_TRIM 0
|
||||||
|
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
|
||||||
|
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
|
||||||
|
/ disk_ioctl() function. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_NOFSINFO 0
|
||||||
|
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
|
||||||
|
/ option, and f_getfree() function at first time after volume mount will force
|
||||||
|
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
|
||||||
|
/
|
||||||
|
/ bit0=0: Use free cluster count in the FSINFO if available.
|
||||||
|
/ bit0=1: Do not trust free cluster count in the FSINFO.
|
||||||
|
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
|
||||||
|
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ System Configurations
|
||||||
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define FF_FS_TINY 0
|
||||||
|
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
|
||||||
|
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
|
||||||
|
/ Instead of private sector buffer eliminated from the file object, common sector
|
||||||
|
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_EXFAT 0
|
||||||
|
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
|
||||||
|
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
|
||||||
|
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_NORTC 0
|
||||||
|
#define FF_NORTC_MON 1
|
||||||
|
#define FF_NORTC_MDAY 1
|
||||||
|
#define FF_NORTC_YEAR 2018
|
||||||
|
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
|
||||||
|
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
|
||||||
|
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
|
||||||
|
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
|
||||||
|
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
|
||||||
|
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
|
||||||
|
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
|
||||||
|
/ These options have no effect at read-only configuration (FF_FS_READONLY = 1). */
|
||||||
|
|
||||||
|
|
||||||
|
#define FF_FS_LOCK 0
|
||||||
|
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
|
||||||
|
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
|
||||||
|
/ is 1.
|
||||||
|
/
|
||||||
|
/ 0: Disable file lock function. To avoid volume corruption, application program
|
||||||
|
/ should avoid illegal open, remove and rename to the open objects.
|
||||||
|
/ >0: Enable file lock function. The value defines how many files/sub-directories
|
||||||
|
/ can be opened simultaneously under file lock control. Note that the file
|
||||||
|
/ lock control is independent of re-entrancy. */
|
||||||
|
|
||||||
|
|
||||||
|
/* #include <somertos.h> // O/S definitions */
|
||||||
|
#define FF_FS_REENTRANT 0
|
||||||
|
#define FF_FS_TIMEOUT 1000
|
||||||
|
#define FF_SYNC_t HANDLE
|
||||||
|
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
|
||||||
|
/ module itself. Note that regardless of this option, file access to different
|
||||||
|
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
|
||||||
|
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
|
||||||
|
/ to the same volume is under control of this function.
|
||||||
|
/
|
||||||
|
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
|
||||||
|
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
|
||||||
|
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
|
||||||
|
/ function, must be added to the project. Samples are available in
|
||||||
|
/ option/syscall.c.
|
||||||
|
/
|
||||||
|
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
|
||||||
|
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
|
||||||
|
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
|
||||||
|
/ included somewhere in the scope of ff.h. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--- End of configuration options ---*/
|
15597
source/fatfs/ffunicode.c
Normal file
15597
source/fatfs/ffunicode.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -23,8 +23,6 @@ extern "C" void userAppInit()
|
|||||||
{
|
{
|
||||||
plInitialize();
|
plInitialize();
|
||||||
pmdmntInitialize();
|
pmdmntInitialize();
|
||||||
pmshellInitialize();
|
|
||||||
splCryptoInitialize();
|
|
||||||
splInitialize();
|
splInitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,8 +30,6 @@ extern "C" void userAppExit()
|
|||||||
{
|
{
|
||||||
plExit();
|
plExit();
|
||||||
pmdmntExit();
|
pmdmntExit();
|
||||||
pmshellExit();
|
|
||||||
splCryptoExit();
|
|
||||||
splExit();
|
splExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +37,8 @@ int main(int argc, char **argv) {
|
|||||||
Common::intro();
|
Common::intro();
|
||||||
|
|
||||||
KeyCollection Keys;
|
KeyCollection Keys;
|
||||||
Common::wait_to_exit(Keys.get_keys());
|
Keys.get_keys();
|
||||||
|
Common::wait_to_exit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,2 +0,0 @@
|
|||||||
This package of mbed TLS is specifically licensed under the GPL 2.0,
|
|
||||||
as can be found in: gpl-2.0.txt
|
|
@ -1,218 +0,0 @@
|
|||||||
README for Mbed TLS
|
|
||||||
===================
|
|
||||||
|
|
||||||
Configuration
|
|
||||||
-------------
|
|
||||||
|
|
||||||
Mbed TLS should build out of the box on most systems. Some platform specific options are available in the fully documented configuration file `include/mbedtls/config.h`, which is also the place where features can be selected. This file can be edited manually, or in a more programmatic way using the Perl script `scripts/config.pl` (use `--help` for usage instructions).
|
|
||||||
|
|
||||||
Compiler options can be set using conventional environment variables such as `CC` and `CFLAGS` when using the Make and CMake build system (see below).
|
|
||||||
|
|
||||||
Compiling
|
|
||||||
---------
|
|
||||||
|
|
||||||
There are currently four active build systems used within Mbed TLS releases:
|
|
||||||
|
|
||||||
- yotta
|
|
||||||
- GNU Make
|
|
||||||
- CMake
|
|
||||||
- Microsoft Visual Studio (Microsoft Visual Studio 2010 or later)
|
|
||||||
|
|
||||||
The main systems used for development are CMake and GNU Make. Those systems are always complete and up-to-date. The others should reflect all changes present in the CMake and Make build system, although features may not be ported there automatically.
|
|
||||||
|
|
||||||
Yotta, as a build system, is slightly different from the other build systems:
|
|
||||||
|
|
||||||
- it provides a minimalistic configuration file by default
|
|
||||||
- depending on the yotta target, features of Mbed OS may be used in examples and tests
|
|
||||||
|
|
||||||
The Make and CMake build systems create three libraries: libmbedcrypto, libmbedx509, and libmbedtls. Note that libmbedtls depends on libmbedx509 and libmbedcrypto, and libmbedx509 depends on libmbedcrypto. As a result, some linkers will expect flags to be in a specific order, for example the GNU linker wants `-lmbedtls -lmbedx509 -lmbedcrypto`. Also, when loading shared libraries using dlopen(), you'll need to load libmbedcrypto first, then libmbedx509, before you can load libmbedtls.
|
|
||||||
|
|
||||||
### Yotta
|
|
||||||
|
|
||||||
[yotta](http://yottabuild.org) is a package manager and build system developed by Mbed, and is the build system of Mbed OS 16.03. To install it on your platform, please follow the yotta [installation instructions](http://docs.yottabuild.org/#installing).
|
|
||||||
|
|
||||||
Once yotta is installed, you can use it to download the latest version of Mbed TLS from the yotta registry with:
|
|
||||||
|
|
||||||
yotta install mbedtls
|
|
||||||
|
|
||||||
and build it with:
|
|
||||||
|
|
||||||
yotta build
|
|
||||||
|
|
||||||
If, on the other hand, you already have a copy of Mbed TLS from a source other than the yotta registry, for example from cloning our GitHub repository, or from downloading a tarball of the standalone edition, then you'll first need to generate the yotta module by running:
|
|
||||||
|
|
||||||
yotta/create-module.sh
|
|
||||||
|
|
||||||
This should be executed from the root Mbed TLS project directory. This will create the yotta module in the `yotta/module` directory within it. You can then change to that directory and build as usual:
|
|
||||||
|
|
||||||
cd yotta/module
|
|
||||||
yotta build
|
|
||||||
|
|
||||||
In any case, you'll probably want to set the yotta target before building unless it has already been set globally. For more information on using yotta, please consult the [yotta documentation](http://docs.yottabuild.org/).
|
|
||||||
|
|
||||||
For more details on the yotta/Mbed OS edition of Mbed TLS, including example programs, please consult the [Readme at the root of the yotta module](https://github.com/ARMmbed/mbedtls/blob/development/yotta/data/README.md).
|
|
||||||
|
|
||||||
### Make
|
|
||||||
|
|
||||||
We require GNU Make. To build the library and the sample programs, GNU Make and a C compiler are sufficient. Some of the more advanced build targets require some Unix/Linux tools.
|
|
||||||
|
|
||||||
We intentionally only use a minimum of functionality in the makefiles in order to keep them as simple and independent of different toolchains as possible, to allow users to more easily move between different platforms. Users who need more features are recommended to use CMake.
|
|
||||||
|
|
||||||
In order to build from the source code using GNU Make, just enter at the command line:
|
|
||||||
|
|
||||||
make
|
|
||||||
|
|
||||||
In order to run the tests, enter:
|
|
||||||
|
|
||||||
make check
|
|
||||||
|
|
||||||
The tests need Perl to be built and run. If you don't have Perl installed, you can skip building the tests with:
|
|
||||||
|
|
||||||
make no_test
|
|
||||||
|
|
||||||
You'll still be able to run a much smaller set of tests with:
|
|
||||||
|
|
||||||
programs/test/selftest
|
|
||||||
|
|
||||||
In order to build for a Windows platform, you should use `WINDOWS_BUILD=1` if the target is Windows but the build environment is Unix-like (for instance when cross-compiling, or compiling from an MSYS shell), and `WINDOWS=1` if the build environment is a Windows shell (for instance using mingw32-make) (in that case some targets will not be available).
|
|
||||||
|
|
||||||
Setting the variable `SHARED` in your environment will build shared libraries in addition to the static libraries. Setting `DEBUG` gives you a debug build. You can override `CFLAGS` and `LDFLAGS` by setting them in your environment or on the make command line; compiler warning options may be overridden separately using `WARNING_CFLAGS`. Some directory-specific options (for example, `-I` directives) are still preserved.
|
|
||||||
|
|
||||||
Please note that setting `CFLAGS` overrides its default value of `-O2` and setting `WARNING_CFLAGS` overrides its default value (starting with `-Wall -W`), so it you just want to add some warning options to the default ones, you can do so by setting `CFLAGS=-O2 -Werror` for example. Setting `WARNING_CFLAGS` is useful when you want to get rid of its default content (for example because your compiler doesn't accept `-Wall` as an option). Directory-specific options cannot be overriden from the command line.
|
|
||||||
|
|
||||||
Depending on your platform, you might run into some issues. Please check the Makefiles in `library/`, `programs/` and `tests/` for options to manually add or remove for specific platforms. You can also check [the Mbed TLS Knowledge Base](https://tls.mbed.org/kb) for articles on your platform or issue.
|
|
||||||
|
|
||||||
In case you find that you need to do something else as well, please let us know what, so we can add it to the [Mbed TLS Knowledge Base](https://tls.mbed.org/kb).
|
|
||||||
|
|
||||||
### CMake
|
|
||||||
|
|
||||||
In order to build the source using CMake in a separate directory (recommended), just enter at the command line:
|
|
||||||
|
|
||||||
mkdir /path/to/build_dir && cd /path/to/build_dir
|
|
||||||
cmake /path/to/mbedtls_source
|
|
||||||
make
|
|
||||||
|
|
||||||
In order to run the tests, enter:
|
|
||||||
|
|
||||||
make test
|
|
||||||
|
|
||||||
The test suites need Perl to be built. If you don't have Perl installed, you'll want to disable the test suites with:
|
|
||||||
|
|
||||||
cmake -DENABLE_TESTING=Off /path/to/mbedtls_source
|
|
||||||
|
|
||||||
If you disabled the test suites, but kept the programs enabled, you can still run a much smaller set of tests with:
|
|
||||||
|
|
||||||
programs/test/selftest
|
|
||||||
|
|
||||||
To configure CMake for building shared libraries, use:
|
|
||||||
|
|
||||||
cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On /path/to/mbedtls_source
|
|
||||||
|
|
||||||
There are many different build modes available within the CMake buildsystem. Most of them are available for gcc and clang, though some are compiler-specific:
|
|
||||||
|
|
||||||
- `Release`. This generates the default code without any unnecessary information in the binary files.
|
|
||||||
- `Debug`. This generates debug information and disables optimization of the code.
|
|
||||||
- `Coverage`. This generates code coverage information in addition to debug information.
|
|
||||||
- `ASan`. This instruments the code with AddressSanitizer to check for memory errors. (This includes LeakSanitizer, with recent version of gcc and clang.) (With recent version of clang, this mode also instruments the code with UndefinedSanitizer to check for undefined behaviour.)
|
|
||||||
- `ASanDbg`. Same as ASan but slower, with debug information and better stack traces.
|
|
||||||
- `MemSan`. This instruments the code with MemorySanitizer to check for uninitialised memory reads. Experimental, needs recent clang on Linux/x86\_64.
|
|
||||||
- `MemSanDbg`. Same as MemSan but slower, with debug information, better stack traces and origin tracking.
|
|
||||||
- `Check`. This activates the compiler warnings that depend on optimization and treats all warnings as errors.
|
|
||||||
|
|
||||||
Switching build modes in CMake is simple. For debug mode, enter at the command line:
|
|
||||||
|
|
||||||
cmake -D CMAKE_BUILD_TYPE=Debug /path/to/mbedtls_source
|
|
||||||
|
|
||||||
To list other available CMake options, use:
|
|
||||||
|
|
||||||
cmake -LH
|
|
||||||
|
|
||||||
Note that, with CMake, you can't adjust the compiler or its flags after the
|
|
||||||
initial invocation of cmake. This means that `CC=your_cc make` and `make
|
|
||||||
CC=your_cc` will *not* work (similarly with `CFLAGS` and other variables).
|
|
||||||
These variables need to be adjusted when invoking cmake for the first time,
|
|
||||||
for example:
|
|
||||||
|
|
||||||
CC=your_cc cmake /path/to/mbedtls_source
|
|
||||||
|
|
||||||
If you already invoked cmake and want to change those settings, you need to
|
|
||||||
remove the build directory and create it again.
|
|
||||||
|
|
||||||
Note that it is possible to build in-place; this will however overwrite the
|
|
||||||
provided Makefiles (see `scripts/tmp_ignore_makefiles.sh` if you want to
|
|
||||||
prevent `git status` from showing them as modified). In order to do so, from
|
|
||||||
the Mbed TLS source directory, use:
|
|
||||||
|
|
||||||
cmake .
|
|
||||||
make
|
|
||||||
|
|
||||||
If you want to change `CC` or `CFLAGS` afterwards, you will need to remove the
|
|
||||||
CMake cache. This can be done with the following command using GNU find:
|
|
||||||
|
|
||||||
find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} +
|
|
||||||
|
|
||||||
You can now make the desired change:
|
|
||||||
|
|
||||||
CC=your_cc cmake .
|
|
||||||
make
|
|
||||||
|
|
||||||
Regarding variables, also note that if you set CFLAGS when invoking cmake,
|
|
||||||
your value of CFLAGS doesn't override the content provided by cmake (depending
|
|
||||||
on the build mode as seen above), it's merely prepended to it.
|
|
||||||
|
|
||||||
### Microsoft Visual Studio
|
|
||||||
|
|
||||||
The build files for Microsoft Visual Studio are generated for Visual Studio 2010.
|
|
||||||
|
|
||||||
The solution file `mbedTLS.sln` contains all the basic projects needed to build the library and all the programs. The files in tests are not generated and compiled, as these need a perl environment as well. However, the selftest program in `programs/test/` is still available.
|
|
||||||
|
|
||||||
Example programs
|
|
||||||
----------------
|
|
||||||
|
|
||||||
We've included example programs for a lot of different features and uses in `programs/`. Most programs only focus on a single feature or usage scenario, so keep that in mind when copying parts of the code.
|
|
||||||
|
|
||||||
Tests
|
|
||||||
-----
|
|
||||||
|
|
||||||
Mbed TLS includes an elaborate test suite in `tests/` that initially requires Perl to generate the tests files (e.g. `test\_suite\_mpi.c`). These files are generated from a `function file` (e.g. `suites/test\_suite\_mpi.function`) and a `data file` (e.g. `suites/test\_suite\_mpi.data`). The `function file` contains the test functions. The `data file` contains the test cases, specified as parameters that will be passed to the test function.
|
|
||||||
|
|
||||||
For machines with a Unix shell and OpenSSL (and optionally GnuTLS) installed, additional test scripts are available:
|
|
||||||
|
|
||||||
- `tests/ssl-opt.sh` runs integration tests for various TLS options (renegotiation, resumption, etc.) and tests interoperability of these options with other implementations.
|
|
||||||
- `tests/compat.sh` tests interoperability of every ciphersuite with other implementations.
|
|
||||||
- `tests/scripts/test-ref-configs.pl` test builds in various reduced configurations.
|
|
||||||
- `tests/scripts/key-exchanges.pl` test builds in configurations with a single key exchange enabled
|
|
||||||
- `tests/scripts/all.sh` runs a combination of the above tests, plus some more, with various build options (such as ASan, full `config.h`, etc).
|
|
||||||
|
|
||||||
Configurations
|
|
||||||
--------------
|
|
||||||
|
|
||||||
We provide some non-standard configurations focused on specific use cases in the `configs/` directory. You can read more about those in `configs/README.txt`
|
|
||||||
|
|
||||||
Porting Mbed TLS
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Mbed TLS can be ported to many different architectures, OS's and platforms. Before starting a port, you may find the following Knowledge Base articles useful:
|
|
||||||
|
|
||||||
- [Porting Mbed TLS to a new environment or OS](https://tls.mbed.org/kb/how-to/how-do-i-port-mbed-tls-to-a-new-environment-OS)
|
|
||||||
- [What external dependencies does Mbed TLS rely on?](https://tls.mbed.org/kb/development/what-external-dependencies-does-mbedtls-rely-on)
|
|
||||||
- [How do I configure Mbed TLS](https://tls.mbed.org/kb/compiling-and-building/how-do-i-configure-mbedtls)
|
|
||||||
|
|
||||||
Contributing
|
|
||||||
------------
|
|
||||||
|
|
||||||
We gratefully accept bug reports and contributions from the community. There are some requirements we need to fulfill in order to be able to integrate contributions:
|
|
||||||
|
|
||||||
- All contributions, whether large or small require a Contributor's License Agreement (CLA) to be accepted. This is because source code can possibly fall under copyright law and we need your consent to share in the ownership of the copyright.
|
|
||||||
- We would ask that contributions conform to [our coding standards](https://tls.mbed.org/kb/development/mbedtls-coding-standards), and that contributions should be fully tested before submission.
|
|
||||||
- As with any open source project, contributions will be reviewed by the project team and community and may need some modifications to be accepted.
|
|
||||||
|
|
||||||
To accept the Contributor’s Licence Agreement (CLA), individual contributors can do this by creating an Mbed account and [accepting the online agreement here with a click through](https://os.mbed.com/contributor_agreement/). Alternatively, for contributions from corporations, or those that do not wish to create an Mbed account, a slightly different agreement can be found [here](https://www.mbed.com/en/about-mbed/contributor-license-agreements/). This agreement should be signed and returned to Arm as described in the instructions given.
|
|
||||||
|
|
||||||
### Making a Contribution
|
|
||||||
|
|
||||||
1. [Check for open issues](https://github.com/ARMmbed/mbedtls/issues) or [start a discussion](https://forums.mbed.com/c/mbed-tls) around a feature idea or a bug.
|
|
||||||
2. Fork the [Mbed TLS repository on GitHub](https://github.com/ARMmbed/mbedtls) to start making your changes. As a general rule, you should use the "development" branch as a basis.
|
|
||||||
3. Write a test which shows that the bug was fixed or that the feature works as expected.
|
|
||||||
4. Send a pull request and bug us until it gets merged and published. Contributions may need some modifications, so work with us to get your change accepted. We will include your name in the ChangeLog :)
|
|
||||||
|
|
@ -1,339 +0,0 @@
|
|||||||
GNU GENERAL PUBLIC LICENSE
|
|
||||||
Version 2, June 1991
|
|
||||||
|
|
||||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
|
||||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
|
||||||
of this license document, but changing it is not allowed.
|
|
||||||
|
|
||||||
Preamble
|
|
||||||
|
|
||||||
The licenses for most software are designed to take away your
|
|
||||||
freedom to share and change it. By contrast, the GNU General Public
|
|
||||||
License is intended to guarantee your freedom to share and change free
|
|
||||||
software--to make sure the software is free for all its users. This
|
|
||||||
General Public License applies to most of the Free Software
|
|
||||||
Foundation's software and to any other program whose authors commit to
|
|
||||||
using it. (Some other Free Software Foundation software is covered by
|
|
||||||
the GNU Lesser General Public License instead.) You can apply it to
|
|
||||||
your programs, too.
|
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
|
||||||
price. Our General Public Licenses are designed to make sure that you
|
|
||||||
have the freedom to distribute copies of free software (and charge for
|
|
||||||
this service if you wish), that you receive source code or can get it
|
|
||||||
if you want it, that you can change the software or use pieces of it
|
|
||||||
in new free programs; and that you know you can do these things.
|
|
||||||
|
|
||||||
To protect your rights, we need to make restrictions that forbid
|
|
||||||
anyone to deny you these rights or to ask you to surrender the rights.
|
|
||||||
These restrictions translate to certain responsibilities for you if you
|
|
||||||
distribute copies of the software, or if you modify it.
|
|
||||||
|
|
||||||
For example, if you distribute copies of such a program, whether
|
|
||||||
gratis or for a fee, you must give the recipients all the rights that
|
|
||||||
you have. You must make sure that they, too, receive or can get the
|
|
||||||
source code. And you must show them these terms so they know their
|
|
||||||
rights.
|
|
||||||
|
|
||||||
We protect your rights with two steps: (1) copyright the software, and
|
|
||||||
(2) offer you this license which gives you legal permission to copy,
|
|
||||||
distribute and/or modify the software.
|
|
||||||
|
|
||||||
Also, for each author's protection and ours, we want to make certain
|
|
||||||
that everyone understands that there is no warranty for this free
|
|
||||||
software. If the software is modified by someone else and passed on, we
|
|
||||||
want its recipients to know that what they have is not the original, so
|
|
||||||
that any problems introduced by others will not reflect on the original
|
|
||||||
authors' reputations.
|
|
||||||
|
|
||||||
Finally, any free program is threatened constantly by software
|
|
||||||
patents. We wish to avoid the danger that redistributors of a free
|
|
||||||
program will individually obtain patent licenses, in effect making the
|
|
||||||
program proprietary. To prevent this, we have made it clear that any
|
|
||||||
patent must be licensed for everyone's free use or not licensed at all.
|
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
|
||||||
modification follow.
|
|
||||||
|
|
||||||
GNU GENERAL PUBLIC LICENSE
|
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
||||||
|
|
||||||
0. This License applies to any program or other work which contains
|
|
||||||
a notice placed by the copyright holder saying it may be distributed
|
|
||||||
under the terms of this General Public License. The "Program", below,
|
|
||||||
refers to any such program or work, and a "work based on the Program"
|
|
||||||
means either the Program or any derivative work under copyright law:
|
|
||||||
that is to say, a work containing the Program or a portion of it,
|
|
||||||
either verbatim or with modifications and/or translated into another
|
|
||||||
language. (Hereinafter, translation is included without limitation in
|
|
||||||
the term "modification".) Each licensee is addressed as "you".
|
|
||||||
|
|
||||||
Activities other than copying, distribution and modification are not
|
|
||||||
covered by this License; they are outside its scope. The act of
|
|
||||||
running the Program is not restricted, and the output from the Program
|
|
||||||
is covered only if its contents constitute a work based on the
|
|
||||||
Program (independent of having been made by running the Program).
|
|
||||||
Whether that is true depends on what the Program does.
|
|
||||||
|
|
||||||
1. You may copy and distribute verbatim copies of the Program's
|
|
||||||
source code as you receive it, in any medium, provided that you
|
|
||||||
conspicuously and appropriately publish on each copy an appropriate
|
|
||||||
copyright notice and disclaimer of warranty; keep intact all the
|
|
||||||
notices that refer to this License and to the absence of any warranty;
|
|
||||||
and give any other recipients of the Program a copy of this License
|
|
||||||
along with the Program.
|
|
||||||
|
|
||||||
You may charge a fee for the physical act of transferring a copy, and
|
|
||||||
you may at your option offer warranty protection in exchange for a fee.
|
|
||||||
|
|
||||||
2. You may modify your copy or copies of the Program or any portion
|
|
||||||
of it, thus forming a work based on the Program, and copy and
|
|
||||||
distribute such modifications or work under the terms of Section 1
|
|
||||||
above, provided that you also meet all of these conditions:
|
|
||||||
|
|
||||||
a) You must cause the modified files to carry prominent notices
|
|
||||||
stating that you changed the files and the date of any change.
|
|
||||||
|
|
||||||
b) You must cause any work that you distribute or publish, that in
|
|
||||||
whole or in part contains or is derived from the Program or any
|
|
||||||
part thereof, to be licensed as a whole at no charge to all third
|
|
||||||
parties under the terms of this License.
|
|
||||||
|
|
||||||
c) If the modified program normally reads commands interactively
|
|
||||||
when run, you must cause it, when started running for such
|
|
||||||
interactive use in the most ordinary way, to print or display an
|
|
||||||
announcement including an appropriate copyright notice and a
|
|
||||||
notice that there is no warranty (or else, saying that you provide
|
|
||||||
a warranty) and that users may redistribute the program under
|
|
||||||
these conditions, and telling the user how to view a copy of this
|
|
||||||
License. (Exception: if the Program itself is interactive but
|
|
||||||
does not normally print such an announcement, your work based on
|
|
||||||
the Program is not required to print an announcement.)
|
|
||||||
|
|
||||||
These requirements apply to the modified work as a whole. If
|
|
||||||
identifiable sections of that work are not derived from the Program,
|
|
||||||
and can be reasonably considered independent and separate works in
|
|
||||||
themselves, then this License, and its terms, do not apply to those
|
|
||||||
sections when you distribute them as separate works. But when you
|
|
||||||
distribute the same sections as part of a whole which is a work based
|
|
||||||
on the Program, the distribution of the whole must be on the terms of
|
|
||||||
this License, whose permissions for other licensees extend to the
|
|
||||||
entire whole, and thus to each and every part regardless of who wrote it.
|
|
||||||
|
|
||||||
Thus, it is not the intent of this section to claim rights or contest
|
|
||||||
your rights to work written entirely by you; rather, the intent is to
|
|
||||||
exercise the right to control the distribution of derivative or
|
|
||||||
collective works based on the Program.
|
|
||||||
|
|
||||||
In addition, mere aggregation of another work not based on the Program
|
|
||||||
with the Program (or with a work based on the Program) on a volume of
|
|
||||||
a storage or distribution medium does not bring the other work under
|
|
||||||
the scope of this License.
|
|
||||||
|
|
||||||
3. You may copy and distribute the Program (or a work based on it,
|
|
||||||
under Section 2) in object code or executable form under the terms of
|
|
||||||
Sections 1 and 2 above provided that you also do one of the following:
|
|
||||||
|
|
||||||
a) Accompany it with the complete corresponding machine-readable
|
|
||||||
source code, which must be distributed under the terms of Sections
|
|
||||||
1 and 2 above on a medium customarily used for software interchange; or,
|
|
||||||
|
|
||||||
b) Accompany it with a written offer, valid for at least three
|
|
||||||
years, to give any third party, for a charge no more than your
|
|
||||||
cost of physically performing source distribution, a complete
|
|
||||||
machine-readable copy of the corresponding source code, to be
|
|
||||||
distributed under the terms of Sections 1 and 2 above on a medium
|
|
||||||
customarily used for software interchange; or,
|
|
||||||
|
|
||||||
c) Accompany it with the information you received as to the offer
|
|
||||||
to distribute corresponding source code. (This alternative is
|
|
||||||
allowed only for noncommercial distribution and only if you
|
|
||||||
received the program in object code or executable form with such
|
|
||||||
an offer, in accord with Subsection b above.)
|
|
||||||
|
|
||||||
The source code for a work means the preferred form of the work for
|
|
||||||
making modifications to it. For an executable work, complete source
|
|
||||||
code means all the source code for all modules it contains, plus any
|
|
||||||
associated interface definition files, plus the scripts used to
|
|
||||||
control compilation and installation of the executable. However, as a
|
|
||||||
special exception, the source code distributed need not include
|
|
||||||
anything that is normally distributed (in either source or binary
|
|
||||||
form) with the major components (compiler, kernel, and so on) of the
|
|
||||||
operating system on which the executable runs, unless that component
|
|
||||||
itself accompanies the executable.
|
|
||||||
|
|
||||||
If distribution of executable or object code is made by offering
|
|
||||||
access to copy from a designated place, then offering equivalent
|
|
||||||
access to copy the source code from the same place counts as
|
|
||||||
distribution of the source code, even though third parties are not
|
|
||||||
compelled to copy the source along with the object code.
|
|
||||||
|
|
||||||
4. You may not copy, modify, sublicense, or distribute the Program
|
|
||||||
except as expressly provided under this License. Any attempt
|
|
||||||
otherwise to copy, modify, sublicense or distribute the Program is
|
|
||||||
void, and will automatically terminate your rights under this License.
|
|
||||||
However, parties who have received copies, or rights, from you under
|
|
||||||
this License will not have their licenses terminated so long as such
|
|
||||||
parties remain in full compliance.
|
|
||||||
|
|
||||||
5. You are not required to accept this License, since you have not
|
|
||||||
signed it. However, nothing else grants you permission to modify or
|
|
||||||
distribute the Program or its derivative works. These actions are
|
|
||||||
prohibited by law if you do not accept this License. Therefore, by
|
|
||||||
modifying or distributing the Program (or any work based on the
|
|
||||||
Program), you indicate your acceptance of this License to do so, and
|
|
||||||
all its terms and conditions for copying, distributing or modifying
|
|
||||||
the Program or works based on it.
|
|
||||||
|
|
||||||
6. Each time you redistribute the Program (or any work based on the
|
|
||||||
Program), the recipient automatically receives a license from the
|
|
||||||
original licensor to copy, distribute or modify the Program subject to
|
|
||||||
these terms and conditions. You may not impose any further
|
|
||||||
restrictions on the recipients' exercise of the rights granted herein.
|
|
||||||
You are not responsible for enforcing compliance by third parties to
|
|
||||||
this License.
|
|
||||||
|
|
||||||
7. If, as a consequence of a court judgment or allegation of patent
|
|
||||||
infringement or for any other reason (not limited to patent issues),
|
|
||||||
conditions are imposed on you (whether by court order, agreement or
|
|
||||||
otherwise) that contradict the conditions of this License, they do not
|
|
||||||
excuse you from the conditions of this License. If you cannot
|
|
||||||
distribute so as to satisfy simultaneously your obligations under this
|
|
||||||
License and any other pertinent obligations, then as a consequence you
|
|
||||||
may not distribute the Program at all. For example, if a patent
|
|
||||||
license would not permit royalty-free redistribution of the Program by
|
|
||||||
all those who receive copies directly or indirectly through you, then
|
|
||||||
the only way you could satisfy both it and this License would be to
|
|
||||||
refrain entirely from distribution of the Program.
|
|
||||||
|
|
||||||
If any portion of this section is held invalid or unenforceable under
|
|
||||||
any particular circumstance, the balance of the section is intended to
|
|
||||||
apply and the section as a whole is intended to apply in other
|
|
||||||
circumstances.
|
|
||||||
|
|
||||||
It is not the purpose of this section to induce you to infringe any
|
|
||||||
patents or other property right claims or to contest validity of any
|
|
||||||
such claims; this section has the sole purpose of protecting the
|
|
||||||
integrity of the free software distribution system, which is
|
|
||||||
implemented by public license practices. Many people have made
|
|
||||||
generous contributions to the wide range of software distributed
|
|
||||||
through that system in reliance on consistent application of that
|
|
||||||
system; it is up to the author/donor to decide if he or she is willing
|
|
||||||
to distribute software through any other system and a licensee cannot
|
|
||||||
impose that choice.
|
|
||||||
|
|
||||||
This section is intended to make thoroughly clear what is believed to
|
|
||||||
be a consequence of the rest of this License.
|
|
||||||
|
|
||||||
8. If the distribution and/or use of the Program is restricted in
|
|
||||||
certain countries either by patents or by copyrighted interfaces, the
|
|
||||||
original copyright holder who places the Program under this License
|
|
||||||
may add an explicit geographical distribution limitation excluding
|
|
||||||
those countries, so that distribution is permitted only in or among
|
|
||||||
countries not thus excluded. In such case, this License incorporates
|
|
||||||
the limitation as if written in the body of this License.
|
|
||||||
|
|
||||||
9. The Free Software Foundation may publish revised and/or new versions
|
|
||||||
of the General Public License from time to time. Such new versions will
|
|
||||||
be similar in spirit to the present version, but may differ in detail to
|
|
||||||
address new problems or concerns.
|
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the Program
|
|
||||||
specifies a version number of this License which applies to it and "any
|
|
||||||
later version", you have the option of following the terms and conditions
|
|
||||||
either of that version or of any later version published by the Free
|
|
||||||
Software Foundation. If the Program does not specify a version number of
|
|
||||||
this License, you may choose any version ever published by the Free Software
|
|
||||||
Foundation.
|
|
||||||
|
|
||||||
10. If you wish to incorporate parts of the Program into other free
|
|
||||||
programs whose distribution conditions are different, write to the author
|
|
||||||
to ask for permission. For software which is copyrighted by the Free
|
|
||||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
|
||||||
make exceptions for this. Our decision will be guided by the two goals
|
|
||||||
of preserving the free status of all derivatives of our free software and
|
|
||||||
of promoting the sharing and reuse of software generally.
|
|
||||||
|
|
||||||
NO WARRANTY
|
|
||||||
|
|
||||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
|
||||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
|
||||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
|
||||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
|
||||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
|
||||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
|
||||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
|
||||||
REPAIR OR CORRECTION.
|
|
||||||
|
|
||||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
|
||||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
|
||||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
|
||||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
|
||||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
|
||||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
|
||||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
|
||||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
|
||||||
POSSIBILITY OF SUCH DAMAGES.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
How to Apply These Terms to Your New Programs
|
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest
|
|
||||||
possible use to the public, the best way to achieve this is to make it
|
|
||||||
free software which everyone can redistribute and change under these terms.
|
|
||||||
|
|
||||||
To do so, attach the following notices to the program. It is safest
|
|
||||||
to attach them to the start of each source file to most effectively
|
|
||||||
convey the exclusion of warranty; and each file should have at least
|
|
||||||
the "copyright" line and a pointer to where the full notice is found.
|
|
||||||
|
|
||||||
<one line to give the program's name and a brief idea of what it does.>
|
|
||||||
Copyright (C) <year> <name of author>
|
|
||||||
|
|
||||||
This program 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 2 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program 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-1301 USA.
|
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
|
||||||
|
|
||||||
If the program is interactive, make it output a short notice like this
|
|
||||||
when it starts in an interactive mode:
|
|
||||||
|
|
||||||
Gnomovision version 69, Copyright (C) year name of author
|
|
||||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
|
||||||
This is free software, and you are welcome to redistribute it
|
|
||||||
under certain conditions; type `show c' for details.
|
|
||||||
|
|
||||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
|
||||||
parts of the General Public License. Of course, the commands you use may
|
|
||||||
be called something other than `show w' and `show c'; they could even be
|
|
||||||
mouse-clicks or menu items--whatever suits your program.
|
|
||||||
|
|
||||||
You should also get your employer (if you work as a programmer) or your
|
|
||||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
|
||||||
necessary. Here is a sample; alter the names:
|
|
||||||
|
|
||||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
|
||||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
|
||||||
|
|
||||||
<signature of Ty Coon>, 1 April 1989
|
|
||||||
Ty Coon, President of Vice
|
|
||||||
|
|
||||||
This General Public License does not permit incorporating your program into
|
|
||||||
proprietary programs. If your program is a subroutine library, you may
|
|
||||||
consider it more useful to permit linking proprietary applications with the
|
|
||||||
library. If this is what you want to do, use the GNU Lesser General
|
|
||||||
Public License instead of this License.
|
|
@ -1,16 +0,0 @@
|
|||||||
option(INSTALL_MBEDTLS_HEADERS "Install mbed TLS headers." ON)
|
|
||||||
|
|
||||||
if(INSTALL_MBEDTLS_HEADERS)
|
|
||||||
|
|
||||||
file(GLOB headers "mbedtls/*.h")
|
|
||||||
|
|
||||||
install(FILES ${headers}
|
|
||||||
DESTINATION include/mbedtls
|
|
||||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
|
||||||
|
|
||||||
endif(INSTALL_MBEDTLS_HEADERS)
|
|
||||||
|
|
||||||
# Make config.h available in an out-of-source build. ssl-opt.sh requires it.
|
|
||||||
if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
link_to_source(mbedtls)
|
|
||||||
endif()
|
|
@ -1,628 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file aes.h
|
|
||||||
*
|
|
||||||
* \brief This file contains AES definitions and functions.
|
|
||||||
*
|
|
||||||
* The Advanced Encryption Standard (AES) specifies a FIPS-approved
|
|
||||||
* cryptographic algorithm that can be used to protect electronic
|
|
||||||
* data.
|
|
||||||
*
|
|
||||||
* The AES algorithm is a symmetric block cipher that can
|
|
||||||
* encrypt and decrypt information. For more information, see
|
|
||||||
* <em>FIPS Publication 197: Advanced Encryption Standard</em> and
|
|
||||||
* <em>ISO/IEC 18033-2:2006: Information technology -- Security
|
|
||||||
* techniques -- Encryption algorithms -- Part 2: Asymmetric
|
|
||||||
* ciphers</em>.
|
|
||||||
*
|
|
||||||
* The AES-XTS block mode is standardized by NIST SP 800-38E
|
|
||||||
* <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
|
|
||||||
* and described in detail by IEEE P1619
|
|
||||||
* <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef MBEDTLS_AES_H
|
|
||||||
#define MBEDTLS_AES_H
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/* padlock.c and aesni.c rely on these values! */
|
|
||||||
#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
|
|
||||||
#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
|
|
||||||
|
|
||||||
/* Error codes in range 0x0020-0x0022 */
|
|
||||||
#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
|
|
||||||
#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
|
|
||||||
|
|
||||||
/* Error codes in range 0x0021-0x0025 */
|
|
||||||
#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
|
|
||||||
#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
|
|
||||||
#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
|
|
||||||
|
|
||||||
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
|
||||||
!defined(inline) && !defined(__cplusplus)
|
|
||||||
#define inline __inline
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_AES_ALT)
|
|
||||||
// Regular implementation
|
|
||||||
//
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The AES context-type definition.
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_aes_context
|
|
||||||
{
|
|
||||||
int nr; /*!< The number of rounds. */
|
|
||||||
uint32_t *rk; /*!< AES round keys. */
|
|
||||||
uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
|
|
||||||
hold 32 extra Bytes, which can be used for
|
|
||||||
one of the following purposes:
|
|
||||||
<ul><li>Alignment if VIA padlock is
|
|
||||||
used.</li>
|
|
||||||
<li>Simplifying key expansion in the 256-bit
|
|
||||||
case by generating an extra round key.
|
|
||||||
</li></ul> */
|
|
||||||
}
|
|
||||||
mbedtls_aes_context;
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
||||||
/**
|
|
||||||
* \brief The AES XTS context-type definition.
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_aes_xts_context
|
|
||||||
{
|
|
||||||
mbedtls_aes_context crypt; /*!< The AES context to use for AES block
|
|
||||||
encryption or decryption. */
|
|
||||||
mbedtls_aes_context tweak; /*!< The AES context used for tweak
|
|
||||||
computation. */
|
|
||||||
} mbedtls_aes_xts_context;
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
|
||||||
|
|
||||||
#else /* MBEDTLS_AES_ALT */
|
|
||||||
#include "aes_alt.h"
|
|
||||||
#endif /* MBEDTLS_AES_ALT */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function initializes the specified AES context.
|
|
||||||
*
|
|
||||||
* It must be the first API called before using
|
|
||||||
* the context.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to initialize.
|
|
||||||
*/
|
|
||||||
void mbedtls_aes_init( mbedtls_aes_context *ctx );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function releases and clears the specified AES context.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to clear.
|
|
||||||
*/
|
|
||||||
void mbedtls_aes_free( mbedtls_aes_context *ctx );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
||||||
/**
|
|
||||||
* \brief This function initializes the specified AES XTS context.
|
|
||||||
*
|
|
||||||
* It must be the first API called before using
|
|
||||||
* the context.
|
|
||||||
*
|
|
||||||
* \param ctx The AES XTS context to initialize.
|
|
||||||
*/
|
|
||||||
void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function releases and clears the specified AES XTS context.
|
|
||||||
*
|
|
||||||
* \param ctx The AES XTS context to clear.
|
|
||||||
*/
|
|
||||||
void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets the encryption key.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to which the key should be bound.
|
|
||||||
* \param key The encryption key.
|
|
||||||
* \param keybits The size of data passed in bits. Valid options are:
|
|
||||||
* <ul><li>128 bits</li>
|
|
||||||
* <li>192 bits</li>
|
|
||||||
* <li>256 bits</li></ul>
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
|
|
||||||
unsigned int keybits );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets the decryption key.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to which the key should be bound.
|
|
||||||
* \param key The decryption key.
|
|
||||||
* \param keybits The size of data passed. Valid options are:
|
|
||||||
* <ul><li>128 bits</li>
|
|
||||||
* <li>192 bits</li>
|
|
||||||
* <li>256 bits</li></ul>
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
|
|
||||||
unsigned int keybits );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
||||||
/**
|
|
||||||
* \brief This function prepares an XTS context for encryption and
|
|
||||||
* sets the encryption key.
|
|
||||||
*
|
|
||||||
* \param ctx The AES XTS context to which the key should be bound.
|
|
||||||
* \param key The encryption key. This is comprised of the XTS key1
|
|
||||||
* concatenated with the XTS key2.
|
|
||||||
* \param keybits The size of \p key passed in bits. Valid options are:
|
|
||||||
* <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
|
|
||||||
* <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
|
|
||||||
const unsigned char *key,
|
|
||||||
unsigned int keybits );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function prepares an XTS context for decryption and
|
|
||||||
* sets the decryption key.
|
|
||||||
*
|
|
||||||
* \param ctx The AES XTS context to which the key should be bound.
|
|
||||||
* \param key The decryption key. This is comprised of the XTS key1
|
|
||||||
* concatenated with the XTS key2.
|
|
||||||
* \param keybits The size of \p key passed in bits. Valid options are:
|
|
||||||
* <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
|
|
||||||
* <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
|
|
||||||
const unsigned char *key,
|
|
||||||
unsigned int keybits );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES single-block encryption or
|
|
||||||
* decryption operation.
|
|
||||||
*
|
|
||||||
* It performs the operation defined in the \p mode parameter
|
|
||||||
* (encrypt or decrypt), on the input data buffer defined in
|
|
||||||
* the \p input parameter.
|
|
||||||
*
|
|
||||||
* mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
|
|
||||||
* mbedtls_aes_setkey_dec() must be called before the first
|
|
||||||
* call to this API with the same context.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
|
|
||||||
* #MBEDTLS_AES_DECRYPT.
|
|
||||||
* \param input The 16-Byte buffer holding the input data.
|
|
||||||
* \param output The 16-Byte buffer holding the output data.
|
|
||||||
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
|
|
||||||
int mode,
|
|
||||||
const unsigned char input[16],
|
|
||||||
unsigned char output[16] );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-CBC encryption or decryption operation
|
|
||||||
* on full blocks.
|
|
||||||
*
|
|
||||||
* It performs the operation defined in the \p mode
|
|
||||||
* parameter (encrypt/decrypt), on the input data buffer defined in
|
|
||||||
* the \p input parameter.
|
|
||||||
*
|
|
||||||
* It can be called as many times as needed, until all the input
|
|
||||||
* data is processed. mbedtls_aes_init(), and either
|
|
||||||
* mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
|
|
||||||
* before the first call to this API with the same context.
|
|
||||||
*
|
|
||||||
* \note This function operates on aligned blocks, that is, the input size
|
|
||||||
* must be a multiple of the AES block size of 16 Bytes.
|
|
||||||
*
|
|
||||||
* \note Upon exit, the content of the IV is updated so that you can
|
|
||||||
* call the same function again on the next
|
|
||||||
* block(s) of data and get the same result as if it was
|
|
||||||
* encrypted in one call. This allows a "streaming" usage.
|
|
||||||
* If you need to retain the contents of the IV, you should
|
|
||||||
* either save it manually or use the cipher module instead.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
|
|
||||||
* #MBEDTLS_AES_DECRYPT.
|
|
||||||
* \param length The length of the input data in Bytes. This must be a
|
|
||||||
* multiple of the block size (16 Bytes).
|
|
||||||
* \param iv Initialization vector (updated after use).
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param output The buffer holding the output data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
|
|
||||||
* on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
|
|
||||||
int mode,
|
|
||||||
size_t length,
|
|
||||||
unsigned char iv[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-XTS encryption or decryption
|
|
||||||
* operation for an entire XTS data unit.
|
|
||||||
*
|
|
||||||
* AES-XTS encrypts or decrypts blocks based on their location as
|
|
||||||
* defined by a data unit number. The data unit number must be
|
|
||||||
* provided by \p data_unit.
|
|
||||||
*
|
|
||||||
* NIST SP 800-38E limits the maximum size of a data unit to 2^20
|
|
||||||
* AES blocks. If the data unit is larger than this, this function
|
|
||||||
* returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
|
|
||||||
*
|
|
||||||
* \param ctx The AES XTS context to use for AES XTS operations.
|
|
||||||
* \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
|
|
||||||
* #MBEDTLS_AES_DECRYPT.
|
|
||||||
* \param length The length of a data unit in bytes. This can be any
|
|
||||||
* length between 16 bytes and 2^24 bytes inclusive
|
|
||||||
* (between 1 and 2^20 block cipher blocks).
|
|
||||||
* \param data_unit The address of the data unit encoded as an array of 16
|
|
||||||
* bytes in little-endian format. For disk encryption, this
|
|
||||||
* is typically the index of the block device sector that
|
|
||||||
* contains the data.
|
|
||||||
* \param input The buffer holding the input data (which is an entire
|
|
||||||
* data unit). This function reads \p length bytes from \p
|
|
||||||
* input.
|
|
||||||
* \param output The buffer holding the output data (which is an entire
|
|
||||||
* data unit). This function writes \p length bytes to \p
|
|
||||||
* output.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
|
|
||||||
* smaller than an AES block in size (16 bytes) or if \p
|
|
||||||
* length is larger than 2^20 blocks (16 MiB).
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
|
|
||||||
int mode,
|
|
||||||
size_t length,
|
|
||||||
const unsigned char data_unit[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-CFB128 encryption or decryption
|
|
||||||
* operation.
|
|
||||||
*
|
|
||||||
* It performs the operation defined in the \p mode
|
|
||||||
* parameter (encrypt or decrypt), on the input data buffer
|
|
||||||
* defined in the \p input parameter.
|
|
||||||
*
|
|
||||||
* For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
|
|
||||||
* regardless of whether you are performing an encryption or decryption
|
|
||||||
* operation, that is, regardless of the \p mode parameter. This is
|
|
||||||
* because CFB mode uses the same key schedule for encryption and
|
|
||||||
* decryption.
|
|
||||||
*
|
|
||||||
* \note Upon exit, the content of the IV is updated so that you can
|
|
||||||
* call the same function again on the next
|
|
||||||
* block(s) of data and get the same result as if it was
|
|
||||||
* encrypted in one call. This allows a "streaming" usage.
|
|
||||||
* If you need to retain the contents of the
|
|
||||||
* IV, you must either save it manually or use the cipher
|
|
||||||
* module instead.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
|
|
||||||
* #MBEDTLS_AES_DECRYPT.
|
|
||||||
* \param length The length of the input data.
|
|
||||||
* \param iv_off The offset in IV (updated after use).
|
|
||||||
* \param iv The initialization vector (updated after use).
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param output The buffer holding the output data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
|
|
||||||
int mode,
|
|
||||||
size_t length,
|
|
||||||
size_t *iv_off,
|
|
||||||
unsigned char iv[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-CFB8 encryption or decryption
|
|
||||||
* operation.
|
|
||||||
*
|
|
||||||
* It performs the operation defined in the \p mode
|
|
||||||
* parameter (encrypt/decrypt), on the input data buffer defined
|
|
||||||
* in the \p input parameter.
|
|
||||||
*
|
|
||||||
* Due to the nature of CFB, you must use the same key schedule for
|
|
||||||
* both encryption and decryption operations. Therefore, you must
|
|
||||||
* use the context initialized with mbedtls_aes_setkey_enc() for
|
|
||||||
* both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
|
|
||||||
*
|
|
||||||
* \note Upon exit, the content of the IV is updated so that you can
|
|
||||||
* call the same function again on the next
|
|
||||||
* block(s) of data and get the same result as if it was
|
|
||||||
* encrypted in one call. This allows a "streaming" usage.
|
|
||||||
* If you need to retain the contents of the
|
|
||||||
* IV, you should either save it manually or use the cipher
|
|
||||||
* module instead.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
|
|
||||||
* #MBEDTLS_AES_DECRYPT
|
|
||||||
* \param length The length of the input data.
|
|
||||||
* \param iv The initialization vector (updated after use).
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param output The buffer holding the output data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
|
|
||||||
int mode,
|
|
||||||
size_t length,
|
|
||||||
unsigned char iv[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif /*MBEDTLS_CIPHER_MODE_CFB */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-OFB (Output Feedback Mode)
|
|
||||||
* encryption or decryption operation.
|
|
||||||
*
|
|
||||||
* For OFB, you must set up the context with
|
|
||||||
* mbedtls_aes_setkey_enc(), regardless of whether you are
|
|
||||||
* performing an encryption or decryption operation. This is
|
|
||||||
* because OFB mode uses the same key schedule for encryption and
|
|
||||||
* decryption.
|
|
||||||
*
|
|
||||||
* The OFB operation is identical for encryption or decryption,
|
|
||||||
* therefore no operation mode needs to be specified.
|
|
||||||
*
|
|
||||||
* \note Upon exit, the content of iv, the Initialisation Vector, is
|
|
||||||
* updated so that you can call the same function again on the next
|
|
||||||
* block(s) of data and get the same result as if it was encrypted
|
|
||||||
* in one call. This allows a "streaming" usage, by initialising
|
|
||||||
* iv_off to 0 before the first call, and preserving its value
|
|
||||||
* between calls.
|
|
||||||
*
|
|
||||||
* For non-streaming use, the iv should be initialised on each call
|
|
||||||
* to a unique value, and iv_off set to 0 on each call.
|
|
||||||
*
|
|
||||||
* If you need to retain the contents of the initialisation vector,
|
|
||||||
* you must either save it manually or use the cipher module
|
|
||||||
* instead.
|
|
||||||
*
|
|
||||||
* \warning For the OFB mode, the initialisation vector must be unique
|
|
||||||
* every encryption operation. Reuse of an initialisation vector
|
|
||||||
* will compromise security.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param length The length of the input data.
|
|
||||||
* \param iv_off The offset in IV (updated after use).
|
|
||||||
* \param iv The initialization vector (updated after use).
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param output The buffer holding the output data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
|
|
||||||
size_t length,
|
|
||||||
size_t *iv_off,
|
|
||||||
unsigned char iv[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_OFB */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
|
||||||
/**
|
|
||||||
* \brief This function performs an AES-CTR encryption or decryption
|
|
||||||
* operation.
|
|
||||||
*
|
|
||||||
* This function performs the operation defined in the \p mode
|
|
||||||
* parameter (encrypt/decrypt), on the input data buffer
|
|
||||||
* defined in the \p input parameter.
|
|
||||||
*
|
|
||||||
* Due to the nature of CTR, you must use the same key schedule
|
|
||||||
* for both encryption and decryption operations. Therefore, you
|
|
||||||
* must use the context initialized with mbedtls_aes_setkey_enc()
|
|
||||||
* for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
|
|
||||||
*
|
|
||||||
* \warning You must never reuse a nonce value with the same key. Doing so
|
|
||||||
* would void the encryption for the two messages encrypted with
|
|
||||||
* the same nonce and key.
|
|
||||||
*
|
|
||||||
* There are two common strategies for managing nonces with CTR:
|
|
||||||
*
|
|
||||||
* 1. You can handle everything as a single message processed over
|
|
||||||
* successive calls to this function. In that case, you want to
|
|
||||||
* set \p nonce_counter and \p nc_off to 0 for the first call, and
|
|
||||||
* then preserve the values of \p nonce_counter, \p nc_off and \p
|
|
||||||
* stream_block across calls to this function as they will be
|
|
||||||
* updated by this function.
|
|
||||||
*
|
|
||||||
* With this strategy, you must not encrypt more than 2**128
|
|
||||||
* blocks of data with the same key.
|
|
||||||
*
|
|
||||||
* 2. You can encrypt separate messages by dividing the \p
|
|
||||||
* nonce_counter buffer in two areas: the first one used for a
|
|
||||||
* per-message nonce, handled by yourself, and the second one
|
|
||||||
* updated by this function internally.
|
|
||||||
*
|
|
||||||
* For example, you might reserve the first 12 bytes for the
|
|
||||||
* per-message nonce, and the last 4 bytes for internal use. In that
|
|
||||||
* case, before calling this function on a new message you need to
|
|
||||||
* set the first 12 bytes of \p nonce_counter to your chosen nonce
|
|
||||||
* value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
|
|
||||||
* stream_block to be ignored). That way, you can encrypt at most
|
|
||||||
* 2**96 messages of up to 2**32 blocks each with the same key.
|
|
||||||
*
|
|
||||||
* The per-message nonce (or information sufficient to reconstruct
|
|
||||||
* it) needs to be communicated with the ciphertext and must be unique.
|
|
||||||
* The recommended way to ensure uniqueness is to use a message
|
|
||||||
* counter. An alternative is to generate random nonces, but this
|
|
||||||
* limits the number of messages that can be securely encrypted:
|
|
||||||
* for example, with 96-bit random nonces, you should not encrypt
|
|
||||||
* more than 2**32 messages with the same key.
|
|
||||||
*
|
|
||||||
* Note that for both stategies, sizes are measured in blocks and
|
|
||||||
* that an AES block is 16 bytes.
|
|
||||||
*
|
|
||||||
* \warning Upon return, \p stream_block contains sensitive data. Its
|
|
||||||
* content must not be written to insecure storage and should be
|
|
||||||
* securely discarded as soon as it's no longer needed.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption or decryption.
|
|
||||||
* \param length The length of the input data.
|
|
||||||
* \param nc_off The offset in the current \p stream_block, for
|
|
||||||
* resuming within the current cipher stream. The
|
|
||||||
* offset pointer should be 0 at the start of a stream.
|
|
||||||
* \param nonce_counter The 128-bit nonce and counter.
|
|
||||||
* \param stream_block The saved stream block for resuming. This is
|
|
||||||
* overwritten by the function.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param output The buffer holding the output data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
|
|
||||||
size_t length,
|
|
||||||
size_t *nc_off,
|
|
||||||
unsigned char nonce_counter[16],
|
|
||||||
unsigned char stream_block[16],
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Internal AES block encryption function. This is only
|
|
||||||
* exposed to allow overriding it using
|
|
||||||
* \c MBEDTLS_AES_ENCRYPT_ALT.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption.
|
|
||||||
* \param input The plaintext block.
|
|
||||||
* \param output The output (ciphertext) block.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
|
|
||||||
const unsigned char input[16],
|
|
||||||
unsigned char output[16] );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Internal AES block decryption function. This is only
|
|
||||||
* exposed to allow overriding it using see
|
|
||||||
* \c MBEDTLS_AES_DECRYPT_ALT.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for decryption.
|
|
||||||
* \param input The ciphertext block.
|
|
||||||
* \param output The output (plaintext) block.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
|
|
||||||
const unsigned char input[16],
|
|
||||||
unsigned char output[16] );
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
||||||
#if defined(MBEDTLS_DEPRECATED_WARNING)
|
|
||||||
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
|
|
||||||
#else
|
|
||||||
#define MBEDTLS_DEPRECATED
|
|
||||||
#endif
|
|
||||||
/**
|
|
||||||
* \brief Deprecated internal AES block encryption function
|
|
||||||
* without return value.
|
|
||||||
*
|
|
||||||
* \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for encryption.
|
|
||||||
* \param input Plaintext block.
|
|
||||||
* \param output Output (ciphertext) block.
|
|
||||||
*/
|
|
||||||
MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
|
|
||||||
const unsigned char input[16],
|
|
||||||
unsigned char output[16] );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Deprecated internal AES block decryption function
|
|
||||||
* without return value.
|
|
||||||
*
|
|
||||||
* \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
|
|
||||||
*
|
|
||||||
* \param ctx The AES context to use for decryption.
|
|
||||||
* \param input Ciphertext block.
|
|
||||||
* \param output Output (plaintext) block.
|
|
||||||
*/
|
|
||||||
MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
|
|
||||||
const unsigned char input[16],
|
|
||||||
unsigned char output[16] );
|
|
||||||
|
|
||||||
#undef MBEDTLS_DEPRECATED
|
|
||||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Checkup routine.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return \c 1 on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_self_test( int verbose );
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* aes.h */
|
|
@ -1,685 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file check_config.h
|
|
||||||
*
|
|
||||||
* \brief Consistency checks for configuration options
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* It is recommended to include this file from your config.h
|
|
||||||
* in order to catch dependency issues early.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef MBEDTLS_CHECK_CONFIG_H
|
|
||||||
#define MBEDTLS_CHECK_CONFIG_H
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We assume CHAR_BIT is 8 in many places. In practice, this is true on our
|
|
||||||
* target platforms, so not an issue, but let's just be extra sure.
|
|
||||||
*/
|
|
||||||
#include <limits.h>
|
|
||||||
#if CHAR_BIT != 8
|
|
||||||
#error "mbed TLS requires a platform with 8-bit chars"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#if !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_C is required on Windows"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Fix the config here. Not convenient to put an #ifdef _WIN32 in config.h as
|
|
||||||
* it would confuse config.pl. */
|
|
||||||
#if !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && \
|
|
||||||
!defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
|
|
||||||
#define MBEDTLS_PLATFORM_SNPRINTF_ALT
|
|
||||||
#endif
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
|
|
||||||
#if defined(TARGET_LIKE_MBED) && \
|
|
||||||
( defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C) )
|
|
||||||
#error "The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_DEPRECATED_WARNING) && \
|
|
||||||
!defined(__GNUC__) && !defined(__clang__)
|
|
||||||
#error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_HAVE_TIME)
|
|
||||||
#error "MBEDTLS_HAVE_TIME_DATE without MBEDTLS_HAVE_TIME does not make sense"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_AESNI_C) && !defined(MBEDTLS_HAVE_ASM)
|
|
||||||
#error "MBEDTLS_AESNI_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C)
|
|
||||||
#error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_DHM_C) && !defined(MBEDTLS_BIGNUM_C)
|
|
||||||
#error "MBEDTLS_DHM_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) && !defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
||||||
#error "MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CMAC_C) && \
|
|
||||||
!defined(MBEDTLS_AES_C) && !defined(MBEDTLS_DES_C)
|
|
||||||
#error "MBEDTLS_CMAC_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_NIST_KW_C) && \
|
|
||||||
( !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CIPHER_C) )
|
|
||||||
#error "MBEDTLS_NIST_KW_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDH_C) && !defined(MBEDTLS_ECP_C)
|
|
||||||
#error "MBEDTLS_ECDH_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDSA_C) && \
|
|
||||||
( !defined(MBEDTLS_ECP_C) || \
|
|
||||||
!defined(MBEDTLS_ASN1_PARSE_C) || \
|
|
||||||
!defined(MBEDTLS_ASN1_WRITE_C) )
|
|
||||||
#error "MBEDTLS_ECDSA_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECJPAKE_C) && \
|
|
||||||
( !defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_MD_C) )
|
|
||||||
#error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) && !defined(MBEDTLS_HMAC_DRBG_C)
|
|
||||||
#error "MBEDTLS_ECDSA_DETERMINISTIC defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_C) && ( !defined(MBEDTLS_BIGNUM_C) || ( \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) ) )
|
|
||||||
#error "MBEDTLS_ECP_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ENTROPY_C) && (!defined(MBEDTLS_SHA512_C) && \
|
|
||||||
!defined(MBEDTLS_SHA256_C))
|
|
||||||
#error "MBEDTLS_ENTROPY_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_SHA512_C) && \
|
|
||||||
defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64)
|
|
||||||
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
|
|
||||||
#endif
|
|
||||||
#if defined(MBEDTLS_ENTROPY_C) && \
|
|
||||||
( !defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_ENTROPY_FORCE_SHA256) ) \
|
|
||||||
&& defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32)
|
|
||||||
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
|
|
||||||
#endif
|
|
||||||
#if defined(MBEDTLS_ENTROPY_C) && \
|
|
||||||
defined(MBEDTLS_ENTROPY_FORCE_SHA256) && !defined(MBEDTLS_SHA256_C)
|
|
||||||
#error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY) && \
|
|
||||||
( !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) )
|
|
||||||
#error "MBEDTLS_TEST_NULL_ENTROPY defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY) && \
|
|
||||||
( defined(MBEDTLS_ENTROPY_NV_SEED) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
|
|
||||||
defined(MBEDTLS_HAVEGE_C) )
|
|
||||||
#error "MBEDTLS_TEST_NULL_ENTROPY defined, but entropy sources too"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_GCM_C) && ( \
|
|
||||||
!defined(MBEDTLS_AES_C) && !defined(MBEDTLS_CAMELLIA_C) )
|
|
||||||
#error "MBEDTLS_GCM_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_RANDOMIZE_JAC_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_ADD_MIXED_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_ADD_MIXED_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_DOUBLE_JAC_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_NORMALIZE_JAC_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_RANDOMIZE_MXZ_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
||||||
#error "MBEDTLS_ECP_NORMALIZE_MXZ_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HAVEGE_C) && !defined(MBEDTLS_TIMING_C)
|
|
||||||
#error "MBEDTLS_HAVEGE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HKDF_C) && !defined(MBEDTLS_MD_C)
|
|
||||||
#error "MBEDTLS_HKDF_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HMAC_DRBG_C) && !defined(MBEDTLS_MD_C)
|
|
||||||
#error "MBEDTLS_HMAC_DRBG_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(MBEDTLS_DHM_C)
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
|
|
||||||
!defined(MBEDTLS_ECDH_C)
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_DHM_C) || !defined(MBEDTLS_RSA_C) || \
|
|
||||||
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_RSA_C) || \
|
|
||||||
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
|
|
||||||
!defined(MBEDTLS_X509_CRT_PARSE_C) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
|
|
||||||
!defined(MBEDTLS_PKCS1_V15) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
|
|
||||||
!defined(MBEDTLS_PKCS1_V15) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
|
||||||
( !defined(MBEDTLS_ECJPAKE_C) || !defined(MBEDTLS_SHA256_C) || \
|
|
||||||
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) )
|
|
||||||
#error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
|
|
||||||
#error "MBEDTLS_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PADLOCK_C) && !defined(MBEDTLS_HAVE_ASM)
|
|
||||||
#error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PEM_PARSE_C) && !defined(MBEDTLS_BASE64_C)
|
|
||||||
#error "MBEDTLS_PEM_PARSE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PEM_WRITE_C) && !defined(MBEDTLS_BASE64_C)
|
|
||||||
#error "MBEDTLS_PEM_WRITE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PK_C) && \
|
|
||||||
( !defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_ECP_C) )
|
|
||||||
#error "MBEDTLS_PK_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_PK_C)
|
|
||||||
#error "MBEDTLS_PK_PARSE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PK_WRITE_C) && !defined(MBEDTLS_PK_C)
|
|
||||||
#error "MBEDTLS_PK_WRITE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PKCS11_C) && !defined(MBEDTLS_PK_C)
|
|
||||||
#error "MBEDTLS_PKCS11_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_EXIT_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_EXIT_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_EXIT) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_EXIT_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_EXIT_MACRO and MBEDTLS_PLATFORM_STD_EXIT/MBEDTLS_PLATFORM_EXIT_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_TIME_ALT) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) ||\
|
|
||||||
!defined(MBEDTLS_HAVE_TIME) )
|
|
||||||
#error "MBEDTLS_PLATFORM_TIME_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_TIME_MACRO) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) ||\
|
|
||||||
!defined(MBEDTLS_HAVE_TIME) )
|
|
||||||
#error "MBEDTLS_PLATFORM_TIME_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) ||\
|
|
||||||
!defined(MBEDTLS_HAVE_TIME) )
|
|
||||||
#error "MBEDTLS_PLATFORM_TIME_TYPE_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_TIME_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_TIME) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_TIME_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_TIME_MACRO and MBEDTLS_PLATFORM_STD_TIME/MBEDTLS_PLATFORM_TIME_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_TIME) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_TIME_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_TIME_TYPE_MACRO and MBEDTLS_PLATFORM_STD_TIME/MBEDTLS_PLATFORM_TIME_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_FPRINTF) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_FPRINTF_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO and MBEDTLS_PLATFORM_STD_FPRINTF/MBEDTLS_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
|
|
||||||
#error "MBEDTLS_PLATFORM_FREE_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
|
|
||||||
defined(MBEDTLS_PLATFORM_STD_FREE)
|
|
||||||
#error "MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_STD_FREE cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && !defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
|
|
||||||
#error "MBEDTLS_PLATFORM_CALLOC_MACRO must be defined if MBEDTLS_PLATFORM_FREE_MACRO is"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
|
|
||||||
#error "MBEDTLS_PLATFORM_CALLOC_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
|
|
||||||
defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
|
||||||
#error "MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && !defined(MBEDTLS_PLATFORM_FREE_MACRO)
|
|
||||||
#error "MBEDTLS_PLATFORM_FREE_MACRO must be defined if MBEDTLS_PLATFORM_CALLOC_MACRO is"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_MEMORY) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_MEMORY defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_PRINTF) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_PRINTF_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_PRINTF_MACRO and MBEDTLS_PLATFORM_STD_PRINTF/MBEDTLS_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#error "MBEDTLS_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_SNPRINTF) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_SNPRINTF_MACRO and MBEDTLS_PLATFORM_STD_SNPRINTF/MBEDTLS_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_FREE) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_FREE defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_EXIT) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_EXIT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_TIME) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_TIME_ALT) ||\
|
|
||||||
!defined(MBEDTLS_HAVE_TIME) )
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_TIME defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_FPRINTF) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_PRINTF) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_PRINTF defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_SNPRINTF) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ENTROPY_NV_SEED) &&\
|
|
||||||
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_ENTROPY_C) )
|
|
||||||
#error "MBEDTLS_ENTROPY_NV_SEED defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) &&\
|
|
||||||
!defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
||||||
#error "MBEDTLS_PLATFORM_NV_SEED_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_NV_SEED_READ defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) &&\
|
|
||||||
!defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
|
||||||
#error "MBEDTLS_PLATFORM_STD_NV_SEED_WRITE defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_NV_SEED_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_NV_SEED_READ_MACRO and MBEDTLS_PLATFORM_STD_NV_SEED_READ cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO) &&\
|
|
||||||
( defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) ||\
|
|
||||||
defined(MBEDTLS_PLATFORM_NV_SEED_ALT) )
|
|
||||||
#error "MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO and MBEDTLS_PLATFORM_STD_NV_SEED_WRITE cannot be defined simultaneously"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_RSA_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
|
|
||||||
!defined(MBEDTLS_OID_C) )
|
|
||||||
#error "MBEDTLS_RSA_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_RSA_C) && ( !defined(MBEDTLS_PKCS1_V21) && \
|
|
||||||
!defined(MBEDTLS_PKCS1_V15) )
|
|
||||||
#error "MBEDTLS_RSA_C defined, but none of the PKCS1 versions enabled"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
|
|
||||||
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_PKCS1_V21) )
|
|
||||||
#error "MBEDTLS_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_SSL3) && ( !defined(MBEDTLS_MD5_C) || \
|
|
||||||
!defined(MBEDTLS_SHA1_C) )
|
|
||||||
#error "MBEDTLS_SSL_PROTO_SSL3 defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_TLS1) && ( !defined(MBEDTLS_MD5_C) || \
|
|
||||||
!defined(MBEDTLS_SHA1_C) )
|
|
||||||
#error "MBEDTLS_SSL_PROTO_TLS1 defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_1) && ( !defined(MBEDTLS_MD5_C) || \
|
|
||||||
!defined(MBEDTLS_SHA1_C) )
|
|
||||||
#error "MBEDTLS_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && ( !defined(MBEDTLS_SHA1_C) && \
|
|
||||||
!defined(MBEDTLS_SHA256_C) && !defined(MBEDTLS_SHA512_C) )
|
|
||||||
#error "MBEDTLS_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_DTLS) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
||||||
#error "MBEDTLS_SSL_PROTO_DTLS defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_TLS_C)
|
|
||||||
#error "MBEDTLS_SSL_CLI_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TLS_C) && ( !defined(MBEDTLS_CIPHER_C) || \
|
|
||||||
!defined(MBEDTLS_MD_C) )
|
|
||||||
#error "MBEDTLS_SSL_TLS_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_TLS_C)
|
|
||||||
#error "MBEDTLS_SSL_SRV_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TLS_C) && (!defined(MBEDTLS_SSL_PROTO_SSL3) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2))
|
|
||||||
#error "MBEDTLS_SSL_TLS_C defined, but no protocols are active"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
|
|
||||||
defined(MBEDTLS_SSL_PROTO_TLS1_1) && !defined(MBEDTLS_SSL_PROTO_TLS1))
|
|
||||||
#error "Illegal protocol selection"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_TLS1) && \
|
|
||||||
defined(MBEDTLS_SSL_PROTO_TLS1_2) && !defined(MBEDTLS_SSL_PROTO_TLS1_1))
|
|
||||||
#error "Illegal protocol selection"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
|
|
||||||
defined(MBEDTLS_SSL_PROTO_TLS1_2) && (!defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1)))
|
|
||||||
#error "Illegal protocol selection"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && !defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
||||||
#error "MBEDTLS_SSL_DTLS_HELLO_VERIFY defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \
|
|
||||||
!defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
|
|
||||||
#error "MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
|
|
||||||
( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
|
|
||||||
#error "MBEDTLS_SSL_DTLS_ANTI_REPLAY defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
|
|
||||||
( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
|
|
||||||
#error "MBEDTLS_SSL_DTLS_BADMAC_LIMIT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
||||||
#error "MBEDTLS_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
||||||
#error "MBEDTLS_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_TICKET_C) && !defined(MBEDTLS_CIPHER_C)
|
|
||||||
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) && \
|
|
||||||
!defined(MBEDTLS_SSL_PROTO_SSL3) && !defined(MBEDTLS_SSL_PROTO_TLS1)
|
|
||||||
#error "MBEDTLS_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
|
|
||||||
!defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
||||||
#error "MBEDTLS_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_THREADING_PTHREAD)
|
|
||||||
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
|
|
||||||
#error "MBEDTLS_THREADING_PTHREAD defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
#define MBEDTLS_THREADING_IMPL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_THREADING_ALT)
|
|
||||||
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
|
|
||||||
#error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
#define MBEDTLS_THREADING_IMPL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
|
|
||||||
#error "MBEDTLS_THREADING_C defined, single threading implementation required"
|
|
||||||
#endif
|
|
||||||
#undef MBEDTLS_THREADING_IMPL
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_VERSION_FEATURES) && !defined(MBEDTLS_VERSION_C)
|
|
||||||
#error "MBEDTLS_VERSION_FEATURES defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_USE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
|
|
||||||
!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_PARSE_C) || \
|
|
||||||
!defined(MBEDTLS_PK_PARSE_C) )
|
|
||||||
#error "MBEDTLS_X509_USE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CREATE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
|
|
||||||
!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_WRITE_C) || \
|
|
||||||
!defined(MBEDTLS_PK_WRITE_C) )
|
|
||||||
#error "MBEDTLS_X509_CREATE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
|
|
||||||
#error "MBEDTLS_X509_CRT_PARSE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CRL_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
|
|
||||||
#error "MBEDTLS_X509_CRL_PARSE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CSR_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
|
|
||||||
#error "MBEDTLS_X509_CSR_PARSE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CRT_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
|
|
||||||
#error "MBEDTLS_X509_CRT_WRITE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CSR_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
|
|
||||||
#error "MBEDTLS_X509_CSR_WRITE_C defined, but not all prerequisites"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64)
|
|
||||||
#error "MBEDTLS_HAVE_INT32 and MBEDTLS_HAVE_INT64 cannot be defined simultaneously"
|
|
||||||
#endif /* MBEDTLS_HAVE_INT32 && MBEDTLS_HAVE_INT64 */
|
|
||||||
|
|
||||||
#if ( defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64) ) && \
|
|
||||||
defined(MBEDTLS_HAVE_ASM)
|
|
||||||
#error "MBEDTLS_HAVE_INT32/MBEDTLS_HAVE_INT64 and MBEDTLS_HAVE_ASM cannot be defined simultaneously"
|
|
||||||
#endif /* (MBEDTLS_HAVE_INT32 || MBEDTLS_HAVE_INT64) && MBEDTLS_HAVE_ASM */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Avoid warning from -pedantic. This is a convenient place for this
|
|
||||||
* workaround since this is included by every single file before the
|
|
||||||
* #if defined(MBEDTLS_xxx_C) that results in emtpy translation units.
|
|
||||||
*/
|
|
||||||
typedef int mbedtls_iso_c_forbids_empty_translation_units;
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CHECK_CONFIG_H */
|
|
@ -1,808 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file cipher.h
|
|
||||||
*
|
|
||||||
* \brief This file contains an abstraction interface for use with the cipher
|
|
||||||
* primitives provided by the library. It provides a common interface to all of
|
|
||||||
* the available cipher operations.
|
|
||||||
*
|
|
||||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef MBEDTLS_CIPHER_H
|
|
||||||
#define MBEDTLS_CIPHER_H
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
|
||||||
#define MBEDTLS_CIPHER_MODE_AEAD
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
||||||
#define MBEDTLS_CIPHER_MODE_WITH_PADDING
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
|
|
||||||
defined(MBEDTLS_CHACHA20_C)
|
|
||||||
#define MBEDTLS_CIPHER_MODE_STREAM
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
|
||||||
!defined(inline) && !defined(__cplusplus)
|
|
||||||
#define inline __inline
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
|
|
||||||
#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
|
|
||||||
|
|
||||||
#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
|
|
||||||
#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Supported cipher types.
|
|
||||||
*
|
|
||||||
* \warning RC4 and DES are considered weak ciphers and their use
|
|
||||||
* constitutes a security risk. Arm recommends considering stronger
|
|
||||||
* ciphers instead.
|
|
||||||
*/
|
|
||||||
typedef enum {
|
|
||||||
MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
|
|
||||||
MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
|
|
||||||
MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
|
|
||||||
} mbedtls_cipher_id_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Supported {cipher type, cipher mode} pairs.
|
|
||||||
*
|
|
||||||
* \warning RC4 and DES are considered weak ciphers and their use
|
|
||||||
* constitutes a security risk. Arm recommends considering stronger
|
|
||||||
* ciphers instead.
|
|
||||||
*/
|
|
||||||
typedef enum {
|
|
||||||
MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
|
|
||||||
MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
|
|
||||||
MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
|
|
||||||
MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
|
|
||||||
MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
|
|
||||||
MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
|
|
||||||
} mbedtls_cipher_type_t;
|
|
||||||
|
|
||||||
/** Supported cipher modes. */
|
|
||||||
typedef enum {
|
|
||||||
MBEDTLS_MODE_NONE = 0, /**< None. */
|
|
||||||
MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
|
|
||||||
MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
|
|
||||||
MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
|
|
||||||
MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
|
|
||||||
MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
|
|
||||||
MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
|
|
||||||
MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
|
|
||||||
MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
|
|
||||||
MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
|
|
||||||
MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
|
|
||||||
} mbedtls_cipher_mode_t;
|
|
||||||
|
|
||||||
/** Supported cipher padding types. */
|
|
||||||
typedef enum {
|
|
||||||
MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
|
|
||||||
MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
|
|
||||||
MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
|
|
||||||
MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
|
|
||||||
MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
|
|
||||||
} mbedtls_cipher_padding_t;
|
|
||||||
|
|
||||||
/** Type of operation. */
|
|
||||||
typedef enum {
|
|
||||||
MBEDTLS_OPERATION_NONE = -1,
|
|
||||||
MBEDTLS_DECRYPT = 0,
|
|
||||||
MBEDTLS_ENCRYPT,
|
|
||||||
} mbedtls_operation_t;
|
|
||||||
|
|
||||||
enum {
|
|
||||||
/** Undefined key length. */
|
|
||||||
MBEDTLS_KEY_LENGTH_NONE = 0,
|
|
||||||
/** Key length, in bits (including parity), for DES keys. */
|
|
||||||
MBEDTLS_KEY_LENGTH_DES = 64,
|
|
||||||
/** Key length in bits, including parity, for DES in two-key EDE. */
|
|
||||||
MBEDTLS_KEY_LENGTH_DES_EDE = 128,
|
|
||||||
/** Key length in bits, including parity, for DES in three-key EDE. */
|
|
||||||
MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Maximum length of any IV, in Bytes. */
|
|
||||||
#define MBEDTLS_MAX_IV_LENGTH 16
|
|
||||||
/** Maximum block size of any cipher, in Bytes. */
|
|
||||||
#define MBEDTLS_MAX_BLOCK_LENGTH 16
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base cipher information (opaque struct).
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* CMAC context (opaque struct).
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cipher information. Allows calling cipher functions
|
|
||||||
* in a generic way.
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_cipher_info_t
|
|
||||||
{
|
|
||||||
/** Full cipher identifier. For example,
|
|
||||||
* MBEDTLS_CIPHER_AES_256_CBC.
|
|
||||||
*/
|
|
||||||
mbedtls_cipher_type_t type;
|
|
||||||
|
|
||||||
/** The cipher mode. For example, MBEDTLS_MODE_CBC. */
|
|
||||||
mbedtls_cipher_mode_t mode;
|
|
||||||
|
|
||||||
/** The cipher key length, in bits. This is the
|
|
||||||
* default length for variable sized ciphers.
|
|
||||||
* Includes parity bits for ciphers like DES.
|
|
||||||
*/
|
|
||||||
unsigned int key_bitlen;
|
|
||||||
|
|
||||||
/** Name of the cipher. */
|
|
||||||
const char * name;
|
|
||||||
|
|
||||||
/** IV or nonce size, in Bytes.
|
|
||||||
* For ciphers that accept variable IV sizes,
|
|
||||||
* this is the recommended size.
|
|
||||||
*/
|
|
||||||
unsigned int iv_size;
|
|
||||||
|
|
||||||
/** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
|
|
||||||
* MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
|
|
||||||
* cipher supports variable IV or variable key sizes, respectively.
|
|
||||||
*/
|
|
||||||
int flags;
|
|
||||||
|
|
||||||
/** The block size, in Bytes. */
|
|
||||||
unsigned int block_size;
|
|
||||||
|
|
||||||
/** Struct for base cipher information and functions. */
|
|
||||||
const mbedtls_cipher_base_t *base;
|
|
||||||
|
|
||||||
} mbedtls_cipher_info_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic cipher context.
|
|
||||||
*/
|
|
||||||
typedef struct mbedtls_cipher_context_t
|
|
||||||
{
|
|
||||||
/** Information about the associated cipher. */
|
|
||||||
const mbedtls_cipher_info_t *cipher_info;
|
|
||||||
|
|
||||||
/** Key length to use. */
|
|
||||||
int key_bitlen;
|
|
||||||
|
|
||||||
/** Operation that the key of the context has been
|
|
||||||
* initialized for.
|
|
||||||
*/
|
|
||||||
mbedtls_operation_t operation;
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
||||||
/** Padding functions to use, if relevant for
|
|
||||||
* the specific cipher mode.
|
|
||||||
*/
|
|
||||||
void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
|
|
||||||
int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Buffer for input that has not been processed yet. */
|
|
||||||
unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
|
|
||||||
|
|
||||||
/** Number of Bytes that have not been processed yet. */
|
|
||||||
size_t unprocessed_len;
|
|
||||||
|
|
||||||
/** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
|
|
||||||
* for XTS-mode. */
|
|
||||||
unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
|
|
||||||
|
|
||||||
/** IV size in Bytes, for ciphers with variable-length IVs. */
|
|
||||||
size_t iv_size;
|
|
||||||
|
|
||||||
/** The cipher-specific context. */
|
|
||||||
void *cipher_ctx;
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CMAC_C)
|
|
||||||
/** CMAC-specific context. */
|
|
||||||
mbedtls_cmac_context_t *cmac_ctx;
|
|
||||||
#endif
|
|
||||||
} mbedtls_cipher_context_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function retrieves the list of ciphers supported by the generic
|
|
||||||
* cipher module.
|
|
||||||
*
|
|
||||||
* \return A statically-allocated array of ciphers. The last entry
|
|
||||||
* is zero.
|
|
||||||
*/
|
|
||||||
const int *mbedtls_cipher_list( void );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function retrieves the cipher-information
|
|
||||||
* structure associated with the given cipher name.
|
|
||||||
*
|
|
||||||
* \param cipher_name Name of the cipher to search for.
|
|
||||||
*
|
|
||||||
* \return The cipher information structure associated with the
|
|
||||||
* given \p cipher_name.
|
|
||||||
* \return NULL if the associated cipher information is not found.
|
|
||||||
*/
|
|
||||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function retrieves the cipher-information
|
|
||||||
* structure associated with the given cipher type.
|
|
||||||
*
|
|
||||||
* \param cipher_type Type of the cipher to search for.
|
|
||||||
*
|
|
||||||
* \return The cipher information structure associated with the
|
|
||||||
* given \p cipher_type.
|
|
||||||
* \return NULL if the associated cipher information is not found.
|
|
||||||
*/
|
|
||||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function retrieves the cipher-information
|
|
||||||
* structure associated with the given cipher ID,
|
|
||||||
* key size and mode.
|
|
||||||
*
|
|
||||||
* \param cipher_id The ID of the cipher to search for. For example,
|
|
||||||
* #MBEDTLS_CIPHER_ID_AES.
|
|
||||||
* \param key_bitlen The length of the key in bits.
|
|
||||||
* \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
|
|
||||||
*
|
|
||||||
* \return The cipher information structure associated with the
|
|
||||||
* given \p cipher_id.
|
|
||||||
* \return NULL if the associated cipher information is not found.
|
|
||||||
*/
|
|
||||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
|
|
||||||
int key_bitlen,
|
|
||||||
const mbedtls_cipher_mode_t mode );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function initializes a \p cipher_context as NONE.
|
|
||||||
*/
|
|
||||||
void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function frees and clears the cipher-specific
|
|
||||||
* context of \p ctx. Freeing \p ctx itself remains the
|
|
||||||
* responsibility of the caller.
|
|
||||||
*/
|
|
||||||
void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function initializes and fills the cipher-context
|
|
||||||
* structure with the appropriate values. It also clears
|
|
||||||
* the structure.
|
|
||||||
*
|
|
||||||
* \param ctx The context to initialize. May not be NULL.
|
|
||||||
* \param cipher_info The cipher to use.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
|
|
||||||
* cipher-specific context fails.
|
|
||||||
*
|
|
||||||
* \internal Currently, the function also clears the structure.
|
|
||||||
* In future versions, the caller will be required to call
|
|
||||||
* mbedtls_cipher_init() on the structure first.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the block size of the given cipher.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The size of the blocks of the cipher.
|
|
||||||
* \return 0 if \p ctx has not been initialized.
|
|
||||||
*/
|
|
||||||
static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return ctx->cipher_info->block_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the mode of operation for
|
|
||||||
* the cipher. For example, MBEDTLS_MODE_CBC.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The mode of operation.
|
|
||||||
* \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
|
|
||||||
*/
|
|
||||||
static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return MBEDTLS_MODE_NONE;
|
|
||||||
|
|
||||||
return ctx->cipher_info->mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the size of the IV or nonce
|
|
||||||
* of the cipher, in Bytes.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The recommended IV size if no IV has been set.
|
|
||||||
* \return \c 0 for ciphers not using an IV or a nonce.
|
|
||||||
* \return The actual size if an IV has been set.
|
|
||||||
*/
|
|
||||||
static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if( ctx->iv_size != 0 )
|
|
||||||
return (int) ctx->iv_size;
|
|
||||||
|
|
||||||
return (int) ctx->cipher_info->iv_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the type of the given cipher.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The type of the cipher.
|
|
||||||
* \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
|
|
||||||
*/
|
|
||||||
static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return MBEDTLS_CIPHER_NONE;
|
|
||||||
|
|
||||||
return ctx->cipher_info->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the name of the given cipher
|
|
||||||
* as a string.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The name of the cipher.
|
|
||||||
* \return NULL if \p ctx has not been not initialized.
|
|
||||||
*/
|
|
||||||
static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return ctx->cipher_info->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the key length of the cipher.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The key length of the cipher in bits.
|
|
||||||
* \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
|
|
||||||
* initialized.
|
|
||||||
*/
|
|
||||||
static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return MBEDTLS_KEY_LENGTH_NONE;
|
|
||||||
|
|
||||||
return (int) ctx->cipher_info->key_bitlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function returns the operation of the given cipher.
|
|
||||||
*
|
|
||||||
* \param ctx The context of the cipher. Must be initialized.
|
|
||||||
*
|
|
||||||
* \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
|
|
||||||
* \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
|
|
||||||
*/
|
|
||||||
static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
|
|
||||||
{
|
|
||||||
if( NULL == ctx || NULL == ctx->cipher_info )
|
|
||||||
return MBEDTLS_OPERATION_NONE;
|
|
||||||
|
|
||||||
return ctx->operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets the key to use with the given context.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context. May not be NULL. Must have
|
|
||||||
* been initialized using mbedtls_cipher_info_from_type()
|
|
||||||
* or mbedtls_cipher_info_from_string().
|
|
||||||
* \param key The key to use.
|
|
||||||
* \param key_bitlen The key length to use, in bits.
|
|
||||||
* \param operation The operation that the key will be used for:
|
|
||||||
* #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
|
|
||||||
int key_bitlen, const mbedtls_operation_t operation );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
||||||
/**
|
|
||||||
* \brief This function sets the padding mode, for cipher modes
|
|
||||||
* that use padding.
|
|
||||||
*
|
|
||||||
* The default passing mode is PKCS7 padding.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param mode The padding mode.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
|
|
||||||
* if the selected padding mode is not supported.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
|
|
||||||
* does not support padding.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets the initialization vector (IV)
|
|
||||||
* or nonce.
|
|
||||||
*
|
|
||||||
* \note Some ciphers do not use IVs nor nonce. For these
|
|
||||||
* ciphers, this function has no effect.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
|
|
||||||
* \param iv_len The IV length for ciphers with variable-size IV.
|
|
||||||
* This parameter is discarded by ciphers with fixed-size IV.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *iv, size_t iv_len );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function resets the cipher state.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
|
||||||
/**
|
|
||||||
* \brief This function adds additional data for AEAD ciphers.
|
|
||||||
* Currently supported with GCM and ChaCha20+Poly1305.
|
|
||||||
* Must be called exactly once, after mbedtls_cipher_reset().
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param ad The additional data to use.
|
|
||||||
* \param ad_len the Length of \p ad.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return A specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *ad, size_t ad_len );
|
|
||||||
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The generic cipher update function. It encrypts or
|
|
||||||
* decrypts using the given cipher context. Writes as
|
|
||||||
* many block-sized blocks of data as possible to output.
|
|
||||||
* Any data that cannot be written immediately is either
|
|
||||||
* added to the next block, or flushed when
|
|
||||||
* mbedtls_cipher_finish() is called.
|
|
||||||
* Exception: For MBEDTLS_MODE_ECB, expects a single block
|
|
||||||
* in size. For example, 16 Bytes for AES.
|
|
||||||
*
|
|
||||||
* \note If the underlying cipher is used in GCM mode, all calls
|
|
||||||
* to this function, except for the last one before
|
|
||||||
* mbedtls_cipher_finish(), must have \p ilen as a
|
|
||||||
* multiple of the block size of the cipher.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
* \param output The buffer for the output data. Must be able to hold at
|
|
||||||
* least \p ilen + block_size. Must not be the same buffer
|
|
||||||
* as input.
|
|
||||||
* \param olen The length of the output data, to be updated with the
|
|
||||||
* actual number of Bytes written.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
|
|
||||||
* unsupported mode for a cipher.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
|
|
||||||
size_t ilen, unsigned char *output, size_t *olen );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The generic cipher finalization function. If data still
|
|
||||||
* needs to be flushed from an incomplete block, the data
|
|
||||||
* contained in it is padded to the size of
|
|
||||||
* the last block, and written to the \p output buffer.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param output The buffer to write data to. Needs block_size available.
|
|
||||||
* \param olen The length of the data written to the \p output buffer.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
|
|
||||||
* expecting a full block but not receiving one.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
|
|
||||||
* while decrypting.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
|
|
||||||
unsigned char *output, size_t *olen );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
|
||||||
/**
|
|
||||||
* \brief This function writes a tag for AEAD ciphers.
|
|
||||||
* Currently supported with GCM and ChaCha20+Poly1305.
|
|
||||||
* Must be called after mbedtls_cipher_finish().
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param tag The buffer to write the tag to.
|
|
||||||
* \param tag_len The length of the tag to write.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return A specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
|
|
||||||
unsigned char *tag, size_t tag_len );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function checks the tag for AEAD ciphers.
|
|
||||||
* Currently supported with GCM and ChaCha20+Poly1305.
|
|
||||||
* Must be called after mbedtls_cipher_finish().
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param tag The buffer holding the tag.
|
|
||||||
* \param tag_len The length of the tag to check.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return A specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *tag, size_t tag_len );
|
|
||||||
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The generic all-in-one encryption/decryption function,
|
|
||||||
* for all ciphers except AEAD constructs.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
|
|
||||||
* \param iv_len The IV length for ciphers with variable-size IV.
|
|
||||||
* This parameter is discarded by ciphers with fixed-size
|
|
||||||
* IV.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
* \param output The buffer for the output data. Must be able to hold at
|
|
||||||
* least \p ilen + block_size. Must not be the same buffer
|
|
||||||
* as input.
|
|
||||||
* \param olen The length of the output data, to be updated with the
|
|
||||||
* actual number of Bytes written.
|
|
||||||
*
|
|
||||||
* \note Some ciphers do not use IVs nor nonce. For these
|
|
||||||
* ciphers, use \p iv = NULL and \p iv_len = 0.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
|
|
||||||
* expecting a full block but not receiving one.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
|
|
||||||
* while decrypting.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *iv, size_t iv_len,
|
|
||||||
const unsigned char *input, size_t ilen,
|
|
||||||
unsigned char *output, size_t *olen );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
|
|
||||||
/**
|
|
||||||
* \brief The generic autenticated encryption (AEAD) function.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
|
|
||||||
* \param iv_len The IV length for ciphers with variable-size IV.
|
|
||||||
* This parameter is discarded by ciphers with fixed-size IV.
|
|
||||||
* \param ad The additional data to authenticate.
|
|
||||||
* \param ad_len The length of \p ad.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
* \param output The buffer for the output data.
|
|
||||||
* Must be able to hold at least \p ilen.
|
|
||||||
* \param olen The length of the output data, to be updated with the
|
|
||||||
* actual number of Bytes written.
|
|
||||||
* \param tag The buffer for the authentication tag.
|
|
||||||
* \param tag_len The desired length of the authentication tag.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *iv, size_t iv_len,
|
|
||||||
const unsigned char *ad, size_t ad_len,
|
|
||||||
const unsigned char *input, size_t ilen,
|
|
||||||
unsigned char *output, size_t *olen,
|
|
||||||
unsigned char *tag, size_t tag_len );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The generic autenticated decryption (AEAD) function.
|
|
||||||
*
|
|
||||||
* \note If the data is not authentic, then the output buffer
|
|
||||||
* is zeroed out to prevent the unauthentic plaintext being
|
|
||||||
* used, making this interface safer.
|
|
||||||
*
|
|
||||||
* \param ctx The generic cipher context.
|
|
||||||
* \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
|
|
||||||
* \param iv_len The IV length for ciphers with variable-size IV.
|
|
||||||
* This parameter is discarded by ciphers with fixed-size IV.
|
|
||||||
* \param ad The additional data to be authenticated.
|
|
||||||
* \param ad_len The length of \p ad.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
* \param output The buffer for the output data.
|
|
||||||
* Must be able to hold at least \p ilen.
|
|
||||||
* \param olen The length of the output data, to be updated with the
|
|
||||||
* actual number of Bytes written.
|
|
||||||
* \param tag The buffer holding the authentication tag.
|
|
||||||
* \param tag_len The length of the authentication tag.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
|
|
||||||
* parameter-verification failure.
|
|
||||||
* \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *iv, size_t iv_len,
|
|
||||||
const unsigned char *ad, size_t ad_len,
|
|
||||||
const unsigned char *input, size_t ilen,
|
|
||||||
unsigned char *output, size_t *olen,
|
|
||||||
const unsigned char *tag, size_t tag_len );
|
|
||||||
#endif /* MBEDTLS_CIPHER_MODE_AEAD */
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CIPHER_H */
|
|
@ -1,127 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file cipher_internal.h
|
|
||||||
*
|
|
||||||
* \brief Cipher wrappers.
|
|
||||||
*
|
|
||||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
#ifndef MBEDTLS_CIPHER_WRAP_H
|
|
||||||
#define MBEDTLS_CIPHER_WRAP_H
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "cipher.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base cipher information. The non-mode specific functions and values.
|
|
||||||
*/
|
|
||||||
struct mbedtls_cipher_base_t
|
|
||||||
{
|
|
||||||
/** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
|
|
||||||
mbedtls_cipher_id_t cipher;
|
|
||||||
|
|
||||||
/** Encrypt using ECB */
|
|
||||||
int (*ecb_func)( void *ctx, mbedtls_operation_t mode,
|
|
||||||
const unsigned char *input, unsigned char *output );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
||||||
/** Encrypt using CBC */
|
|
||||||
int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length,
|
|
||||||
unsigned char *iv, const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
|
||||||
/** Encrypt using CFB (Full length) */
|
|
||||||
int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
|
|
||||||
unsigned char *iv, const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
|
||||||
/** Encrypt using OFB (Full length) */
|
|
||||||
int (*ofb_func)( void *ctx, size_t length, size_t *iv_off,
|
|
||||||
unsigned char *iv,
|
|
||||||
const unsigned char *input,
|
|
||||||
unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
|
||||||
/** Encrypt using CTR */
|
|
||||||
int (*ctr_func)( void *ctx, size_t length, size_t *nc_off,
|
|
||||||
unsigned char *nonce_counter, unsigned char *stream_block,
|
|
||||||
const unsigned char *input, unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
||||||
/** Encrypt or decrypt using XTS. */
|
|
||||||
int (*xts_func)( void *ctx, mbedtls_operation_t mode, size_t length,
|
|
||||||
const unsigned char data_unit[16],
|
|
||||||
const unsigned char *input, unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
|
|
||||||
/** Encrypt using STREAM */
|
|
||||||
int (*stream_func)( void *ctx, size_t length,
|
|
||||||
const unsigned char *input, unsigned char *output );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Set key for encryption purposes */
|
|
||||||
int (*setkey_enc_func)( void *ctx, const unsigned char *key,
|
|
||||||
unsigned int key_bitlen );
|
|
||||||
|
|
||||||
/** Set key for decryption purposes */
|
|
||||||
int (*setkey_dec_func)( void *ctx, const unsigned char *key,
|
|
||||||
unsigned int key_bitlen);
|
|
||||||
|
|
||||||
/** Allocate a new context */
|
|
||||||
void * (*ctx_alloc_func)( void );
|
|
||||||
|
|
||||||
/** Free the given context */
|
|
||||||
void (*ctx_free_func)( void *ctx );
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
mbedtls_cipher_type_t type;
|
|
||||||
const mbedtls_cipher_info_t *info;
|
|
||||||
} mbedtls_cipher_definition_t;
|
|
||||||
|
|
||||||
extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
|
|
||||||
|
|
||||||
extern int mbedtls_cipher_supported[];
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CIPHER_WRAP_H */
|
|
@ -1,208 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file cmac.h
|
|
||||||
*
|
|
||||||
* \brief This file contains CMAC definitions and functions.
|
|
||||||
*
|
|
||||||
* The Cipher-based Message Authentication Code (CMAC) Mode for
|
|
||||||
* Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef MBEDTLS_CMAC_H
|
|
||||||
#define MBEDTLS_CMAC_H
|
|
||||||
|
|
||||||
#include "cipher.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
|
|
||||||
|
|
||||||
#define MBEDTLS_AES_BLOCK_SIZE 16
|
|
||||||
#define MBEDTLS_DES3_BLOCK_SIZE 8
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_AES_C)
|
|
||||||
#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
|
|
||||||
#else
|
|
||||||
#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CMAC_ALT)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The CMAC context structure.
|
|
||||||
*/
|
|
||||||
struct mbedtls_cmac_context_t
|
|
||||||
{
|
|
||||||
/** The internal state of the CMAC algorithm. */
|
|
||||||
unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
||||||
|
|
||||||
/** Unprocessed data - either data that was not block aligned and is still
|
|
||||||
* pending processing, or the final block. */
|
|
||||||
unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
||||||
|
|
||||||
/** The length of data pending processing. */
|
|
||||||
size_t unprocessed_len;
|
|
||||||
};
|
|
||||||
|
|
||||||
#else /* !MBEDTLS_CMAC_ALT */
|
|
||||||
#include "cmac_alt.h"
|
|
||||||
#endif /* !MBEDTLS_CMAC_ALT */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets the CMAC key, and prepares to authenticate
|
|
||||||
* the input data.
|
|
||||||
* Must be called with an initialized cipher context.
|
|
||||||
*
|
|
||||||
* \param ctx The cipher context used for the CMAC operation, initialized
|
|
||||||
* as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
|
|
||||||
* MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
|
|
||||||
* or MBEDTLS_CIPHER_DES_EDE3_ECB.
|
|
||||||
* \param key The CMAC key.
|
|
||||||
* \param keybits The length of the CMAC key in bits.
|
|
||||||
* Must be supported by the cipher.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return A cipher-specific error code on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *key, size_t keybits );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function feeds an input buffer into an ongoing CMAC
|
|
||||||
* computation.
|
|
||||||
*
|
|
||||||
* It is called between mbedtls_cipher_cmac_starts() or
|
|
||||||
* mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
|
|
||||||
* Can be called repeatedly.
|
|
||||||
*
|
|
||||||
* \param ctx The cipher context used for the CMAC operation.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
|
|
||||||
* if parameter verification fails.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
|
|
||||||
const unsigned char *input, size_t ilen );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function finishes the CMAC operation, and writes
|
|
||||||
* the result to the output buffer.
|
|
||||||
*
|
|
||||||
* It is called after mbedtls_cipher_cmac_update().
|
|
||||||
* It can be followed by mbedtls_cipher_cmac_reset() and
|
|
||||||
* mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
|
|
||||||
*
|
|
||||||
* \param ctx The cipher context used for the CMAC operation.
|
|
||||||
* \param output The output buffer for the CMAC checksum result.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
|
|
||||||
* if parameter verification fails.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
|
|
||||||
unsigned char *output );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function prepares the authentication of another
|
|
||||||
* message with the same key as the previous CMAC
|
|
||||||
* operation.
|
|
||||||
*
|
|
||||||
* It is called after mbedtls_cipher_cmac_finish()
|
|
||||||
* and before mbedtls_cipher_cmac_update().
|
|
||||||
*
|
|
||||||
* \param ctx The cipher context used for the CMAC operation.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
|
|
||||||
* if parameter verification fails.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function calculates the full generic CMAC
|
|
||||||
* on the input buffer with the provided key.
|
|
||||||
*
|
|
||||||
* The function allocates the context, performs the
|
|
||||||
* calculation, and frees the context.
|
|
||||||
*
|
|
||||||
* The CMAC result is calculated as
|
|
||||||
* output = generic CMAC(cmac key, input buffer).
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* \param cipher_info The cipher information.
|
|
||||||
* \param key The CMAC key.
|
|
||||||
* \param keylen The length of the CMAC key in bits.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param ilen The length of the input data.
|
|
||||||
* \param output The buffer for the generic CMAC result.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
|
|
||||||
* if parameter verification fails.
|
|
||||||
*/
|
|
||||||
int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
|
|
||||||
const unsigned char *key, size_t keylen,
|
|
||||||
const unsigned char *input, size_t ilen,
|
|
||||||
unsigned char *output );
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_AES_C)
|
|
||||||
/**
|
|
||||||
* \brief This function implements the AES-CMAC-PRF-128 pseudorandom
|
|
||||||
* function, as defined in
|
|
||||||
* <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
|
|
||||||
* Message Authentication Code-Pseudo-Random Function-128
|
|
||||||
* (AES-CMAC-PRF-128) Algorithm for the Internet Key
|
|
||||||
* Exchange Protocol (IKE).</em>
|
|
||||||
*
|
|
||||||
* \param key The key to use.
|
|
||||||
* \param key_len The key length in Bytes.
|
|
||||||
* \param input The buffer holding the input data.
|
|
||||||
* \param in_len The length of the input data in Bytes.
|
|
||||||
* \param output The buffer holding the generated 16 Bytes of
|
|
||||||
* pseudorandom output.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
*/
|
|
||||||
int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
|
|
||||||
const unsigned char *input, size_t in_len,
|
|
||||||
unsigned char output[16] );
|
|
||||||
#endif /* MBEDTLS_AES_C */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
|
|
||||||
/**
|
|
||||||
* \brief The CMAC checkup routine.
|
|
||||||
*
|
|
||||||
* \return \c 0 on success.
|
|
||||||
* \return \c 1 on failure.
|
|
||||||
*/
|
|
||||||
int mbedtls_cmac_self_test( int verbose );
|
|
||||||
#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CMAC_H */
|
|
@ -1,49 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file config.h
|
|
||||||
*
|
|
||||||
* \brief Configuration options (set of defines)
|
|
||||||
*
|
|
||||||
* This set of compile-time options may be used to enable
|
|
||||||
* or disable features selectively, and reduce the global
|
|
||||||
* memory footprint.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
// modified 13 nov 2018 by shchmue // -> pared down to essentials
|
|
||||||
/////////////////////////////////////
|
|
||||||
|
|
||||||
#ifndef MBEDTLS_CONFIG_H
|
|
||||||
#define MBEDTLS_CONFIG_H
|
|
||||||
|
|
||||||
#define MBEDTLS_HAVE_ASM
|
|
||||||
|
|
||||||
#define MBEDTLS_AES_C
|
|
||||||
|
|
||||||
#define MBEDTLS_CIPHER_C
|
|
||||||
#define MBEDTLS_CMAC_C
|
|
||||||
|
|
||||||
#define MBEDTLS_CIPHER_MODE_CTR
|
|
||||||
|
|
||||||
#include "check_config.h"
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_CONFIG_H */
|
|
@ -1,64 +0,0 @@
|
|||||||
/**
|
|
||||||
* \file platform_util.h
|
|
||||||
*
|
|
||||||
* \brief Common and shared functions used by multiple modules in the Mbed TLS
|
|
||||||
* library.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2018, Arm Limited, All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
#ifndef MBEDTLS_PLATFORM_UTIL_H
|
|
||||||
#define MBEDTLS_PLATFORM_UTIL_H
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Securely zeroize a buffer
|
|
||||||
*
|
|
||||||
* The function is meant to wipe the data contained in a buffer so
|
|
||||||
* that it can no longer be recovered even if the program memory
|
|
||||||
* is later compromised. Call this function on sensitive data
|
|
||||||
* stored on the stack before returning from a function, and on
|
|
||||||
* sensitive data stored on the heap before freeing the heap
|
|
||||||
* object.
|
|
||||||
*
|
|
||||||
* It is extremely difficult to guarantee that calls to
|
|
||||||
* mbedtls_platform_zeroize() are not removed by aggressive
|
|
||||||
* compiler optimizations in a portable way. For this reason, Mbed
|
|
||||||
* TLS provides the configuration option
|
|
||||||
* MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
|
|
||||||
* mbedtls_platform_zeroize() to use a suitable implementation for
|
|
||||||
* their platform and needs
|
|
||||||
*
|
|
||||||
* \param buf Buffer to be zeroized
|
|
||||||
* \param len Length of the buffer in bytes
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void mbedtls_platform_zeroize( void *buf, size_t len );
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MBEDTLS_PLATFORM_UTIL_H */
|
|
@ -1,181 +0,0 @@
|
|||||||
option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
|
|
||||||
option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
|
|
||||||
option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
|
|
||||||
|
|
||||||
set(src_crypto
|
|
||||||
aes.c
|
|
||||||
aesni.c
|
|
||||||
arc4.c
|
|
||||||
aria.c
|
|
||||||
asn1parse.c
|
|
||||||
asn1write.c
|
|
||||||
base64.c
|
|
||||||
bignum.c
|
|
||||||
blowfish.c
|
|
||||||
camellia.c
|
|
||||||
ccm.c
|
|
||||||
chacha20.c
|
|
||||||
chachapoly.c
|
|
||||||
cipher.c
|
|
||||||
cipher_wrap.c
|
|
||||||
cmac.c
|
|
||||||
ctr_drbg.c
|
|
||||||
des.c
|
|
||||||
dhm.c
|
|
||||||
ecdh.c
|
|
||||||
ecdsa.c
|
|
||||||
ecjpake.c
|
|
||||||
ecp.c
|
|
||||||
ecp_curves.c
|
|
||||||
entropy.c
|
|
||||||
entropy_poll.c
|
|
||||||
error.c
|
|
||||||
gcm.c
|
|
||||||
havege.c
|
|
||||||
hkdf.c
|
|
||||||
hmac_drbg.c
|
|
||||||
md.c
|
|
||||||
md2.c
|
|
||||||
md4.c
|
|
||||||
md5.c
|
|
||||||
md_wrap.c
|
|
||||||
memory_buffer_alloc.c
|
|
||||||
nist_kw.c
|
|
||||||
oid.c
|
|
||||||
padlock.c
|
|
||||||
pem.c
|
|
||||||
pk.c
|
|
||||||
pk_wrap.c
|
|
||||||
pkcs12.c
|
|
||||||
pkcs5.c
|
|
||||||
pkparse.c
|
|
||||||
pkwrite.c
|
|
||||||
platform.c
|
|
||||||
platform_util.c
|
|
||||||
poly1305.c
|
|
||||||
ripemd160.c
|
|
||||||
rsa.c
|
|
||||||
rsa_internal.c
|
|
||||||
sha1.c
|
|
||||||
sha256.c
|
|
||||||
sha512.c
|
|
||||||
threading.c
|
|
||||||
timing.c
|
|
||||||
version.c
|
|
||||||
version_features.c
|
|
||||||
xtea.c
|
|
||||||
)
|
|
||||||
|
|
||||||
set(src_x509
|
|
||||||
certs.c
|
|
||||||
pkcs11.c
|
|
||||||
x509.c
|
|
||||||
x509_create.c
|
|
||||||
x509_crl.c
|
|
||||||
x509_crt.c
|
|
||||||
x509_csr.c
|
|
||||||
x509write_crt.c
|
|
||||||
x509write_csr.c
|
|
||||||
)
|
|
||||||
|
|
||||||
set(src_tls
|
|
||||||
debug.c
|
|
||||||
net_sockets.c
|
|
||||||
ssl_cache.c
|
|
||||||
ssl_ciphersuites.c
|
|
||||||
ssl_cli.c
|
|
||||||
ssl_cookie.c
|
|
||||||
ssl_srv.c
|
|
||||||
ssl_ticket.c
|
|
||||||
ssl_tls.c
|
|
||||||
)
|
|
||||||
|
|
||||||
if(CMAKE_COMPILER_IS_GNUCC)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
|
|
||||||
endif(CMAKE_COMPILER_IS_GNUCC)
|
|
||||||
|
|
||||||
if(CMAKE_COMPILER_IS_CLANG)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
|
|
||||||
endif(CMAKE_COMPILER_IS_CLANG)
|
|
||||||
|
|
||||||
if(WIN32)
|
|
||||||
set(libs ${libs} ws2_32)
|
|
||||||
endif(WIN32)
|
|
||||||
|
|
||||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
|
||||||
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
|
|
||||||
SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
|
|
||||||
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
|
|
||||||
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(HAIKU)
|
|
||||||
set(libs ${libs} network)
|
|
||||||
endif(HAIKU)
|
|
||||||
|
|
||||||
if(USE_PKCS11_HELPER_LIBRARY)
|
|
||||||
set(libs ${libs} pkcs11-helper)
|
|
||||||
endif(USE_PKCS11_HELPER_LIBRARY)
|
|
||||||
|
|
||||||
if(ENABLE_ZLIB_SUPPORT)
|
|
||||||
set(libs ${libs} ${ZLIB_LIBRARIES})
|
|
||||||
endif(ENABLE_ZLIB_SUPPORT)
|
|
||||||
|
|
||||||
if(LINK_WITH_PTHREAD)
|
|
||||||
set(libs ${libs} pthread)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
|
|
||||||
endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
|
|
||||||
if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
set(mbedtls_static_target "mbedtls_static")
|
|
||||||
set(mbedx509_static_target "mbedx509_static")
|
|
||||||
set(mbedcrypto_static_target "mbedcrypto_static")
|
|
||||||
elseif(USE_STATIC_MBEDTLS_LIBRARY)
|
|
||||||
set(mbedtls_static_target "mbedtls")
|
|
||||||
set(mbedx509_static_target "mbedx509")
|
|
||||||
set(mbedcrypto_static_target "mbedcrypto")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(USE_STATIC_MBEDTLS_LIBRARY)
|
|
||||||
add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
|
|
||||||
set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
|
|
||||||
target_link_libraries(${mbedcrypto_static_target} ${libs})
|
|
||||||
|
|
||||||
add_library(${mbedx509_static_target} STATIC ${src_x509})
|
|
||||||
set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
|
|
||||||
target_link_libraries(${mbedx509_static_target} ${libs} ${mbedcrypto_static_target})
|
|
||||||
|
|
||||||
add_library(${mbedtls_static_target} STATIC ${src_tls})
|
|
||||||
set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
|
|
||||||
target_link_libraries(${mbedtls_static_target} ${libs} ${mbedx509_static_target})
|
|
||||||
|
|
||||||
install(TARGETS ${mbedtls_static_target} ${mbedx509_static_target} ${mbedcrypto_static_target}
|
|
||||||
DESTINATION ${LIB_INSTALL_DIR}
|
|
||||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
|
||||||
endif(USE_STATIC_MBEDTLS_LIBRARY)
|
|
||||||
|
|
||||||
if(USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
add_library(mbedcrypto SHARED ${src_crypto})
|
|
||||||
set_target_properties(mbedcrypto PROPERTIES VERSION 2.13.0 SOVERSION 3)
|
|
||||||
target_link_libraries(mbedcrypto ${libs})
|
|
||||||
|
|
||||||
add_library(mbedx509 SHARED ${src_x509})
|
|
||||||
set_target_properties(mbedx509 PROPERTIES VERSION 2.13.0 SOVERSION 0)
|
|
||||||
target_link_libraries(mbedx509 ${libs} mbedcrypto)
|
|
||||||
|
|
||||||
add_library(mbedtls SHARED ${src_tls})
|
|
||||||
set_target_properties(mbedtls PROPERTIES VERSION 2.13.0 SOVERSION 12)
|
|
||||||
target_link_libraries(mbedtls ${libs} mbedx509)
|
|
||||||
|
|
||||||
install(TARGETS mbedtls mbedx509 mbedcrypto
|
|
||||||
DESTINATION ${LIB_INSTALL_DIR}
|
|
||||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
|
||||||
endif(USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
|
|
||||||
add_custom_target(lib DEPENDS mbedcrypto mbedx509 mbedtls)
|
|
||||||
if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
|
|
||||||
add_dependencies(lib mbedcrypto_static mbedx509_static mbedtls_static)
|
|
||||||
endif()
|
|
@ -1,46 +0,0 @@
|
|||||||
#---------------------------------------------------------------------------------
|
|
||||||
.SUFFIXES:
|
|
||||||
#---------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
ifeq ($(strip $(DEVKITPRO)),)
|
|
||||||
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
|
|
||||||
endif
|
|
||||||
|
|
||||||
TOPDIR ?= $(CURDIR)
|
|
||||||
include $(DEVKITPRO)/libnx/switch_rules
|
|
||||||
|
|
||||||
# Also see "include/mbedtls/config.h"
|
|
||||||
|
|
||||||
ARCH := -march=armv8-a+crypto -mtune=cortex-a57 -mtp=soft -fPIE
|
|
||||||
CFLAGS := -g -Wall -O3 -ffunction-sections $(ARCH) $(DEFINES)
|
|
||||||
CFLAGS += -I ../include -D__SWITCH__
|
|
||||||
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
|
|
||||||
ASFLAGS := -g $(ARCH)
|
|
||||||
LDFLAGS ?= -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
|
||||||
|
|
||||||
|
|
||||||
OBJS_CRYPTO= aes.o cipher.o cipher_wrap.o \
|
|
||||||
cmac.o platform_util.o
|
|
||||||
|
|
||||||
.SILENT:
|
|
||||||
|
|
||||||
.PHONY: all static clean
|
|
||||||
|
|
||||||
all: static
|
|
||||||
|
|
||||||
static: libmbedcrypto.a
|
|
||||||
|
|
||||||
# crypto
|
|
||||||
libmbedcrypto.a: $(OBJS_CRYPTO)
|
|
||||||
echo " AR $@"
|
|
||||||
$(AR) -rc $@ $(OBJS_CRYPTO)
|
|
||||||
echo " RL $@"
|
|
||||||
$(AR) -s $@
|
|
||||||
|
|
||||||
|
|
||||||
.c.o:
|
|
||||||
echo " CC $<"
|
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) -c $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f *.o libmbed*
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* Common and shared functions used by multiple modules in the Mbed TLS
|
|
||||||
* library.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2018, Arm Limited, All Rights Reserved
|
|
||||||
* SPDX-License-Identifier: GPL-2.0
|
|
||||||
*
|
|
||||||
* This program 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 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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-1301 USA.
|
|
||||||
*
|
|
||||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "mbedtls/config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "mbedtls/platform_util.h"
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
|
|
||||||
/*
|
|
||||||
* This implementation should never be optimized out by the compiler
|
|
||||||
*
|
|
||||||
* This implementation for mbedtls_platform_zeroize() was inspired from Colin
|
|
||||||
* Percival's blog article at:
|
|
||||||
*
|
|
||||||
* http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
|
|
||||||
*
|
|
||||||
* It uses a volatile function pointer to the standard memset(). Because the
|
|
||||||
* pointer is volatile the compiler expects it to change at
|
|
||||||
* any time and will not optimize out the call that could potentially perform
|
|
||||||
* other operations on the input buffer instead of just setting it to 0.
|
|
||||||
* Nevertheless, as pointed out by davidtgoldblatt on Hacker News
|
|
||||||
* (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
|
|
||||||
* details), optimizations of the following form are still possible:
|
|
||||||
*
|
|
||||||
* if( memset_func != memset )
|
|
||||||
* memset_func( buf, 0, len );
|
|
||||||
*
|
|
||||||
* Note that it is extremely difficult to guarantee that
|
|
||||||
* mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
|
|
||||||
* in a portable way. For this reason, Mbed TLS also provides the configuration
|
|
||||||
* option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
|
|
||||||
* mbedtls_platform_zeroize() to use a suitable implementation for their
|
|
||||||
* platform and needs.
|
|
||||||
*/
|
|
||||||
static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
|
|
||||||
|
|
||||||
void mbedtls_platform_zeroize( void *buf, size_t len )
|
|
||||||
{
|
|
||||||
memset_func( buf, 0, len );
|
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
|
|
64
source/nx/es.c
Normal file
64
source/nx/es.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include "es.h"
|
||||||
|
|
||||||
|
#include "../service_guard.h"
|
||||||
|
|
||||||
|
#include <switch/kernel/ipc.h>
|
||||||
|
#include <switch/services/sm.h>
|
||||||
|
|
||||||
|
static Service g_esSrv;
|
||||||
|
|
||||||
|
NX_GENERATE_SERVICE_GUARD(es);
|
||||||
|
|
||||||
|
Result _esInitialize() {
|
||||||
|
return smGetService(&g_esSrv, "es");
|
||||||
|
}
|
||||||
|
|
||||||
|
void _esCleanup() {
|
||||||
|
serviceClose(&g_esSrv);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result esCountCommonTicket(u32 *out_count)
|
||||||
|
{
|
||||||
|
u32 num_tickets;
|
||||||
|
|
||||||
|
Result rc = serviceDispatchOut(&g_esSrv, 9, num_tickets);
|
||||||
|
if (R_SUCCEEDED(rc) && out_count) *out_count = num_tickets;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result esCountPersonalizedTicket(u32 *out_count)
|
||||||
|
{
|
||||||
|
u32 num_tickets;
|
||||||
|
|
||||||
|
Result rc = serviceDispatchOut(&g_esSrv, 10, num_tickets);
|
||||||
|
if (R_SUCCEEDED(rc) && out_count) *out_count = num_tickets;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result esListCommonTicket(u32 *numRightsIdsWritten, RightsId *outBuf, size_t bufSize)
|
||||||
|
{
|
||||||
|
u32 num_rights_ids_written;
|
||||||
|
|
||||||
|
Result rc = serviceDispatchOut(&g_esSrv, 11, num_rights_ids_written,
|
||||||
|
.buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
|
||||||
|
.buffers = { { outBuf, bufSize } },
|
||||||
|
);
|
||||||
|
if (R_SUCCEEDED(rc) && numRightsIdsWritten) *numRightsIdsWritten = num_rights_ids_written;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result esListPersonalizedTicket(u32 *numRightsIdsWritten, RightsId *outBuf, size_t bufSize)
|
||||||
|
{
|
||||||
|
u32 num_rights_ids_written;
|
||||||
|
|
||||||
|
Result rc = serviceDispatchOut(&g_esSrv, 12, num_rights_ids_written,
|
||||||
|
.buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
|
||||||
|
.buffers = { { outBuf, bufSize } },
|
||||||
|
);
|
||||||
|
if (R_SUCCEEDED(rc) && numRightsIdsWritten) *numRightsIdsWritten = num_rights_ids_written;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
16
source/nx/es.h
Normal file
16
source/nx/es.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <switch/types.h>
|
||||||
|
#include <switch/services/ncm.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u8 c[0x10];
|
||||||
|
} RightsId;
|
||||||
|
|
||||||
|
Result esInitialize();
|
||||||
|
void esExit();
|
||||||
|
|
||||||
|
Result esCountCommonTicket(u32 *num_tickets); //9
|
||||||
|
Result esCountPersonalizedTicket(u32 *num_tickets); // 10
|
||||||
|
Result esListCommonTicket(u32 *numRightsIdsWritten, RightsId *outBuf, size_t bufSize);
|
||||||
|
Result esListPersonalizedTicket(u32 *numRightsIdsWritten, RightsId *outBuf, size_t bufSize);
|
56
source/service_guard.h
Normal file
56
source/service_guard.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <switch/types.h>
|
||||||
|
#include <switch/result.h>
|
||||||
|
#include <switch/kernel/mutex.h>
|
||||||
|
#include <switch/sf/service.h>
|
||||||
|
#include <switch/services/sm.h>
|
||||||
|
|
||||||
|
typedef struct ServiceGuard {
|
||||||
|
Mutex mutex;
|
||||||
|
u32 refCount;
|
||||||
|
} ServiceGuard;
|
||||||
|
|
||||||
|
NX_INLINE bool serviceGuardBeginInit(ServiceGuard* g)
|
||||||
|
{
|
||||||
|
mutexLock(&g->mutex);
|
||||||
|
return (g->refCount++) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
NX_INLINE Result serviceGuardEndInit(ServiceGuard* g, Result rc, void (*cleanupFunc)(void))
|
||||||
|
{
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
cleanupFunc();
|
||||||
|
--g->refCount;
|
||||||
|
}
|
||||||
|
mutexUnlock(&g->mutex);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
NX_INLINE void serviceGuardExit(ServiceGuard* g, void (*cleanupFunc)(void))
|
||||||
|
{
|
||||||
|
mutexLock(&g->mutex);
|
||||||
|
if (g->refCount && (--g->refCount) == 0)
|
||||||
|
cleanupFunc();
|
||||||
|
mutexUnlock(&g->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NX_GENERATE_SERVICE_GUARD_PARAMS(name, _paramdecl, _parampass) \
|
||||||
|
\
|
||||||
|
static ServiceGuard g_##name##Guard; \
|
||||||
|
NX_INLINE Result _##name##Initialize _paramdecl; \
|
||||||
|
static void _##name##Cleanup(void); \
|
||||||
|
\
|
||||||
|
Result name##Initialize _paramdecl \
|
||||||
|
{ \
|
||||||
|
Result rc = 0; \
|
||||||
|
if (serviceGuardBeginInit(&g_##name##Guard)) \
|
||||||
|
rc = _##name##Initialize _parampass; \
|
||||||
|
return serviceGuardEndInit(&g_##name##Guard, rc, _##name##Cleanup); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void name##Exit(void) \
|
||||||
|
{ \
|
||||||
|
serviceGuardExit(&g_##name##Guard, _##name##Cleanup); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NX_GENERATE_SERVICE_GUARD(name) NX_GENERATE_SERVICE_GUARD_PARAMS(name, (void), ())
|
@ -1,56 +0,0 @@
|
|||||||
#include "set_ext.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <switch.h>
|
|
||||||
#include <switch/arm/atomics.h>
|
|
||||||
|
|
||||||
static Service g_setcalSrv;
|
|
||||||
static u64 g_refCntCal;
|
|
||||||
|
|
||||||
Result setcalInitialize(void) {
|
|
||||||
atomicIncrement64(&g_refCntCal);
|
|
||||||
|
|
||||||
if (serviceIsActive(&g_setcalSrv))
|
|
||||||
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized);
|
|
||||||
|
|
||||||
return smGetService(&g_setcalSrv, "set:cal");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setcalExit(void) {
|
|
||||||
if (atomicDecrement64(&g_refCntCal) == 0) {
|
|
||||||
serviceClose(&g_setcalSrv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Result setcalGetEticketDeviceKey(u8 *key) {
|
|
||||||
IpcCommand c;
|
|
||||||
ipcInitialize(&c);
|
|
||||||
ipcAddRecvBuffer(&c, key, 0x244, 0);
|
|
||||||
|
|
||||||
struct {
|
|
||||||
u64 magic;
|
|
||||||
u64 cmd_id;
|
|
||||||
} *raw;
|
|
||||||
|
|
||||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
|
||||||
|
|
||||||
raw->magic = SFCI_MAGIC;
|
|
||||||
raw->cmd_id = 21;
|
|
||||||
|
|
||||||
Result rc = serviceIpcDispatch(&g_setcalSrv);
|
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc)) {
|
|
||||||
IpcParsedCommand r;
|
|
||||||
ipcParse(&r);
|
|
||||||
|
|
||||||
struct {
|
|
||||||
u64 magic;
|
|
||||||
u64 result;
|
|
||||||
} *resp = r.Raw;
|
|
||||||
|
|
||||||
rc = resp->result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <switch.h>
|
|
||||||
|
|
||||||
Result setcalInitialize(void);
|
|
||||||
void setcalExit(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Gets the extended ETicket RSA-2048 Key from CAL0
|
|
||||||
* @param key Pointer to 0x244-byte output buffer.
|
|
||||||
*/
|
|
||||||
Result setcalGetEticketDeviceKey(u8 *key);
|
|
113
source/sha256.c
113
source/sha256.c
@ -1,113 +0,0 @@
|
|||||||
/* Based on linux source code */
|
|
||||||
/*
|
|
||||||
* sha256_base.h - core logic for SHA-256 implementations
|
|
||||||
*
|
|
||||||
* Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License version 2 as
|
|
||||||
* published by the Free Software Foundation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include "sha256.h"
|
|
||||||
|
|
||||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
||||||
|
|
||||||
void sha256_block_data_order (uint32_t *ctx, const void *in, size_t num);
|
|
||||||
|
|
||||||
int sha256_init(struct sha256_state *sctx)
|
|
||||||
{
|
|
||||||
sctx->state[0] = SHA256_H0;
|
|
||||||
sctx->state[1] = SHA256_H1;
|
|
||||||
sctx->state[2] = SHA256_H2;
|
|
||||||
sctx->state[3] = SHA256_H3;
|
|
||||||
sctx->state[4] = SHA256_H4;
|
|
||||||
sctx->state[5] = SHA256_H5;
|
|
||||||
sctx->state[6] = SHA256_H6;
|
|
||||||
sctx->state[7] = SHA256_H7;
|
|
||||||
sctx->count = 0;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sha256_update(struct sha256_state *sctx,
|
|
||||||
const void *data,
|
|
||||||
size_t len)
|
|
||||||
{
|
|
||||||
const u8 *data8 = (const u8 *)data;
|
|
||||||
unsigned int len32 = (unsigned int)len;
|
|
||||||
unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
|
|
||||||
|
|
||||||
sctx->count += len32;
|
|
||||||
|
|
||||||
if (unlikely((partial + len32) >= SHA256_BLOCK_SIZE)) {
|
|
||||||
int blocks;
|
|
||||||
|
|
||||||
if (partial) {
|
|
||||||
int p = SHA256_BLOCK_SIZE - partial;
|
|
||||||
|
|
||||||
memcpy(sctx->buf + partial, data8, p);
|
|
||||||
data8 += p;
|
|
||||||
len32 -= p;
|
|
||||||
|
|
||||||
sha256_block_data_order(sctx->state, sctx->buf, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
blocks = len32 / SHA256_BLOCK_SIZE;
|
|
||||||
len32 %= SHA256_BLOCK_SIZE;
|
|
||||||
|
|
||||||
if (blocks) {
|
|
||||||
sha256_block_data_order(sctx->state, data8, blocks);
|
|
||||||
data8 += blocks * SHA256_BLOCK_SIZE;
|
|
||||||
}
|
|
||||||
partial = 0;
|
|
||||||
}
|
|
||||||
if (len32)
|
|
||||||
memcpy(sctx->buf + partial, data8, len32);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sha256_finalize(struct sha256_state *sctx)
|
|
||||||
{
|
|
||||||
const int bit_offset = SHA256_BLOCK_SIZE - sizeof(u64);
|
|
||||||
u64 *bits = (u64 *)(sctx->buf + bit_offset);
|
|
||||||
unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
|
|
||||||
|
|
||||||
sctx->buf[partial++] = 0x80;
|
|
||||||
if (partial > bit_offset) {
|
|
||||||
memset(sctx->buf + partial, 0x0, SHA256_BLOCK_SIZE - partial);
|
|
||||||
partial = 0;
|
|
||||||
|
|
||||||
sha256_block_data_order(sctx->state, sctx->buf, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(sctx->buf + partial, 0x0, bit_offset - partial);
|
|
||||||
*bits = __builtin_bswap64(sctx->count << 3);
|
|
||||||
sha256_block_data_order(sctx->state, sctx->buf, 1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sha256_finish(struct sha256_state *sctx, void *out)
|
|
||||||
{
|
|
||||||
unsigned int digest_size = 32;
|
|
||||||
u32 *digest = (u32 *)out;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
// Switch: misalignment shouldn't be a problem here...
|
|
||||||
for (i = 0; digest_size > 0; i++, digest_size -= sizeof(u32))
|
|
||||||
*digest++ = __builtin_bswap32(sctx->state[i]);
|
|
||||||
|
|
||||||
*sctx = (struct sha256_state){};
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,36 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
/* Based on linux source code */
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <switch/types.h>
|
|
||||||
|
|
||||||
#define SHA256_DIGEST_SIZE 32
|
|
||||||
#define SHA256_BLOCK_SIZE 64
|
|
||||||
|
|
||||||
#define SHA256_H0 0x6a09e667UL
|
|
||||||
#define SHA256_H1 0xbb67ae85UL
|
|
||||||
#define SHA256_H2 0x3c6ef372UL
|
|
||||||
#define SHA256_H3 0xa54ff53aUL
|
|
||||||
#define SHA256_H4 0x510e527fUL
|
|
||||||
#define SHA256_H5 0x9b05688cUL
|
|
||||||
#define SHA256_H6 0x1f83d9abUL
|
|
||||||
#define SHA256_H7 0x5be0cd19UL
|
|
||||||
|
|
||||||
struct sha256_state {
|
|
||||||
u32 state[SHA256_DIGEST_SIZE / 4];
|
|
||||||
u64 count;
|
|
||||||
u8 buf[SHA256_BLOCK_SIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
int sha256_init(struct sha256_state *sctx);
|
|
||||||
int sha256_update(struct sha256_state *sctx, const void *data, size_t len);
|
|
||||||
int sha256_finalize(struct sha256_state *sctx);
|
|
||||||
int sha256_finish(struct sha256_state *sctx, void *out);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,163 +0,0 @@
|
|||||||
.section .text.sha256_armv8, "ax", %progbits
|
|
||||||
.align 5
|
|
||||||
.arch armv8-a+crypto
|
|
||||||
|
|
||||||
# SHA256 assembly implementation for ARMv8 AArch64 (based on linux source code)
|
|
||||||
|
|
||||||
.global sha256_block_data_order
|
|
||||||
.type sha256_block_data_order,%function
|
|
||||||
sha256_block_data_order:
|
|
||||||
|
|
||||||
.Lsha256prolog:
|
|
||||||
|
|
||||||
stp x29, x30, [sp,#-64]!
|
|
||||||
mov x29, sp
|
|
||||||
adr x3, .LKConstant256
|
|
||||||
str q8, [sp, #16]
|
|
||||||
ld1 {v16.4s-v19.4s}, [x3], #64
|
|
||||||
ld1 {v0.4s}, [x0], #16
|
|
||||||
ld1 {v20.4s-v23.4s}, [x3], #64
|
|
||||||
add x2, x1, x2, lsl #6
|
|
||||||
ld1 {v1.4s}, [x0]
|
|
||||||
ld1 {v24.4s-v27.4s}, [x3], #64
|
|
||||||
sub x0, x0, #16
|
|
||||||
str q9, [sp, #32]
|
|
||||||
str q10, [sp, #48]
|
|
||||||
ld1 {v28.4s-v31.4s}, [x3], #64
|
|
||||||
|
|
||||||
.Lsha256loop:
|
|
||||||
|
|
||||||
ld1 {v5.16b-v8.16b}, [x1], #64
|
|
||||||
mov v2.16b, v0.16b
|
|
||||||
mov v3.16b, v1.16b
|
|
||||||
|
|
||||||
rev32 v5.16b, v5.16b
|
|
||||||
rev32 v6.16b, v6.16b
|
|
||||||
add v9.4s, v5.4s, v16.4s
|
|
||||||
rev32 v7.16b, v7.16b
|
|
||||||
add v10.4s, v6.4s, v17.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v5.4s, v6.4s
|
|
||||||
rev32 v8.16b, v8.16b
|
|
||||||
add v9.4s, v7.4s, v18.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v6.4s, v7.4s
|
|
||||||
sha256su1 v5.4s, v7.4s, v8.4s
|
|
||||||
add v10.4s, v8.4s, v19.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v7.4s, v8.4s
|
|
||||||
sha256su1 v6.4s, v8.4s, v5.4s
|
|
||||||
add v9.4s, v5.4s, v20.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v8.4s, v5.4s
|
|
||||||
sha256su1 v7.4s, v5.4s, v6.4s
|
|
||||||
add v10.4s, v6.4s, v21.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v5.4s, v6.4s
|
|
||||||
sha256su1 v8.4s, v6.4s, v7.4s
|
|
||||||
add v9.4s, v7.4s, v22.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v6.4s, v7.4s
|
|
||||||
sha256su1 v5.4s, v7.4s, v8.4s
|
|
||||||
add v10.4s, v8.4s, v23.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v7.4s, v8.4s
|
|
||||||
sha256su1 v6.4s, v8.4s, v5.4s
|
|
||||||
add v9.4s, v5.4s, v24.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v8.4s, v5.4s
|
|
||||||
sha256su1 v7.4s, v5.4s, v6.4s
|
|
||||||
add v10.4s, v6.4s, v25.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v5.4s, v6.4s
|
|
||||||
sha256su1 v8.4s, v6.4s, v7.4s
|
|
||||||
add v9.4s, v7.4s, v26.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v6.4s, v7.4s
|
|
||||||
sha256su1 v5.4s, v7.4s, v8.4s
|
|
||||||
add v10.4s, v8.4s, v27.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su0 v7.4s, v8.4s
|
|
||||||
sha256su1 v6.4s, v8.4s, v5.4s
|
|
||||||
add v9.4s, v5.4s, v28.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
sha256su0 v8.4s, v5.4s
|
|
||||||
sha256su1 v7.4s, v5.4s, v6.4s
|
|
||||||
add v10.4s, v6.4s, v29.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
sha256su1 v8.4s, v6.4s, v7.4s
|
|
||||||
add v9.4s, v7.4s, v30.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
add v10.4s, v8.4s, v31.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v9.4s
|
|
||||||
sha256h2 q3, q4, v9.4s
|
|
||||||
mov v4.16b, v2.16b
|
|
||||||
sha256h q2, q3, v10.4s
|
|
||||||
sha256h2 q3, q4, v10.4s
|
|
||||||
cmp x1, x2
|
|
||||||
add v1.4s, v1.4s, v3.4s
|
|
||||||
add v0.4s, v0.4s, v2.4s
|
|
||||||
b.ne .Lsha256loop
|
|
||||||
|
|
||||||
.Lsha256epilog:
|
|
||||||
|
|
||||||
st1 {v0.4s,v1.4s}, [x0]
|
|
||||||
ldr q10, [sp, #48]
|
|
||||||
ldr q9, [sp, #32]
|
|
||||||
ldr q8, [sp, #16]
|
|
||||||
ldr x29, [sp], #64
|
|
||||||
ret
|
|
||||||
|
|
||||||
.align 5
|
|
||||||
.LKConstant256:
|
|
||||||
.word 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
|
|
||||||
.word 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
|
|
||||||
.word 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
|
|
||||||
.word 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
|
|
||||||
.word 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
|
|
||||||
.word 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
|
|
||||||
.word 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
|
|
||||||
.word 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
|
|
||||||
.word 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
|
|
||||||
.word 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
|
|
||||||
.word 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
|
|
||||||
.word 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
|
|
||||||
.word 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
|
|
||||||
.word 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
|
|
||||||
.word 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
|
|
||||||
.word 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
|
||||||
|
|
||||||
.size sha256_block_data_order,.-sha256_block_data_order
|
|
||||||
.align 2
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user