⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drmadphelper.c

📁 ZORAN 962/966 SOURCE CODE,DVD chip
💻 C
字号:
/*
// $Header: /I76/I76_Common/I76_Reference/Playcore/Nav_Clips/AviDrm/libDrmDecrypt/DrmAdpHelper.c 3     3/01/04 6:28p 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.
*/

#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmAdpHelper.h"

#ifdef AVI_DRM_SUPPORT

#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmAdpApi.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmTypes.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmSizes.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmErrors.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmLocal.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\Bits.h"

#include <string.h>
/* static */ UINT8 far randomByteBucket[RANDOM_BYTE_BUCKET_SIZE];
static UINT16 far randomSamples = 0;


static CONST uint8_t userKeyMask[KEY_SIZE_BYTES] =
{ 118, 151, 74, 140, 206, 221, 122, 171,
  122, 86, 188, 103, 0, 52, 55, 76};

void drmHelperSetRandomSample(void)
{
	randomSamples++;
	if (randomSamples < RANDOM_BYTE_BUCKET_SIZE)
	{

		randomByteBucket[randomSamples] = localGetRapidUpdater();;
	}
	else
	{
		randomSamples--;
	}
}

uint8_t getRandomByte(uint8_t *randomByte)
{
	uint8_t result = DRM_ERROR_NONE;

	if (randomSamples > 0)
	{
		*randomByte = randomByteBucket[randomSamples];
		randomSamples--;
	}
	else
	{
		result = DRM_ERROR_NEED_MORE_RANDOM_SAMPLE;
	}

	return result;
}

uint32_t drmHelperGetRandomSampleCounter()
{
	return randomSamples;
}

/* This function is a bit long. Careful, it has multiple return statements.*/
uint8_t getSessionKey(DrmAdpTargetHeader fileInfo, uint8_t *sessionKey)
{
    uint8_t returnCode = DRM_ERROR_NONE;
    uint8_t i;
	uint8_t j;
    uint8_t useLimit = 255;  /* Ensure use limit initialized to max for correct security logic. */
	DrmMemory memory;
	uint8_t clearKey[KEY_SIZE_BYTES] = {0};
	uint16_t fileSerialNumber = 0;
	uint16_t memorySerialNumber = 1;
	uint16_t   uiIndex = 0,uiIndexMax;

	if (DRM_TYPE_BASE == fileInfo.drmMode)
	{
		memcpy(sessionKey, fileInfo.sessionKey, KEY_SIZE_BYTES);
		return DRM_ERROR_NONE;
	}

	returnCode = loadDrmMemory(&memory);
    if (returnCode != DRM_ERROR_NONE)
    {
        return returnCode;
    }  

	/* Check if we need to activate. */
    if (DRM_TYPE_ACTIVATION_PURCHASE == fileInfo.drmMode ||
		DRM_TYPE_ACTIVATION_RENTAL == fileInfo.drmMode)
	{
		/* Timer delay to prevent programatic retry of activation. */
		localWaitSecond();

		/* Check guard. */
		if (0 == memcmp(fileInfo.activateRecord.memoryGuard, memory.owner.guard, OWNER_GUARD_BYTES))
		{

			/* Check to see if we are being tested for initial guard of zero.  If so return. */
			if (0 == memcmp(nullGuard, memory.owner.guard, OWNER_GUARD_BYTES))
			{
				return DRM_ERROR_NULL_GUARD_SET_SIGNAL;
			}

            srand(uiIndex);
            
            drmSetRandomSample();

            uiIndexMax = rand() % 10000;
            tr_printf(("first loop %d\n",uiIndexMax ));
            for (uiIndex=0; uiIndex< uiIndexMax ; uiIndex++)
               ;//random delay
            drmSetRandomSample();
            uiIndexMax = rand() % 10000;
            for (uiIndex=0; uiIndex< uiIndexMax ; uiIndex++)
               ;//random delay
            drmSetRandomSample();
   
			/* Reset guard for next activation. */
			returnCode = randomizeGuard(&memory);
			if (returnCode == DRM_ERROR_NEED_MORE_RANDOM_SAMPLE)
			{
				return returnCode;
			}
			
			/* Handle user key.  Xor record key before setting. */
			for (i = 0; i < KEY_SIZE_BYTES; i++)
			{
				clearKey[i] = fileInfo.activateRecord.userKey[i] ^ userKeyMask[i];
			}
			memcpy(memory.owner.key, clearKey, KEY_SIZE_BYTES);
			memset(&clearKey, 0, KEY_SIZE_BYTES);

			/* User id. */
			memcpy(memory.owner.userId, fileInfo.userId, OWNER_USER_ID_BYTES);
			
			/* Reset slots. */
			for (i = 0; i < TOTAL_PLAY_SLOTS; i++)
			{
				memset(&(memory.slots[i].counter), 0, SLOT_USE_DATA_BYTES);
				memset(&(memory.slots[i].serialNumber), 0, SLOT_SERIAL_NUMBER_BYTES);
			}
			returnCode = saveDrmMemory(&memory);
			if (returnCode != DRM_ERROR_NONE)
			{
				return returnCode;
			}
		}
	}

    /* Purchase logic. */
    if (DRM_TYPE_PURCHASE == fileInfo.drmMode ||
		DRM_TYPE_ACTIVATION_PURCHASE == fileInfo.drmMode)
    {        
		if (0 == memcmp(fileInfo.userId, memory.owner.userId, OWNER_USER_ID_BYTES))
		{
			for (i = 0; i < KEY_SIZE_BYTES; i++)
			{
				sessionKey[i] = fileInfo.sessionKey[i] ^ memory.owner.key[i];
			} 
		}
		else
		{
			return DRM_ERROR_NOT_AUTH_USER;
		}
    }

	/* Rental logic. */
	if (DRM_TYPE_RENTAL == fileInfo.drmMode ||
		DRM_TYPE_ACTIVATION_RENTAL == fileInfo.drmMode)
	{
		/* Ensure correct user. */
		if (0 == memcmp(fileInfo.userId, memory.owner.userId, OWNER_USER_ID_BYTES))
		{
			/* Ensure slot number is in range. */
			if (fileInfo.rentalRecord.slotNumber >= TOTAL_PLAY_SLOTS)
			{
				return DRM_ERROR_BAD_SLOT_NUMBER;
			}

			/* Put serial number is form we can compare to. */
			memorySerialNumber = pack(memory.slots[fileInfo.rentalRecord.slotNumber].serialNumber[0],
			                    memory.slots[fileInfo.rentalRecord.slotNumber].serialNumber[1]);
            fileSerialNumber = pack(fileInfo.rentalRecord.serialNumber[0],
									fileInfo.rentalRecord.serialNumber[1]);

			/* Check first serial number. */
			if (fileSerialNumber > memorySerialNumber)
			{
				/* Update serial number. */
				memcpy(memory.slots[fileInfo.rentalRecord.slotNumber].serialNumber,
					   fileInfo.rentalRecord.serialNumber, SLOT_SERIAL_NUMBER_BYTES);
				memorySerialNumber = fileSerialNumber;

				/* Reset counter. */
				memory.slots[fileInfo.rentalRecord.slotNumber].counter = 0;
			}

			/* Check subsequent serial number. */
			if (fileSerialNumber == memorySerialNumber)
			{
				/* Find use limit id. */
				for (j = 0; j < USE_LIMITS; j++)
				{
					if (useLimitIdInfo[j].id == fileInfo.rentalRecord.useLimitId)
					{
						useLimit = useLimitIdInfo[j].uses;
						break;
					}
				}

				/* Check use limit id */
				if ( (useLimit > memory.slots[fileInfo.rentalRecord.slotNumber].counter) ||
					 (USE_LIMIT_ID_UNLIMITED == fileInfo.rentalRecord.useLimitId) )
				{
					/* Update counter, yes still does if unlimited.  This is ok. */
					memory.slots[fileInfo.rentalRecord.slotNumber].counter++;

					/* And finally copy over the key. */
					for (i = 0; i < KEY_SIZE_BYTES; i++)
					{
						sessionKey[i] = fileInfo.sessionKey[i] ^ memory.owner.key[i];
					} 
				}
				else
				{
					return DRM_ERROR_RENTAL_EXPIRED;
				}
			}
			else
			{
				return DRM_ERROR_RENTAL_EXPIRED;
			}
		}
		else
		{
			return DRM_ERROR_NOT_AUTH_USER;
		}
	}

    
    if (returnCode == DRM_ERROR_NONE)
    {
        returnCode = saveDrmMemory(&memory);
    }
    
    return returnCode;
}

#endif // AVI_DRM_SUPPORT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -