#include "rtv.h" typedef unsigned char byte; typedef unsigned long uint32; #define BEEDLLAPI #include "beecrypt/beecrypt.h" #include "beecrypt/elgamal.h" typedef struct { u32 length; u32 * val; } vec; typedef struct { vec p, n, g, y; } egkey; static u32 p0[] = {0xbf0f914f, 0x28355833, 0x98a50adf, 0x328985ef, 0x86503559, 0xc540e415, 0xcb68c6b6, 0x3cbddd86, 0x00b78647, 0xf850b98d, 0xf36d4500, 0x91f1cd50, 0xa7e9f37a, 0x65a601d9, 0x3c24b23f, 0xed87895b}; static u32 n0[] = {0xbf0f914f, 0x28355833, 0x98a50adf, 0x328985ef, 0x86503559, 0xc540e415, 0xcb68c6b6, 0x3cbddd86, 0x00b78647, 0xf850b98d, 0xf36d4500, 0x91f1cd50, 0xa7e9f37a, 0x65a601d9, 0x3c24b23f, 0xed87895a}; static u32 g0[] = {0x6}; static u32 y0[] = {0x9e73a44d, 0x869c5832, 0x70b54fec, 0xe554d094, 0x95f50317, 0x162ccf1d, 0x27fb9d0f, 0x9aeb3e20, 0x20e60531, 0x6bde25c4, 0x573d0765, 0x243ba91d, 0x3cfbde99, 0xea142b2c, 0xcc829f09, 0xa74a0395}; static egkey egkey0 = { {sizeof(p0)/4, p0}, {sizeof(n0)/4, n0}, {sizeof(g0)/4, g0}, {sizeof(y0)/4, y0} }; static u32 p1[] = {0xb06acfc7, 0xfe4b5202, 0x4a88e8ec, 0x7ba7a600, 0x50f06ed7, 0x53fdadc9, 0x63004b01, 0x5407be8b, 0x2c4c0d7c, 0x6fba9b2f, 0x4f20e5a5, 0xba601b1b, 0x142dfd0f, 0x7ad7e564, 0x2f21bc7a, 0xede47e2a, 0x095f4702, 0xa5e7b7fa, 0xf19995b6, 0x6aa0ded9, 0x1e701033, 0x8299cfb4, 0x57c8946b, 0x554ef76f}; static u32 n1[] = {0xb06acfc7, 0xfe4b5202, 0x4a88e8ec, 0x7ba7a600, 0x50f06ed7, 0x53fdadc9, 0x63004b01, 0x5407be8b, 0x2c4c0d7c, 0x6fba9b2f, 0x4f20e5a5, 0xba601b1b, 0x142dfd0f, 0x7ad7e564, 0x2f21bc7a, 0xede47e2a, 0x095f4702, 0xa5e7b7fa, 0xf19995b6, 0x6aa0ded9, 0x1e701033, 0x8299cfb4, 0x57c8946b, 0x554ef76e}; static u32 g1[] = {0x5}; static u32 y1[] = {0x1a613a67, 0x9b9b1911, 0xb4b58452, 0x47a444c7, 0xa335acfc, 0x19afb7ba, 0x284ec56c, 0xf828481d, 0xe4e4d336, 0xc73be3c2, 0xb75e2a8f, 0x6a265b18, 0xf0b8bbb5, 0x1ef31704, 0xcd17730e, 0x6fd4d9b8, 0x22e00150, 0xf594ab83, 0x4225d0c1, 0xdc55b64d, 0x3ece48ac, 0x3c920126, 0xcee12397, 0x01decf40}; static egkey egkey1 = { {sizeof(p1)/4, p1}, {sizeof(n1)/4, n1}, {sizeof(g1)/4, g1}, {sizeof(y1)/4, y1} }; static egkey * keys[] = {&egkey0, &egkey1}; static void data_to_mp32(mp32number * num, unsigned int len, unsigned char * text) { unsigned char * p; u32 i, v, d; mp32nzero(num); mp32nsize(num, (len+3)/4); if (len == 0) return; v = 0; d = 0; for (p = text, i = 0; i < len; p++, i++) { v = (v << 8) + *p; if ((i & 3) == 3) { num->data[d++] = v; v = 0; } } if (i & 3) /* I'm not sure this is quite right -- */ num->data[d++] = v; /* may need to left shift v -- doesn't */ return; /* matter in practice for this purpose */ } static u32 length_data_to_mp32n(mp32number * num, unsigned char ** pp, u32 * plength) { u32 length; unsigned char * p; int i; length = *plength; mp32nzero(num); if (length < 4) return 0; mp32nsize(num, rtv_to_u32(pp)); length -= 4; if (num->size == 0) goto exit; for (i = 0; i < num->size; i++) { if (length < 4) return 0; num->data[i] = rtv_to_u32(pp); length -= 4; } exit: *plength = length; return 1; } static int eg_verify(egkey * key, void * text, u32 len, void * sig, u32 sig_len) { unsigned char * sigp; mp32barrett p, n; mp32number g, y, hm, r, s; sigp = sig; mp32bzero(&p); mp32bzero(&n); mp32bset(&p, key->p.length, key->p.val); mp32bset(&n, key->n.length, key->n.val); mp32ninit(&g, key->g.length, key->g.val); mp32ninit(&y, key->y.length, key->y.val); data_to_mp32(&hm, len, text); if (length_data_to_mp32n(&r, &sigp, &sig_len) == 0) return 0; if (length_data_to_mp32n(&s, &sigp, &sig_len) == 0) return 0; if (sig_len) return 0; return elgv3vrfy(&p, &n, &g, &hm, &y, &r, &s); } int check_signature(int key_number, void * text, u32 len, void * sig, u32 sig_len) { return eg_verify(keys[key_number], text, len, sig, sig_len); } #if DEBUG int main(int argc, char ** argv) { int r; unsigned char text0_0[] = {0x8e, 0x09, 0xdd, 0x80, 0x85, 0x86, 0x99, 0xd3, 0xdc, 0x10, 0x97, 0xd4, 0xc5, 0xcc, 0x96, 0xa0}; unsigned char sig0_0[] = {0x00, 0x00, 0x00, 0x10, 0x84, 0x63, 0x63, 0x64, 0xb0, 0xf8, 0x07, 0x9b, 0xb1, 0x86, 0x2b, 0x88, 0xad, 0x44, 0xa6, 0x1b, 0x4f, 0x1e, 0xde, 0x2a, 0x52, 0x77, 0xb7, 0xb1, 0x5d, 0x24, 0x46, 0x55, 0x87, 0x79, 0x39, 0xc1, 0xe2, 0x6b, 0x46, 0x30, 0xfa, 0xf5, 0xc1, 0x15, 0x73, 0xb0, 0xeb, 0x2b, 0xf5, 0x13, 0x13, 0xd9, 0x7a, 0x15, 0x5a, 0x44, 0x5d, 0x2a, 0x02, 0x8c, 0x48, 0x6d, 0xb0, 0x5d, 0x61, 0x72, 0x4f, 0xe0, 0x00, 0x00, 0x00, 0x10, 0xa5, 0x1f, 0xb1, 0xa9, 0x47, 0x11, 0x4a, 0x4e, 0xc0, 0x1b, 0x66, 0x52, 0xb5, 0x02, 0xe9, 0x2d, 0x95, 0xce, 0x91, 0x83, 0x70, 0xef, 0x31, 0xeb, 0xa0, 0xd0, 0xf1, 0xca, 0xaa, 0x12, 0x43, 0x2c, 0x6e, 0xfb, 0x05, 0x5c, 0x3d, 0x37, 0x88, 0x1c, 0x13, 0x41, 0x94, 0x36, 0xab, 0x6b, 0x3b, 0xfc, 0x7f, 0xe5, 0x78, 0x36, 0xea, 0x8f, 0xff, 0x34, 0x74, 0x0c, 0x39, 0xc5, 0x88, 0xaa, 0xd5, 0xf6}; unsigned char text0_1[] ={0x52, 0x51, 0x27, 0x0e, 0x99, 0xb6, 0xe0, 0x95, 0x2a, 0x5e, 0x30, 0x17, 0x79, 0x75, 0xae, 0xcd}; unsigned char sig0_1[] = {0x00, 0x00, 0x00, 0x10, 0x9e, 0x8c, 0x98, 0xe6, 0xde, 0x78, 0x63, 0xb9, 0xf9, 0x79, 0xa4, 0xb0, 0x53, 0xf7, 0x22, 0x8c, 0xa9, 0x74, 0x97, 0xf9, 0xad, 0x08, 0x75, 0x1c, 0x0a, 0x07, 0xef, 0x8b, 0x85, 0xf6, 0xc5, 0x63, 0xe0, 0x0f, 0x36, 0x24, 0x12, 0xa0, 0x1a, 0x22, 0xbf, 0xb1, 0x92, 0xa7, 0x29, 0xb9, 0x1a, 0x9c, 0xe9, 0x9f, 0x0f, 0x64, 0x8c, 0xe9, 0x08, 0xc5, 0x71, 0xac, 0x25, 0x55, 0x11, 0xbc, 0x69, 0xa9, 0x00, 0x00, 0x00, 0x10, 0x46, 0x8a, 0x50, 0xec, 0x91, 0x3b, 0x39, 0x3a, 0x90, 0x8e, 0x4a, 0x74, 0xd4, 0x12, 0xf7, 0x41, 0x3f, 0x0e, 0x3b, 0xb7, 0xde, 0x74, 0xa9, 0xbb, 0xa9, 0xfb, 0x6a, 0xbf, 0xb2, 0x31, 0x4a, 0x7b, 0x55, 0x73, 0x72, 0xbd, 0x4a, 0x2b, 0x77, 0xa9, 0x9e, 0x73, 0x9d, 0xdc, 0x5c, 0x0f, 0xd8, 0x7e, 0x7f, 0x92, 0xa6, 0xfe, 0x18, 0x7e, 0x13, 0x27, 0xab, 0x23, 0x97, 0x99, 0xec, 0x1e, 0x29, 0x3f}; unsigned char text0_2[] ={0x3d, 0x61, 0x7c, 0x06, 0xe5, 0x14, 0x71, 0xb1, 0xe7, 0x57, 0x58, 0x39, 0xa2, 0x43, 0x39, 0x1f}; unsigned char sig0_2[] = {0x00, 0x00, 0x00, 0x10, 0xa0, 0xcd, 0x0c, 0xa0, 0x75, 0xec, 0x00, 0x6e, 0xb3, 0xe7, 0xb1, 0x5a, 0xd7, 0x2e, 0xb3, 0xa8, 0x5a, 0xd7, 0x77, 0xb5, 0x49, 0x7e, 0x1f, 0x81, 0xfe, 0x45, 0x73, 0x60, 0x5e, 0x22, 0xed, 0x3c, 0x55, 0x11, 0xd4, 0x0a, 0x59, 0x81, 0xfc, 0x48, 0x4e, 0x89, 0xfd, 0x75, 0x1d, 0x36, 0xf0, 0x5b, 0x19, 0x60, 0xf9, 0x22, 0x84, 0xbc, 0x27, 0xb6, 0xa6, 0x03, 0x87, 0xde, 0x23, 0x41, 0x1c, 0x9a, 0x00, 0x00, 0x00, 0x10, 0x61, 0xd1, 0x59, 0xd5, 0xfd, 0x33, 0xa0, 0xc8, 0x8f, 0x3a, 0xb2, 0x38, 0x44, 0xeb, 0xdc, 0xe7, 0x52, 0x89, 0x75, 0xed, 0x5d, 0x11, 0x52, 0x83, 0xda, 0x5c, 0x62, 0xf3, 0x21, 0x34, 0xd6, 0xf8, 0xf9, 0x92, 0x9c, 0xb6, 0x0a, 0x01, 0x91, 0xa9, 0x12, 0x6b, 0xa6, 0x0d, 0x45, 0x31, 0x16, 0x2c, 0x0a, 0x82, 0x67, 0x33, 0xa5, 0xc9, 0x27, 0x7c, 0x9d, 0x11, 0x40, 0xcb, 0xf7, 0x3b, 0xca, 0xf3}; unsigned char text1[] = {0x52, 0x51, 0x27, 0x0e, 0x99, 0xb6, 0xe0, 0x95, 0x2a, 0x5e, 0x30, 0x17, 0x79, 0x75, 0xae, 0xcd}; unsigned char sig1[] = {0x00, 0x00, 0x00, 0x18, 0x9d, 0xad, 0xb8, 0xc3, 0x0c, 0xb9, 0x21, 0x5f, 0xe1, 0x19, 0x9d, 0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, 0xa7}; printf("key 0 check 0 (rom.bin.g2): %d\n", check_signature(0, text0_0, sizeof(text0_0), sig0_0, sizeof(sig0_0))); printf("key 0 check 1 (ptv.bin.g2): %d\n", check_signature(0, text0_1, sizeof(text0_1), sig0_1, sizeof(sig0_1))); printf("key 0 check 2 (auth registry): %d\n", check_signature(0, text0_2, sizeof(text0_2), sig0_2, sizeof(sig0_2))); printf("key 1 check (ptv.bin.g -- fails): %d\n", check_signature(1, text1, sizeof(text1), sig1, sizeof(sig1))); } #endif