#roloFlash 2, v07.*
! ***************************************************************************
! *
! * Sample script for encrypt any file
! *
! * Copyright (C) 2009-2025 by halec embedded solutions
! *
! ***************************************************************************
! For all files on the microSD card, the following applies:
! - File name has to be in 8.3 format
! - File name must contain only CAPITAL LETTERS
! - (see manual, chapter "Files")
! *** Please adapt to your requirements! ***
plainFilename = "FLASH.HEX" ! Any file you want to encrypt, for a example
! a hex file.
plainFileSystem = SDCARD ! The filesystem of the given file.
! Can be SDCARD, FLASHVARS or FLASHDISK.
cryptedFilename = "FLASH.CRY" ! The name of the encrypted file to create.
cryptedFileSystem = SDCARD ! The filesystem of the given file.
! Can be SDCARD, FLASHVARS or FLASHDISK.
algo = SEC_AES ! Specify the Crypt Algorithm, here:
width = 256 ! AES with 256 bit in CBC mode, which needs
mode = SEC_CBC ! an additional initialisation vector (iv)
padding = SEC_PKCS7 ! Crypt algorthm is a blockcyper. To
! encrypt/decrpyt the padding algorithm PKCS7
! is recommended.
! A key and an initialisation vector (iv) is needed, REPLACE BY OWN KEY AND IV!
! For AES-256 a 32 Byte bey is needed.
! For CBC mode a 16 Byte initialisation vector is needed.
key = char($b0, $cd, $18, $be, $a5, $66, $92, $49, _
$11, $05, $f6, $29, $34, $18, $27, $12, _
$90, $d0, $71, $ff, $c9, $72, $21, $c2, _
$e4, $d7, $de, $e9, $05, $9e, $b7, $0d, _
$7d, $21, $09, $40, $33, $d4, $a3, $bb, _
$89, $13, $bd, $54, $88, $ea, $3b, $a6, _
$8c, $1a, $c2, $08, $87, $5a, $9b, $01, _
$9a, $1d, $1d, $e5, $50, $ee, $d9, $1f)
iv = char($48, $09, $fc, $c2, $3f, $1d, $a8, $e0, _
$39, $d6, $a8, $8d, $5d, $bb, $f5, $30, _
$2d, $1b, $50, $bf, $57, $38, $9e, $8c, _
$31, $be, $81, $f0, $78, $3e, $bf, $d3)
! Green running light from LED 1 to LED 2 -> symbolizes script processing
! (LED 5 is kept free for display of "Done")
led_runningLight 1, 2, COLOR_GREEN, 200
! ---- Preparations ----
! Delete old log file, if present
!
f = "LOG.TXT"
if fs_fileExists(0,f)
fs_remove 0, f
endif
! Write software version of roloFlash and script name to LOG.TXT
print "softwareVersion=", sys_softwareVersion, "\r\n"
print "Running script copied from scripts/secureApi/ENCRYPT.BAS\r\n"
!
! ---- A general purpose file encrypt/decrpyt procedure ----
!
procedure copyFileSEC fsSrc, filenameSrc, fsDst, filenameDst, cryptSpec, doEncrypt
fileSizeSrc = fs_fileSize(fsSrc, filenameSrc)
src = fs_open(fsSrc, filenameSrc)
for pass = 0 to 1
if pass = 0
pass = 1
if fsDst <> SDCARD
if doEncrypt
fileSizeDst = (fileSizeSrc and $fffffff0) + 16
elseif cryptSpec[0][2] = SEC_ECB
filePos = fileSizeSrc - 16
a = fs_read(src, filePos, 16)
sec_decrypt a, cryptSpec, SEC_SINGLEBLOCK
fileSizeDst = filePos + size(a)
else
pass = 0
endif
endif
endif
if pass = 1
if fsDst = SDCARD
fs_create fsDst, filenameDst
else
fs_create fsDst, filenameDst, fileSizeDst
endif
dst = fs_open(fsDst, filenameDst)
endif
blockSize = 8192
filePos = 0
do while filePos < filesizeSrc > 0
mySize = blockSize
if mySize > filesizeSrc - filePos
mySize = filesizeSrc - filePos
endif
a = fs_read(src, filePos, mySize)
if filesizeSrc <= blockSize
opState = SEC_SINGLEBLOCK
elseif filePos = 0
opState = SEC_FIRSTBLOCK
elseif mySize >= filesizeSrc - filePos
opState = SEC_LASTBLOCK
else
opState = SEC_NEXTBLOCK
endif
if doEncrypt
sec_encrypt a, cryptSpec, opState
else
sec_decrypt a, cryptSpec, opState
endif
if pass = 1
fs_write dst, filePos, a
endif
filePos = filePos + mySize
loop
if pass = 0
fileSizeDst = fileSizeSrc - 16 + size(a)
endif
next
fs_close dst
fs_close src
end
!
! Build cryptSpec and encrypt
!
algoSpec = vari(algo, width, mode)
dataSpec = vari(key, iv, padding)
cryptSpec = vari(algoSpec, dataSpec)
copyFileSEC plainFileSystem, plainFilename, cryptedFileSystem, cryptedFilename, cryptSpec, 1
! ---- Check for possibly occurred exceptions, write ----
! ---- evaluation to log file and signal it via LEDs ----
catch exception
print "Duration [ms]: ", sys_getSystemTime(), "\r\n"
catch dummyException ! If the last print throws an exception
if exception <> 0
! There has been an error, record the error in LOG.TXT
print "ERROR: Exception ", exception
! Throw exception again, after it has been caught. As a result, the number
! of the exception gets displayed via LED blink codes. The blink codes
! are documented in the manual, chapter "Meaning of LED Codes", subchapter
! "Exception has Occurred"
throw exception
else
! No errors: write to log file and switch LED 5 to green
print "Script ran successfully.\r\n"
led_on 5, COLOR_GREEN
endif