-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsademo.py
51 lines (40 loc) · 993 Bytes
/
rsademo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import hashlib
import random
def is_prime(n,k=40):
if n<2:return False
for p in[2,3,5,7,11,13,17,19,23,29]:
if n<p*p:return n>1
if n%p==0:return False
s,d=0,n-1
while d%2==0:d,s=d>>1,s+1
for i in range(k):
x=pow(random.randint(2,n-2),d,n)
if x==1 or x==n-1:continue
for r in range(1,s):
x=pow(x,2,n)
if x==n-1:break
else:return False
return True
def sample_prime():
p = random.randint(2**1023,2**1024-1)
while not is_prime(p):p=random.randint(2**1023,2**1024-1)
return p
if __name__ == '__main__':
p = sample_prime()
q = sample_prime()
N = p*q
print('RSA modulus N: ', hex(N))
# Compute the hash
h = hashlib.sha256(N.to_bytes(256, 'big')).digest()
print('sha2(N):', h.hex())
### Remote attestation
# Set the user data
with open("/dev/attestation/user_report_data", "wb") as f:
f.write(h)
# Read the quote
with open("/dev/attestation/quote", "rb") as f:
quote = f.read()
# Write the quote
print("quote")
print(quote.hex())