os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4/cryptojobs.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
/**
sl@0
    20
 @file
sl@0
    21
 @internalComponent
sl@0
    22
 @released
sl@0
    23
*/
sl@0
    24
#ifndef __CRYPTOJOBS__
sl@0
    25
#define __CRYPTOJOBS__
sl@0
    26
#include <e32cmn.h>
sl@0
    27
#include <e32ver.h>
sl@0
    28
#include <e32def.h>
sl@0
    29
#include "cryptodriver.h"
sl@0
    30
sl@0
    31
#define TRACE_FUNCTION(funcName) do{ static TraceFunction tf( __FILE__ , funcName ); tf.Inc(); }while(0)
sl@0
    32
sl@0
    33
class TraceFunction
sl@0
    34
	{
sl@0
    35
public:
sl@0
    36
	TraceFunction(const char *aFileName, const char *aFunctionName)
sl@0
    37
		: iFileName(aFileName),
sl@0
    38
		  iFunctionName(aFunctionName),
sl@0
    39
		  iHitCount(0),
sl@0
    40
		  iNext(HeadRef())
sl@0
    41
		{
sl@0
    42
		HeadRef() = this;
sl@0
    43
		}
sl@0
    44
	
sl@0
    45
	void Inc()
sl@0
    46
		{
sl@0
    47
		++iHitCount;
sl@0
    48
		}
sl@0
    49
sl@0
    50
static void DumpCounts()
sl@0
    51
		{
sl@0
    52
		Kern::Printf("TraceFunction::DumpCounts\n");
sl@0
    53
		TraceFunction *p = HeadRef();
sl@0
    54
		while(p)
sl@0
    55
			{
sl@0
    56
			Kern::Printf("%d\t%s:%s\n", p->iHitCount, p->iFileName, p->iFunctionName);
sl@0
    57
			p->iHitCount = 0;
sl@0
    58
			p = p->iNext;
sl@0
    59
			}
sl@0
    60
		}
sl@0
    61
sl@0
    62
IMPORT_C static TraceFunction *&HeadRef();
sl@0
    63
sl@0
    64
private:
sl@0
    65
	const char *iFileName;
sl@0
    66
	const char *iFunctionName;
sl@0
    67
	TUint32 iHitCount;
sl@0
    68
sl@0
    69
	TraceFunction *iNext;
sl@0
    70
	};
sl@0
    71
sl@0
    72
sl@0
    73
class CryptoJob;
sl@0
    74
class MCryptoJobCallbacks;
sl@0
    75
class DCryptoJobScheduler : public DBase
sl@0
    76
	{
sl@0
    77
public:
sl@0
    78
	IMPORT_C DCryptoJobScheduler();
sl@0
    79
	IMPORT_C ~DCryptoJobScheduler();
sl@0
    80
sl@0
    81
	/**
sl@0
    82
	   ScheduleJob
sl@0
    83
sl@0
    84
	   If job is already queued, and is the current job, and is not
sl@0
    85
	   already running, attempt to schedule it and return.  Otherwise,
sl@0
    86
	   if already in the queue, just return.
sl@0
    87
sl@0
    88
	   If the job is not in the queue, then add it - If the queue is
sl@0
    89
	   empty it is added at the front and we attempt to run it,
sl@0
    90
	   otherwise we add it second in the queue for later scheduling.
sl@0
    91
	 */
sl@0
    92
	IMPORT_C void ScheduleJob(CryptoJob *aJob);
sl@0
    93
sl@0
    94
	/**
sl@0
    95
	   Abort the job synchronously
sl@0
    96
sl@0
    97
	   In the EReady state call DoReleaseHw derived function which
sl@0
    98
	   should, before returning, abort the job.
sl@0
    99
sl@0
   100
	   Changes the state to Idle.
sl@0
   101
	   
sl@0
   102
	   Removes from the scheduler.
sl@0
   103
	   
sl@0
   104
	   The caller is expected to complete any outstanding user request.
sl@0
   105
	*/
sl@0
   106
	IMPORT_C void DeScheduleJob(CryptoJob *aJob);
sl@0
   107
sl@0
   108
	/**
sl@0
   109
	   SliceComplete
sl@0
   110
sl@0
   111
	   Called by a job when the current slice is complete. The job
sl@0
   112
	   should update its internal state before calling us.
sl@0
   113
	   
sl@0
   114
	   The specified job MUST be the current job.
sl@0
   115
	 */
sl@0
   116
	IMPORT_C void SliceComplete(CryptoJob *aJob, TInt aStatus);
sl@0
   117
sl@0
   118
	/**
sl@0
   119
	   JobComplete
sl@0
   120
sl@0
   121
	   Called by a job when the final slice is complete.
sl@0
   122
	   
sl@0
   123
	   The specified job MUST be the current job.
sl@0
   124
	 */
sl@0
   125
	IMPORT_C void JobComplete(CryptoJob *aJob, TInt aStatus);
sl@0
   126
private:
sl@0
   127
	/**
sl@0
   128
	   Schedule
sl@0
   129
	   
sl@0
   130
	   If current job is not running make it run
sl@0
   131
	 */
sl@0
   132
	void Schedule(TBool aReschedule);
sl@0
   133
sl@0
   134
	CryptoJob *iJobList;
sl@0
   135
	};
sl@0
   136
sl@0
   137
sl@0
   138
class DCryptoLddChannel;
sl@0
   139
class CryptoJob
sl@0
   140
	{
sl@0
   141
public:
sl@0
   142
	IMPORT_C CryptoJob();
sl@0
   143
	IMPORT_C virtual ~CryptoJob();
sl@0
   144
	virtual void SetDfcQ(TDfcQue *aDfcQue) = 0;
sl@0
   145
sl@0
   146
	IMPORT_C void Stalled();  // This job MUST be the current job
sl@0
   147
	IMPORT_C void Resume();
sl@0
   148
	IMPORT_C void DeScheduleJob();
sl@0
   149
	IMPORT_C void SetRunning(TBool aRunning);
sl@0
   150
sl@0
   151
	virtual void GetToPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore) = 0;
sl@0
   152
	virtual void BytesWrittenToPdd(TUint32 aBytes) = 0;
sl@0
   153
sl@0
   154
	virtual void GetFromPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore) = 0;
sl@0
   155
	virtual void BytesReadFromPdd(TUint32 aBytes) = 0;
sl@0
   156
sl@0
   157
	enum CryptoJobState 
sl@0
   158
	{
sl@0
   159
		ECreated, 			// Not registered with LDD factory
sl@0
   160
		EReadyForFirstRun, 	// Not running, but could run next for the first time
sl@0
   161
		ERunning,			// H/w is currently working on this job
sl@0
   162
		EReady,			  	// Is the current job (h/w is setup), but h/w is idle
sl@0
   163
		EReadyNoSavedState,	// Ready to run
sl@0
   164
		EReadySavedState,	// Ready to run, but requires state restore
sl@0
   165
		// EStalled
sl@0
   166
		//
sl@0
   167
		// Three cases:-
sl@0
   168
		//
sl@0
   169
		// 1) Already got enough data but LDD hasn't read it yet.
sl@0
   170
		// 2) No space for more h/w output
sl@0
   171
		// 3) No more data to feed to h/w
sl@0
   172
		//
sl@0
   173
		// Ultimately recoverd by the LDD reading/writing data and/or
sl@0
   174
		// deleting the job.
sl@0
   175
		EStalled
sl@0
   176
	};
sl@0
   177
sl@0
   178
 	IMPORT_C CryptoJobState State() const;
sl@0
   179
	
sl@0
   180
private:
sl@0
   181
	friend class DCryptoJobScheduler;
sl@0
   182
	/**
sl@0
   183
	   
sl@0
   184
	 */
sl@0
   185
	virtual void DoSlice(TBool aFirstSlice) = 0;
sl@0
   186
sl@0
   187
	/**
sl@0
   188
	   DoSaveState
sl@0
   189
sl@0
   190
	   Save the current job h/w state so that the job can be later
sl@0
   191
	   restored when we are re-scheduled.
sl@0
   192
sl@0
   193
	   If there is state to save it should return ETrue (in which case
sl@0
   194
	   DoRestoreState will be called before our DoSlice function is
sl@0
   195
	   next called).
sl@0
   196
	   
sl@0
   197
	   If the h/w state does not require saving/restore it should
sl@0
   198
	   return EFalse.
sl@0
   199
	*/
sl@0
   200
	virtual TBool DoSaveState() = 0;
sl@0
   201
sl@0
   202
	/**
sl@0
   203
	   DoRestoreState
sl@0
   204
	   
sl@0
   205
	   Restore the h/w state. The caller will almost certainly call
sl@0
   206
	   DoSlice just after this to continue the job.
sl@0
   207
	*/
sl@0
   208
	virtual void DoRestoreState() = 0;
sl@0
   209
sl@0
   210
	/**
sl@0
   211
	   DoReleaseHw
sl@0
   212
	   
sl@0
   213
	   Only called in state EReady or ERunning
sl@0
   214
sl@0
   215
	   Should abort the operation and release h/w (unhook ISRs etc)
sl@0
   216
	   before returning.
sl@0
   217
sl@0
   218
	   Later the caller will probably complete the user request...
sl@0
   219
	 */
sl@0
   220
	virtual void DoReleaseHw() = 0;
sl@0
   221
	
sl@0
   222
	CryptoJobState iState;
sl@0
   223
sl@0
   224
protected:
sl@0
   225
	DCryptoJobScheduler *iJobScheduler;
sl@0
   226
	MCryptoJobCallbacks *iCallbacks;
sl@0
   227
private:
sl@0
   228
sl@0
   229
	CryptoJob *iNext;
sl@0
   230
	TBool iInJobList;
sl@0
   231
	};
sl@0
   232
sl@0
   233
class MCryptoJobCallbacks
sl@0
   234
	{
sl@0
   235
public:
sl@0
   236
	/**
sl@0
   237
	   DataRequired
sl@0
   238
sl@0
   239
	   PDD is requesting more data to process.
sl@0
   240
sl@0
   241
	   Normally the LDD will write one or more bytes into the supplied
sl@0
   242
	   buffer and return the number of bytes written. It is also legal
sl@0
   243
	   for the LDD to not write any bytes and return 0.
sl@0
   244
sl@0
   245
	   If not enough bytes are supplied, the PDD will underrun and
sl@0
   246
	   stall (this function might not be re-called).
sl@0
   247
sl@0
   248
	   The LDD should provide more data to the h/w via the
sl@0
   249
	   GetToPddBuffer/BytesWrittenToPdd functions.
sl@0
   250
sl@0
   251
	   @return KErrNone or error code
sl@0
   252
	*/
sl@0
   253
	virtual TInt DataRequired() = 0;
sl@0
   254
sl@0
   255
	/**
sl@0
   256
	   DataAvailable
sl@0
   257
sl@0
   258
	   PDD has output data available for copying to the user.
sl@0
   259
sl@0
   260
	   This function will be called when the PDD is running low out
sl@0
   261
	   space for storing output data of if all input data has been
sl@0
   262
	   processed.
sl@0
   263
sl@0
   264
	   Normally the LDD would copy all the supplied data to the user.
sl@0
   265
sl@0
   266
	   It is legal for the LDD to copy less bytes, but this may cause
sl@0
   267
	   and the PDD to overrun and stall (this function might not be
sl@0
   268
	   re-called).
sl@0
   269
sl@0
   270
	   The LDD should use the GetFromPddBuffer/BytesReadFromPdd
sl@0
   271
	   functions to read data from the PDD.
sl@0
   272
sl@0
   273
	   @return KErrNone or error code
sl@0
   274
sl@0
   275
	*/
sl@0
   276
	virtual TInt DataAvailable() = 0;
sl@0
   277
sl@0
   278
	/**
sl@0
   279
	   JobComplete
sl@0
   280
sl@0
   281
	   The job scheduler has declared this job complete (maybe with an
sl@0
   282
	   error), and calls this function to tell the LDD to complete the
sl@0
   283
	   user request.
sl@0
   284
	*/
sl@0
   285
	virtual void JobComplete(TInt aResult) = 0;
sl@0
   286
	};
sl@0
   287
sl@0
   288
sl@0
   289
class CryptoJobRandom : public CryptoJob
sl@0
   290
	{
sl@0
   291
public:
sl@0
   292
	/**
sl@0
   293
	   SetDetails
sl@0
   294
	   
sl@0
   295
	   Setup and start job. Results later available via ResultsBuffer()
sl@0
   296
sl@0
   297
	   @param Ptr to the DCryptoJobScheduler for this type of job
sl@0
   298
	   @param Ptr to the LDD object for MCryptoJobCallbacks
sl@0
   299
	   @param aNumOfBytes Total number of random numbers the user requires
sl@0
   300
	*/
sl@0
   301
	virtual void SetDetails(DCryptoJobScheduler *aJobScheduler, 
sl@0
   302
							MCryptoJobCallbacks *aCallbacks,
sl@0
   303
							TUint32 aNumOfBytes) = 0;
sl@0
   304
	};
sl@0
   305
sl@0
   306
class CryptoJobAes : public CryptoJob
sl@0
   307
	{
sl@0
   308
public:
sl@0
   309
	/**
sl@0
   310
	   GetKeyBuffer
sl@0
   311
sl@0
   312
	   Get ptr to KEY buffer to copy user data into. Max key length is
sl@0
   313
	   32 bytes (256 bits)
sl@0
   314
sl@0
   315
	   @param Pointer to key buffer
sl@0
   316
	*/
sl@0
   317
	virtual TUint8 *GetKeyBuffer() = 0;
sl@0
   318
sl@0
   319
	/**
sl@0
   320
	   GetIVBuffer
sl@0
   321
sl@0
   322
	   Get ptr to IV buffer to copy user IV data into. 16 bytes long.
sl@0
   323
sl@0
   324
	   @param Pointer to IV buffer
sl@0
   325
	*/
sl@0
   326
	virtual TUint8 *GetIVBuffer() = 0;
sl@0
   327
sl@0
   328
	/**
sl@0
   329
	   MaxBytes
sl@0
   330
sl@0
   331
	   @param Length of buffer returned by Buffer().
sl@0
   332
	*/
sl@0
   333
	virtual TUint32 MaxBytes() const = 0;
sl@0
   334
sl@0
   335
	/**
sl@0
   336
	   Buffer
sl@0
   337
sl@0
   338
	   Get ptr to PDD buffer to copy user data into and results from
sl@0
   339
	   Buffer length <= MaxBytes().
sl@0
   340
sl@0
   341
	   @param Pointer to key buffer
sl@0
   342
	*/
sl@0
   343
	virtual TUint8 *GetIOBuffer() = 0;
sl@0
   344
sl@0
   345
	/**
sl@0
   346
	   SetDetails
sl@0
   347
sl@0
   348
	   @param Ptr to the DCryptoJobScheduler for this type of job
sl@0
   349
	   @param Ptr to the LDD object for MCryptoJobCallbacks
sl@0
   350
	   @param aEncrypt True requests encryption, false decryption
sl@0
   351
	   @param aKeyLength Length of key in bytes
sl@0
   352
	   @param aMode See RCryptoDriver::TChainingMode
sl@0
   353
	   @return KErrNone or error code
sl@0
   354
	*/
sl@0
   355
	virtual TInt SetDetails(DCryptoJobScheduler *aJobScheduler, 
sl@0
   356
							MCryptoJobCallbacks *aCallbacks,
sl@0
   357
							TBool aEncrypt, 
sl@0
   358
							TInt aKeyLengthBytes,
sl@0
   359
							RCryptoDriver::TChainingMode aMode) = 0;
sl@0
   360
sl@0
   361
	virtual void NotifyReadRequestLength(TUint32 aReadRequestLength) = 0;
sl@0
   362
	virtual void HwPerfCheck() = 0;
sl@0
   363
	};
sl@0
   364
sl@0
   365
#endif