:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: File Name: keygen.bat
::
:: Version: 1.0
::
:: Description:
::  Simple script to generate the RSA-2048 public and private keys using OpenSSL.
::  Once created, the script calls a python script to print out the public key
::  modulus that is ready to be inserted into cy_publicKey struct in
::  cy_si_keyStorage.c.
::  The script also generates a 128 bit random number to be used for the AES
::  private key and EIV.
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Copyright 2017-2018, Cypress Semiconductor Corporation. All rights reserved.
:: This software is owned by Cypress Semiconductor Corporation and is protected
:: by and subject to worldwide patent and copyright laws and treaties.
:: Therefore, you may use this software only as provided in the license agreement
:: accompanying the software package from which you obtained this software.
:: CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
:: WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
:: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off
set OUT_DIR="%~dp0\keys_generated"
set LOCAL_DIR=keys_generated
set PRIV_NAME=rsa_private_generated.txt
set PUB_NAME=rsa_public_generated.txt
set MOD_NAME=rsa_to_c_generated.txt

set AES_TEMP=aes_private_generated_temp.txt
set EIV_TEMP=aes_eiv_generated_temp.txt
set AES_NAME=aes_private_generated.txt
set EIV_NAME=aes_eiv_generated.txt
set AES_ARRAY=aes_private_array_generated.txt

:: Check if OpenSSL and Python are correctly installed
set ALLOK=1
CALL :testOpenSSL
CALL :testPython
IF %ALLOK% == 0 (
	echo.
	pause
	goto :end
)
IF NOT EXIST %OUT_DIR% mkdir %OUT_DIR%

:: Delete temp files
IF EXIST %OUT_DIR%\%AES_TEMP% DEL /F %OUT_DIR%\%AES_TEMP%
IF EXIST %OUT_DIR%\%EIV_TEMP% DEL /F %OUT_DIR%\%EIV_TEMP%

:: Generate the RSA-2048 public and private keys
openssl genrsa -out %OUT_DIR%\%PRIV_NAME% 2048
openssl rsa -in %OUT_DIR%\%PRIV_NAME% -outform PEM -pubout -out %OUT_DIR%\%PUB_NAME%

:: Generate a 128bit random number
openssl rand -hex -out %OUT_DIR%\%AES_TEMP% 16
openssl rand -hex -out %OUT_DIR%\%EIV_TEMP% 16

:: Check if files exist before processing
IF NOT EXIST %LOCAL_DIR%\%AES_TEMP% (
	echo Could not find OpenSSL generated files. If the error persists, check OpenSSL installation and permissions.
	echo.
	pause
	goto :end
)
IF NOT EXIST %LOCAL_DIR%\%AES_TEMP% (
	echo Could not find OpenSSL generated files. If the error persists, check OpenSSL installation and permissions.
	echo.
	pause
	goto :end
)

IF EXIST %OUT_DIR%\%AES_NAME% DEL /F %OUT_DIR%\%AES_NAME%
IF EXIST %OUT_DIR%\%EIV_NAME% DEL /F %OUT_DIR%\%EIV_NAME%
IF EXIST %OUT_DIR%\%AES_ARRAY% DEL /F %OUT_DIR%\%AES_ARRAY%

:: Remove new line characters from AES and EIV files
FOR /F "Usebackq Tokens=*" %%@ IN ("%LOCAL_DIR%\%AES_TEMP%") DO (
<NUL Set /P "=%%@"
) >> %LOCAL_DIR%\%AES_NAME%

FOR /F "Usebackq Tokens=*" %%@ IN ("%LOCAL_DIR%\%EIV_TEMP%") DO (
<NUL Set /P "=%%@"
) >> %LOCAL_DIR%\%EIV_NAME%

:: Delete temp files
IF EXIST %OUT_DIR%\%AES_TEMP% DEL /F %OUT_DIR%\%AES_TEMP%
IF EXIST %OUT_DIR%\%EIV_TEMP% DEL /F %OUT_DIR%\%EIV_TEMP%

:: Generate a C array with the AES private key
setlocal enableDelayedExpansion
set /p str=<%LOCAL_DIR%\%AES_NAME%
set counter=1
set "out="
for /f delims^=^ eol^= %%A in ('cmd /u /v:on /c echo(^^!str^^!^|more') do (
	IF "!counter!" == "1" (
		set "out=!out!, 0x^%%A"
		set "counter=0"
	) ELSE (
		set "out=!out!%%A"
		set "counter=1"
	)
 )
set "out=!out:~2!"

echo static const uint8_t AES128_Key[16] = {!out!}; >> %OUT_DIR%\%AES_ARRAY%

:: Create C-code ready public key
%~dp0\rsa_to_c.py %OUT_DIR%\%PUB_NAME% > %OUT_DIR%\%MOD_NAME%

goto :end

:testOpenSSL
openssl version >nul 2>nul
IF ERRORLEVEL 1 CALL :errOpenSSL
EXIT /B

:testPython
python --version >nul 2>nul
IF ERRORLEVEL 1 CALL :errPython
EXIT /B

:errOpenSSL
echo.
echo OpenSSL could not be found. 
echo If OpenSSL is installed, add the OpenSSL binaries directory to the system Path variable. 
echo A restart may be required.
set ALLOK=0
EXIT /B

:errPython
echo.
echo Python could not be found. 
echo Python is required to generate the RSA public key C array. 
echo Please install Python or check that it is included in the system Path variable.
set ALLOK=0
EXIT /B

:end
pause
