sign.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2026 Aarav Ravindra Kharade
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import sys
  18. import hashlib
  19. def sign_file(key_file, target_file, output_file):
  20. # 1. Read the secret key
  21. with open(key_file, 'r') as f:
  22. lines = f.readlines()
  23. key = None
  24. for line in lines:
  25. line = line.strip()
  26. # The user places their key below the comment
  27. if line.startswith('ARK-OS-'):
  28. key = line
  29. break
  30. if not key:
  31. print("Error: Could not find ARK-OS key in", key_file)
  32. sys.exit(1)
  33. # 2. Read the target file
  34. with open(target_file, 'rb') as f:
  35. target_data = f.read()
  36. # 3. Compute HMAC-like SHA256(KEY + DATA)
  37. m = hashlib.sha256()
  38. m.update(key.encode('utf-8'))
  39. m.update(target_data)
  40. signature_hex = m.hexdigest()
  41. # 4. Write signature
  42. with open(output_file, 'w') as f:
  43. f.write(signature_hex)
  44. print(f"[Verified Boot] Signed {target_file} with key {key[:15]}... -> {output_file}")
  45. if __name__ == "__main__":
  46. if len(sys.argv) != 4:
  47. print("Usage: sign.py <key_file> <target_file> <output_file>")
  48. sys.exit(1)
  49. sign_file(sys.argv[1], sys.argv[2], sys.argv[3])