forked from commaai/panda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*.pyc | ||
.*.swp | ||
*.o | ||
a.out | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
gcc -DTEST_RSA test_rsa.c ../crypto/rsa.c ../crypto/sha.c && ./a.out | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define MAX_LEN 0x40000 | ||
char buf[MAX_LEN]; | ||
|
||
#include "../crypto/sha.h" | ||
#include "../crypto/rsa.h" | ||
#include "../obj/cert.h" | ||
|
||
int main() { | ||
FILE *f = fopen("../obj/panda.bin", "rb"); | ||
int tlen = fread(buf, 1, MAX_LEN, f); | ||
fclose(f); | ||
printf("read %d\n", tlen); | ||
uint32_t *_app_start = (uint32_t *)buf; | ||
|
||
int len = _app_start[0]; | ||
char digest[SHA_DIGEST_SIZE]; | ||
SHA_hash(&_app_start[1], len-4, digest); | ||
printf("SHA hash done\n"); | ||
|
||
if (!RSA_verify(&rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) { | ||
printf("RSA fail\n"); | ||
} else { | ||
printf("RSA match!!!\n"); | ||
} | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
#!/usr/bin/env python | ||
import sys | ||
import struct | ||
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)) | ||
x /= (2**32) | ||
return "{"+'U,'.join(map(str, nums))+"U}" | ||
|
||
rsa = RSA.importKey(open(sys.argv[1]).read()) | ||
mod = (hex(rsa.n)[2:-1].rjust(0x100, '0')) | ||
hh = ''.join('\\x'+mod[i:i+2] for i in range(0, 0x100, 2)) | ||
print 'char rsa_mod[] = "'+hh+'";' | ||
print 'int rsa_e = %d;' % rsa.e | ||
rr = pow(2**1024, 2, rsa.n) | ||
n0inv = 2**32 - modinv(rsa.n, 2**32) | ||
|
||
print 'RSAPublicKey rsa_key = {.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 '};' | ||
|
||
|