@@ -271,6 +271,69 @@ STATIC mp_obj_t getrandbits(mp_obj_t bits) {
271
271
}
272
272
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (getrandbits_obj , getrandbits );
273
273
274
+ STATIC mp_obj_t mod_crypt_generate_rsa_signature (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
275
+
276
+ STATIC const mp_arg_t mod_pycom_generate_rsa_signature_args [] = {
277
+ { MP_QSTR_message , MP_ARG_OBJ | MP_ARG_REQUIRED , {} },
278
+ { MP_QSTR_private_key , MP_ARG_OBJ | MP_ARG_REQUIRED , {} },
279
+ { MP_QSTR_pers , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } }
280
+ };
281
+
282
+ // parse args
283
+ mp_arg_val_t args [MP_ARRAY_SIZE (mod_pycom_generate_rsa_signature_args )];
284
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (mod_pycom_generate_rsa_signature_args ), mod_pycom_generate_rsa_signature_args , args );
285
+
286
+ const char * message = mp_obj_str_get_str (args [0 ].u_obj );
287
+ const char * private_key = mp_obj_str_get_str (args [1 ].u_obj );
288
+
289
+ char * pers = "esp32-tls" ;
290
+ if (args [2 ].u_obj != MP_OBJ_NULL ) {
291
+ pers = (char * )mp_obj_str_get_str (args [2 ].u_obj );
292
+ }
293
+
294
+ mbedtls_pk_context pk_context ;
295
+ mbedtls_pk_init (& pk_context );
296
+
297
+ int rc = mbedtls_pk_parse_key (& pk_context , (const unsigned char * )private_key , strlen (private_key )+ 1 , NULL , 0 );
298
+ if (rc != 0 ) {
299
+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "Invalid Private Key, error code: %d" , rc ));
300
+ }
301
+
302
+ mbedtls_entropy_context entropy ;
303
+ mbedtls_ctr_drbg_context ctr_drbg ;
304
+ mbedtls_ctr_drbg_init (& ctr_drbg );
305
+ mbedtls_entropy_init (& entropy );
306
+
307
+ mbedtls_ctr_drbg_seed (
308
+ & ctr_drbg ,
309
+ mbedtls_entropy_func ,
310
+ & entropy ,
311
+ (const unsigned char * )pers ,
312
+ strlen (pers ));
313
+
314
+ uint8_t digest [32 ];
315
+ rc = mbedtls_md (mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ), (const unsigned char * )message , strlen (message ), digest );
316
+ if (rc != 0 ) {
317
+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_RuntimeError , "Message Digest operation failed, error code: %d" , rc ));
318
+ }
319
+
320
+ unsigned char * signature = m_malloc (5000 );
321
+ size_t signature_length ;
322
+
323
+ rc = mbedtls_pk_sign (& pk_context , MBEDTLS_MD_SHA256 , digest , sizeof (digest ), signature , & signature_length , mbedtls_ctr_drbg_random , & ctr_drbg );
324
+ if (rc != 0 ) {
325
+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_RuntimeError , "Signing failed, error code: %d!" , rc ));
326
+ }
327
+
328
+ mp_obj_t ret_signature = mp_obj_new_bytes ((const byte * )signature , signature_length );
329
+
330
+ mbedtls_pk_free (& pk_context );
331
+ m_free ((char * )signature );
332
+
333
+ return ret_signature ;
334
+ }
335
+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mod_crypt_generate_rsa_signature_obj , 2 , mod_crypt_generate_rsa_signature );
336
+
274
337
STATIC const mp_map_elem_t mp_module_AES_dict_table [] = {
275
338
{ MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_uAES ) },
276
339
{ MP_OBJ_NEW_QSTR (MP_QSTR_MODE_ECB ), MP_OBJ_NEW_SMALL_INT (CRYPT_MODE_ECB ) },
@@ -292,9 +355,10 @@ STATIC const mp_obj_type_t mod_crypt_aes = {
292
355
293
356
294
357
STATIC const mp_map_elem_t module_ucrypto_globals_table [] = {
295
- { MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_ucrypto ) },
296
- { MP_OBJ_NEW_QSTR (MP_QSTR_AES ), (mp_obj_t )& mod_crypt_aes },
297
- { MP_OBJ_NEW_QSTR (MP_QSTR_getrandbits ), (mp_obj_t )& getrandbits_obj },
358
+ { MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_ucrypto ) },
359
+ { MP_OBJ_NEW_QSTR (MP_QSTR_AES ), (mp_obj_t )& mod_crypt_aes },
360
+ { MP_OBJ_NEW_QSTR (MP_QSTR_getrandbits ), (mp_obj_t )& getrandbits_obj },
361
+ { MP_OBJ_NEW_QSTR (MP_QSTR_generate_rsa_signature ), (mp_obj_t )& mod_crypt_generate_rsa_signature_obj },
298
362
};
299
363
300
364
STATIC MP_DEFINE_CONST_DICT (module_ucrypto_globals , module_ucrypto_globals_table );
0 commit comments