-
Notifications
You must be signed in to change notification settings - Fork 196
Extend AES minimal and rdseed examples #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
padelsbach
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
padelsbach:padelsbach/aesgcm-rdseed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+515
−50
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /* aesgcm-file-minimal.c | ||
| * | ||
| * Copyright (C) 2006-2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfSSL. | ||
| * | ||
| * wolfSSL is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfSSL is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| #ifndef WOLFSSL_USER_SETTINGS | ||
| #include <wolfssl/options.h> | ||
| #endif | ||
| #include <wolfssl/wolfcrypt/aes.h> | ||
| #include <wolfssl/wolfcrypt/random.h> | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #define KEY_SZ AES_256_KEY_SIZE | ||
| #define NONCE_SZ GCM_NONCE_MID_SZ | ||
| #define TAG_SZ AES_BLOCK_SIZE | ||
|
|
||
| static int Encrypt(const byte* key, const byte* iv, const byte* in, | ||
| word32 inSz, byte* out, byte* tag) | ||
| { | ||
| Aes aes; | ||
| int ret; | ||
| ret = wc_AesInit(&aes, NULL, INVALID_DEVID); | ||
| if (ret == 0) { | ||
| ret = wc_AesGcmSetKey(&aes, key, KEY_SZ); | ||
| } | ||
| if (ret == 0) { | ||
| ret = wc_AesGcmEncrypt(&aes, out, in, inSz, iv, NONCE_SZ, | ||
| tag, TAG_SZ, NULL, 0); | ||
| } | ||
| wc_AesFree(&aes); | ||
| return ret; | ||
| } | ||
|
|
||
| static int Decrypt(const byte* key, const byte* iv, const byte* in, | ||
| word32 inSz, byte* out, const byte* tag) | ||
| { | ||
| Aes aes; | ||
| int ret; | ||
| ret = wc_AesInit(&aes, NULL, INVALID_DEVID); | ||
| if (ret == 0) { | ||
| ret = wc_AesGcmSetKey(&aes, key, KEY_SZ); | ||
| } | ||
| if (ret == 0) { | ||
| ret = wc_AesGcmDecrypt(&aes, out, in, inSz, iv, NONCE_SZ, | ||
| tag, TAG_SZ, NULL, 0); | ||
| } | ||
| wc_AesFree(&aes); | ||
| return ret; | ||
| } | ||
|
|
||
| static int ReadFile(const char* path, byte** data, word32* sz) | ||
| { | ||
| FILE* file; | ||
| long fileSz; | ||
| byte* buf; | ||
|
|
||
| *data = NULL; | ||
| *sz = 0; | ||
| file = fopen(path, "rb"); | ||
| if (file == NULL) { | ||
| return -1; | ||
| } | ||
| if (fseek(file, 0, SEEK_END) != 0) { | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| fileSz = ftell(file); | ||
| if (fileSz < 0) { | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| if (fseek(file, 0, SEEK_SET) != 0) { | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| buf = (byte*)malloc((size_t)fileSz); | ||
| if (buf == NULL && fileSz != 0) { | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| if (fileSz != 0 && fread(buf, 1, (size_t)fileSz, file) != (size_t)fileSz) { | ||
| free(buf); | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| fclose(file); | ||
| *data = buf; | ||
| *sz = fileSz; | ||
| return 0; | ||
| } | ||
|
|
||
| static int WriteFile(const char* path, const byte* data, long dataSz) | ||
| { | ||
| FILE* file = fopen(path, "wb"); | ||
| if (file == NULL) { | ||
| return -1; | ||
| } | ||
| if (dataSz != 0 && | ||
| fwrite(data, 1, (size_t)dataSz, file) != (size_t)dataSz) { | ||
| fclose(file); | ||
| return -1; | ||
| } | ||
| fclose(file); | ||
| return 0; | ||
| } | ||
|
|
||
| static void print_hex(const char* label, const byte* data, word32 sz) | ||
| { | ||
| word32 i; | ||
| printf("%s: ", label); | ||
| for (i = 0; i < sz; i++) { | ||
| printf("%02x", data[i]); | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| byte key[KEY_SZ], iv[NONCE_SZ], tag[TAG_SZ]; | ||
| byte* plaintext = NULL; | ||
| byte* ciphertext = NULL; | ||
| byte* decrypted = NULL; | ||
| word32 plaintextSz = 0; | ||
| WC_RNG rng; | ||
| int ret; | ||
|
|
||
| if (argc != 4) { | ||
| printf("Usage: %s <input-file> <encrypted-file> <decrypted-file>\n", | ||
| argv[0]); | ||
| return 1; | ||
| } | ||
|
|
||
| if (ReadFile(argv[1], &plaintext, &plaintextSz) != 0) { | ||
| printf("Failed to read: %s\n", argv[1]); | ||
| return 1; | ||
| } | ||
|
|
||
| ciphertext = (byte*)malloc((size_t)plaintextSz); | ||
| decrypted = (byte*)malloc((size_t)plaintextSz); | ||
| if ((ciphertext == NULL || decrypted == NULL) && plaintextSz != 0) { | ||
| printf("alloc failed\n"); | ||
| ret = 1; | ||
| goto exit; | ||
| } | ||
|
|
||
| ret = wc_InitRng(&rng); | ||
| if (ret == 0) { | ||
| ret = wc_RNG_GenerateBlock(&rng, key, KEY_SZ); | ||
| if (ret == 0) { | ||
| ret = wc_RNG_GenerateBlock(&rng, iv, NONCE_SZ); | ||
| } | ||
| wc_FreeRng(&rng); | ||
| } | ||
| if (ret != 0) { | ||
| printf("Key/IV generation failed: %d\n", ret); | ||
| goto exit; | ||
| } | ||
|
|
||
| ret = Encrypt(key, iv, plaintext, plaintextSz, ciphertext, tag); | ||
| if (ret != 0) { | ||
| printf("Encryption failed: %d\n", ret); | ||
| goto exit; | ||
| } | ||
|
|
||
| if (WriteFile(argv[2], ciphertext, plaintextSz) != 0) { | ||
| printf("Failed to write: %s\n", argv[2]); | ||
| ret = 1; | ||
| goto exit; | ||
| } | ||
|
|
||
| ret = Decrypt(key, iv, ciphertext, plaintextSz, decrypted, tag); | ||
| if (ret != 0) { | ||
| printf("Decryption failed: %d\n", ret); | ||
| goto exit; | ||
| } | ||
|
|
||
| if (WriteFile(argv[3], decrypted, plaintextSz) != 0) { | ||
| printf("Failed to write: %s\n", argv[3]); | ||
| ret = 1; | ||
| goto exit; | ||
| } | ||
|
|
||
| print_hex("Key", key, KEY_SZ); | ||
| print_hex("IV", iv, NONCE_SZ); | ||
| print_hex("Tag", tag, TAG_SZ); | ||
| printf("Encrypted %u bytes to %s\n", plaintextSz, argv[2]); | ||
| printf("Decrypted %u bytes to %s\n", plaintextSz, argv[3]); | ||
|
|
||
| if (memcmp(plaintext, decrypted, (size_t)plaintextSz) == 0) { | ||
| printf("Round-trip OK: decrypted output matches original input\n"); | ||
| } | ||
| else { | ||
| printf("Round-trip FAILED: mismatch\n"); | ||
| ret = 1; | ||
| } | ||
|
|
||
| exit: | ||
| free(decrypted); | ||
| free(ciphertext); | ||
| free(plaintext); | ||
| return ret; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,6 @@ | |
| #include <wolfssl/options.h> | ||
| #endif | ||
| #include <wolfssl/wolfcrypt/aes.h> | ||
| #include <wolfssl/wolfcrypt/error-crypt.h> | ||
| #include <wolfssl/wolfcrypt/random.h> | ||
|
|
||
| #include <stdio.h> | ||
|
|
@@ -33,58 +32,12 @@ | |
| #define NONCE_SZ GCM_NONCE_MID_SZ | ||
| #define TAG_SZ AES_BLOCK_SIZE | ||
|
|
||
| /* Optional setup path: seed wolfSSL RNG via RDSEED when available. */ | ||
| #if defined(__x86_64__) | ||
| #define RDSEED_ENABLED 1 | ||
| #endif | ||
|
|
||
| #if defined(WC_RNG_SEED_CB) && defined(RDSEED_ENABLED) | ||
| #include <immintrin.h> | ||
|
|
||
| /* wc_RngSeed_Cb: feed wolfSSL DRBG seed using RDSEED. */ | ||
| __attribute__((target("rdseed"))) | ||
| static int RdseedSeedCb(OS_Seed* os, byte* seed, word32 sz) | ||
| { | ||
| word32 i = 0; | ||
| (void)os; | ||
|
|
||
| while (i < sz) { | ||
| unsigned long long v = 0; | ||
| int ok = 0; | ||
| int tries; | ||
| word32 n; | ||
|
|
||
| for (tries = 0; tries < 16; tries++) { | ||
| if (_rdseed64_step(&v)) { | ||
| ok = 1; | ||
| break; | ||
| } | ||
| } | ||
| if (!ok) { | ||
| return RNG_FAILURE_E; | ||
| } | ||
|
|
||
| n = (sz - i < (word32)sizeof(v)) ? (sz - i) : (word32)sizeof(v); | ||
| memcpy(seed + i, &v, n); | ||
| i += n; | ||
| } | ||
| return 0; | ||
| } | ||
| #endif | ||
|
|
||
| static int GenerateKeyAndIv(byte* key, byte* iv) | ||
| { | ||
| WC_RNG rng; | ||
| int ret; | ||
|
|
||
| /* Setup: initialize RNG and (optionally) override seed source. */ | ||
| #if defined(WC_RNG_SEED_CB) && defined(RDSEED_ENABLED) | ||
| wc_SetSeed_Cb(RdseedSeedCb); | ||
| ret = wc_InitRng(&rng); | ||
| wc_SetSeed_Cb(NULL); | ||
| #else | ||
| ret = wc_InitRng(&rng); | ||
| #endif | ||
| if (ret != 0) { | ||
| return ret; | ||
| } | ||
|
|
@@ -137,8 +90,9 @@ static int Decrypt(const byte* key, const byte* iv, const byte* ciphertext, | |
|
|
||
| void print_hex(const char* label, const byte* data, word32 sz) | ||
| { | ||
| word32 i; | ||
| printf("%s: ", label); | ||
| for (word32 i = 0; i < sz; i++) { | ||
| for (i = 0; i < sz; i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. word32 i to match the fix in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| printf("%02x", data[i]); | ||
| } | ||
| printf("\n"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| CC=gcc | ||
| CFLAGS=-Wall -DWOLFSSL_USER_SETTINGS -I. | ||
| WOLFSSL_INSTALL_DIR=/usr/local | ||
| LIBS=-L$(WOLFSSL_INSTALL_DIR)/lib -lwolfssl -lm | ||
|
|
||
| all: aesgcm-rdseed | ||
|
|
||
| aesgcm-rdseed: aesgcm-rdseed.o | ||
| $(CC) -o $@ $^ $(CFLAGS) $(LIBS) | ||
|
|
||
| .PHONY: clean | ||
|
|
||
| clean: | ||
| rm -f *.o aesgcm-rdseed |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.