@@ -41,6 +41,12 @@ define_property(TARGET
4141 BRIEF_DOCS "AES key for encrypting"
4242 FULL_DOCS "AES key for encrypting"
4343)
44+ define_property (TARGET
45+ PROPERTY PICOTOOL_EMBED_DECRYPTION
46+ INHERITED
47+ BRIEF_DOCS "Embed decryption stage into encrypted binary"
48+ FULL_DOCS "Embed decryption stage into encrypted binary"
49+ )
4450define_property (TARGET
4551 PROPERTY PICOTOOL_ENC_SIGFILE
4652 INHERITED
@@ -360,7 +366,7 @@ endfunction()
360366
361367# pico_encrypt_binary(TARGET AESFILE [SIGFILE])
362368# Encrypt the target binary with the given AES key (should be a binary
363- # file containing 32 bytes of a random key), and sign the encrypted binary.
369+ # file containing 128 bytes of a random key), and sign the encrypted binary.
364370# This sets PICOTOOL_AESFILE to AESFILE, and PICOTOOL_ENC_SIGFILE to SIGFILE
365371# if present, else PICOTOOL_SIGFILE.
366372function (pico_encrypt_binary TARGET AESFILE)
@@ -387,6 +393,37 @@ function(pico_encrypt_binary TARGET AESFILE)
387393 endif ()
388394endfunction ()
389395
396+ # pico_create_decrypting_binary(TARGET AESFILE [SIGFILE])
397+ # Encrypt the target binary with the given AES key (should be a binary
398+ # file containing 128 bytes of a random key), add a decryption stage to
399+ # decrypt the binary at runtime, and then sign the encrypted binary.
400+ # This sets PICOTOOL_AESFILE to AESFILE, PICOTOOL_EMBED_DECRYPTION to TRUE,
401+ # and PICOTOOL_ENC_SIGFILE to SIGFILE if present, else PICOTOOL_SIGFILE.
402+ function (pico_create_decrypting_binary TARGET AESFILE)
403+ picotool_check_configurable(${TARGET} )
404+ set_target_properties (${TARGET} PROPERTIES
405+ PICOTOOL_AESFILE ${AESFILE}
406+ PICOTOOL_EMBED_DECRYPTION TRUE
407+ )
408+ if (ARGC EQUAL 3)
409+ set_target_properties (${TARGET} PROPERTIES
410+ PICOTOOL_ENC_SIGFILE ${ARGV2}
411+ )
412+ else ()
413+ get_target_property (enc_sig_file ${TARGET} PICOTOOL_ENC_SIGFILE)
414+ if (NOT enc_sig_file)
415+ get_target_property (sig_file ${TARGET} PICOTOOL_SIGFILE)
416+ if (NOT sig_file)
417+ message (FATAL_ERROR "Signature file not set for ${TARGET} " )
418+ else ()
419+ set_target_properties (${TARGET} PROPERTIES
420+ PICOTOOL_ENC_SIGFILE ${sig_file}
421+ )
422+ endif ()
423+ endif ()
424+ endif ()
425+ endfunction ()
426+
390427# pico_add_uf2_output(TARGET)
391428# Add a UF2 output using picotool - must be called after
392429# all required properties have been set
@@ -519,9 +556,20 @@ function(picotool_postprocess_binary TARGET)
519556 endif ()
520557 # Encryption
521558 if (picotool_aesfile)
559+ get_target_property (picotool_embed_decryption ${TARGET} PICOTOOL_EMBED_DECRYPTION)
560+ if (picotool_embed_decryption)
561+ list (APPEND picotool_encrypt_args "--embed" )
562+ endif ()
563+
522564 add_custom_command (TARGET ${TARGET} POST_BUILD
523565 DEPENDS ${picotool_enc_sigfile} ${picotool_aesfile}
524- COMMAND picotool encrypt --quiet --hash --sign $<TARGET_FILE:${TARGET} > $<TARGET_FILE:${TARGET} > ${picotool_aesfile} ${picotool_enc_sigfile}
566+ COMMAND picotool
567+ ARGS encrypt
568+ --quiet --hash --sign
569+ ${picotool_encrypt_args}
570+ $<TARGET_FILE:${TARGET} > $<TARGET_FILE:${TARGET} >
571+ ${picotool_aesfile} ${picotool_enc_sigfile}
572+ COMMAND_EXPAND_LISTS
525573 VERBATIM )
526574 if (ARGC EQUAL 2)
527575 set (${ARGV1} TRUE PARENT_SCOPE)
0 commit comments