You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					46 lines
				
				1018 B
			
		
		
			
		
	
	
					46 lines
				
				1018 B
			| 
											6 years ago
										 | #!/usr/bin/env python3
 | ||
| 
											6 years ago
										 | import sys
 | ||
|  | from Crypto.PublicKey import RSA
 | ||
|  | 
 | ||
|  | def egcd(a, b):
 | ||
|  |   if a == 0:
 | ||
|  |     return (b, 0, 1)
 | ||
|  |   else:
 | ||
|  |     g, y, x = egcd(b % a, a)
 | ||
|  |     return (g, x - (b // a) * y, y)
 | ||
|  | 
 | ||
|  | def modinv(a, m):
 | ||
|  |   g, x, y = egcd(a, m)
 | ||
|  |   if g != 1:
 | ||
|  |     raise Exception('modular inverse does not exist')
 | ||
|  |   else:
 | ||
|  |     return x % m
 | ||
|  | 
 | ||
|  | def to_c_string(x):
 | ||
|  |   mod = (hex(x)[2:-1].rjust(0x100, '0'))
 | ||
|  |   hh = ''.join('\\x'+mod[i:i+2] for i in range(0, 0x100, 2))
 | ||
|  |   return hh
 | ||
|  | 
 | ||
|  | def to_c_uint32(x):
 | ||
|  |   nums = []
 | ||
|  |   for i in range(0x20):
 | ||
|  |     nums.append(x%(2**32))
 | ||
| 
											6 years ago
										 |     x //= (2**32)
 | ||
| 
											6 years ago
										 |   return "{"+'U,'.join(map(str, nums))+"U}"
 | ||
|  | 
 | ||
|  | for fn in sys.argv[1:]:
 | ||
|  |   rsa = RSA.importKey(open(fn).read())
 | ||
|  |   rr = pow(2**1024, 2, rsa.n)
 | ||
|  |   n0inv = 2**32 - modinv(rsa.n, 2**32)
 | ||
|  | 
 | ||
|  |   cname = fn.split("/")[-1].split(".")[0] + "_rsa_key"
 | ||
|  | 
 | ||
| 
											6 years ago
										 |   print('RSAPublicKey '+cname+' = {.len = 0x20,')
 | ||
|  |   print('  .n0inv = %dU,' % n0inv)
 | ||
|  |   print('  .n = %s,' % to_c_uint32(rsa.n))
 | ||
|  |   print('  .rr = %s,' % to_c_uint32(rr))
 | ||
|  |   print('  .exponent = %d,' % rsa.e)
 | ||
|  |   print('};')
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 |