-
Notifications
You must be signed in to change notification settings - Fork 206
Algo examples #610
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
aidankeefe2022
wants to merge
16
commits into
wolfSSL:master
Choose a base branch
from
aidankeefe2022:algo_examples
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.
+3,194
−12
Open
Algo examples #610
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dbf6e9a
Add SLH-DSA (FIPS 205) example
anhu eba0390
Add SM2/SM3/SM4 examples
anhu 73dde4a
Add BLAKE2b/BLAKE2s examples
anhu 9f75129
Add SipHash example
anhu f09fb73
Add KDF examples
anhu 96fb285
Add HPKE seal/open context example
anhu 6398f45
Add SRP-6a SHA-256 example
anhu 0aeba02
Add MIKEY-SAKKE (ECCSI + SAKKE) example
anhu fa32abf
Register new algorithm examples in CI manifest
anhu a6933bd
Mikey-Sakke example revised
aidankeefe2022 edac069
srp_sha256 revised
aidankeefe2022 6715f35
hpke_context revised
aidankeefe2022 23c020e
sm2-ecdh revised
aidankeefe2022 5626aa3
Mikey-sakke format fix
aidankeefe2022 213cb56
revised slh_dsa
aidankeefe2022 5c56a1c
removed useless sig corruption
aidankeefe2022 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| CC=gcc | ||
| WOLFSSL_INSTALL_DIR=/usr/local | ||
| CFLAGS=-Wall -I$(WOLFSSL_INSTALL_DIR)/include | ||
| LIBS=-L$(WOLFSSL_INSTALL_DIR)/lib -lwolfssl -lm | ||
|
|
||
| all: hkdf pbkdf2 scrypt-kdf | ||
|
|
||
| hkdf: hkdf.o | ||
| $(CC) -o $@ $^ $(CFLAGS) $(LIBS) | ||
|
|
||
| pbkdf2: pbkdf2.o | ||
| $(CC) -o $@ $^ $(CFLAGS) $(LIBS) | ||
|
|
||
| scrypt-kdf: scrypt-kdf.o | ||
| $(CC) -o $@ $^ $(CFLAGS) $(LIBS) | ||
|
|
||
| .PHONY: clean all check | ||
|
|
||
| clean: | ||
| rm -f *.o hkdf pbkdf2 scrypt-kdf | ||
|
|
||
| check: all | ||
| out=$$(./hkdf) && printf '%s' "$$out" | grep -q 'matches RFC 5869 Test Case 1' | ||
| out=$$(./pbkdf2) && printf '%s' "$$out" | grep -q 'matches RFC 7914 test vector' | ||
| out=$$(./scrypt-kdf) && printf '%s' "$$out" | grep -q 'matches RFC 7914 test vector' | ||
| @echo "PASS: crypto-kdf checks" |
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,34 @@ | ||
| # wolfSSL KDF Examples | ||
|
|
||
| Demonstrates the main wolfCrypt key derivation functions, each verified | ||
| against its RFC known-answer test vector. | ||
|
|
||
| * `hkdf.c` - HKDF (RFC 5869): extract-then-expand derivation from existing | ||
| keying material, shown both as separate `wc_HKDF_Extract()` / | ||
| `wc_HKDF_Expand()` steps and as the one-shot `wc_HKDF()`. | ||
| * `pbkdf2.c` - PBKDF2 (RFC 2898) via `wc_PBKDF2()`: deriving keys from | ||
| passwords with a salt and an iteration work factor. | ||
| * `scrypt-kdf.c` - scrypt (RFC 7914) via `wc_scrypt()`: memory-hard | ||
| password-based derivation for stronger resistance to GPU/ASIC attacks. | ||
|
|
||
| Use HKDF when the input is already a high-entropy secret (e.g. a DH shared | ||
| secret); use PBKDF2 or scrypt when the input is a password. | ||
|
|
||
| ## Building wolfSSL | ||
|
|
||
| ``` | ||
| ./configure --enable-hkdf --enable-scrypt | ||
| make | ||
| sudo make install | ||
| ``` | ||
|
|
||
| PBKDF2 is enabled by default (disabled only by `NO_PWDBASED`). | ||
|
|
||
| ## Building and running the examples | ||
|
|
||
| ``` | ||
| make | ||
| ./hkdf | ||
| ./pbkdf2 | ||
| ./scrypt-kdf | ||
| ``` |
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,130 @@ | ||
| /* hkdf.c | ||
| * | ||
| * Copyright (C) 2006-2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfSSL. (formerly known as CyaSSL) | ||
| * | ||
| * 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-1301, USA | ||
| */ | ||
|
|
||
| /* Example of HKDF (RFC 5869): extract-then-expand key derivation, run against | ||
| * RFC 5869 Test Case 1. */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include <wolfssl/options.h> | ||
| #include <wolfssl/wolfcrypt/settings.h> | ||
| #include <wolfssl/wolfcrypt/hmac.h> | ||
|
|
||
| #ifdef HAVE_HKDF | ||
|
|
||
| /* RFC 5869 Test Case 1 (SHA-256). */ | ||
| static const byte ikm[22] = { | ||
| 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, | ||
| 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, | ||
| 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b | ||
| }; | ||
| static const byte salt[13] = { | ||
| 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
| 0x08, 0x09, 0x0a, 0x0b, 0x0c | ||
| }; | ||
| static const byte info[10] = { | ||
| 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, | ||
| 0xf8, 0xf9 | ||
| }; | ||
| static const byte expected_prk[32] = { | ||
| 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, | ||
| 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, | ||
| 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, | ||
| 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5 | ||
| }; | ||
| static const byte expected_okm[42] = { | ||
| 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, | ||
| 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, | ||
| 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, | ||
| 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, | ||
| 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, | ||
| 0x58, 0x65 | ||
| }; | ||
|
|
||
| static void print_hex(const char* label, const byte* data, word32 len) | ||
| { | ||
| word32 i; | ||
|
|
||
| printf("%s: ", label); | ||
| for (i = 0; i < len; i++) | ||
| printf("%02x", data[i]); | ||
| printf("\n"); | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| int ret; | ||
| byte prk[32]; | ||
| byte okm[42]; | ||
|
|
||
| /* Extract: concentrate the input keying material into a fixed-size PRK. */ | ||
| ret = wc_HKDF_Extract(WC_SHA256, salt, sizeof(salt), ikm, sizeof(ikm), | ||
| prk); | ||
| if (ret != 0) { | ||
| printf("wc_HKDF_Extract failed %d\n", ret); | ||
| return 1; | ||
| } | ||
| print_hex("PRK", prk, sizeof(prk)); | ||
| if (memcmp(prk, expected_prk, sizeof(prk)) != 0) { | ||
| printf("PRK does not match RFC 5869 test vector!\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| /* Expand: stretch the PRK into the output keying material. */ | ||
| ret = wc_HKDF_Expand(WC_SHA256, prk, sizeof(prk), info, sizeof(info), | ||
| okm, sizeof(okm)); | ||
| if (ret != 0) { | ||
| printf("wc_HKDF_Expand failed %d\n", ret); | ||
| return 1; | ||
| } | ||
| print_hex("OKM", okm, sizeof(okm)); | ||
| if (memcmp(okm, expected_okm, sizeof(okm)) != 0) { | ||
| printf("OKM does not match RFC 5869 test vector!\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| /* wc_HKDF does both steps in one call. */ | ||
| memset(okm, 0, sizeof(okm)); | ||
| ret = wc_HKDF(WC_SHA256, ikm, sizeof(ikm), salt, sizeof(salt), info, | ||
| sizeof(info), okm, sizeof(okm)); | ||
| if (ret != 0) { | ||
| printf("wc_HKDF failed %d\n", ret); | ||
| return 1; | ||
| } | ||
| if (memcmp(okm, expected_okm, sizeof(okm)) != 0) { | ||
| printf("One-shot OKM does not match!\n"); | ||
| return 1; | ||
| } | ||
| printf("HKDF output matches RFC 5869 Test Case 1\n"); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| int main(void) | ||
| { | ||
| printf("Please build wolfSSL with ./configure --enable-hkdf\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| #endif /* HAVE_HKDF */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make the test clone it and test it. Thanks