Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions lib/crypto/rsa.toit
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import .sha
import .sha1
import ..io as io
import monitor show ResourceState_
import encoding.base64

/**
Expand Down Expand Up @@ -124,7 +125,18 @@ class RsaKey:
*/
static generate --bits/int=2048 -> RsaKeyPair:
if bits != 1024 and bits != 2048 and bits != 3072 and bits != 4096: throw "INVALID_ARGUMENT"
pair := rsa-generate_ bits
pair := null
catch --trace=(: it != "UNIMPLEMENTED"):
resource := RsaGenerationResource_ bits
try:
resource.wait
pair = resource.finish
finally:
resource.close

if not pair:
pair = rsa-generate_ bits

return RsaKeyPair
RsaKey.internal_ pair[0] true
RsaKey.internal_ pair[1] false
Expand Down Expand Up @@ -279,4 +291,44 @@ rsa-encrypt_ public-key-der/ByteArray data/ByteArray padding/int hash/int -> Byt
// Primitive: decrypt data with a private key DER blob.
rsa-decrypt_ private-key-der/ByteArray data/ByteArray padding/int hash/int -> ByteArray:
#primitive.crypto.rsa-decrypt


class RsaGenerationResource_:
group_ := null
state_ := null

constructor bits/int:
group_ = rsa-generate-init_
add-finalizer this:: close
resource-id := rsa-generate-start_ group_ bits
state_ = ResourceState_ group_ resource-id

wait -> none:
state_.wait

finish -> List:
return rsa-generate-finish_ state_.resource

close:
if not group_: return
critical-do:
if state_: state_.dispose
state_ = null
rsa-generate-close_ group_
group_ = null
remove-finalizer this

/** Initializes a resource group for RSA key generation. */
rsa-generate-init_:
#primitive.crypto.rsa-generate-init

/** Starts the asynchronous RSA key generation. */
rsa-generate-start_ group bits/int:
#primitive.crypto.rsa-generate-start

/** Finishes the asynchronous RSA key generation and returns the key pair. */
rsa-generate-finish_ resource-id -> List:
#primitive.crypto.rsa-generate-finish

/** Closes the RSA generation resource group. */
rsa-generate-close_ group -> none:
#primitive.crypto.rsa-generate-close
4 changes: 4 additions & 0 deletions src/compiler/propagation/type_primitive_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ TYPE_PRIMITIVE_ANY(rsa_verify)
TYPE_PRIMITIVE_ANY(rsa_generate)
TYPE_PRIMITIVE_ANY(rsa_encrypt)
TYPE_PRIMITIVE_ANY(rsa_decrypt)
TYPE_PRIMITIVE_ANY(rsa_generate_init)
TYPE_PRIMITIVE_ANY(rsa_generate_start)
TYPE_PRIMITIVE_ANY(rsa_generate_finish)
TYPE_PRIMITIVE_ANY(rsa_generate_close)

} // namespace toit::compiler
} // namespace toit
2 changes: 1 addition & 1 deletion src/event_sources/async_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "../top.h"

#if defined(TOIT_POSIX)
#if defined(TOIT_POSIX) || defined(TOIT_ESP32)

#include "async_posix.h"

Expand Down
2 changes: 1 addition & 1 deletion src/event_sources/async_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "../top.h"

#if defined(TOIT_POSIX)
#if defined(TOIT_POSIX) || defined(TOIT_ESP32)

#include "../linked.h"
#include "../resource.h"
Expand Down
5 changes: 5 additions & 0 deletions src/os_esp32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ void Thread::run() {
thread_start(void_cast(this));
}

void Thread::cancel() {
// No-op on ESP32. The thread will check for STOPPED state after
// completing its current work item.
}

void Thread::join() {
ASSERT(handle_ != null);
auto thread = reinterpret_cast<ThreadData*>(handle_);
Expand Down
8 changes: 7 additions & 1 deletion src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ namespace toit {
PRIMITIVE(rsa_get_private_key_der, 2) \
PRIMITIVE(rsa_get_public_key_der, 1) \
PRIMITIVE(rsa_encrypt, 4) \
PRIMITIVE(rsa_decrypt, 4)
PRIMITIVE(rsa_decrypt, 4) \
PRIMITIVE(rsa_generate_init, 0) \
PRIMITIVE(rsa_generate_start, 2) \
PRIMITIVE(rsa_generate_finish, 1) \
PRIMITIVE(rsa_generate_close, 1)

#define MODULE_CRYPTO_RANDOM(PRIMITIVE) \
PRIMITIVE(random, 1) \
Expand Down Expand Up @@ -1092,6 +1096,7 @@ Object* get_absolute_path(Process* process, const wchar_t* pathname, wchar_t* ou
#define _A_T_RmtResourceGroup(N, name) MAKE_UNPACKING_MACRO(RmtResourceGroup, N, name)
#define _A_T_PcntUnitResourceGroup(N, name) MAKE_UNPACKING_MACRO(PcntUnitResourceGroup, N, name)
#define _A_T_EspNowResourceGroup(N, name) MAKE_UNPACKING_MACRO(EspNowResourceGroup, N, name)
#define _A_T_RsaGenerationResourceGroup(N, name) MAKE_UNPACKING_MACRO(RsaGenerationResourceGroup, N, name)

#define _A_T_Resource(N, name) MAKE_UNPACKING_MACRO(Resource, N, name)
#define _A_T_Directory(N, name) MAKE_UNPACKING_MACRO(Directory, N, name)
Expand All @@ -1109,6 +1114,7 @@ Object* get_absolute_path(Process* process, const wchar_t* pathname, wchar_t* ou
#define _A_T_EthernetEvents(N, name) MAKE_UNPACKING_MACRO(EthernetEvents, N, name)
#define _A_T_EthernetIpEvents(N, name) MAKE_UNPACKING_MACRO(EthernetIpEvents, N, name)
#define _A_T_MbedTlsSocket(N, name) MAKE_UNPACKING_MACRO(MbedTlsSocket, N, name)
#define _A_T_RsaGenerationResource(N, name) MAKE_UNPACKING_MACRO(RsaGenerationResource, N, name)
#define _A_T_BaseMbedTlsSocket(N, name) MAKE_UNPACKING_MACRO(BaseMbedTlsSocket, N, name)
#define _A_T_X509Certificate(N, name) MAKE_UNPACKING_MACRO(X509Certificate, N, name)
#define _A_T_AesContext(N, name) MAKE_UNPACKING_MACRO(AesContext, N, name)
Expand Down
Loading