Skip to content

Commit

Permalink
feat: updated procedure for generating token in jtag_example_helper.py
Browse files Browse the repository at this point in the history
This commit updated API for generating token providing ability to
pass more arguments and updated readme file as well.
  • Loading branch information
nileshkale123 authored and AdityaHPatwardhan committed Jun 27, 2024
1 parent a57aa71 commit 40059de
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/security/hmac_soft_jtag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ espefuse.py -p $ESPPORT burn_key <KEY_BLOCK_NO> <KEY_FILE>.bin HMAC_DOWN_ALL
**Step 4:** Generate token data from the HMAC key. Keep this token data handy before re-enabling JTAG access.

```bash
python jtag_example_helper.py generate_token <KEY_FILE>.bin
python jtag_example_helper.py generate_token <KEY_FILE>.bin <OUTPUT_FILE(optional)>
```
**Note**: The OUTPUT_FILE argument is optional. If provided, the token data will be stored in the specified file in either binary format (.bin) or hexadecimal format (.hex) based on the file extension. If no OUTPUT_FILE is specified, the token data will be printed to the console.

### Configure the project

Expand Down
19 changes: 16 additions & 3 deletions examples/security/hmac_soft_jtag/jtag_example_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@
import hmac
import os
import subprocess
from typing import Optional


def generate_token_data(hmac_key_file: str) -> None:
def generate_token_data(hmac_key_file: str, output_file: Optional[str] = None) -> None:
with open(hmac_key_file, 'rb') as file:
key_data = file.read()
data = bytes([0] * 32)
token_data = hmac.HMAC(key_data, data, hashlib.sha256).digest()
token_hex = binascii.hexlify(token_data).decode('utf-8')
print(token_hex)

if output_file:
if output_file.endswith('.bin'):
with open(output_file, 'wb') as out_file:
out_file.write(token_data)
elif output_file.endswith('.hex'):
with open(output_file, 'w') as out_file:
out_file.write(token_hex)
else:
print(f'Unsupported file format for output file: {output_file}')
else:
print(token_hex)


def generate_hmac_key(hmac_key_file: str) -> None:
Expand Down Expand Up @@ -69,6 +81,7 @@ def main() -> None:

token_generator_parser = subparsers.add_parser('generate_token')
token_generator_parser.add_argument('hmac_key_file', help='File containing the HMAC key')
token_generator_parser.add_argument('output_file', nargs='?', help='File to store the generated token (optional)')

args = parser.parse_args()

Expand All @@ -77,7 +90,7 @@ def main() -> None:
elif args.command == 'generate_hmac_key':
generate_hmac_key(args.hmac_key_file)
elif args.command == 'generate_token':
generate_token_data(args.hmac_key_file)
generate_token_data(args.hmac_key_file, args.output_file)
else:
parser.print_help()

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/�f ~=ʝ�ͷ���iq:+���<�����e?>��e6� ᯪ�J_�(��l@"�
���.��f�n�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1,39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R/DhJdAoHBAI8j+abPyQDw
0iSi5sx76Abtlb/P8nFZ4Njqf8ojQaytMQGO8DG7e1YMPyq/xIKUVQyFmf8ITx7a
RxlU98sMTiDylVueG2Mx1xNsXlIaX4FDFsvJXUKD4xss4PVv8ZhOgb7uJpd18Op/
mKfSByfQ/CFjnq+QBBs2KOFyHvwgtJhvn1KddyXFa/E4qEIpjjTZTKfaksweFVCv
GmQ/GFqqd6Ih00Q19bgilpvTDDKjrklkAd0DR13TkLLEGqEaLbjXzA==
-----END RSA PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������{qػ�a��768�Lpg���������{qػ�a��768�Lpg������Ŕ�1�㏉�a��>����z�(̍��Q
Expand Down

0 comments on commit 40059de

Please # to comment.