int git_hash_init(git_hash_ctx *ctx) { assert(ctx); mbedtls_sha1_init(&ctx->c); mbedtls_sha1_starts(&ctx->c); return 0; }
static void *sha1_ctx_alloc( void ) { void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) ); if( ctx != NULL ) mbedtls_sha1_init( (mbedtls_sha1_context *) ctx ); return( ctx ); }
void winpr_SHA1_Init(WINPR_SHA1_CTX* ctx) { #if defined(WITH_OPENSSL) SHA1_Init((SHA_CTX*) ctx); #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C) mbedtls_sha1_init((mbedtls_sha1_context*) ctx); mbedtls_sha1_starts((mbedtls_sha1_context*) ctx); #endif }
static void call_action(void *arg) { schedule_msg_t* schedule_msg = (schedule_msg_t*)arg; if (schedule_msg != NULL) { if (schedule_msg->type == ON_CONNECT) { char productKey[64] = {0}; char deviceName[64] = {0}; char deviceSecret[64] = {0}; sscanf((char*)schedule_msg->msg, "%[^|]|%[^|]|%s", productKey, deviceName, deviceSecret); if (strlen(productKey) == 0 || strlen(deviceName) == 0 || strlen(deviceSecret) == 0) { be_error(JS_ZIGBEE_TAG, "productKey or deviceName or deviceSecret not exist"); return; } unsigned char content[256] = {0}; sprintf((char*)content, "clientId%sdeviceName%sproductKey%s", deviceName, deviceName, productKey); mbedtls_sha1_context sha1_ctx; unsigned char k_ipad[64] = {0}; unsigned char k_opad[64] = {0}; unsigned char out[20]; memcpy(k_ipad, deviceSecret, strlen(deviceSecret)); memcpy(k_opad, deviceSecret, strlen(deviceSecret)); for (int i = 0; i < sizeof(k_ipad); i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } mbedtls_sha1_init(&sha1_ctx); mbedtls_sha1_starts(&sha1_ctx); mbedtls_sha1_update(&sha1_ctx, k_ipad, sizeof(k_ipad)); mbedtls_sha1_update(&sha1_ctx, content, strlen((char*)content)); mbedtls_sha1_finish(&sha1_ctx, out); mbedtls_sha1_init(&sha1_ctx); mbedtls_sha1_starts(&sha1_ctx); mbedtls_sha1_update(&sha1_ctx, k_opad, sizeof(k_opad)); mbedtls_sha1_update(&sha1_ctx, out, sizeof(out)); mbedtls_sha1_finish(&sha1_ctx, out); mbedtls_sha1_free(&sha1_ctx); char sign[41] = {0}; for (int i=0; i<sizeof(out); ++i) { unsigned char byte[2] = {0}; byte[0] = out[i] / 16; byte[1] = out[i] % 16; for(int j=0; j<2; ++j) { if(byte[j] >= 0 && byte[j] <= 9) sign[2 * i + j] = '0' + byte[j]; else sign[2 * i + j] = 'a' + byte[j] - 10; } } char json_str[256] = {0}; sprintf(json_str, "{\"productKey\": \"%s\", \"deviceName\": \"%s\", \"deviceSecret\": \"%s\", \"sign\": \"%s\"}", productKey, deviceName, deviceSecret, sign); be_jse_symbol_t* argv[2]; char addr_str[12] = {0}; sprintf(addr_str, "%d", schedule_msg->addr); argv[0] = new_str_symbol(addr_str); argv[1] = new_json_symbol(json_str, strlen(json_str)); //symbol_relock(argv[0]); //symbol_relock(argv[1]); be_jse_execute_func(bone_engine_get_executor(), on_connect_func, 2, argv); //symbol_unlock(argv[0]); //symbol_unlock(argv[1]); } else if (schedule_msg->type == ON_MESSAGE) { be_jse_symbol_t* argv[2]; char addr_str[12] = {0}; sprintf(addr_str, "%d", schedule_msg->addr); argv[0] = new_str_symbol(addr_str); argv[1] = new_json_symbol((char*)schedule_msg->msg, schedule_msg->msg_size); //symbol_relock(argv[0]); //symbol_relock(argv[1]); be_jse_execute_func(bone_engine_get_executor(), on_message_func, 2, argv); //symbol_unlock(argv[0]); //symbol_unlock(argv[1]); } else if (schedule_msg->type == BZB_NOTIFY) { bone_zigbee_notify(&zigbee); } else if (schedule_msg->type == BZB_SEARCH) { bone_zigbee_search(&zigbee); } free(schedule_msg); schedule_msg = NULL; } }
bool checkTTP(char region, bool isNew, char* path) { // Verifies the integrity of a TTP file and if it corresponds to the console. (needs sha1.c) mbedtls_sha1_context context; // Just figured out there's a cleaner syntax to perform sdmc reads. Note to future. Handle file; FS_Archive archive = {ARCHIVE_SDMC, {PATH_EMPTY, 0, 0}}; FSUSER_OpenArchive(&archive); Result res = FSUSER_OpenFile(&file, archive, fsMakePath(PATH_ASCII, path), FS_OPEN_READ, 0); FSUSER_CloseArchive(&archive); u32 bytesRead = 0; char* buf = malloc(0x3); FSFILE_Read(file, &bytesRead, 0x0, buf, 0x3); if ((u8)buf[0] != 116 || (u8)buf[1] != 116 || (u8)buf[2] != 112) { free(buf); return false; } // if (buf != "ttp") free(buf); // not sure if freeing all the time is needed, maybe allocating 0x14 since the beginning should be enough buf = malloc(0x1); FSFILE_Read(file, &bytesRead, 0x3, buf, 0x1); if (*buf != region && buf != 0xFF) { free(buf); return false; } //free(buf); //buf = malloc(0x1); FSFILE_Read(file, &bytesRead, 0x4, buf, 0x1); if (*buf != (char)isNew && buf != 0xFF) { free(buf); return false; } free(buf); // SHA-1 check buf = malloc(0x14); FSFILE_Read(file, &bytesRead, 0x5, buf, 0x14); mbedtls_sha1_init(&context); mbedtls_sha1_starts(&context); char t[4]; FSFILE_Read(file, &bytesRead, 0x19, t, 4); // t is an array, so it's a pointer here u32 ulSize = ( ((u32)t[0]) << (8*3) ) | // t[0] stores MSB of UInt32 ( ((u32)t[1]) << (8*2) ) | // .. ( ((u32)t[2]) << (8*1) ) | // .. ( ((u32)t[3]) << (8*0) ) ; // t[3] stores LSB of UInt32 u32 blockAmount = ulSize / 0x160000; u32 i; char* block = malloc(0x160000); for (i = 0; i < blockAmount; i++) { FSFILE_Read(file, &bytesRead, 0x1D+0x160000*i, block, 0x160000); mbedtls_sha1_update(&context, block, 0x160000); } if (ulSize % 0x160000 != 0) { FSFILE_Read(file, &bytesRead, 0x1D+0x160000*blockAmount, block, ulSize-0x160000*blockAmount); mbedtls_sha1_update(&context, block, bytesRead); } free(block); FSFILE_Close(file); //FSUSER_CloseArchive(&archive); char hash[20]; mbedtls_sha1_finish(&context, hash); //shaBytes.c = buf; for (i = 0; i < 20; i++) { if (hash[i] != buf[i]) { free(buf); return false; } } free(buf); return true; }