Skip to content

Commit

Permalink
espsecure digest_secure_bootloader: Fix Python 3 string vs bytes usage
Browse files Browse the repository at this point in the history
Closes #382
  • Loading branch information
projectgus committed Dec 20, 2018
1 parent f262eac commit 057a7e2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -43,6 +43,7 @@ script:
# doesn't allow running esptool.py as a script...
- pip install pyserial pyelftools
- python test/test_imagegen.py
- python test/test_espsecure.py
- pip install flake8 flake8-future-import # matches tests_require in setup.py, a bit hacky
- python setup.py flake8
- python setup.py install
Expand Down
2 changes: 1 addition & 1 deletion espsecure.py
Expand Up @@ -91,7 +91,7 @@ def digest_secure_bootloader(args):
# if image isn't 128 byte multiple then pad with 0xFF (ie unwritten flash)
# as this is what the secure boot engine will see
if len(plaintext_image) % 128 != 0:
plaintext_image += "\xFF" * (128 - (len(plaintext_image) % 128))
plaintext_image += b"\xFF" * (128 - (len(plaintext_image) % 128))

plaintext = iv + plaintext_image

Expand Down
1 change: 1 addition & 0 deletions test/secure_images/256bit_iv.bin
@@ -0,0 +1 @@
EV8z"� �%�F�+�xk��)~�k�7JI��G>
1 change: 1 addition & 0 deletions test/secure_images/256bit_key.bin
@@ -0,0 +1 @@
�>\'*���8%��q+�8��Lm*�E����
Binary file added test/secure_images/bootloader.bin
Binary file not shown.
Binary file added test/secure_images/bootloader_digested.bin
Binary file not shown.
Binary file added test/secure_images/digest_iv.bin
Binary file not shown.
78 changes: 78 additions & 0 deletions test/test_espsecure.py
@@ -0,0 +1,78 @@
#!/usr/bin/env python
#
# Tests for espsecure.py
#
# Assumes openssl binary is in the PATH
import unittest
import subprocess
import os
import os.path
import io
import sys
import tempfile
from collections import namedtuple

TEST_DIR = os.path.abspath(os.path.dirname(__file__))
os.chdir(TEST_DIR)

try:
import espsecure
except ImportError:
sys.path.insert(0, os.path.join(TEST_DIR, ".."))
import espsecure

import esptool

class EspSecureTestCase(unittest.TestCase):

def run_espsecure(self, args):
""" Run espsecure.py with the specified arguments
Returns output as a string if there is any, raises an exception if espsecure.py fails
"""
cmd = [sys.executable, ESPSECURE_PY ] + args.split(" ")
print("Running %s..." % (" ".join(cmd)))

try:
output = subprocess.check_output([str(s) for s in cmd],
cwd=TEST_DIR,
stderr=subprocess.STDOUT)
print(output)
return output.decode("utf-8")
except subprocess.CalledProcessError as e:
print(e.output)
raise e

def _open(image_file):
return open(os.path.join('secure_images', image_file), 'rb')

class SecureBootloaderTests(unittest.TestCase):

def test_digest_bootloader(self):
DBArgs = namedtuple('digest_bootloader_args', [
'keyfile',
'output',
'iv',
'image' ])

try:
output_file = tempfile.NamedTemporaryFile(delete=False)
output_file.close()

args = DBArgs(_open('256bit_key.bin'),
output_file.name,
_open('256bit_iv.bin'),
_open('bootloader.bin'))
espsecure.digest_secure_bootloader(args)

with open(output_file.name, 'rb') as of:
with _open('bootloader_digested.bin') as ef:
self.assertEqual(ef.read(), of.read())
finally:
os.unlink(output_file.name)


if __name__ == '__main__':
print("Running espsecure tests...")
print("Using espsecure %s at %s" % (esptool.__version__, os.path.abspath(espsecure.__file__)))
unittest.main(buffer=True)

0 comments on commit 057a7e2

Please sign in to comment.