📄 psfdemux_drm.c
字号:
cipher.block_size = (context->key_size)<< 3; /* 16 * 8 = 128bits */ cipher.encrypted_packet_format = EMhwlibAES_UDAC; /* ignored for precipher */ err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AESCipherEntry, &cipher, sizeof(cipher), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTable:Error RMDemuxTaskPropertyID_AESCipherEntry\n")); return RM_ERROR; } /************ Allocate AES 1st key Entry *************/ cipher_type = EMhwlibCipher_AES; err = RUAExchangeProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AllocateKeyEntry, &cipher_type, sizeof(cipher_type), &(key_1st), sizeof(key_1st)); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTable: Error 1st RMDemuxTaskPropertyID_AllocateKeyEntry\n")); return RM_ERROR; } context->key_index[context->key_count] = key_1st; context->key_count++; /************ Allocate AES 2nd key Entry *************/ err = RUAExchangeProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AllocateKeyEntry, &cipher_type, sizeof(cipher_type), &(key_2nd), sizeof(key_2nd)); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTable :Error 2nd RMDemuxTaskPropertyID_AllocateKeyEntry\n")); return RM_ERROR; } context->key_index[context->key_count] = key_2nd; context->key_count++; } /*############## AES_CBC_PRECIPHER CODE END ###############*/#if (ALLOW_LIBRMARIB) if ((context->app_type == dvbcsa_decryption) || (context->app_type == multi2_decryption)) { RMascii * error; RMstatus (*init_xtask)(void); /* * Open dynamic library librmarib.so */ context->drm_handle = dlopen("librmarib.so", RTLD_LAZY); if (context->drm_handle == NULL) { RMDBGLOG((ENABLE, "Error loading librmarib.so : %s\n", dlerror())); return(RM_ERROR); } /* * Load and start xtask */ init_xtask = dlsym(context->drm_handle, "rmdrm_init_xtask"); if ((error = dlerror()) != NULL || init_xtask == NULL) { RMDBGLOG((ENABLE, "Error getting librmarib.so interface : %s\n", error)); dlclose(context->drm_handle); context->drm_handle = NULL; return(RM_ERROR); } return((*init_xtask)()); }#endif // #if (EM86XX_CHIP == EM86XX_CHIPID_TANGO2) return RM_OK;}RMstatus FreeKeyAndCipherTable(struct context_per_task *context){ RMstatus err; RMuint32 i; for (i = 0; i < context->cipher_count; i++) { err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_FreeCipherEntry, &context->cipher_index[i], sizeof(RMuint32), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "FreeKeyAndCipherTable Error RMDemuxTaskPropertyID_FreeCipherEntry")); return err; } RMDBGLOG((ENABLE, "FreeKeyAndCipherTable RMDemuxTaskPropertyID_FreeCipherEntry %ld\n", i)); } context->cipher_count = 0; for (i = 0; i < context->key_count; i++) { struct DemuxTask_FreeKeyEntry_type free_key; if (context->app_type == dvbcsa_decryption) { free_key.cipher_type = EMhwlibCipher_DVBCSA; } else if (context->app_type == multi2_decryption) { free_key.cipher_type = EMhwlibCipher_Multi2; } else if ((context->app_type == aes_cbc_decryption) || (context->app_type == aes_ecb_decryption) || (context->app_type == aes_nsa_decryption) || (context->app_type == aes_ofb_decryption) ) { free_key.cipher_type = EMhwlibCipher_AES; } /* ############## AES_CBC_PRECIPHER CODE END ############### */ else if ((context->test_aes_precipher == TRUE) || (context->dtcpip_streaming == TRUE)) { free_key.cipher_type = EMhwlibCipher_AES; } /* ############## AES_CBC_PRECIPHER CODE END ############### */#if (ALLOW_LIBRMARIB) /* Free dynamic library librmarib.so */ if (context->drm_handle != NULL) { /* Stop and unload xtask */ RMstatus (*term_xtask)(void) = dlsym(context->drm_handle, "rmdrm_term_xtask"); if (term_xtask != NULL) (*term_xtask)(); dlclose(context->drm_handle); context->drm_handle = NULL; }#endif // #if (ALLOW_LIBRMARIB) free_key.key_index = context->key_index[i]; err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_FreeKeyEntry, &free_key, sizeof(free_key), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "FreeKeyAndCipherTable Error RMDemuxTaskPropertyID_FreeKeyEntry")); return err; } RMDBGLOG((ENABLE, "FreeKeyAndCipherTable RMDemuxTaskPropertyID_FreeKeyEntry %ld\n", i)); } context->key_count = 0; return RM_OK;}RMstatus InitKeyAndCipherTableForRecipher(struct context_per_task *context){ RMstatus err = RM_OK; /* recipher only support AES CBC OFB */ /* allocate cipher */ { RMuint32 cipher; err = RUAGetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AllocateCipherEntry, &cipher, sizeof(cipher)); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_AllocateCipherEntry")); return err; } context->recipher_cipher_entry = cipher; RMDBGLOG((KEYDBG, "InitKeyAndCipherTableForRecipher RMDemuxTaskPropertyID_AllocateCipherEntry %ld\n", cipher)); } /* setup cipher config */ { struct DemuxTask_AESCipherEntry_type cipher; cipher.index = context->recipher_cipher_entry; cipher.mode = EMhwlibAES_CBC_encryption; cipher.key_size = 128; /* hardcode to 128bits */ cipher.block_size = 128; /* hardcode to 128bits */ cipher.encrypted_packet_format = EMhwlibAES_OFB; /* encryption starting from end of TS payload */ RMDBGLOG((KEYDBG, "InitKeyAndCipherTableForRecipher mode=0x%lx key_size=%ld block_size=%ld format=0x%lx\n", cipher.mode, cipher.key_size, cipher.block_size, cipher.encrypted_packet_format)); err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AESCipherEntry, &cipher, sizeof(cipher), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_AESCipherEntry\n")); return RM_ERROR; } } /* since we are using another cipher, different key ram, a different key allocator is needed */ /* hardcode for now. only good for one task */ context->recipher_even_key_index = 0; context->recipher_odd_key_index = 1; /* allocate recipher even key */ { RMuint32 key; enum EMhwlibCipher cipher_type = EMhwlibCipher_AES; err = RUAExchangeProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AllocateKeyEntry, &cipher_type, sizeof(cipher_type), &(key), sizeof(key)); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_AllocateKeyEntry\n")); return RM_ERROR; } context->recipher_even_key_index = key; RMDBGLOG((KEYDBG, "InitKeyAndCipherTableForRecipher Allocated key %ld, for cipher %ld\n", context->recipher_even_key_index, context->recipher_cipher_entry )); } /* allocate recipher odd key */ { RMuint32 key; enum EMhwlibCipher cipher_type = EMhwlibCipher_AES; err = RUAExchangeProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_AllocateKeyEntry, &cipher_type, sizeof(cipher_type), &(key), sizeof(key)); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "InitKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_AllocateKeyEntry\n")); return RM_ERROR; } context->recipher_odd_key_index = key; RMDBGLOG((KEYDBG, "InitKeyAndCipherTableForRecipher Allocated key %ld, for cipher %ld\n", context->recipher_odd_key_index, context->recipher_cipher_entry )); } return RM_OK;}RMstatus FreeKeyAndCipherTableForRecipher(struct context_per_task *context){ RMstatus err = RM_OK; { struct DemuxTask_FreeKeyEntry_type free_key; free_key.cipher_type = EMhwlibCipher_AES; free_key.key_index = context->recipher_even_key_index; err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_FreeKeyEntry, &free_key, sizeof(free_key), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "FreeKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_FreeKeyEntry")); return err; } RMDBGLOG((KEYDBG, "FreeKeyAndCipherTableForRecipher RMDemuxTaskPropertyID_FreeKeyEntry %ld\n", free_key.key_index)); } { struct DemuxTask_FreeKeyEntry_type free_key; free_key.cipher_type = EMhwlibCipher_AES; free_key.key_index = context->recipher_odd_key_index; err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_FreeKeyEntry, &free_key, sizeof(free_key), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "FreeKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_FreeKeyEntry")); return err; } RMDBGLOG((KEYDBG, "FreeKeyAndCipherTableForRecipher RMDemuxTaskPropertyID_FreeKeyEntry %ld\n", free_key.key_index)); } { RMuint32 cipher = context->recipher_cipher_entry; err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_FreeCipherEntry, &cipher, sizeof(cipher), 0); if (RMFAILED(err)) { RMDBGLOG((ENABLE, "FreeKeyAndCipherTableForRecipher Error RMDemuxTaskPropertyID_FreeCipherEntry")); return err; } RMDBGLOG((KEYDBG, "FreeKeyAndCipherTableForRecipher RMDemuxTaskPropertyID_FreeCipherEntry %ld\n", cipher)); } return RM_OK;}#ifdef USE_XPU_WRITE_KEY#define DEMUX_WRITE_KEY_XRPC_SIZE (64 * 1024)RMstatus SetAesCbcOfbKeyThroughXPU(struct context_per_task *context, RMuint32 key_index, RMuint32 key_size ){ RMuint32 xrpc_base_addr = 0; RMuint32 xrpc_size = DEMUX_WRITE_KEY_XRPC_SIZE; struct RUA *pRUA = context->pRUA; RMstatus status = RM_OK; struct DemuxTask_OutbandKeyChange_type outband; RMDBGLOG(( ENABLE, " \n\n\nSetAesCbcOfbKeyThroughXPU\n\n\n" )); /* Allocate an RUA buffer for XRPC commuication */ xrpc_base_addr = RUAMalloc(pRUA, 0, RUA_DRAM_UNPROTECTED, xrpc_size); if (xrpc_base_addr == 0) { RMDBGLOG(( ENABLE, "RUAMalloc failed\n" )); status = RM_ERROR; goto error_exit; } status = initDemuxWriteKey(xrpc_base_addr,xrpc_size,1); if ( status != RM_OK ) { RMDBGLOG(( ENABLE, "initDemuxWriteKey error \n" )); goto error_exit; } status = setInternalDemuxAesKey( context->recipher_xpu_key, /* key index in xpu's storage */ context->key_index[0] ); /* key index in AES cipher's key ram */ fprintf(stderr, "SetInternalDemuxAesKey xpu_index %ld to demux_key %ld %s\n", context->recipher_xpu_key, key_index, status == RM_OK ? "OK" : "FAIL"); if ( status != RM_OK ) { status = RM_ERROR; RMDBGLOG(( ENABLE, "setInternalDemuxAesKey error \n" )); /* fall through try to terminate the module */ } status = termDemuxWriteKey(); if ( status != RM_OK ) { RMDBGLOG(( ENABLE, "termDemuxWriteKey error \n" )); goto error_exit; } { /* xpu has already written the key, inform the demux key change */ RMstatus err = RM_OK; outband.key_index = context->key_index[0]; /* hardcode key index */ outband.cipher_index = context->cipher_index[0]; /* hardcode cipher index */ outband.cipher_type = EMhwlibCipher_AES; outband.scrambling = EMhwlibScramblingBits_10; fprintf( stderr, "SetAesCbcOfbKeyThroughXPU key_index %ld cipher_index %ld cipher type %d scrambling %d\n\n", outband.key_index, outband.cipher_index, outband.cipher_type, outband.scrambling ); err = RUASetProperty(context->pRUA, context->demux_task, RMDemuxTaskPropertyID_OutbandKeyChange, &outband, sizeof(outband), 0); if( RMFAILED( err )){ RMDBGLOG(( ENABLE, "Error set property RMDemuxTaskPropertyID_OutbandKeyChang\n" )); goto error_exit; } } error_exit: if( xrpc_base_addr ) RUAFree( pRUA, xrpc_base_addr ); return status;}RMstatus SetRecipherKeyThroughXPU(struct context_per_task *context, union Demux_Cipher_Key *pKey, enum EMhwlibCipher cipher_type, RMuint32 key_index, RMuint32 key_size ){ RMuint32 xrpc_base_addr = 0; RMuint32 xrpc_size = DEMUX_WRITE_KEY_XRPC_SIZE; struct RUA *pRUA = context->pRUA; RMstatus status = RM_OK; enum DEMUX_WRITE_XOS_Key_Type type = 0; RMbool is_multi2 = FALSE; RMuint32 key_size_in_bits = key_size * 8; switch( cipher_type ){ case EMhwlibCipher_DES: /* 0 */ type = DEMUX_WRITE_DES_64BITS; /*1*/ break; case EMhwlibCipher_AES: /* 1 */ if( key_size_in_bits == 256 ) type = DEMUX_WRITE_AES_256BITS_KEY_IV; else if( key_size_in_bits == 192 ) //type = DEMUX_WRITE_AES_128BITS_KEY_IV; RMDBGLOG(( ENABLE, "Unimplemented aes key size\n" )); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -