📄 s3c2510des.c
字号:
/* s3c2510Des.c - SAMSUNG S3C2510 DES/3DES driver */
/* Copyright 2002 SAMSUNG ELECTRONICS */
/*
modification history
--------------------
01a,15apr02,jmLee created.
*/
#include "vxWorks.h"
#include "intLib.h"
#include "netLib.h"
#include "end.h"
#include "endLib.h"
#include "cacheLib.h"
#include "miiLib.h"
#include "errno.h"
#include "stdio.h"
#include "logLib.h"
#include "taskLib.h"
#include "drv/multi/s3c2510.h"
#include "config.h"
#include "drv/intrCtl/s3c2510Intr.h"
#include "s3c2510Des.h"
#undef DEBUG_TRACE
#define DEBUG_LOG(x, p1, p2, p3, p4, p5, p6) \
logMsg(x, (int)(UINT32)(p1), (int)(UINT32)(p2), (int)(UINT32)(p3), (int)(UINT32)(p4), (int)(UINT32)(p5), (int)(UINT32)(p6))
/* Forward Function Declarations */
#ifndef DES_GDMA
LOCAL void s3c2510DesInt(void);
#endif /* DES_GDMA */
/* Local Variables */
LOCAL SEM_ID semMutex = NULL;
#ifndef DES_GDMA
LOCAL UINT32 *input;
LOCAL UINT32 *output;
LOCAL int iCount;
LOCAL int oCount;
#endif /* DES_GDMA */
/*******************************************************************************
*
* s3c2510DesInit - initialize S3C2510 DES/3DES
*
* RETURNS: OK, or ERROR.
*/
STATUS s3c2510DesInit(void)
{
/* Disable interrupt. */
intDisable(INT_LVL_DES);
/* Create semaphore. */
if (!semMutex)
{
semMutex = semBCreate(SEM_Q_FIFO, SEM_FULL);
if (!semMutex)
{
printf("Failed to create binary SEMAPHORE\n");
return ERROR;
}
}
/* Enable peripheral clock. */
*S3C2510_PCLKDIS &= ~S3C2510_PCLKDIS_DES;
/* Reset FIFO. */
*S3C2510_DESCON = S3C2510_DESCON_FIFO_RESET;
/* Initialize DES Control Register. */
*S3C2510_DESCON =
#ifdef DES_4WORD
S3C2510_DESCON_4WORD_REQ | /* DES Generate Valid DESOUTFIFO Bit when it has 4 Word Valid Data */
#else /* DES_4WORD */
S3C2510_DESCON_2WORD_REQ | /* DES Generate Valid DESOUTFIFO Bit when it has 2 Word Valid Data */
#endif /* DES_4WORD */
#ifdef DES_CBC
S3C2510_DESCON_CBC | /* CBC */
#else /* DES_CBC */
S3C2510_DESCON_ECB | /* ECB */
#endif /* DES_CBC */
#ifdef DES_3DES
S3C2510_DESCON_3DES | /* 3DES */
#else /* DES_3DES */
S3C2510_DESCON_DES | /* DES */
#endif /* DES_3DES */
#ifdef DES_GDMA
S3C2510_DESCON_OUTDATA_DMA | /* GDMA Transfer Output Data */
S3C2510_DESCON_INDATA_DMA | /* GDMA Transfer Input Data */
#endif /* DES_GDMA */
0;
/* Clear DES Interrupt Enable Register. */
*S3C2510_DESINT = 0;
#ifdef DES_GDMA
/* Enable peripheral clock. */
*S3C2510_PCLKDIS &= ~(S3C2510_PCLKDIS_GDMA0 << DES_IGDMA);
/* Enable peripheral clock. */
*S3C2510_PCLKDIS &= ~(S3C2510_PCLKDIS_GDMA0 << DES_OGDMA);
#else /* DES_GDMA */
input = NULL;
output = NULL;
iCount = 0;
oCount = 0;
#ifndef DES_FALLING
/* Connect interrupt. */
intConnect(INT_VEC_DES, (VOIDFUNCPTR)s3c2510DesInt, 0);
#endif /* DES_FALLING */
#endif /* DES_GDMA */
return OK;
}
#ifndef DES_GDMA
/*******************************************************************************
*
* s3c2510DesInt - interrupt service routine
*
* RETURNS : N/A
*/
void s3c2510DesInt(void)
{
#ifdef DES_FALLING
while (oCount > 0)
{
#endif /* DES_FALLING */
UINT32 status = *S3C2510_DESSTA;
if (status & S3C2510_DESSTA_IFIFO_AVAIL)
{
if (input != NULL)
{
*S3C2510_DESINFIFO = *input++;
*S3C2510_DESINFIFO = *input++;
#ifdef DES_4WORD
*S3C2510_DESINFIFO = *input++;
*S3C2510_DESINFIFO = *input++;
#endif /* DES_4WORD */
if (--iCount <= 0)
{
/* Stop input. */
input = NULL;
}
}
}
if (status & (S3C2510_DESSTA_OFIFO_VALID | S3C2510_DESSTA_OFIFO_FULL))
{
if (output != NULL)
{
*output++ = *S3C2510_DESOUTFIFO;
*output++ = *S3C2510_DESOUTFIFO;
#ifdef DES_4WORD
*output++ = *S3C2510_DESOUTFIFO;
*output++ = *S3C2510_DESOUTFIFO;
#endif /* DES_4WORD */
if (--oCount <= 0)
{
/* Stop output. */
output = NULL;
/* Disable interrupt.*/
*S3C2510_DESINT = 0;
}
}
}
#ifdef DES_FALLING
}
#endif /* DES_FALLING */
}
#endif /* DES_GDMA */
/*******************************************************************************
*
* s3c2510DesRun - start DES encryption / decryption
*
* RETURNS : N/A
*/
STATUS s3c2510DesRun(
UCHAR *inBuffer, /* pointer to the input buffer */
UCHAR *outBuffer, /* pointer to the output buffer */
int length, /* length of data */
UINT32 key1L, /* key 1 left */
UINT32 key1R, /* key 1 right */
UINT32 key2L, /* key 2 left */
UINT32 key2R, /* key 2 right */
UINT32 key3L, /* key 3 left */
UINT32 key3R, /* key 3 right */
UINT32 ivL, /* iv left */
UINT32 ivR, /* iv right */
BOOL bEncrypt /* TRUE if encryption */
)
{
if (!semMutex)
{
return ERROR;
}
#ifdef DES_4WORD
if ((length % 32) != 0)
#else /* DES_4WORD */
if ((length % 8) != 0)
#endif /* DES_4WORD */
{
printf("Invalid length, %d\n", length);
return ERROR;
}
semTake(semMutex, WAIT_FOREVER);
*S3C2510_DESKEY1L = key1L;
*S3C2510_DESKEY1R = key1R;
*S3C2510_DESKEY2L = key2L;
*S3C2510_DESKEY2R = key2R;
*S3C2510_DESKEY3L = key3L;
*S3C2510_DESKEY3R = key3R;
*S3C2510_DESIVL = ivL;
*S3C2510_DESIVR = ivR;
/* Initialize DES Control Register. */
*S3C2510_DESCON &= ~S3C2510_DESCON_MODE_MASK;
if (bEncrypt)
{
*S3C2510_DESCON |= S3C2510_DESCON_ENCRYPTION | /* Encryption */
S3C2510_DESCON_RUN_ENABLE; /* DES/3DES Enable */
}
else
{
*S3C2510_DESCON |= S3C2510_DESCON_DECRYPTION | /* Decryption */
S3C2510_DESCON_RUN_ENABLE; /* DES/3DES Enable */
}
#ifdef DES_GDMA
/* Setup GDMA Register. */
*S3C2510_DCON(DES_IGDMA) = S3C2510_DCON_DD_FIX | /* Do not change Destination Address */
S3C2510_DCON_SD_INC | /* Increase Source Address */
S3C2510_DCON_TS_32 | /* Transfer Size is 32 Bits */
#ifdef DES_4WORD
S3C2510_DCON_FB | /* Enable 4 Data Burst Mode */
#endif /* DES_4WORD */
S3C2510_DCON_MODE_DESIN; /* Memory to DES */
*S3C2510_DSAR(DES_IGDMA) = (UINT32)inBuffer;
*S3C2510_DDAR(DES_IGDMA) = 0;
*S3C2510_DTCR(DES_IGDMA) = length;
*S3C2510_DRER(DES_IGDMA) = 1;
*S3C2510_DIPR(DES_IGDMA) = 1;
/* Setup GDMA Register. */
*S3C2510_DCON(DES_OGDMA) = S3C2510_DCON_DD_INC | /* Increase Destination Address */
S3C2510_DCON_SD_FIX | /* Do not change Source Address */
S3C2510_DCON_TS_32 | /* Transfer Size is 32 Bits */
#ifdef DES_4WORD
S3C2510_DCON_FB | /* Enable 4 Data Burst Mode */
#endif /* DES_4WORD */
S3C2510_DCON_MODE_DESOUT; /* DES to Memory */
*S3C2510_DSAR(DES_OGDMA) = 0;
*S3C2510_DDAR(DES_OGDMA) = (UINT32)outBuffer;
*S3C2510_DTCR(DES_OGDMA) = length;
*S3C2510_DRER(DES_OGDMA) = 1;
*S3C2510_DIPR(DES_OGDMA) = 1;
while ((*S3C2510_DTCR(DES_IGDMA) != 0) || (*S3C2510_DTCR(DES_OGDMA) != 0));
#else /* DES_GDMA */
input = (UINT32 *)inBuffer;
output = (UINT32 *)outBuffer;
#ifdef DES_4WORD
iCount = length / 32;
oCount = length / 32;
#else /* DES_4WORD */
iCount = length / 8;
oCount = length / 8;
#endif /* DES_4WORD */
#ifdef DES_FALLING
s3c2510DesInt();
#else /* DES_FALLING */
/* Initialize DES Interrupt Enable Register. */
*S3C2510_DESINT = S3C2510_DESINT_OFIFO_VALID | /* Output FIFO Valid Interrupt Enable */
S3C2510_DESINT_IFIFO_AVAIL; /* Input FIFO Avail Interrupt Enable */
/* Enable interrupt */
intEnable(INT_LVL_DES);
while (oCount > 0);
/* Disable interrupt */
intDisable(INT_LVL_DES);
#endif /* DES_FALLING */
#endif /* DES_GDMA */
semGive(semMutex);
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -