-
Notifications
You must be signed in to change notification settings - Fork 94
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
Added basic support for CH32V003. #153
Open
CharlesScoville
wants to merge
10
commits into
jedisct1:master
Choose a base branch
from
CharlesScoville:master
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.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f79a168
Added basic support for CH32V003. Entropy is observed from reading LS…
CharlesScoville f760f1c
Changes proposed in main fork, pull request #153. All Von Neuman extr…
CharlesScoville 4ad5224
...Added aditional request from comments
CharlesScoville ce02d48
Merge branch 'master' of https://github.com/CharlesScoville/libhydrogen
CharlesScoville 6e03425
Merge branch 'master' of https://github.com/CharlesScoville/libhydrogen
CharlesScoville 84b9525
Merge branch 'master' of https://github.com/CharlesScoville/libhydrogen
CharlesScoville 4d9a408
Merge branch 'master' of https://github.com/CharlesScoville/libhydrogen
CharlesScoville be1aeca
...Added aditional request from comments.
CharlesScoville 4073536
...Added aditional request from comments.
CharlesScoville 6957e6b
Merge branch 'master' of https://github.com/CharlesScoville/libhydrogen
CharlesScoville 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 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 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,100 @@ | ||
/********************************************************************************** | ||
* File Name : ch32v0xx.h (Tentative) | ||
* Author : Charles Lee Scoville | ||
* Version : V0.0.2 (Pull request #153 modifications) | ||
* Start Date : 2024/01/02 | ||
* Description : TRNG initialization shim | ||
* | ||
* The CH32V0XX does not have a hardware random number generator. This leaves us with | ||
* only a few options for collecting entropy. | ||
* | ||
* - Attach an entropy source circuit to the chip. (biased Zener, transistor, etc.) | ||
* - Use uninitialized data from SRAM proceeding chip power on. | ||
* - Timing difference (jitter) between two *independent* clock sources. | ||
* - Read some voltage with the ADC and look only at low bit(s). | ||
* | ||
* *TODO* I intend to survey all options and make code for each, and a complete | ||
* write up. Until then, at least I have something working today. Tangible PoC | ||
* beats "good ideas" that haven't been written yet any day. | ||
* | ||
* *TODO* Compilation flags to switch between the different entropy options | ||
* in a way that is transparent to this shim would be really neato. | ||
*********************************************************************************/ | ||
|
||
#include <ch32v00x.h> | ||
|
||
/* This must be defined in your CH32V003 project, and must be filled with raw / | ||
* unfiltered samples from the internal reference voltage, as the name implies*/ | ||
extern uint16_t ADC_Channel_Vrefint_value; | ||
|
||
|
||
uint32_t entropy_condenser_extractor(void) { | ||
|
||
volatile uint64_t entropy_buffer = 0; | ||
volatile uint8_t entropy_buffer_count = 0; | ||
|
||
volatile uint32_t extract = 0; | ||
volatile uint8_t extract_count = 0; | ||
volatile FlagStatus extract_RDY = RESET; | ||
|
||
if ( !((ADC1->CTLR2 & 1) != 0) ) | ||
while(1); /* ADC1 OFF!!! PANIK! */ | ||
|
||
while (extract_RDY == RESET) { | ||
|
||
volatile uint32_t compare = ADC_Channel_Vrefint_value; | ||
|
||
/* Wait for new ADC data */ | ||
while (compare == ADC_Channel_Vrefint_value) | ||
__NOP(); | ||
|
||
|
||
entropy_buffer = entropy_buffer << 1; | ||
|
||
entropy_buffer = entropy_buffer + ((ADC_Channel_Vrefint_value & 0x40) == 0x40); | ||
|
||
entropy_buffer_count++; | ||
if (entropy_buffer_count > 63) { | ||
entropy_buffer_count = 0; | ||
|
||
for (uint8_t j = 0; (j < 31); j++) { | ||
|
||
/* 64->32 bit Von Neuman extractor */ | ||
if ((entropy_buffer & 1) ^ ((entropy_buffer >> 1) & 1)) { | ||
extract = (extract << 1)+((entropy_buffer >> 1) & 1); | ||
extract_count++; | ||
} | ||
|
||
entropy_buffer = entropy_buffer >> 2; | ||
|
||
if (extract_count > 31) { | ||
extract_count = 0; | ||
extract_RDY = SET; | ||
} | ||
} | ||
} | ||
} | ||
return extract; | ||
} | ||
|
||
|
||
extern int hydro_random_init(void) { | ||
const char ctx[hydro_hash_CONTEXTBYTES] = {'h','y','d','r','o','P','R','G'}; | ||
hydro_hash_state st; | ||
uint16_t ebits = 0; | ||
|
||
hydro_hash_init(&st, ctx, NULL); | ||
|
||
while (ebits < 512) { | ||
|
||
uint32_t r = entropy_condenser_extractor(); | ||
|
||
hydro_hash_update(&st, (const uint32_t *) &r, sizeof r); | ||
ebits += 32; | ||
} | ||
|
||
hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state); | ||
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state); | ||
|
||
return 0; | ||
} |
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.
suggestion: dont block your app/system, make usage of errno*
errno = ENOXXX ;
return 0;
https://man7.org/linux/man-pages/man3/errno.3.html
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.
Good ask! +1
I'll see if I can get time to do that this weekend.
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.
It is done.... Took me long enough!
(Had irl issues I had to clear up.)