gemba 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/THIRD_PARTY_NOTICES +37 -2
- data/assets/placeholder_boxart.png +0 -0
- data/bin/gemba +2 -2
- data/ext/gemba/extconf.rb +24 -1
- data/ext/gemba/gemba_ext.c +436 -2
- data/ext/gemba/gemba_ext.h +2 -0
- data/gemba.gemspec +5 -3
- data/lib/gemba/achievements/achievement.rb +23 -0
- data/lib/gemba/achievements/backend.rb +186 -0
- data/lib/gemba/achievements/cache.rb +70 -0
- data/lib/gemba/achievements/credentials_presenter.rb +142 -0
- data/lib/gemba/achievements/fake_backend.rb +205 -0
- data/lib/gemba/achievements/null_backend.rb +11 -0
- data/lib/gemba/achievements/offline_backend.rb +168 -0
- data/lib/gemba/achievements/retro_achievements/backend.rb +453 -0
- data/lib/gemba/achievements/retro_achievements/cli_sync_requester.rb +64 -0
- data/lib/gemba/achievements/retro_achievements/ping_worker.rb +27 -0
- data/lib/gemba/achievements.rb +19 -0
- data/lib/gemba/achievements_window.rb +556 -0
- data/lib/gemba/app_controller.rb +1015 -0
- data/lib/gemba/bios.rb +54 -0
- data/lib/gemba/boxart_fetcher/libretro_backend.rb +39 -0
- data/lib/gemba/boxart_fetcher/null_backend.rb +12 -0
- data/lib/gemba/boxart_fetcher.rb +79 -0
- data/lib/gemba/bus_emitter.rb +13 -0
- data/lib/gemba/child_window.rb +24 -1
- data/lib/gemba/cli/commands/config_cmd.rb +83 -0
- data/lib/gemba/cli/commands/decode.rb +154 -0
- data/lib/gemba/cli/commands/patch.rb +78 -0
- data/lib/gemba/cli/commands/play.rb +78 -0
- data/lib/gemba/cli/commands/record.rb +114 -0
- data/lib/gemba/cli/commands/replay.rb +161 -0
- data/lib/gemba/cli/commands/retro_achievements.rb +213 -0
- data/lib/gemba/cli/commands/version.rb +22 -0
- data/lib/gemba/cli.rb +52 -364
- data/lib/gemba/config.rb +135 -1
- data/lib/gemba/data/gb_games.json +1 -0
- data/lib/gemba/data/gb_md5.json +1 -0
- data/lib/gemba/data/gba_games.json +1 -0
- data/lib/gemba/data/gba_md5.json +1 -0
- data/lib/gemba/data/gbc_games.json +1 -0
- data/lib/gemba/data/gbc_md5.json +1 -0
- data/lib/gemba/emulator_frame.rb +1060 -0
- data/lib/gemba/event_bus.rb +48 -0
- data/lib/gemba/frame_stack.rb +60 -0
- data/lib/gemba/game_index.rb +84 -0
- data/lib/gemba/game_picker_frame.rb +268 -0
- data/lib/gemba/gamepad_map.rb +103 -0
- data/lib/gemba/headless.rb +6 -5
- data/lib/gemba/headless_player.rb +33 -3
- data/lib/gemba/help_window.rb +61 -0
- data/lib/gemba/hotkey_map.rb +3 -1
- data/lib/gemba/input_recorder.rb +107 -0
- data/lib/gemba/input_replayer.rb +119 -0
- data/lib/gemba/keyboard_map.rb +90 -0
- data/lib/gemba/locales/en.yml +97 -5
- data/lib/gemba/locales/ja.yml +97 -5
- data/lib/gemba/main_window.rb +56 -0
- data/lib/gemba/modal_stack.rb +81 -0
- data/lib/gemba/patcher_window.rb +223 -0
- data/lib/gemba/platform/gb.rb +21 -0
- data/lib/gemba/platform/gba.rb +21 -0
- data/lib/gemba/platform/gbc.rb +23 -0
- data/lib/gemba/platform.rb +20 -0
- data/lib/gemba/platform_open.rb +19 -0
- data/lib/gemba/recorder.rb +4 -3
- data/lib/gemba/replay_player.rb +691 -0
- data/lib/gemba/rom_info.rb +57 -0
- data/lib/gemba/rom_info_window.rb +16 -3
- data/lib/gemba/rom_library.rb +106 -0
- data/lib/gemba/rom_overrides.rb +47 -0
- data/lib/gemba/rom_patcher/bps.rb +161 -0
- data/lib/gemba/rom_patcher/ips.rb +101 -0
- data/lib/gemba/rom_patcher/ups.rb +118 -0
- data/lib/gemba/rom_patcher.rb +109 -0
- data/lib/gemba/{rom_loader.rb → rom_resolver.rb} +7 -6
- data/lib/gemba/runtime.rb +59 -26
- data/lib/gemba/save_state_manager.rb +4 -7
- data/lib/gemba/save_state_picker.rb +17 -4
- data/lib/gemba/session_logger.rb +64 -0
- data/lib/gemba/settings/audio_tab.rb +77 -0
- data/lib/gemba/settings/gamepad_tab.rb +351 -0
- data/lib/gemba/settings/hotkeys_tab.rb +259 -0
- data/lib/gemba/settings/paths.rb +11 -0
- data/lib/gemba/settings/recording_tab.rb +83 -0
- data/lib/gemba/settings/save_states_tab.rb +91 -0
- data/lib/gemba/settings/system_tab.rb +362 -0
- data/lib/gemba/settings/video_tab.rb +318 -0
- data/lib/gemba/settings_window.rb +162 -1036
- data/lib/gemba/version.rb +1 -1
- data/lib/gemba/virtual_keyboard.rb +19 -0
- data/lib/gemba.rb +2 -12
- data/test/achievements_window/test_bulk_sync.rb +218 -0
- data/test/achievements_window/test_bus_events.rb +125 -0
- data/test/achievements_window/test_close_confirmation.rb +201 -0
- data/test/achievements_window/test_initial_state.rb +164 -0
- data/test/achievements_window/test_sorting.rb +227 -0
- data/test/achievements_window/test_tree_rendering.rb +133 -0
- data/test/fixtures/fake_bios.bin +0 -0
- data/test/fixtures/pong.gba +0 -0
- data/test/fixtures/test.gb +0 -0
- data/test/fixtures/test.gbc +0 -0
- data/test/fixtures/test_quicksave.ss +0 -0
- data/test/screenshots/no_focus.png +0 -0
- data/test/shared/teek_test_worker.rb +17 -1
- data/test/shared/tk_test_helper.rb +91 -4
- data/test/support/achievements_window_helpers.rb +18 -0
- data/test/support/fake_core.rb +25 -0
- data/test/support/fake_ra_runtime.rb +74 -0
- data/test/support/fake_requester.rb +68 -0
- data/test/support/player_helpers.rb +20 -5
- data/test/test_achievement.rb +32 -0
- data/test/{test_player.rb → test_app_controller.rb} +353 -85
- data/test/test_bios.rb +123 -0
- data/test/test_boxart_fetcher.rb +150 -0
- data/test/test_cli.rb +17 -265
- data/test/test_cli_config.rb +64 -0
- data/test/test_cli_decode.rb +97 -0
- data/test/test_cli_patch.rb +58 -0
- data/test/test_cli_play.rb +213 -0
- data/test/test_cli_ra.rb +175 -0
- data/test/test_cli_record.rb +69 -0
- data/test/test_cli_replay.rb +72 -0
- data/test/test_cli_sync_requester.rb +152 -0
- data/test/test_cli_version.rb +27 -0
- data/test/test_config.rb +2 -3
- data/test/test_config_ra.rb +69 -0
- data/test/test_core.rb +62 -1
- data/test/test_credentials_presenter.rb +192 -0
- data/test/test_event_bus.rb +100 -0
- data/test/test_fake_backend_achievements.rb +130 -0
- data/test/test_fake_backend_auth.rb +68 -0
- data/test/test_game_index.rb +77 -0
- data/test/test_game_picker_frame.rb +310 -0
- data/test/test_gamepad_map.rb +1 -3
- data/test/test_headless_player.rb +17 -3
- data/test/test_help_window.rb +82 -0
- data/test/test_hotkey_map.rb +22 -1
- data/test/test_input_recorder.rb +179 -0
- data/test/test_input_replay_determinism.rb +113 -0
- data/test/test_input_replayer.rb +162 -0
- data/test/test_keyboard_map.rb +1 -3
- data/test/test_libretro_backend.rb +41 -0
- data/test/test_locale.rb +1 -1
- data/test/test_logging.rb +123 -0
- data/test/test_null_backend.rb +42 -0
- data/test/test_offline_backend.rb +116 -0
- data/test/test_overlay_renderer.rb +1 -1
- data/test/test_platform.rb +149 -0
- data/test/test_ra_backend.rb +313 -0
- data/test/test_ra_backend_unlock_gate.rb +56 -0
- data/test/test_recorder.rb +0 -3
- data/test/test_replay_player.rb +316 -0
- data/test/test_rom_info.rb +149 -0
- data/test/test_rom_overrides.rb +86 -0
- data/test/test_rom_patcher.rb +382 -0
- data/test/{test_rom_loader.rb → test_rom_resolver.rb} +25 -26
- data/test/test_save_state_manager.rb +2 -4
- data/test/test_settings_audio.rb +107 -0
- data/test/test_settings_hotkeys.rb +83 -66
- data/test/test_settings_recording.rb +49 -0
- data/test/test_settings_save_states.rb +97 -0
- data/test/test_settings_system.rb +133 -0
- data/test/test_settings_video.rb +450 -0
- data/test/test_settings_window.rb +76 -507
- data/test/test_tip_service.rb +6 -6
- data/test/test_toast_overlay.rb +1 -1
- data/test/test_virtual_events.rb +156 -0
- data/test/test_virtual_keyboard.rb +1 -1
- data/vendor/rcheevos/CHANGELOG.md +495 -0
- data/vendor/rcheevos/LICENSE +21 -0
- data/vendor/rcheevos/Package.swift +33 -0
- data/vendor/rcheevos/README.md +67 -0
- data/vendor/rcheevos/include/module.modulemap +70 -0
- data/vendor/rcheevos/include/rc_api_editor.h +296 -0
- data/vendor/rcheevos/include/rc_api_info.h +280 -0
- data/vendor/rcheevos/include/rc_api_request.h +77 -0
- data/vendor/rcheevos/include/rc_api_runtime.h +417 -0
- data/vendor/rcheevos/include/rc_api_user.h +262 -0
- data/vendor/rcheevos/include/rc_client.h +877 -0
- data/vendor/rcheevos/include/rc_client_raintegration.h +101 -0
- data/vendor/rcheevos/include/rc_consoles.h +138 -0
- data/vendor/rcheevos/include/rc_error.h +59 -0
- data/vendor/rcheevos/include/rc_export.h +100 -0
- data/vendor/rcheevos/include/rc_hash.h +200 -0
- data/vendor/rcheevos/include/rc_runtime.h +148 -0
- data/vendor/rcheevos/include/rc_runtime_types.h +452 -0
- data/vendor/rcheevos/include/rc_util.h +51 -0
- data/vendor/rcheevos/include/rcheevos.h +8 -0
- data/vendor/rcheevos/src/rapi/rc_api_common.c +1379 -0
- data/vendor/rcheevos/src/rapi/rc_api_common.h +88 -0
- data/vendor/rcheevos/src/rapi/rc_api_editor.c +625 -0
- data/vendor/rcheevos/src/rapi/rc_api_info.c +587 -0
- data/vendor/rcheevos/src/rapi/rc_api_runtime.c +901 -0
- data/vendor/rcheevos/src/rapi/rc_api_user.c +483 -0
- data/vendor/rcheevos/src/rc_client.c +6941 -0
- data/vendor/rcheevos/src/rc_client_external.c +281 -0
- data/vendor/rcheevos/src/rc_client_external.h +177 -0
- data/vendor/rcheevos/src/rc_client_external_versions.h +171 -0
- data/vendor/rcheevos/src/rc_client_internal.h +409 -0
- data/vendor/rcheevos/src/rc_client_raintegration.c +566 -0
- data/vendor/rcheevos/src/rc_client_raintegration_internal.h +61 -0
- data/vendor/rcheevos/src/rc_client_types.natvis +396 -0
- data/vendor/rcheevos/src/rc_compat.c +251 -0
- data/vendor/rcheevos/src/rc_compat.h +121 -0
- data/vendor/rcheevos/src/rc_libretro.c +915 -0
- data/vendor/rcheevos/src/rc_libretro.h +98 -0
- data/vendor/rcheevos/src/rc_util.c +199 -0
- data/vendor/rcheevos/src/rc_version.c +11 -0
- data/vendor/rcheevos/src/rc_version.h +32 -0
- data/vendor/rcheevos/src/rcheevos/alloc.c +312 -0
- data/vendor/rcheevos/src/rcheevos/condition.c +754 -0
- data/vendor/rcheevos/src/rcheevos/condset.c +777 -0
- data/vendor/rcheevos/src/rcheevos/consoleinfo.c +1215 -0
- data/vendor/rcheevos/src/rcheevos/format.c +330 -0
- data/vendor/rcheevos/src/rcheevos/lboard.c +287 -0
- data/vendor/rcheevos/src/rcheevos/memref.c +805 -0
- data/vendor/rcheevos/src/rcheevos/operand.c +607 -0
- data/vendor/rcheevos/src/rcheevos/rc_internal.h +390 -0
- data/vendor/rcheevos/src/rcheevos/rc_runtime_types.natvis +541 -0
- data/vendor/rcheevos/src/rcheevos/rc_validate.c +1406 -0
- data/vendor/rcheevos/src/rcheevos/rc_validate.h +18 -0
- data/vendor/rcheevos/src/rcheevos/richpresence.c +922 -0
- data/vendor/rcheevos/src/rcheevos/runtime.c +852 -0
- data/vendor/rcheevos/src/rcheevos/runtime_progress.c +1073 -0
- data/vendor/rcheevos/src/rcheevos/trigger.c +344 -0
- data/vendor/rcheevos/src/rcheevos/value.c +935 -0
- data/vendor/rcheevos/src/rhash/aes.c +480 -0
- data/vendor/rcheevos/src/rhash/aes.h +49 -0
- data/vendor/rcheevos/src/rhash/cdreader.c +838 -0
- data/vendor/rcheevos/src/rhash/hash.c +1402 -0
- data/vendor/rcheevos/src/rhash/hash_disc.c +1340 -0
- data/vendor/rcheevos/src/rhash/hash_encrypted.c +566 -0
- data/vendor/rcheevos/src/rhash/hash_rom.c +426 -0
- data/vendor/rcheevos/src/rhash/hash_zip.c +460 -0
- data/vendor/rcheevos/src/rhash/md5.c +382 -0
- data/vendor/rcheevos/src/rhash/md5.h +91 -0
- data/vendor/rcheevos/src/rhash/rc_hash_internal.h +116 -0
- data/vendor/rcheevos/test/libretro.h +205 -0
- data/vendor/rcheevos/test/rapi/test_rc_api_common.c +941 -0
- data/vendor/rcheevos/test/rapi/test_rc_api_editor.c +931 -0
- data/vendor/rcheevos/test/rapi/test_rc_api_info.c +545 -0
- data/vendor/rcheevos/test/rapi/test_rc_api_runtime.c +2213 -0
- data/vendor/rcheevos/test/rapi/test_rc_api_user.c +998 -0
- data/vendor/rcheevos/test/rcheevos/mock_memory.h +32 -0
- data/vendor/rcheevos/test/rcheevos/test_condition.c +570 -0
- data/vendor/rcheevos/test/rcheevos/test_condset.c +5170 -0
- data/vendor/rcheevos/test/rcheevos/test_consoleinfo.c +203 -0
- data/vendor/rcheevos/test/rcheevos/test_format.c +112 -0
- data/vendor/rcheevos/test/rcheevos/test_lboard.c +746 -0
- data/vendor/rcheevos/test/rcheevos/test_memref.c +520 -0
- data/vendor/rcheevos/test/rcheevos/test_operand.c +692 -0
- data/vendor/rcheevos/test/rcheevos/test_rc_validate.c +502 -0
- data/vendor/rcheevos/test/rcheevos/test_richpresence.c +1564 -0
- data/vendor/rcheevos/test/rcheevos/test_runtime.c +1667 -0
- data/vendor/rcheevos/test/rcheevos/test_runtime_progress.c +1821 -0
- data/vendor/rcheevos/test/rcheevos/test_timing.c +166 -0
- data/vendor/rcheevos/test/rcheevos/test_trigger.c +2521 -0
- data/vendor/rcheevos/test/rcheevos/test_value.c +870 -0
- data/vendor/rcheevos/test/rcheevos-test.sln +46 -0
- data/vendor/rcheevos/test/rcheevos-test.vcxproj +239 -0
- data/vendor/rcheevos/test/rcheevos-test.vcxproj.filters +335 -0
- data/vendor/rcheevos/test/rhash/data.c +657 -0
- data/vendor/rcheevos/test/rhash/data.h +32 -0
- data/vendor/rcheevos/test/rhash/mock_filereader.c +236 -0
- data/vendor/rcheevos/test/rhash/mock_filereader.h +31 -0
- data/vendor/rcheevos/test/rhash/test_cdreader.c +920 -0
- data/vendor/rcheevos/test/rhash/test_hash.c +310 -0
- data/vendor/rcheevos/test/rhash/test_hash_disc.c +1450 -0
- data/vendor/rcheevos/test/rhash/test_hash_rom.c +899 -0
- data/vendor/rcheevos/test/rhash/test_hash_zip.c +551 -0
- data/vendor/rcheevos/test/test.c +113 -0
- data/vendor/rcheevos/test/test_framework.h +205 -0
- data/vendor/rcheevos/test/test_rc_client.c +10509 -0
- data/vendor/rcheevos/test/test_rc_client_external.c +2197 -0
- data/vendor/rcheevos/test/test_rc_client_raintegration.c +441 -0
- data/vendor/rcheevos/test/test_rc_libretro.c +952 -0
- data/vendor/rcheevos/test/test_types.natvis +9 -0
- data/vendor/rcheevos/validator/validator.c +658 -0
- data/vendor/rcheevos/validator/validator.vcxproj +152 -0
- data/vendor/rcheevos/validator/validator.vcxproj.filters +82 -0
- metadata +274 -11
- data/lib/gemba/input_mappings.rb +0 -214
- data/lib/gemba/player.rb +0 -1515
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
/* This file is sourced from https://github.com/kokke/tiny-AES-c, with unused code excised.
|
|
2
|
+
* This code is licensed under the Unlicense license, effectively public domain.
|
|
3
|
+
* https://github.com/kokke/tiny-AES-c/blob/f06ac37/unlicense.txt
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
|
|
8
|
+
This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode.
|
|
9
|
+
Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
|
|
10
|
+
|
|
11
|
+
The implementation is verified against the test vectors in:
|
|
12
|
+
National Institute of Standards and Technology Special Publication 800-38A 2001 ED
|
|
13
|
+
|
|
14
|
+
ECB-AES128
|
|
15
|
+
----------
|
|
16
|
+
|
|
17
|
+
plain-text:
|
|
18
|
+
6bc1bee22e409f96e93d7e117393172a
|
|
19
|
+
ae2d8a571e03ac9c9eb76fac45af8e51
|
|
20
|
+
30c81c46a35ce411e5fbc1191a0a52ef
|
|
21
|
+
f69f2445df4f9b17ad2b417be66c3710
|
|
22
|
+
|
|
23
|
+
key:
|
|
24
|
+
2b7e151628aed2a6abf7158809cf4f3c
|
|
25
|
+
|
|
26
|
+
resulting cipher
|
|
27
|
+
3ad77bb40d7a3660a89ecaf32466ef97
|
|
28
|
+
f5d3d58503b9699de785895a96fdbaaf
|
|
29
|
+
43b1cd7f598ece23881b00e3ed030688
|
|
30
|
+
7b0c785e27e8ad3f8223207104725dd4
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
|
|
34
|
+
You should pad the end of the string with zeros if this is not the case.
|
|
35
|
+
For AES192/256 the key size is proportionally larger.
|
|
36
|
+
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/*****************************************************************************/
|
|
41
|
+
/* Includes: */
|
|
42
|
+
/*****************************************************************************/
|
|
43
|
+
#include <string.h> /* CBC mode, for memset */
|
|
44
|
+
#include "aes.h"
|
|
45
|
+
|
|
46
|
+
/*****************************************************************************/
|
|
47
|
+
/* Defines: */
|
|
48
|
+
/*****************************************************************************/
|
|
49
|
+
|
|
50
|
+
/* The number of columns comprising a state in AES. This is a constant in AES. Value=4 */
|
|
51
|
+
#define Nb 4
|
|
52
|
+
|
|
53
|
+
#define Nk 4 /* The number of 32 bit words in a key. */
|
|
54
|
+
#define Nr 10 /* The number of rounds in AES Cipher. */
|
|
55
|
+
|
|
56
|
+
/*****************************************************************************/
|
|
57
|
+
/* Private variables: */
|
|
58
|
+
/*****************************************************************************/
|
|
59
|
+
|
|
60
|
+
/* state - array holding the intermediate results during decryption. */
|
|
61
|
+
typedef uint8_t state_t[4][4];
|
|
62
|
+
|
|
63
|
+
/* The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
|
|
64
|
+
* The numbers below can be computed dynamically trading ROM for RAM -
|
|
65
|
+
* This can be useful in (embedded) bootloader applications, where ROM is often limited.
|
|
66
|
+
*/
|
|
67
|
+
static const uint8_t sbox[256] = {
|
|
68
|
+
/*0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
69
|
+
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
|
70
|
+
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
|
71
|
+
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
|
72
|
+
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
|
73
|
+
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
|
74
|
+
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
|
75
|
+
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
|
76
|
+
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
|
77
|
+
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
|
78
|
+
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
|
79
|
+
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
|
80
|
+
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
|
81
|
+
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
|
82
|
+
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
|
83
|
+
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
|
84
|
+
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
|
|
85
|
+
|
|
86
|
+
static const uint8_t rsbox[256] = {
|
|
87
|
+
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
|
88
|
+
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
|
89
|
+
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
|
90
|
+
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
|
91
|
+
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
|
|
92
|
+
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
|
93
|
+
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
|
|
94
|
+
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
|
|
95
|
+
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
|
96
|
+
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
|
|
97
|
+
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
|
98
|
+
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
|
99
|
+
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
|
100
|
+
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
|
101
|
+
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
|
102
|
+
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
|
|
103
|
+
|
|
104
|
+
/* The round constant word array, Rcon[i], contains the values given by
|
|
105
|
+
* x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
|
|
106
|
+
*/
|
|
107
|
+
static const uint8_t Rcon[11] = {
|
|
108
|
+
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
|
|
109
|
+
|
|
110
|
+
/*
|
|
111
|
+
* Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES-C/pull/12),
|
|
112
|
+
* that you can remove most of the elements in the Rcon array, because they are unused.
|
|
113
|
+
*
|
|
114
|
+
* From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
|
|
115
|
+
*
|
|
116
|
+
* "Only the first some of these constants are actually used – up to rcon[10] for AES-128 (as 11 round keys are needed),
|
|
117
|
+
* up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/*****************************************************************************/
|
|
122
|
+
/* Private functions: */
|
|
123
|
+
/*****************************************************************************/
|
|
124
|
+
|
|
125
|
+
#define getSBoxValue(num) (sbox[(num)])
|
|
126
|
+
|
|
127
|
+
/* This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. */
|
|
128
|
+
static void KeyExpansion(uint8_t RoundKey[AES_keyExpSize], const uint8_t Key[AES_KEYLEN])
|
|
129
|
+
{
|
|
130
|
+
unsigned i, j, k;
|
|
131
|
+
uint8_t tempa[4]; /* Used for the column/row operations */
|
|
132
|
+
|
|
133
|
+
/* The first round key is the key itself. */
|
|
134
|
+
for (i = 0; i < Nk; ++i)
|
|
135
|
+
{
|
|
136
|
+
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
|
|
137
|
+
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
|
|
138
|
+
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
|
|
139
|
+
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* All other round keys are found from the previous round keys. */
|
|
143
|
+
for (i = Nk; i < Nb * (Nr + 1); ++i)
|
|
144
|
+
{
|
|
145
|
+
{
|
|
146
|
+
k = (i - 1) * 4;
|
|
147
|
+
tempa[0]=RoundKey[k + 0];
|
|
148
|
+
tempa[1]=RoundKey[k + 1];
|
|
149
|
+
tempa[2]=RoundKey[k + 2];
|
|
150
|
+
tempa[3]=RoundKey[k + 3];
|
|
151
|
+
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (i % Nk == 0)
|
|
155
|
+
{
|
|
156
|
+
/* This function shifts the 4 bytes in a word to the left once. */
|
|
157
|
+
/* [a0,a1,a2,a3] becomes [a1,a2,a3,a0] */
|
|
158
|
+
|
|
159
|
+
/* Function RotWord() */
|
|
160
|
+
{
|
|
161
|
+
const uint8_t u8tmp = tempa[0];
|
|
162
|
+
tempa[0] = tempa[1];
|
|
163
|
+
tempa[1] = tempa[2];
|
|
164
|
+
tempa[2] = tempa[3];
|
|
165
|
+
tempa[3] = u8tmp;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* SubWord() is a function that takes a four-byte input word and
|
|
169
|
+
* applies the S-box to each of the four bytes to produce an output word.
|
|
170
|
+
*/
|
|
171
|
+
|
|
172
|
+
/* Function Subword() */
|
|
173
|
+
{
|
|
174
|
+
tempa[0] = getSBoxValue(tempa[0]);
|
|
175
|
+
tempa[1] = getSBoxValue(tempa[1]);
|
|
176
|
+
tempa[2] = getSBoxValue(tempa[2]);
|
|
177
|
+
tempa[3] = getSBoxValue(tempa[3]);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
tempa[0] = tempa[0] ^ Rcon[i/Nk];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
j = i * 4; k=(i - Nk) * 4;
|
|
184
|
+
RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
|
|
185
|
+
RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
|
|
186
|
+
RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
|
|
187
|
+
RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t key[AES_KEYLEN])
|
|
192
|
+
{
|
|
193
|
+
KeyExpansion(ctx->RoundKey, key);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t key[AES_KEYLEN], const uint8_t iv[AES_BLOCKLEN])
|
|
197
|
+
{
|
|
198
|
+
KeyExpansion(ctx->RoundKey, key);
|
|
199
|
+
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t iv[AES_BLOCKLEN])
|
|
203
|
+
{
|
|
204
|
+
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* This function adds the round key to state.
|
|
208
|
+
* The round key is added to the state by an XOR function.
|
|
209
|
+
*/
|
|
210
|
+
static void AddRoundKey(uint8_t round, state_t* state, const uint8_t RoundKey[AES_keyExpSize])
|
|
211
|
+
{
|
|
212
|
+
uint8_t i,j;
|
|
213
|
+
for (i = 0; i < 4; ++i)
|
|
214
|
+
{
|
|
215
|
+
for (j = 0; j < 4; ++j)
|
|
216
|
+
{
|
|
217
|
+
(*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/* The SubBytes Function Substitutes the values in the
|
|
223
|
+
* state matrix with values in an S-box.
|
|
224
|
+
*/
|
|
225
|
+
static void SubBytes(state_t* state)
|
|
226
|
+
{
|
|
227
|
+
uint8_t i, j;
|
|
228
|
+
for (i = 0; i < 4; ++i)
|
|
229
|
+
{
|
|
230
|
+
for (j = 0; j < 4; ++j)
|
|
231
|
+
{
|
|
232
|
+
(*state)[j][i] = getSBoxValue((*state)[j][i]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* The ShiftRows() function shifts the rows in the state to the left.
|
|
238
|
+
* Each row is shifted with different offset.
|
|
239
|
+
* Offset = Row number. So the first row is not shifted.
|
|
240
|
+
*/
|
|
241
|
+
static void ShiftRows(state_t* state)
|
|
242
|
+
{
|
|
243
|
+
uint8_t temp;
|
|
244
|
+
|
|
245
|
+
/* Rotate first row 1 columns to left */
|
|
246
|
+
temp = (*state)[0][1];
|
|
247
|
+
(*state)[0][1] = (*state)[1][1];
|
|
248
|
+
(*state)[1][1] = (*state)[2][1];
|
|
249
|
+
(*state)[2][1] = (*state)[3][1];
|
|
250
|
+
(*state)[3][1] = temp;
|
|
251
|
+
|
|
252
|
+
/* Rotate second row 2 columns to left */
|
|
253
|
+
temp = (*state)[0][2];
|
|
254
|
+
(*state)[0][2] = (*state)[2][2];
|
|
255
|
+
(*state)[2][2] = temp;
|
|
256
|
+
|
|
257
|
+
temp = (*state)[1][2];
|
|
258
|
+
(*state)[1][2] = (*state)[3][2];
|
|
259
|
+
(*state)[3][2] = temp;
|
|
260
|
+
|
|
261
|
+
/* Rotate third row 3 columns to left */
|
|
262
|
+
temp = (*state)[0][3];
|
|
263
|
+
(*state)[0][3] = (*state)[3][3];
|
|
264
|
+
(*state)[3][3] = (*state)[2][3];
|
|
265
|
+
(*state)[2][3] = (*state)[1][3];
|
|
266
|
+
(*state)[1][3] = temp;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static uint8_t xtime(uint8_t x)
|
|
270
|
+
{
|
|
271
|
+
return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* MixColumns function mixes the columns of the state matrix */
|
|
275
|
+
static void MixColumns(state_t* state)
|
|
276
|
+
{
|
|
277
|
+
uint8_t i;
|
|
278
|
+
uint8_t Tmp, Tm, t;
|
|
279
|
+
for (i = 0; i < 4; ++i)
|
|
280
|
+
{
|
|
281
|
+
t = (*state)[i][0];
|
|
282
|
+
Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
|
|
283
|
+
Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
|
|
284
|
+
Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
|
|
285
|
+
Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
|
|
286
|
+
Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/* Multiply is used to multiply numbers in the field GF(2^8)
|
|
291
|
+
* Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary
|
|
292
|
+
* The compiler seems to be able to vectorize the operation better this way.
|
|
293
|
+
* See https://github.com/kokke/tiny-AES-c/pull/34
|
|
294
|
+
*/
|
|
295
|
+
|
|
296
|
+
#define Multiply(x, y) \
|
|
297
|
+
( ((y & 1) * x) ^ \
|
|
298
|
+
((y>>1 & 1) * xtime(x)) ^ \
|
|
299
|
+
((y>>2 & 1) * xtime(xtime(x))) ^ \
|
|
300
|
+
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
|
|
301
|
+
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
|
|
302
|
+
|
|
303
|
+
#define getSBoxInvert(num) (rsbox[(num)])
|
|
304
|
+
|
|
305
|
+
/* MixColumns function mixes the columns of the state matrix.
|
|
306
|
+
* The method used to multiply may be difficult to understand for the inexperienced.
|
|
307
|
+
* Please use the references to gain more information.
|
|
308
|
+
*/
|
|
309
|
+
static void InvMixColumns(state_t* state)
|
|
310
|
+
{
|
|
311
|
+
int i;
|
|
312
|
+
uint8_t a, b, c, d;
|
|
313
|
+
for (i = 0; i < 4; ++i)
|
|
314
|
+
{
|
|
315
|
+
a = (*state)[i][0];
|
|
316
|
+
b = (*state)[i][1];
|
|
317
|
+
c = (*state)[i][2];
|
|
318
|
+
d = (*state)[i][3];
|
|
319
|
+
|
|
320
|
+
(*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
|
|
321
|
+
(*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
|
|
322
|
+
(*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
|
|
323
|
+
(*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
/* The SubBytes Function Substitutes the values in the
|
|
329
|
+
* state matrix with values in an S-box.
|
|
330
|
+
*/
|
|
331
|
+
static void InvSubBytes(state_t* state)
|
|
332
|
+
{
|
|
333
|
+
uint8_t i, j;
|
|
334
|
+
for (i = 0; i < 4; ++i)
|
|
335
|
+
{
|
|
336
|
+
for (j = 0; j < 4; ++j)
|
|
337
|
+
{
|
|
338
|
+
(*state)[j][i] = getSBoxInvert((*state)[j][i]);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static void InvShiftRows(state_t* state)
|
|
344
|
+
{
|
|
345
|
+
uint8_t temp;
|
|
346
|
+
|
|
347
|
+
/* Rotate first row 1 columns to right */
|
|
348
|
+
temp = (*state)[3][1];
|
|
349
|
+
(*state)[3][1] = (*state)[2][1];
|
|
350
|
+
(*state)[2][1] = (*state)[1][1];
|
|
351
|
+
(*state)[1][1] = (*state)[0][1];
|
|
352
|
+
(*state)[0][1] = temp;
|
|
353
|
+
|
|
354
|
+
/* Rotate second row 2 columns to right */
|
|
355
|
+
temp = (*state)[0][2];
|
|
356
|
+
(*state)[0][2] = (*state)[2][2];
|
|
357
|
+
(*state)[2][2] = temp;
|
|
358
|
+
|
|
359
|
+
temp = (*state)[1][2];
|
|
360
|
+
(*state)[1][2] = (*state)[3][2];
|
|
361
|
+
(*state)[3][2] = temp;
|
|
362
|
+
|
|
363
|
+
/* Rotate third row 3 columns to right */
|
|
364
|
+
temp = (*state)[0][3];
|
|
365
|
+
(*state)[0][3] = (*state)[1][3];
|
|
366
|
+
(*state)[1][3] = (*state)[2][3];
|
|
367
|
+
(*state)[2][3] = (*state)[3][3];
|
|
368
|
+
(*state)[3][3] = temp;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/* Cipher is the main function that encrypts the PlainText. */
|
|
372
|
+
static void Cipher(state_t* state, const uint8_t RoundKey[AES_keyExpSize])
|
|
373
|
+
{
|
|
374
|
+
uint8_t round = 0;
|
|
375
|
+
|
|
376
|
+
/* Add the First round key to the state before starting the rounds. */
|
|
377
|
+
AddRoundKey(0, state, RoundKey);
|
|
378
|
+
|
|
379
|
+
/* There will be Nr rounds.
|
|
380
|
+
* The first Nr-1 rounds are identical.
|
|
381
|
+
* These Nr rounds are executed in the loop below.
|
|
382
|
+
* Last one without MixColumns()
|
|
383
|
+
*/
|
|
384
|
+
for (round = 1; ; ++round)
|
|
385
|
+
{
|
|
386
|
+
SubBytes(state);
|
|
387
|
+
ShiftRows(state);
|
|
388
|
+
if (round == Nr) {
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
MixColumns(state);
|
|
392
|
+
AddRoundKey(round, state, RoundKey);
|
|
393
|
+
}
|
|
394
|
+
/* Add round key to last round */
|
|
395
|
+
AddRoundKey(Nr, state, RoundKey);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
static void InvCipher(state_t* state, const uint8_t RoundKey[AES_keyExpSize])
|
|
399
|
+
{
|
|
400
|
+
uint8_t round = 0;
|
|
401
|
+
|
|
402
|
+
/* Add the First round key to the state before starting the rounds. */
|
|
403
|
+
AddRoundKey(Nr, state, RoundKey);
|
|
404
|
+
|
|
405
|
+
/* There will be Nr rounds.
|
|
406
|
+
* The first Nr-1 rounds are identical.
|
|
407
|
+
* These Nr rounds are executed in the loop below.
|
|
408
|
+
* Last one without InvMixColumn()
|
|
409
|
+
*/
|
|
410
|
+
for (round = (Nr - 1); ; --round)
|
|
411
|
+
{
|
|
412
|
+
InvShiftRows(state);
|
|
413
|
+
InvSubBytes(state);
|
|
414
|
+
AddRoundKey(round, state, RoundKey);
|
|
415
|
+
if (round == 0) {
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
InvMixColumns(state);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/*****************************************************************************/
|
|
423
|
+
/* Public functions: */
|
|
424
|
+
/*****************************************************************************/
|
|
425
|
+
|
|
426
|
+
static void XorWithIv(uint8_t* buf, const uint8_t Iv[AES_BLOCKLEN])
|
|
427
|
+
{
|
|
428
|
+
uint8_t i;
|
|
429
|
+
for (i = 0; i < AES_BLOCKLEN; ++i) /* The block in AES is always 128bit no matter the key size */
|
|
430
|
+
{
|
|
431
|
+
buf[i] ^= Iv[i];
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
|
|
436
|
+
{
|
|
437
|
+
size_t i;
|
|
438
|
+
uint8_t storeNextIv[AES_BLOCKLEN];
|
|
439
|
+
for (i = 0; i < length; i += AES_BLOCKLEN)
|
|
440
|
+
{
|
|
441
|
+
memcpy(storeNextIv, buf, AES_BLOCKLEN);
|
|
442
|
+
InvCipher((state_t*)buf, ctx->RoundKey);
|
|
443
|
+
XorWithIv(buf, ctx->Iv);
|
|
444
|
+
memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
|
|
445
|
+
buf += AES_BLOCKLEN;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */
|
|
450
|
+
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
|
|
451
|
+
{
|
|
452
|
+
uint8_t buffer[AES_BLOCKLEN];
|
|
453
|
+
|
|
454
|
+
size_t i;
|
|
455
|
+
int bi;
|
|
456
|
+
for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
|
|
457
|
+
{
|
|
458
|
+
if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
|
|
459
|
+
{
|
|
460
|
+
memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
|
|
461
|
+
Cipher((state_t*)buffer, ctx->RoundKey);
|
|
462
|
+
|
|
463
|
+
/* Increment Iv and handle overflow */
|
|
464
|
+
for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
|
|
465
|
+
{
|
|
466
|
+
/* inc will overflow */
|
|
467
|
+
if (ctx->Iv[bi] == 255)
|
|
468
|
+
{
|
|
469
|
+
ctx->Iv[bi] = 0;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
ctx->Iv[bi] += 1;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
bi = 0;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
buf[i] = (buf[i] ^ buffer[bi]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#ifndef AES_H
|
|
2
|
+
#define AES_H
|
|
3
|
+
|
|
4
|
+
/* This file is sourced from https://github.com/kokke/tiny-AES-c, with unused code excised.
|
|
5
|
+
* This code is licensed under the Unlicense license, effectively public domain.
|
|
6
|
+
* https://github.com/kokke/tiny-AES-c/blob/f06ac37/unlicense.txt
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <stdint.h>
|
|
10
|
+
#include <stddef.h>
|
|
11
|
+
|
|
12
|
+
#define AES_BLOCKLEN 16 /* Block length in bytes - AES is 128b block only */
|
|
13
|
+
#define AES_KEYLEN 16 /* Key length in bytes */
|
|
14
|
+
#define AES_keyExpSize 176
|
|
15
|
+
|
|
16
|
+
struct AES_ctx
|
|
17
|
+
{
|
|
18
|
+
uint8_t RoundKey[AES_keyExpSize];
|
|
19
|
+
uint8_t Iv[AES_BLOCKLEN];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
#ifdef __cplusplus
|
|
23
|
+
extern "C" {
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t key[AES_KEYLEN]);
|
|
27
|
+
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t key[AES_KEYLEN], const uint8_t iv[AES_BLOCKLEN]);
|
|
28
|
+
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t iv[AES_BLOCKLEN]);
|
|
29
|
+
|
|
30
|
+
/* buffer size MUST be mutile of AES_BLOCKLEN;
|
|
31
|
+
* Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
|
|
32
|
+
* NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
|
|
33
|
+
* no IV should ever be reused with the same key
|
|
34
|
+
*/
|
|
35
|
+
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
|
|
36
|
+
|
|
37
|
+
/* Same function for encrypting as for decrypting.
|
|
38
|
+
* IV is incremented for every block, and used after encryption as XOR-compliment for output
|
|
39
|
+
* Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
|
|
40
|
+
* NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
|
|
41
|
+
* no IV should ever be reused with the same key
|
|
42
|
+
*/
|
|
43
|
+
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
|
|
44
|
+
|
|
45
|
+
#ifdef __cplusplus
|
|
46
|
+
}
|
|
47
|
+
#endif
|
|
48
|
+
|
|
49
|
+
#endif /* AES_H */
|