?? drmmessage.c
字號(hào):
/*
// $Header: /I76/I76_Common/I76_Reference/Playcore/Nav_Clips/AviDrm/libDrmDecrypt/DrmMessage.c 3 2/15/04 7:42p Lotan $
// Copyright (c) DivXNetworks, Inc. http://www.divxnetworks.com
// All rights reserved.
//
// This software is the confidential and proprietary information of DivxNetworks
// Inc. ("Confidential Information"). You shall not disclose such Confidential
// Information and shall use it only in accordance with the terms of the license
// agreement you entered into with DivXNetworks, Inc.
*/
/*
Notes: All the pack functions are very similar in logic. If you find a defect
in one, it is probably in all of them.
*/
#include "Config.h" // Global Configuration - do not remove!
#include "Services\Memory\malloc.h" // when using far malloc we should add this include
#include "Services\Include\_heap.h"
#ifdef AVI_DRM_SUPPORT
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmMessage.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\Bits.h" /* assign() */
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmLocal.h"
#ifdef USE_ONE_MASTER_KEY
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\master_key_one.h"
#else
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\master_key_all.h"
#endif
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\base64.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\sha1.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmErrors.h"
#include <limits.h> /* CHAR_BIT */
#define PROTECTION_FORMAT_VALID 0
#define PROTECTION_FORMAT_INVALID 1
#define SPREAD_RATIO_SWITCH 3
#define SHA1_HASH_SIZE 20
#define KEY_BAD 4
#define KEY_OK 5
#define USE_ALTERNATE_KEY "0"
#define VALID_KEY_TOTAL 14
#define KEY_INDEX_TOTAL 16
typedef struct MessageKeysStruct
{
uint8_t status;
char keyString[DRM_PORTABLE_KEY_LENGTH + 1];
uint8_t alternateKeyIndex;
} MessageKeys;
#ifdef USE_ONE_MASTER_KEY
CONST MessageKeys keyMap[KEY_INDEX_TOTAL] =
{
PROTECTION_FORMAT_INVALID, "", 0
,PROTECTION_FORMAT_INVALID, "", 0
,PROTECTION_FORMAT_VALID, "77658077013762464610390453611281332768339559", 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
,PROTECTION_FORMAT_VALID, USE_ALTERNATE_KEY, 2
};
#else
CONST MessageKeys keyMap[KEY_INDEX_TOTAL] =
{
{PROTECTION_FORMAT_INVALID, "", 0}
,{PROTECTION_FORMAT_INVALID, "", 0}
,{PROTECTION_FORMAT_VALID, "68350122382077155371609790526077843323069757", 2}
,{PROTECTION_FORMAT_VALID, "17526956348125519278333379155775791404842465", 3}
,{PROTECTION_FORMAT_VALID, "07307313514643969633523034904779465997361500", 4}
,{PROTECTION_FORMAT_VALID, "54733977128285238605431520951629624437529331", 5}
,{PROTECTION_FORMAT_VALID, "95271872508096602223778323164547956766606317", 6}
,{PROTECTION_FORMAT_VALID, "19667620404347912572341155491659903450839945", 7}
,{PROTECTION_FORMAT_VALID, "58243572270469309545831683269013152299633661", 8}
,{PROTECTION_FORMAT_VALID, "48838714380920672270656324638255297385378768", 9}
,{PROTECTION_FORMAT_VALID, "06784853705815447409483489174064856573505938", 10}
,{PROTECTION_FORMAT_VALID, "00770111886686676706976265862940452724180617", 11}
,{PROTECTION_FORMAT_VALID, "53815691764967922077658113912876220915162495", 12}
,{PROTECTION_FORMAT_VALID, "04312177729617650695571887594358724097271608", 13}
,{PROTECTION_FORMAT_VALID, "63945301141543851075872730428987261348525394", 14}
,{PROTECTION_FORMAT_VALID, "66414933091815151592855735169031788977421980", 15}
};
#endif
void wrapSecureMessage(DrmMessagePacked *transformMessage)
{
uint8_t current;
uint8_t carryNext;
uint8_t carryLast;
uint8_t maskHigh = 0x0F;
uint8_t maskLow = 0xF0;
uint8_t i;
uint8_t loopSize;
uint8_t protectionFormatIndex;
uint8_t masterKeyHash[SHA1_HASH_SIZE] = {0};
uint8_t cryptoCode;
transformMessage->sizeInBits += PROTECTION_FORMAT_BITS;
loopSize = getByteLoopSizeFromBits(transformMessage->sizeInBits);
/* Derive protection format id from message itself. Do not use invalid key. */
protectionFormatIndex = (transformMessage->message[0] % VALID_KEY_TOTAL) + (KEY_INDEX_TOTAL - VALID_KEY_TOTAL);
carryLast = protectionFormatIndex << 4; /* Shift over to make room for message. */
cryptoCode = setHash(protectionFormatIndex, masterKeyHash);
/* COPYPASTE_WARNING: This loop logic is very close to unwrapSecureMessage loop. */
for (i = 0; i < loopSize; i++)
{
current = transformMessage->message[i];
carryNext = current & maskHigh;
current = current & maskLow;
current = current >> 4;
/* Handle 4 bits of first byte. */
if (i == 0)
{
current = current ^ masterKeyHash[i];
current = current & maskHigh;
}
current = current | carryLast;
/* XOR in crypto */
if (i != 0)
{
current = current ^ masterKeyHash[i];
}
transformMessage->message[i] = current;
carryLast = carryNext << 4;
}
memset(masterKeyHash, 0xF1, SHA1_HASH_SIZE); /* Clear out hash. */
if (cryptoCode == KEY_BAD) /* Clear out message if key is bad. */
{
memset(transformMessage->message, 0, MAX_MESSAGE_SIZE_BYTES);
}
}
// <<< Robin_0915_2004
#ifndef AVI_DRM_OPTIMIZATION
void unwrapSecureMessage(DrmMessagePacked *transformMessage)
{
uint8_t current;
uint8_t carryNext;
uint8_t carryLast;
uint8_t maskHigh = 0x0F;
uint8_t maskLow = 0xF0;
char i;
uint8_t loopSize;
uint8_t protectionFormatIndex;
uint8_t masterKeyHash[SHA1_HASH_SIZE] = {0};
loopSize = getByteLoopSizeFromBits(transformMessage->sizeInBits);
protectionFormatIndex = transformMessage->message[0] >> 4;
carryLast = 0;
setHash(protectionFormatIndex, masterKeyHash);
/* COPYPASTE_WARNING: This loop logic is very close to wrapSecureMessage loop. */
for (i = loopSize - 1; i >= 0; i--)
{
current = transformMessage->message[i];
/* XOR in crypto */
if (i != 0)
{
current = current ^ masterKeyHash[i];
}
carryNext = current & maskLow;
current = current & maskHigh;
/* Handle 4 bits of first byte. */
if (i == 0)
{
current = current ^ masterKeyHash[i];
current = current & maskHigh;
}
current = current << 4;
current = current | carryLast;
transformMessage->message[i] = current;
carryLast = carryNext >> 4;
}
memset(masterKeyHash, 0xF1, SHA1_HASH_SIZE); /* Clear out hash. */
transformMessage->sizeInBits -= PROTECTION_FORMAT_BITS;
}
#endif
// >>> Robin_0915_2004
uint8_t setHash(uint8_t protectionFormatIndex, uint8_t *keyHash)
{
char keyString[DRM_PORTABLE_KEY_LENGTH + 1];
uint8_t portableMasterKey[DRM_PORTABLE_KEY_LENGTH] = {0};
uint8_t *base64;
int outLength;
uint8_t result;
char *hashResult;
uint8_t hardwareSecret[MASTER_KEY_SIZE_BYTES];
// uint8_t *masterKey;
char masterKey[33];
int masterKeySize;
result = KEY_BAD;
/* See if we need to use alternate key. */
if (strcmp(keyMap[protectionFormatIndex].keyString, USE_ALTERNATE_KEY) == 0)
{
strcpy(keyString, keyMap[(keyMap[protectionFormatIndex].alternateKeyIndex)].keyString);
}
else
{
strcpy(keyString, keyMap[protectionFormatIndex].keyString);
}
if (DRM_ERROR_NONE == localGetHardwareKey(hardwareSecret))
{
if (get_local_master_key_from_id((unsigned char *)keyString, (unsigned char *)portableMasterKey) != 1)
{
return KEY_BAD;
}
}
else
{
if (get_master_key_from_id((unsigned char *)keyString, (unsigned char *)portableMasterKey) != 1)
{
return KEY_BAD;
}
}
{
{
base64Decode((char*)portableMasterKey, DRM_PORTABLE_KEY_LENGTH, (unsigned int *)&masterKeySize, masterKey);
if(masterKeySize != MASTER_KEY_SIZE_BYTES)
{
return KEY_BAD;
}
// Handle locally protected master keys.
result = localGetHardwareKey(hardwareSecret);
if (DRM_ERROR_NONE == result)
{
result = getLocalizedMasterKey((unsigned char *)masterKey);
if (DRM_ERROR_NONE != result)
{
return KEY_BAD;
}
base64 = (UINT8*)base64Encode(masterKey, MASTER_KEY_SIZE_BYTES, (unsigned int *)&outLength);
memcpy(portableMasterKey, base64, outLength);
MEM_Free(SC_POOL, base64);
}
}
result = KEY_OK;
hashResult = sha1GetHash((unsigned char *)portableMasterKey, DRM_PORTABLE_KEY_LENGTH, (unsigned int *)&outLength);
memcpy(keyHash, hashResult, SHA1_HASH_SIZE);
MEM_Free(SC_POOL, hashResult);
}
memset(portableMasterKey, 0, DRM_PORTABLE_KEY_LENGTH);
return result;
}
void packRegistrationRequest(RegistrationRequest request, DrmMessagePacked *messagePacked)
{
packRegistrationLogic(&request, messagePacked, (uint8_t) B_BECOMES_A);
registrationSecuritySwap(messagePacked);
}
void packRegistrationLogic(RegistrationRequest *request, DrmMessagePacked *messagePacked, uint8_t direction)
{
uint8_t packedIndex;
uint8_t guardIndex;
uint8_t modelIndex;
packedIndex = 0;
for (guardIndex = 0; guardIndex < OWNER_GUARD_BYTES; guardIndex++)
{
assign(&(request->userIdGuard[guardIndex]), &(messagePacked->message[packedIndex++]), direction);
}
for (modelIndex = 0; modelIndex < MODEL_ID_BYTES; modelIndex++)
{
assign(&(request->modelId[modelIndex]), &(messagePacked->message[packedIndex++]), direction);
}
messagePacked->sizeInBits = REGISTRATION_CODE_BITS;
}
void registrationSecuritySwap(DrmMessagePacked *messagePacked)
{
/* Mix in random number with static model number. */
halfByteSwap(&(messagePacked->message[0]), &(messagePacked->message[4]), 0);
halfByteSwap(&(messagePacked->message[2]), &(messagePacked->message[3]), 1);
}
#endif // AVI_DRM_SUPPORT
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -