Berry crypto add ``random`` to generate series of random bytes (#17482)

This commit is contained in:
s-hadinger 2022-12-22 18:57:20 +01:00 committed by GitHub
parent 23e0bc27e7
commit 970e36f44d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -5,7 +5,8 @@ All notable changes to this project will be documented in this file.
## [12.3.1.2]
### Added
- Berry crypto add ``EC_P256`` and ``PBKDF2_HMAC_SHA256`` algorithms required by Matter protocol (#17473)
- Berry crypto add ``EC_P256`` and ``PBKDF2_HMAC_SHA256`` algorithms required by Matter protocol
- Berry crypto add ``random`` to generate series of random bytes
### Breaking Changed

View File

@ -9,6 +9,7 @@
#include "be_mapping.h"
extern int be_class_crypto_member(bvm *vm);
extern int m_crypto_random(bvm *vm);
extern int m_aes_gcm_init(bvm *vm);
extern int m_aes_gcm_encryt(bvm *vm);
@ -145,6 +146,7 @@ class be_class_pbkdf2_hmac_sha256 (scope: global, name: PBKDF2_HMAC_SHA256) {
module crypto (scope: global) {
member, func(be_class_crypto_member)
random, func(m_crypto_random)
}
@const_object_info_end */

View File

@ -39,6 +39,32 @@ extern "C" {
}
}
/*********************************************************************************************\
* Random bytes generator
*
* As long as Wifi or BLE is enable, it uses a hardware source for true randomnesss
*
\*********************************************************************************************/
extern "C" {
// `crypto.random(num_bytes:int) -> bytes(num_bytes)`
//
// Generates a series of random bytes
int m_crypto_random(bvm *vm);
int m_crypto_random(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isint(vm, 1)) {
int32_t n = be_toint(vm, 1);
if (n < 0 || n > 4096) { be_raise(vm, "value_error", ""); }
uint8_t rand_bytes[n];
esp_fill_random(rand_bytes, n);
be_pushbytes(vm, rand_bytes, n);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
}
/*********************************************************************************************\
* AES_GCM class
*