1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4/cryptojobs.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,365 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +/**
1.23 + @file
1.24 + @internalComponent
1.25 + @released
1.26 +*/
1.27 +#ifndef __CRYPTOJOBS__
1.28 +#define __CRYPTOJOBS__
1.29 +#include <e32cmn.h>
1.30 +#include <e32ver.h>
1.31 +#include <e32def.h>
1.32 +#include "cryptodriver.h"
1.33 +
1.34 +#define TRACE_FUNCTION(funcName) do{ static TraceFunction tf( __FILE__ , funcName ); tf.Inc(); }while(0)
1.35 +
1.36 +class TraceFunction
1.37 + {
1.38 +public:
1.39 + TraceFunction(const char *aFileName, const char *aFunctionName)
1.40 + : iFileName(aFileName),
1.41 + iFunctionName(aFunctionName),
1.42 + iHitCount(0),
1.43 + iNext(HeadRef())
1.44 + {
1.45 + HeadRef() = this;
1.46 + }
1.47 +
1.48 + void Inc()
1.49 + {
1.50 + ++iHitCount;
1.51 + }
1.52 +
1.53 +static void DumpCounts()
1.54 + {
1.55 + Kern::Printf("TraceFunction::DumpCounts\n");
1.56 + TraceFunction *p = HeadRef();
1.57 + while(p)
1.58 + {
1.59 + Kern::Printf("%d\t%s:%s\n", p->iHitCount, p->iFileName, p->iFunctionName);
1.60 + p->iHitCount = 0;
1.61 + p = p->iNext;
1.62 + }
1.63 + }
1.64 +
1.65 +IMPORT_C static TraceFunction *&HeadRef();
1.66 +
1.67 +private:
1.68 + const char *iFileName;
1.69 + const char *iFunctionName;
1.70 + TUint32 iHitCount;
1.71 +
1.72 + TraceFunction *iNext;
1.73 + };
1.74 +
1.75 +
1.76 +class CryptoJob;
1.77 +class MCryptoJobCallbacks;
1.78 +class DCryptoJobScheduler : public DBase
1.79 + {
1.80 +public:
1.81 + IMPORT_C DCryptoJobScheduler();
1.82 + IMPORT_C ~DCryptoJobScheduler();
1.83 +
1.84 + /**
1.85 + ScheduleJob
1.86 +
1.87 + If job is already queued, and is the current job, and is not
1.88 + already running, attempt to schedule it and return. Otherwise,
1.89 + if already in the queue, just return.
1.90 +
1.91 + If the job is not in the queue, then add it - If the queue is
1.92 + empty it is added at the front and we attempt to run it,
1.93 + otherwise we add it second in the queue for later scheduling.
1.94 + */
1.95 + IMPORT_C void ScheduleJob(CryptoJob *aJob);
1.96 +
1.97 + /**
1.98 + Abort the job synchronously
1.99 +
1.100 + In the EReady state call DoReleaseHw derived function which
1.101 + should, before returning, abort the job.
1.102 +
1.103 + Changes the state to Idle.
1.104 +
1.105 + Removes from the scheduler.
1.106 +
1.107 + The caller is expected to complete any outstanding user request.
1.108 + */
1.109 + IMPORT_C void DeScheduleJob(CryptoJob *aJob);
1.110 +
1.111 + /**
1.112 + SliceComplete
1.113 +
1.114 + Called by a job when the current slice is complete. The job
1.115 + should update its internal state before calling us.
1.116 +
1.117 + The specified job MUST be the current job.
1.118 + */
1.119 + IMPORT_C void SliceComplete(CryptoJob *aJob, TInt aStatus);
1.120 +
1.121 + /**
1.122 + JobComplete
1.123 +
1.124 + Called by a job when the final slice is complete.
1.125 +
1.126 + The specified job MUST be the current job.
1.127 + */
1.128 + IMPORT_C void JobComplete(CryptoJob *aJob, TInt aStatus);
1.129 +private:
1.130 + /**
1.131 + Schedule
1.132 +
1.133 + If current job is not running make it run
1.134 + */
1.135 + void Schedule(TBool aReschedule);
1.136 +
1.137 + CryptoJob *iJobList;
1.138 + };
1.139 +
1.140 +
1.141 +class DCryptoLddChannel;
1.142 +class CryptoJob
1.143 + {
1.144 +public:
1.145 + IMPORT_C CryptoJob();
1.146 + IMPORT_C virtual ~CryptoJob();
1.147 + virtual void SetDfcQ(TDfcQue *aDfcQue) = 0;
1.148 +
1.149 + IMPORT_C void Stalled(); // This job MUST be the current job
1.150 + IMPORT_C void Resume();
1.151 + IMPORT_C void DeScheduleJob();
1.152 + IMPORT_C void SetRunning(TBool aRunning);
1.153 +
1.154 + virtual void GetToPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore) = 0;
1.155 + virtual void BytesWrittenToPdd(TUint32 aBytes) = 0;
1.156 +
1.157 + virtual void GetFromPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore) = 0;
1.158 + virtual void BytesReadFromPdd(TUint32 aBytes) = 0;
1.159 +
1.160 + enum CryptoJobState
1.161 + {
1.162 + ECreated, // Not registered with LDD factory
1.163 + EReadyForFirstRun, // Not running, but could run next for the first time
1.164 + ERunning, // H/w is currently working on this job
1.165 + EReady, // Is the current job (h/w is setup), but h/w is idle
1.166 + EReadyNoSavedState, // Ready to run
1.167 + EReadySavedState, // Ready to run, but requires state restore
1.168 + // EStalled
1.169 + //
1.170 + // Three cases:-
1.171 + //
1.172 + // 1) Already got enough data but LDD hasn't read it yet.
1.173 + // 2) No space for more h/w output
1.174 + // 3) No more data to feed to h/w
1.175 + //
1.176 + // Ultimately recoverd by the LDD reading/writing data and/or
1.177 + // deleting the job.
1.178 + EStalled
1.179 + };
1.180 +
1.181 + IMPORT_C CryptoJobState State() const;
1.182 +
1.183 +private:
1.184 + friend class DCryptoJobScheduler;
1.185 + /**
1.186 +
1.187 + */
1.188 + virtual void DoSlice(TBool aFirstSlice) = 0;
1.189 +
1.190 + /**
1.191 + DoSaveState
1.192 +
1.193 + Save the current job h/w state so that the job can be later
1.194 + restored when we are re-scheduled.
1.195 +
1.196 + If there is state to save it should return ETrue (in which case
1.197 + DoRestoreState will be called before our DoSlice function is
1.198 + next called).
1.199 +
1.200 + If the h/w state does not require saving/restore it should
1.201 + return EFalse.
1.202 + */
1.203 + virtual TBool DoSaveState() = 0;
1.204 +
1.205 + /**
1.206 + DoRestoreState
1.207 +
1.208 + Restore the h/w state. The caller will almost certainly call
1.209 + DoSlice just after this to continue the job.
1.210 + */
1.211 + virtual void DoRestoreState() = 0;
1.212 +
1.213 + /**
1.214 + DoReleaseHw
1.215 +
1.216 + Only called in state EReady or ERunning
1.217 +
1.218 + Should abort the operation and release h/w (unhook ISRs etc)
1.219 + before returning.
1.220 +
1.221 + Later the caller will probably complete the user request...
1.222 + */
1.223 + virtual void DoReleaseHw() = 0;
1.224 +
1.225 + CryptoJobState iState;
1.226 +
1.227 +protected:
1.228 + DCryptoJobScheduler *iJobScheduler;
1.229 + MCryptoJobCallbacks *iCallbacks;
1.230 +private:
1.231 +
1.232 + CryptoJob *iNext;
1.233 + TBool iInJobList;
1.234 + };
1.235 +
1.236 +class MCryptoJobCallbacks
1.237 + {
1.238 +public:
1.239 + /**
1.240 + DataRequired
1.241 +
1.242 + PDD is requesting more data to process.
1.243 +
1.244 + Normally the LDD will write one or more bytes into the supplied
1.245 + buffer and return the number of bytes written. It is also legal
1.246 + for the LDD to not write any bytes and return 0.
1.247 +
1.248 + If not enough bytes are supplied, the PDD will underrun and
1.249 + stall (this function might not be re-called).
1.250 +
1.251 + The LDD should provide more data to the h/w via the
1.252 + GetToPddBuffer/BytesWrittenToPdd functions.
1.253 +
1.254 + @return KErrNone or error code
1.255 + */
1.256 + virtual TInt DataRequired() = 0;
1.257 +
1.258 + /**
1.259 + DataAvailable
1.260 +
1.261 + PDD has output data available for copying to the user.
1.262 +
1.263 + This function will be called when the PDD is running low out
1.264 + space for storing output data of if all input data has been
1.265 + processed.
1.266 +
1.267 + Normally the LDD would copy all the supplied data to the user.
1.268 +
1.269 + It is legal for the LDD to copy less bytes, but this may cause
1.270 + and the PDD to overrun and stall (this function might not be
1.271 + re-called).
1.272 +
1.273 + The LDD should use the GetFromPddBuffer/BytesReadFromPdd
1.274 + functions to read data from the PDD.
1.275 +
1.276 + @return KErrNone or error code
1.277 +
1.278 + */
1.279 + virtual TInt DataAvailable() = 0;
1.280 +
1.281 + /**
1.282 + JobComplete
1.283 +
1.284 + The job scheduler has declared this job complete (maybe with an
1.285 + error), and calls this function to tell the LDD to complete the
1.286 + user request.
1.287 + */
1.288 + virtual void JobComplete(TInt aResult) = 0;
1.289 + };
1.290 +
1.291 +
1.292 +class CryptoJobRandom : public CryptoJob
1.293 + {
1.294 +public:
1.295 + /**
1.296 + SetDetails
1.297 +
1.298 + Setup and start job. Results later available via ResultsBuffer()
1.299 +
1.300 + @param Ptr to the DCryptoJobScheduler for this type of job
1.301 + @param Ptr to the LDD object for MCryptoJobCallbacks
1.302 + @param aNumOfBytes Total number of random numbers the user requires
1.303 + */
1.304 + virtual void SetDetails(DCryptoJobScheduler *aJobScheduler,
1.305 + MCryptoJobCallbacks *aCallbacks,
1.306 + TUint32 aNumOfBytes) = 0;
1.307 + };
1.308 +
1.309 +class CryptoJobAes : public CryptoJob
1.310 + {
1.311 +public:
1.312 + /**
1.313 + GetKeyBuffer
1.314 +
1.315 + Get ptr to KEY buffer to copy user data into. Max key length is
1.316 + 32 bytes (256 bits)
1.317 +
1.318 + @param Pointer to key buffer
1.319 + */
1.320 + virtual TUint8 *GetKeyBuffer() = 0;
1.321 +
1.322 + /**
1.323 + GetIVBuffer
1.324 +
1.325 + Get ptr to IV buffer to copy user IV data into. 16 bytes long.
1.326 +
1.327 + @param Pointer to IV buffer
1.328 + */
1.329 + virtual TUint8 *GetIVBuffer() = 0;
1.330 +
1.331 + /**
1.332 + MaxBytes
1.333 +
1.334 + @param Length of buffer returned by Buffer().
1.335 + */
1.336 + virtual TUint32 MaxBytes() const = 0;
1.337 +
1.338 + /**
1.339 + Buffer
1.340 +
1.341 + Get ptr to PDD buffer to copy user data into and results from
1.342 + Buffer length <= MaxBytes().
1.343 +
1.344 + @param Pointer to key buffer
1.345 + */
1.346 + virtual TUint8 *GetIOBuffer() = 0;
1.347 +
1.348 + /**
1.349 + SetDetails
1.350 +
1.351 + @param Ptr to the DCryptoJobScheduler for this type of job
1.352 + @param Ptr to the LDD object for MCryptoJobCallbacks
1.353 + @param aEncrypt True requests encryption, false decryption
1.354 + @param aKeyLength Length of key in bytes
1.355 + @param aMode See RCryptoDriver::TChainingMode
1.356 + @return KErrNone or error code
1.357 + */
1.358 + virtual TInt SetDetails(DCryptoJobScheduler *aJobScheduler,
1.359 + MCryptoJobCallbacks *aCallbacks,
1.360 + TBool aEncrypt,
1.361 + TInt aKeyLengthBytes,
1.362 + RCryptoDriver::TChainingMode aMode) = 0;
1.363 +
1.364 + virtual void NotifyReadRequestLength(TUint32 aReadRequestLength) = 0;
1.365 + virtual void HwPerfCheck() = 0;
1.366 + };
1.367 +
1.368 +#endif