xref: /OK3568_Linux_fs/tools/linux/Linux_SecurityAVB/scripts/avb-challenge-verify.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#/user/bin/env python
2*4882a593Smuzhiyun"this is a test module for getting unlock challenge"
3*4882a593Smuzhiyunimport sys
4*4882a593Smuzhiyunimport  os
5*4882a593Smuzhiyunfrom hashlib import sha256
6*4882a593Smuzhiyun
7*4882a593Smuzhiyundef challenge_verify():
8*4882a593Smuzhiyun	if (len(sys.argv) != 3) :
9*4882a593Smuzhiyun		print "Usage: rkpublickey.py [challenge_file] [product_id_file]"
10*4882a593Smuzhiyun		return
11*4882a593Smuzhiyun	if ((sys.argv[1] == "-h") or (sys.argv[1] == "--h")):
12*4882a593Smuzhiyun		print "Usage: rkpublickey.py [challenge_file] [product_id_file]"
13*4882a593Smuzhiyun		return
14*4882a593Smuzhiyun	try:
15*4882a593Smuzhiyun		challenge_file = open(sys.argv[1], 'rb')
16*4882a593Smuzhiyun		product_id_file = open(sys.argv[2], 'rb')
17*4882a593Smuzhiyun		challenge_random_file = open('unlock_challenge.bin', 'wb')
18*4882a593Smuzhiyun		challenge_data = challenge_file.read(52)
19*4882a593Smuzhiyun		product_id_data = product_id_file.read(16)
20*4882a593Smuzhiyun		product_id_hash = sha256(product_id_data).digest()
21*4882a593Smuzhiyun		print("The challege version is %d" %ord(challenge_data[0]))
22*4882a593Smuzhiyun		if (product_id_hash != challenge_data[4:36]) :
23*4882a593Smuzhiyun			print("Product id verify error!")
24*4882a593Smuzhiyun			return
25*4882a593Smuzhiyun		challenge_random_file.write(challenge_data[36:52])
26*4882a593Smuzhiyun		print("Success!")
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun	finally:
29*4882a593Smuzhiyun		if challenge_file:
30*4882a593Smuzhiyun			challenge_file.close()
31*4882a593Smuzhiyun		if product_id_file:
32*4882a593Smuzhiyun			product_id_file.close()
33*4882a593Smuzhiyun		if challenge_random_file:
34*4882a593Smuzhiyun			challenge_random_file.close()
35*4882a593Smuzhiyun
36*4882a593Smuzhiyunif __name__ == '__main__':
37*4882a593Smuzhiyun	challenge_verify()
38