summaryrefslogtreecommitdiff
path: root/target/linux/generic-2.6/files/crypto/ocf/ep80579/icp_asym.c
blob: 1a9bd28bf4d1ed521dbe388bc8a83e227fbc8056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/***************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or 
 *   redistributing this file, you may do so under either license.
 * 
 *   GPL LICENSE SUMMARY
 * 
 *   Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
 * 
 *   This program is free software; you can redistribute it and/or modify 
 *   it under the terms of version 2 of the GNU General Public License as
 *   published by the Free Software Foundation.
 * 
 *   This program is distributed in the hope that it will be useful, but 
 *   WITHOUT ANY WARRANTY; without even the implied warranty of 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 *   General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License 
 *   along with this program; if not, write to the Free Software 
 *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *   The full GNU General Public License is included in this distribution 
 *   in the file called LICENSE.GPL.
 * 
 *   Contact Information:
 *   Intel Corporation
 * 
 *   BSD LICENSE 
 * 
 *   Copyright(c) 2007,2008 Intel Corporation. All rights reserved.
 *   All rights reserved.
 * 
 *   Redistribution and use in source and binary forms, with or without 
 *   modification, are permitted provided that the following conditions 
 *   are met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in 
 *       the documentation and/or other materials provided with the 
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its 
 *       contributors may be used to endorse or promote products derived 
 *       from this software without specific prior written permission.
 * 
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * 
 *  version: Security.L.1.0.130
 *
 ***************************************************************************/

#include "icp_ocf.h"

/*The following define values (containing the word 'INDEX') are used to find
the index of each input buffer of the crypto_kop struct (see OCF cryptodev.h).
These values were found through analysis of the OCF OpenSSL patch. If the
calling program uses different input buffer positions, these defines will have
to be changed.*/

/*DIFFIE HELLMAN buffer index values*/
#define ICP_DH_KRP_PARAM_PRIME_INDEX				(0)
#define ICP_DH_KRP_PARAM_BASE_INDEX				(1)
#define ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX			(2)
#define ICP_DH_KRP_PARAM_RESULT_INDEX				(3)

/*MOD EXP buffer index values*/
#define ICP_MOD_EXP_KRP_PARAM_BASE_INDEX			(0)
#define ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX			(1)
#define ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX			(2)
#define ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX			(3)

#define SINGLE_BYTE_VALUE					(4)

/*MOD EXP CRT buffer index values*/
#define ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX			(0)
#define ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX			(1)
#define ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX			(2)
#define ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX		(3)
#define ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX		(4)
#define ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX		(5)
#define ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX			(6)

/*DSA sign buffer index values*/
#define ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX			(0)
#define ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX			(1)
#define ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX			(2)
#define ICP_DSA_SIGN_KRP_PARAM_G_INDEX				(3)
#define ICP_DSA_SIGN_KRP_PARAM_X_INDEX				(4)
#define ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX			(5)
#define ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX			(6)

/*DSA verify buffer index values*/
#define ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX			(0)
#define ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX			(1)
#define ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX			(2)
#define ICP_DSA_VERIFY_KRP_PARAM_G_INDEX			(3)
#define ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX			(4)
#define ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX			(5)
#define ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX			(6)

/*DSA sign prime Q vs random number K size check values*/
#define DONT_RUN_LESS_THAN_CHECK				(0)
#define FAIL_A_IS_GREATER_THAN_B				(1)
#define FAIL_A_IS_EQUAL_TO_B					(1)
#define SUCCESS_A_IS_LESS_THAN_B				(0)
#define DSA_SIGN_RAND_GEN_VAL_CHECK_MAX_ITERATIONS		(500)

/* We need to set a cryptokp success value just in case it is set or allocated
   and not set to zero outside of this module */
#define CRYPTO_OP_SUCCESS					(0)

static int icp_ocfDrvDHComputeKey(struct cryptkop *krp);

static int icp_ocfDrvModExp(struct cryptkop *krp);

static int icp_ocfDrvModExpCRT(struct cryptkop *krp);

static int
icp_ocfDrvCheckALessThanB(CpaFlatBuffer * pK, CpaFlatBuffer * pQ, int *doCheck);

static int icp_ocfDrvDsaSign(struct cryptkop *krp);

static int icp_ocfDrvDsaVerify(struct cryptkop *krp);

static void
icp_ocfDrvDhP1CallBack(void *callbackTag,
		       CpaStatus status,
		       void *pOpData, CpaFlatBuffer * pLocalOctetStringPV);

static void
icp_ocfDrvModExpCallBack(void *callbackTag,
			 CpaStatus status,
			 void *pOpData, CpaFlatBuffer * pResult);

static void
icp_ocfDrvModExpCRTCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData, CpaFlatBuffer * pOutputData);

static void
icp_ocfDrvDsaVerifyCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData, CpaBoolean verifyStatus);

static void
icp_ocfDrvDsaRSSignCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData,
			    CpaBoolean protocolStatus,
			    CpaFlatBuffer * pR, CpaFlatBuffer * pS);

/* Name        : icp_ocfDrvPkeProcess
 *
 * Description : This function will choose which PKE process to follow
 * based on the input arguments
 */
int icp_ocfDrvPkeProcess(device_t dev, struct cryptkop *krp, int hint)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;

	if (NULL == krp) {
		DPRINTK("%s(): Invalid input parameters, cryptkop = %p\n",
			__FUNCTION__, krp);
		return EINVAL;
	}

	if (CPA_TRUE == atomic_read(&icp_ocfDrvIsExiting)) {
		krp->krp_status = ECANCELED;
		return ECANCELED;
	}

	switch (krp->krp_op) {
	case CRK_DH_COMPUTE_KEY:
		DPRINTK("%s() doing DH_COMPUTE_KEY\n", __FUNCTION__);
		lacStatus = icp_ocfDrvDHComputeKey(krp);
		if (CPA_STATUS_SUCCESS != lacStatus) {
			EPRINTK("%s(): icp_ocfDrvDHComputeKey failed "
				"(%d).\n", __FUNCTION__, lacStatus);
			krp->krp_status = ECANCELED;
			return ECANCELED;
		}

		break;

	case CRK_MOD_EXP:
		DPRINTK("%s() doing MOD_EXP \n", __FUNCTION__);
		lacStatus = icp_ocfDrvModExp(krp);
		if (CPA_STATUS_SUCCESS != lacStatus) {
			EPRINTK("%s(): icp_ocfDrvModExp failed (%d).\n",
				__FUNCTION__, lacStatus);
			krp->krp_status = ECANCELED;
			return ECANCELED;
		}

		break;

	case CRK_MOD_EXP_CRT:
		DPRINTK("%s() doing MOD_EXP_CRT \n", __FUNCTION__);
		lacStatus = icp_ocfDrvModExpCRT(krp);
		if (CPA_STATUS_SUCCESS != lacStatus) {
			EPRINTK("%s(): icp_ocfDrvModExpCRT "
				"failed (%d).\n", __FUNCTION__, lacStatus);
			krp->krp_status = ECANCELED;
			return ECANCELED;
		}

		break;

	case CRK_DSA_SIGN:
		DPRINTK("%s() doing DSA_SIGN \n", __FUNCTION__);
		lacStatus = icp_ocfDrvDsaSign(krp);
		if (CPA_STATUS_SUCCESS != lacStatus) {
			EPRINTK("%s(): icp_ocfDrvDsaSign "
				"failed (%d).\n", __FUNCTION__, lacStatus);
			krp->krp_status = ECANCELED;
			return ECANCELED;
		}

		break;

	case CRK_DSA_VERIFY:
		DPRINTK("%s() doing DSA_VERIFY \n", __FUNCTION__);
		lacStatus = icp_ocfDrvDsaVerify(krp);
		if (CPA_STATUS_SUCCESS != lacStatus) {
			EPRINTK("%s(): icp_ocfDrvDsaVerify "
				"failed (%d).\n", __FUNCTION__, lacStatus);
			krp->krp_status = ECANCELED;
			return ECANCELED;
		}

		break;

	default:
		EPRINTK("%s(): Asymettric function not "
			"supported (%d).\n", __FUNCTION__, krp->krp_op);
		krp->krp_status = EOPNOTSUPP;
		return EOPNOTSUPP;
	}

	return ICP_OCF_DRV_STATUS_SUCCESS;
}

/* Name        : icp_ocfDrvSwapBytes
 *
 * Description : This function is used to swap the byte order of a buffer.
 * It has been seen that in general we are passed little endian byte order
 * buffers, but LAC only accepts big endian byte order buffers.
 */
static void inline
icp_ocfDrvSwapBytes(u_int8_t * num, u_int32_t buff_len_bytes)
{

	int i;
	u_int8_t *end_ptr;
	u_int8_t hold_val;

	end_ptr = num + (buff_len_bytes - 1);
	buff_len_bytes = buff_len_bytes >> 1;
	for (i = 0; i < buff_len_bytes; i++) {
		hold_val = *num;
		*num = *end_ptr;
		num++;
		*end_ptr = hold_val;
		end_ptr--;
	}
}

/* Name        : icp_ocfDrvDHComputeKey
 *
 * Description : This function will map Diffie Hellman calls from OCF
 * to the LAC API. OCF uses this function for Diffie Hellman Phase1 and
 * Phase2. LAC has a separate Diffie Hellman Phase2 call, however both phases
 * break down to a modular exponentiation.
 */
static int icp_ocfDrvDHComputeKey(struct cryptkop *krp)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	void *callbackTag = NULL;
	CpaCyDhPhase1KeyGenOpData *pPhase1OpData = NULL;
	CpaFlatBuffer *pLocalOctetStringPV = NULL;
	uint32_t dh_prime_len_bytes = 0, dh_prime_len_bits = 0;

	/* Input checks - check prime is a multiple of 8 bits to allow for
	   allocation later */
	dh_prime_len_bits =
	    (krp->krp_param[ICP_DH_KRP_PARAM_PRIME_INDEX].crp_nbits);

	/* LAC can reject prime lengths based on prime key sizes, we just
	   need to make sure we can allocate space for the base and
	   exponent buffers correctly */
	if ((dh_prime_len_bits % NUM_BITS_IN_BYTE) != 0) {
		APRINTK("%s(): Warning Prime number buffer size is not a "
			"multiple of 8 bits\n", __FUNCTION__);
	}

	/* Result storage space should be the same size as the prime as this
	   value can take up the same amount of storage space */
	if (dh_prime_len_bits !=
	    krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_nbits) {
		DPRINTK("%s(): Return Buffer must be the same size "
			"as the Prime buffer\n", __FUNCTION__);
		krp->krp_status = EINVAL;
		return EINVAL;
	}
	/* Switch to size in bytes */
	BITS_TO_BYTES(dh_prime_len_bytes, dh_prime_len_bits);

	callbackTag = krp;

	pPhase1OpData = kmem_cache_zalloc(drvDH_zone, GFP_KERNEL);
	if (NULL == pPhase1OpData) {
		APRINTK("%s():Failed to get memory for key gen data\n",
			__FUNCTION__);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	pLocalOctetStringPV = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
	if (NULL == pLocalOctetStringPV) {
		APRINTK("%s():Failed to get memory for pLocalOctetStringPV\n",
			__FUNCTION__);
		kmem_cache_free(drvDH_zone, pPhase1OpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	/* Link parameters */
	pPhase1OpData->primeP.pData =
	    krp->krp_param[ICP_DH_KRP_PARAM_PRIME_INDEX].crp_p;

	pPhase1OpData->primeP.dataLenInBytes = dh_prime_len_bytes;

	icp_ocfDrvSwapBytes(pPhase1OpData->primeP.pData, dh_prime_len_bytes);

	pPhase1OpData->baseG.pData =
	    krp->krp_param[ICP_DH_KRP_PARAM_BASE_INDEX].crp_p;

	BITS_TO_BYTES(pPhase1OpData->baseG.dataLenInBytes,
		      krp->krp_param[ICP_DH_KRP_PARAM_BASE_INDEX].crp_nbits);

	icp_ocfDrvSwapBytes(pPhase1OpData->baseG.pData,
			    pPhase1OpData->baseG.dataLenInBytes);

	pPhase1OpData->privateValueX.pData =
	    krp->krp_param[ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX].crp_p;

	BITS_TO_BYTES(pPhase1OpData->privateValueX.dataLenInBytes,
		      krp->krp_param[ICP_DH_KRP_PARAM_PRIVATE_VALUE_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(pPhase1OpData->privateValueX.pData,
			    pPhase1OpData->privateValueX.dataLenInBytes);

	/* Output parameters */
	pLocalOctetStringPV->pData =
	    krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_p;

	BITS_TO_BYTES(pLocalOctetStringPV->dataLenInBytes,
		      krp->krp_param[ICP_DH_KRP_PARAM_RESULT_INDEX].crp_nbits);

	lacStatus = cpaCyDhKeyGenPhase1(CPA_INSTANCE_HANDLE_SINGLE,
					icp_ocfDrvDhP1CallBack,
					callbackTag, pPhase1OpData,
					pLocalOctetStringPV);

	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): DH Phase 1 Key Gen failed (%d).\n",
			__FUNCTION__, lacStatus);
		icp_ocfDrvFreeFlatBuffer(pLocalOctetStringPV);
		kmem_cache_free(drvDH_zone, pPhase1OpData);
	}

	return lacStatus;
}

/* Name        : icp_ocfDrvModExp
 *
 * Description : This function will map ordinary Modular Exponentiation calls
 * from OCF to the LAC API.
 *
 */
static int icp_ocfDrvModExp(struct cryptkop *krp)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	void *callbackTag = NULL;
	CpaCyLnModExpOpData *pModExpOpData = NULL;
	CpaFlatBuffer *pResult = NULL;

	if ((krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_nbits %
	     NUM_BITS_IN_BYTE) != 0) {
		DPRINTK("%s(): Warning - modulus buffer size (%d) is not a "
			"multiple of 8 bits\n", __FUNCTION__,
			krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].
			crp_nbits);
	}

	/* Result storage space should be the same size as the prime as this
	   value can take up the same amount of storage space */
	if (krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_nbits >
	    krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].crp_nbits) {
		APRINTK("%s(): Return Buffer size must be the same or"
			" greater than the Modulus buffer\n", __FUNCTION__);
		krp->krp_status = EINVAL;
		return EINVAL;
	}

	callbackTag = krp;

	pModExpOpData = kmem_cache_zalloc(drvLnModExp_zone, GFP_KERNEL);
	if (NULL == pModExpOpData) {
		APRINTK("%s():Failed to get memory for key gen data\n",
			__FUNCTION__);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	pResult = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
	if (NULL == pResult) {
		APRINTK("%s():Failed to get memory for ModExp result\n",
			__FUNCTION__);
		kmem_cache_free(drvLnModExp_zone, pModExpOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	/* Link parameters */
	pModExpOpData->modulus.pData =
	    krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].crp_p;
	BITS_TO_BYTES(pModExpOpData->modulus.dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_KRP_PARAM_MODULUS_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(pModExpOpData->modulus.pData,
			    pModExpOpData->modulus.dataLenInBytes);

	/*OCF patch to Openswan Pluto regularly sends the base value as 2
	   bits in size. In this case, it has been found it is better to
	   use the base size memory space as the input buffer (if the number
	   is in bits is less than a byte, the number of bits is the input
	   value) */
	if (krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits <
	    NUM_BITS_IN_BYTE) {
		DPRINTK("%s : base is small (%d)\n", __FUNCTION__, krp->
			krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits);
		pModExpOpData->base.dataLenInBytes = SINGLE_BYTE_VALUE;
		pModExpOpData->base.pData =
		    (uint8_t *) & (krp->
				   krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
				   crp_nbits);
		*((uint32_t *) pModExpOpData->base.pData) =
		    htonl(*((uint32_t *) pModExpOpData->base.pData));

	} else {

		DPRINTK("%s : base is big (%d)\n", __FUNCTION__, krp->
			krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_nbits);
		pModExpOpData->base.pData =
		    krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].crp_p;
		BITS_TO_BYTES(pModExpOpData->base.dataLenInBytes,
			      krp->krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
			      crp_nbits);
		icp_ocfDrvSwapBytes(pModExpOpData->base.pData,
				    pModExpOpData->base.dataLenInBytes);
	}

	pModExpOpData->exponent.pData =
	    krp->krp_param[ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX].crp_p;
	BITS_TO_BYTES(pModExpOpData->exponent.dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_KRP_PARAM_EXPONENT_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(pModExpOpData->exponent.pData,
			    pModExpOpData->exponent.dataLenInBytes);
	/* Output parameters */
	pResult->pData =
	    krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].crp_p,
	    BITS_TO_BYTES(pResult->dataLenInBytes,
			  krp->krp_param[ICP_MOD_EXP_KRP_PARAM_RESULT_INDEX].
			  crp_nbits);

	lacStatus = cpaCyLnModExp(CPA_INSTANCE_HANDLE_SINGLE,
				  icp_ocfDrvModExpCallBack,
				  callbackTag, pModExpOpData, pResult);

	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): Mod Exp Operation failed (%d).\n",
			__FUNCTION__, lacStatus);
		krp->krp_status = ECANCELED;
		icp_ocfDrvFreeFlatBuffer(pResult);
		kmem_cache_free(drvLnModExp_zone, pModExpOpData);
	}

	return lacStatus;
}

/* Name        : icp_ocfDrvModExpCRT
 *
 * Description : This function will map ordinary Modular Exponentiation Chinese
 * Remainder Theorem implementaion calls from OCF to the LAC API.
 *
 * Note : Mod Exp CRT for this driver is accelerated through LAC RSA type 2
 * decrypt operation. Therefore P and Q input values must always be prime
 * numbers. Although basic primality checks are done in LAC, it is up to the
 * user to do any correct prime number checking before passing the inputs.
 */

static int icp_ocfDrvModExpCRT(struct cryptkop *krp)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	CpaCyRsaDecryptOpData *rsaDecryptOpData = NULL;
	void *callbackTag = NULL;
	CpaFlatBuffer *pOutputData = NULL;

	/*Parameter input checks are all done by LAC, no need to repeat
	   them here. */
	callbackTag = krp;

	rsaDecryptOpData = kmem_cache_zalloc(drvRSADecrypt_zone, GFP_KERNEL);
	if (NULL == rsaDecryptOpData) {
		APRINTK("%s():Failed to get memory"
			" for MOD EXP CRT Op data struct\n", __FUNCTION__);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	rsaDecryptOpData->pRecipientPrivateKey
	    = kmem_cache_zalloc(drvRSAPrivateKey_zone, GFP_KERNEL);
	if (NULL == rsaDecryptOpData->pRecipientPrivateKey) {
		APRINTK("%s():Failed to get memory for MOD EXP CRT"
			" private key values struct\n", __FUNCTION__);
		kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	rsaDecryptOpData->pRecipientPrivateKey->
	    version = CPA_CY_RSA_VERSION_TWO_PRIME;
	rsaDecryptOpData->pRecipientPrivateKey->
	    privateKeyRepType = CPA_CY_RSA_PRIVATE_KEY_REP_TYPE_2;

	pOutputData = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
	if (NULL == pOutputData) {
		APRINTK("%s():Failed to get memory"
			" for MOD EXP CRT output data\n", __FUNCTION__);
		kmem_cache_free(drvRSAPrivateKey_zone,
				rsaDecryptOpData->pRecipientPrivateKey);
		kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	rsaDecryptOpData->pRecipientPrivateKey->
	    version = CPA_CY_RSA_VERSION_TWO_PRIME;
	rsaDecryptOpData->pRecipientPrivateKey->
	    privateKeyRepType = CPA_CY_RSA_PRIVATE_KEY_REP_TYPE_2;

	/* Link parameters */
	rsaDecryptOpData->inputData.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->inputData.dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_I_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->inputData.pData,
			    rsaDecryptOpData->inputData.dataLenInBytes);

	rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.prime1P.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
		      prime1P.dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_P_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.prime1P.pData,
			    rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.prime1P.dataLenInBytes);

	rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.prime2Q.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
		      prime2Q.dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_PRIME_Q_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.prime2Q.pData,
			    rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.prime2Q.dataLenInBytes);

	rsaDecryptOpData->pRecipientPrivateKey->
	    privateKeyRep2.exponent1Dp.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->privateKeyRep2.
		      exponent1Dp.dataLenInBytes,
		      krp->
		      krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DP_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.exponent1Dp.pData,
			    rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.exponent1Dp.dataLenInBytes);

	rsaDecryptOpData->pRecipientPrivateKey->
	    privateKeyRep2.exponent2Dq.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->
		      privateKeyRep2.exponent2Dq.dataLenInBytes,
		      krp->
		      krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_EXPONENT_DQ_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.exponent2Dq.pData,
			    rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.exponent2Dq.dataLenInBytes);

	rsaDecryptOpData->pRecipientPrivateKey->
	    privateKeyRep2.coefficientQInv.pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX].crp_p;
	BITS_TO_BYTES(rsaDecryptOpData->pRecipientPrivateKey->
		      privateKeyRep2.coefficientQInv.dataLenInBytes,
		      krp->
		      krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_COEFF_QINV_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.coefficientQInv.pData,
			    rsaDecryptOpData->pRecipientPrivateKey->
			    privateKeyRep2.coefficientQInv.dataLenInBytes);

	/* Output Parameter */
	pOutputData->pData =
	    krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX].crp_p;
	BITS_TO_BYTES(pOutputData->dataLenInBytes,
		      krp->krp_param[ICP_MOD_EXP_CRT_KRP_PARAM_RESULT_INDEX].
		      crp_nbits);

	lacStatus = cpaCyRsaDecrypt(CPA_INSTANCE_HANDLE_SINGLE,
				    icp_ocfDrvModExpCRTCallBack,
				    callbackTag, rsaDecryptOpData, pOutputData);

	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): Mod Exp CRT Operation failed (%d).\n",
			__FUNCTION__, lacStatus);
		krp->krp_status = ECANCELED;
		icp_ocfDrvFreeFlatBuffer(pOutputData);
		kmem_cache_free(drvRSAPrivateKey_zone,
				rsaDecryptOpData->pRecipientPrivateKey);
		kmem_cache_free(drvRSADecrypt_zone, rsaDecryptOpData);
	}

	return lacStatus;
}

/* Name        : icp_ocfDrvCheckALessThanB
 *
 * Description : This function will check whether the first argument is less
 * than the second. It is used to check whether the DSA RS sign Random K
 * value is less than the Prime Q value (as defined in the specification)
 *
 */
static int
icp_ocfDrvCheckALessThanB(CpaFlatBuffer * pK, CpaFlatBuffer * pQ, int *doCheck)
{

	uint8_t *MSB_K = pK->pData;
	uint8_t *MSB_Q = pQ->pData;
	uint32_t buffer_lengths_in_bytes = pQ->dataLenInBytes;

	if (DONT_RUN_LESS_THAN_CHECK == *doCheck) {
		return FAIL_A_IS_GREATER_THAN_B;
	}

/*Check MSBs
if A == B, check next MSB
if A > B, return A_IS_GREATER_THAN_B
if A < B, return A_IS_LESS_THAN_B (success)
*/
	while (*MSB_K == *MSB_Q) {
		MSB_K++;
		MSB_Q++;

		buffer_lengths_in_bytes--;
		if (0 == buffer_lengths_in_bytes) {
			DPRINTK("%s() Buffers have equal value!!\n",
				__FUNCTION__);
			return FAIL_A_IS_EQUAL_TO_B;
		}

	}

	if (*MSB_K < *MSB_Q) {
		return SUCCESS_A_IS_LESS_THAN_B;
	} else {
		return FAIL_A_IS_GREATER_THAN_B;
	}

}

/* Name        : icp_ocfDrvDsaSign
 *
 * Description : This function will map DSA RS Sign from OCF to the LAC API.
 *
 * NOTE: From looking at OCF patch to OpenSSL and even the number of input
 * parameters, OCF expects us to generate the random seed value. This value
 * is generated and passed to LAC, however the number is discared in the
 * callback and not returned to the user.
 */
static int icp_ocfDrvDsaSign(struct cryptkop *krp)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	CpaCyDsaRSSignOpData *dsaRsSignOpData = NULL;
	void *callbackTag = NULL;
	CpaCyRandGenOpData randGenOpData;
	int primeQSizeInBytes = 0;
	int doCheck = 0;
	CpaFlatBuffer randData;
	CpaBoolean protocolStatus = CPA_FALSE;
	CpaFlatBuffer *pR = NULL;
	CpaFlatBuffer *pS = NULL;

	callbackTag = krp;

	BITS_TO_BYTES(primeQSizeInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].
		      crp_nbits);

	if (DSA_RS_SIGN_PRIMEQ_SIZE_IN_BYTES != primeQSizeInBytes) {
		APRINTK("%s(): DSA PRIME Q size not equal to the "
			"FIPS defined 20bytes, = %d\n",
			__FUNCTION__, primeQSizeInBytes);
		krp->krp_status = EDOM;
		return EDOM;
	}

	dsaRsSignOpData = kmem_cache_zalloc(drvDSARSSign_zone, GFP_KERNEL);
	if (NULL == dsaRsSignOpData) {
		APRINTK("%s():Failed to get memory"
			" for DSA RS Sign Op data struct\n", __FUNCTION__);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	dsaRsSignOpData->K.pData =
	    kmem_cache_alloc(drvDSARSSignKValue_zone, GFP_ATOMIC);

	if (NULL == dsaRsSignOpData->K.pData) {
		APRINTK("%s():Failed to get memory"
			" for DSA RS Sign Op Random value\n", __FUNCTION__);
		kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	pR = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
	if (NULL == pR) {
		APRINTK("%s():Failed to get memory"
			" for DSA signature R\n", __FUNCTION__);
		kmem_cache_free(drvDSARSSignKValue_zone,
				dsaRsSignOpData->K.pData);
		kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	pS = kmem_cache_zalloc(drvFlatBuffer_zone, GFP_KERNEL);
	if (NULL == pS) {
		APRINTK("%s():Failed to get memory"
			" for DSA signature S\n", __FUNCTION__);
		icp_ocfDrvFreeFlatBuffer(pR);
		kmem_cache_free(drvDSARSSignKValue_zone,
				dsaRsSignOpData->K.pData);
		kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	/*link prime number parameter for ease of processing */
	dsaRsSignOpData->P.pData =
	    krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX].crp_p;
	BITS_TO_BYTES(dsaRsSignOpData->P.dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_P_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(dsaRsSignOpData->P.pData,
			    dsaRsSignOpData->P.dataLenInBytes);

	dsaRsSignOpData->Q.pData =
	    krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].crp_p;
	BITS_TO_BYTES(dsaRsSignOpData->Q.dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_PRIME_Q_INDEX].
		      crp_nbits);

	icp_ocfDrvSwapBytes(dsaRsSignOpData->Q.pData,
			    dsaRsSignOpData->Q.dataLenInBytes);

	/*generate random number with equal buffer size to Prime value Q,
	   but value less than Q */
	dsaRsSignOpData->K.dataLenInBytes = dsaRsSignOpData->Q.dataLenInBytes;

	randGenOpData.generateBits = CPA_TRUE;
	randGenOpData.lenInBytes = dsaRsSignOpData->K.dataLenInBytes;

	icp_ocfDrvPtrAndLenToFlatBuffer(dsaRsSignOpData->K.pData,
					dsaRsSignOpData->K.dataLenInBytes,
					&randData);

	doCheck = 0;
	while (icp_ocfDrvCheckALessThanB(&(dsaRsSignOpData->K),
					 &(dsaRsSignOpData->Q), &doCheck)) {

		if (CPA_STATUS_SUCCESS
		    != cpaCyRandGen(CPA_INSTANCE_HANDLE_SINGLE,
				    NULL, NULL, &randGenOpData, &randData)) {
			APRINTK("%s(): ERROR - Failed to generate DSA RS Sign K"
				"value\n", __FUNCTION__);
			icp_ocfDrvFreeFlatBuffer(pS);
			icp_ocfDrvFreeFlatBuffer(pR);
			kmem_cache_free(drvDSARSSignKValue_zone,
					dsaRsSignOpData->K.pData);
			kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
			krp->krp_status = EAGAIN;
			return EAGAIN;
		}

		doCheck++;
		if (DSA_SIGN_RAND_GEN_VAL_CHECK_MAX_ITERATIONS == doCheck) {
			APRINTK("%s(): ERROR - Failed to find DSA RS Sign K "
				"value less than Q value\n", __FUNCTION__);
			icp_ocfDrvFreeFlatBuffer(pS);
			icp_ocfDrvFreeFlatBuffer(pR);
			kmem_cache_free(drvDSARSSignKValue_zone,
					dsaRsSignOpData->K.pData);
			kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
			krp->krp_status = EAGAIN;
			return EAGAIN;
		}

	}
	/*Rand Data - no need to swap bytes for pK */

	/* Link parameters */
	dsaRsSignOpData->G.pData =
	    krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_G_INDEX].crp_p;
	BITS_TO_BYTES(dsaRsSignOpData->G.dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_G_INDEX].crp_nbits);

	icp_ocfDrvSwapBytes(dsaRsSignOpData->G.pData,
			    dsaRsSignOpData->G.dataLenInBytes);

	dsaRsSignOpData->X.pData =
	    krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_X_INDEX].crp_p;
	BITS_TO_BYTES(dsaRsSignOpData->X.dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_X_INDEX].crp_nbits);
	icp_ocfDrvSwapBytes(dsaRsSignOpData->X.pData,
			    dsaRsSignOpData->X.dataLenInBytes);

	dsaRsSignOpData->M.pData =
	    krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX].crp_p;
	BITS_TO_BYTES(dsaRsSignOpData->M.dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_DGST_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaRsSignOpData->M.pData,
			    dsaRsSignOpData->M.dataLenInBytes);

	/* Output Parameters */
	pS->pData = krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX].crp_p;
	BITS_TO_BYTES(pS->dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_S_RESULT_INDEX].
		      crp_nbits);

	pR->pData = krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX].crp_p;
	BITS_TO_BYTES(pR->dataLenInBytes,
		      krp->krp_param[ICP_DSA_SIGN_KRP_PARAM_R_RESULT_INDEX].
		      crp_nbits);

	lacStatus = cpaCyDsaSignRS(CPA_INSTANCE_HANDLE_SINGLE,
				   icp_ocfDrvDsaRSSignCallBack,
				   callbackTag, dsaRsSignOpData,
				   &protocolStatus, pR, pS);

	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): DSA RS Sign Operation failed (%d).\n",
			__FUNCTION__, lacStatus);
		krp->krp_status = ECANCELED;
		icp_ocfDrvFreeFlatBuffer(pS);
		icp_ocfDrvFreeFlatBuffer(pR);
		kmem_cache_free(drvDSARSSignKValue_zone,
				dsaRsSignOpData->K.pData);
		kmem_cache_free(drvDSARSSign_zone, dsaRsSignOpData);
	}

	return lacStatus;
}

/* Name        : icp_ocfDrvDsaVerify
 *
 * Description : This function will map DSA RS Verify from OCF to the LAC API.
 *
 */
static int icp_ocfDrvDsaVerify(struct cryptkop *krp)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	CpaCyDsaVerifyOpData *dsaVerifyOpData = NULL;
	void *callbackTag = NULL;
	CpaBoolean verifyStatus = CPA_FALSE;

	callbackTag = krp;

	dsaVerifyOpData = kmem_cache_zalloc(drvDSAVerify_zone, GFP_KERNEL);
	if (NULL == dsaVerifyOpData) {
		APRINTK("%s():Failed to get memory"
			" for DSA Verify Op data struct\n", __FUNCTION__);
		krp->krp_status = ENOMEM;
		return ENOMEM;
	}

	/* Link parameters */
	dsaVerifyOpData->P.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->P.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_P_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->P.pData,
			    dsaVerifyOpData->P.dataLenInBytes);

	dsaVerifyOpData->Q.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->Q.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PRIME_Q_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->Q.pData,
			    dsaVerifyOpData->Q.dataLenInBytes);

	dsaVerifyOpData->G.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_G_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->G.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_G_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->G.pData,
			    dsaVerifyOpData->G.dataLenInBytes);

	dsaVerifyOpData->Y.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->Y.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_PUBKEY_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->Y.pData,
			    dsaVerifyOpData->Y.dataLenInBytes);

	dsaVerifyOpData->M.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->M.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_DGST_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->M.pData,
			    dsaVerifyOpData->M.dataLenInBytes);

	dsaVerifyOpData->R.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->R.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_R_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->R.pData,
			    dsaVerifyOpData->R.dataLenInBytes);

	dsaVerifyOpData->S.pData =
	    krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX].crp_p;
	BITS_TO_BYTES(dsaVerifyOpData->S.dataLenInBytes,
		      krp->krp_param[ICP_DSA_VERIFY_KRP_PARAM_SIG_S_INDEX].
		      crp_nbits);
	icp_ocfDrvSwapBytes(dsaVerifyOpData->S.pData,
			    dsaVerifyOpData->S.dataLenInBytes);

	lacStatus = cpaCyDsaVerify(CPA_INSTANCE_HANDLE_SINGLE,
				   icp_ocfDrvDsaVerifyCallBack,
				   callbackTag, dsaVerifyOpData, &verifyStatus);

	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): DSA Verify Operation failed (%d).\n",
			__FUNCTION__, lacStatus);
		kmem_cache_free(drvDSAVerify_zone, dsaVerifyOpData);
		krp->krp_status = ECANCELED;
	}

	return lacStatus;
}

/* Name        : icp_ocfDrvReadRandom
 *
 * Description : This function will map RNG functionality calls from OCF
 * to the LAC API.
 */
int icp_ocfDrvReadRandom(void *arg, uint32_t * buf, int maxwords)
{
	CpaStatus lacStatus = CPA_STATUS_SUCCESS;
	CpaCyRandGenOpData randGenOpData;
	CpaFlatBuffer randData;

	if (NULL == buf) {
		APRINTK("%s(): Invalid input parameters\n", __FUNCTION__);
		return EINVAL;
	}

	/* maxwords here is number of integers to generate data for */
	randGenOpData.generateBits = CPA_TRUE;

	randGenOpData.lenInBytes = maxwords * sizeof(uint32_t);

	icp_ocfDrvPtrAndLenToFlatBuffer((Cpa8U *) buf,
					randGenOpData.lenInBytes, &randData);

	lacStatus = cpaCyRandGen(CPA_INSTANCE_HANDLE_SINGLE,
				 NULL, NULL, &randGenOpData, &randData);
	if (CPA_STATUS_SUCCESS != lacStatus) {
		EPRINTK("%s(): icp_LacSymRandGen failed (%d). \n",
			__FUNCTION__, lacStatus);
		return RETURN_RAND_NUM_GEN_FAILED;
	}

	return randGenOpData.lenInBytes / sizeof(uint32_t);
}

/* Name        : icp_ocfDrvDhP1Callback
 *
 * Description : When this function returns it signifies that the LAC
 * component has completed the DH operation.
 */
static void
icp_ocfDrvDhP1CallBack(void *callbackTag,
		       CpaStatus status,
		       void *pOpData, CpaFlatBuffer * pLocalOctetStringPV)
{
	struct cryptkop *krp = NULL;
	CpaCyDhPhase1KeyGenOpData *pPhase1OpData = NULL;

	if (NULL == callbackTag) {
		DPRINTK("%s(): Invalid input parameters - "
			"callbackTag data is NULL\n", __FUNCTION__);
		return;
	}
	krp = (struct cryptkop *)callbackTag;

	if (NULL == pOpData) {
		DPRINTK("%s(): Invalid input parameters - "
			"Operation Data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}
	pPhase1OpData = (CpaCyDhPhase1KeyGenOpData *) pOpData;

	if (NULL == pLocalOctetStringPV) {
		DPRINTK("%s(): Invalid input parameters - "
			"pLocalOctetStringPV Data is NULL\n", __FUNCTION__);
		memset(pPhase1OpData, 0, sizeof(CpaCyDhPhase1KeyGenOpData));
		kmem_cache_free(drvDH_zone, pPhase1OpData);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}

	if (CPA_STATUS_SUCCESS == status) {
		krp->krp_status = CRYPTO_OP_SUCCESS;
	} else {
		APRINTK("%s(): Diffie Hellman Phase1 Key Gen failed - "
			"Operation Status = %d\n", __FUNCTION__, status);
		krp->krp_status = ECANCELED;
	}

	icp_ocfDrvSwapBytes(pLocalOctetStringPV->pData,
			    pLocalOctetStringPV->dataLenInBytes);

	icp_ocfDrvFreeFlatBuffer(pLocalOctetStringPV);
	memset(pPhase1OpData, 0, sizeof(CpaCyDhPhase1KeyGenOpData));
	kmem_cache_free(drvDH_zone, pPhase1OpData);

	crypto_kdone(krp);

	return;
}

/* Name        : icp_ocfDrvModExpCallBack
 *
 * Description : When this function returns it signifies that the LAC
 * component has completed the Mod Exp operation.
 */
static void
icp_ocfDrvModExpCallBack(void *callbackTag,
			 CpaStatus status,
			 void *pOpdata, CpaFlatBuffer * pResult)
{
	struct cryptkop *krp = NULL;
	CpaCyLnModExpOpData *pLnModExpOpData = NULL;

	if (NULL == callbackTag) {
		DPRINTK("%s(): Invalid input parameters - "
			"callbackTag data is NULL\n", __FUNCTION__);
		return;
	}
	krp = (struct cryptkop *)callbackTag;

	if (NULL == pOpdata) {
		DPRINTK("%s(): Invalid Mod Exp input parameters - "
			"Operation Data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}
	pLnModExpOpData = (CpaCyLnModExpOpData *) pOpdata;

	if (NULL == pResult) {
		DPRINTK("%s(): Invalid input parameters - "
			"pResult data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		memset(pLnModExpOpData, 0, sizeof(CpaCyLnModExpOpData));
		kmem_cache_free(drvLnModExp_zone, pLnModExpOpData);
		crypto_kdone(krp);
		return;
	}

	if (CPA_STATUS_SUCCESS == status) {
		krp->krp_status = CRYPTO_OP_SUCCESS;
	} else {
		APRINTK("%s(): LAC Mod Exp Operation failed - "
			"Operation Status = %d\n", __FUNCTION__, status);
		krp->krp_status = ECANCELED;
	}

	icp_ocfDrvSwapBytes(pResult->pData, pResult->dataLenInBytes);

	/*switch base size value back to original */
	if (pLnModExpOpData->base.pData ==
	    (uint8_t *) & (krp->
			   krp_param[ICP_MOD_EXP_KRP_PARAM_BASE_INDEX].
			   crp_nbits)) {
		*((uint32_t *) pLnModExpOpData->base.pData) =
		    ntohl(*((uint32_t *) pLnModExpOpData->base.pData));
	}
	icp_ocfDrvFreeFlatBuffer(pResult);
	memset(pLnModExpOpData, 0, sizeof(CpaCyLnModExpOpData));
	kmem_cache_free(drvLnModExp_zone, pLnModExpOpData);

	crypto_kdone(krp);

	return;

}

/* Name        : icp_ocfDrvModExpCRTCallBack
 *
 * Description : When this function returns it signifies that the LAC
 * component has completed the Mod Exp CRT operation.
 */
static void
icp_ocfDrvModExpCRTCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData, CpaFlatBuffer * pOutputData)
{
	struct cryptkop *krp = NULL;
	CpaCyRsaDecryptOpData *pDecryptData = NULL;

	if (NULL == callbackTag) {
		DPRINTK("%s(): Invalid input parameters - "
			"callbackTag data is NULL\n", __FUNCTION__);
		return;
	}

	krp = (struct cryptkop *)callbackTag;

	if (NULL == pOpData) {
		DPRINTK("%s(): Invalid input parameters - "
			"Operation Data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}
	pDecryptData = (CpaCyRsaDecryptOpData *) pOpData;

	if (NULL == pOutputData) {
		DPRINTK("%s(): Invalid input parameter - "
			"pOutputData is NULL\n", __FUNCTION__);
		memset(pDecryptData->pRecipientPrivateKey, 0,
		       sizeof(CpaCyRsaPrivateKey));
		kmem_cache_free(drvRSAPrivateKey_zone,
				pDecryptData->pRecipientPrivateKey);
		memset(pDecryptData, 0, sizeof(CpaCyRsaDecryptOpData));
		kmem_cache_free(drvRSADecrypt_zone, pDecryptData);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}

	if (CPA_STATUS_SUCCESS == status) {
		krp->krp_status = CRYPTO_OP_SUCCESS;
	} else {
		APRINTK("%s(): LAC Mod Exp CRT operation failed - "
			"Operation Status = %d\n", __FUNCTION__, status);
		krp->krp_status = ECANCELED;
	}

	icp_ocfDrvSwapBytes(pOutputData->pData, pOutputData->dataLenInBytes);

	icp_ocfDrvFreeFlatBuffer(pOutputData);
	memset(pDecryptData->pRecipientPrivateKey, 0,
	       sizeof(CpaCyRsaPrivateKey));
	kmem_cache_free(drvRSAPrivateKey_zone,
			pDecryptData->pRecipientPrivateKey);
	memset(pDecryptData, 0, sizeof(CpaCyRsaDecryptOpData));
	kmem_cache_free(drvRSADecrypt_zone, pDecryptData);

	crypto_kdone(krp);

	return;
}

/* Name        : icp_ocfDrvDsaRSSignCallBack
 *
 * Description : When this function returns it signifies that the LAC
 * component has completed the DSA RS sign operation.
 */
static void
icp_ocfDrvDsaRSSignCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData,
			    CpaBoolean protocolStatus,
			    CpaFlatBuffer * pR, CpaFlatBuffer * pS)
{
	struct cryptkop *krp = NULL;
	CpaCyDsaRSSignOpData *pSignData = NULL;

	if (NULL == callbackTag) {
		DPRINTK("%s(): Invalid input parameters - "
			"callbackTag data is NULL\n", __FUNCTION__);
		return;
	}

	krp = (struct cryptkop *)callbackTag;

	if (NULL == pOpData) {
		DPRINTK("%s(): Invalid input parameters - "
			"Operation Data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}
	pSignData = (CpaCyDsaRSSignOpData *) pOpData;

	if (NULL == pR) {
		DPRINTK("%s(): Invalid input parameter - "
			"pR sign is NULL\n", __FUNCTION__);
		icp_ocfDrvFreeFlatBuffer(pS);
		kmem_cache_free(drvDSARSSign_zone, pSignData);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}

	if (NULL == pS) {
		DPRINTK("%s(): Invalid input parameter - "
			"pS sign is NULL\n", __FUNCTION__);
		icp_ocfDrvFreeFlatBuffer(pR);
		kmem_cache_free(drvDSARSSign_zone, pSignData);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}

	if (CPA_STATUS_SUCCESS != status) {
		APRINTK("%s(): LAC DSA RS Sign operation failed - "
			"Operation Status = %d\n", __FUNCTION__, status);
		krp->krp_status = ECANCELED;
	} else {
		krp->krp_status = CRYPTO_OP_SUCCESS;

		if (CPA_TRUE != protocolStatus) {
			DPRINTK("%s(): LAC DSA RS Sign operation failed due "
				"to protocol error\n", __FUNCTION__);
			krp->krp_status = EIO;
		}
	}

	/* Swap bytes only when the callback status is successful and
	   protocolStatus is set to true */
	if (CPA_STATUS_SUCCESS == status && CPA_TRUE == protocolStatus) {
		icp_ocfDrvSwapBytes(pR->pData, pR->dataLenInBytes);
		icp_ocfDrvSwapBytes(pS->pData, pS->dataLenInBytes);
	}

	icp_ocfDrvFreeFlatBuffer(pR);
	icp_ocfDrvFreeFlatBuffer(pS);
	memset(pSignData->K.pData, 0, pSignData->K.dataLenInBytes);
	kmem_cache_free(drvDSARSSignKValue_zone, pSignData->K.pData);
	memset(pSignData, 0, sizeof(CpaCyDsaRSSignOpData));
	kmem_cache_free(drvDSARSSign_zone, pSignData);
	crypto_kdone(krp);

	return;
}

/* Name        : icp_ocfDrvDsaVerifyCallback
 *
 * Description : When this function returns it signifies that the LAC
 * component has completed the DSA Verify operation.
 */
static void
icp_ocfDrvDsaVerifyCallBack(void *callbackTag,
			    CpaStatus status,
			    void *pOpData, CpaBoolean verifyStatus)
{

	struct cryptkop *krp = NULL;
	CpaCyDsaVerifyOpData *pVerData = NULL;

	if (NULL == callbackTag) {
		DPRINTK("%s(): Invalid input parameters - "
			"callbackTag data is NULL\n", __FUNCTION__);
		return;
	}

	krp = (struct cryptkop *)callbackTag;

	if (NULL == pOpData) {
		DPRINTK("%s(): Invalid input parameters - "
			"Operation Data is NULL\n", __FUNCTION__);
		krp->krp_status = ECANCELED;
		crypto_kdone(krp);
		return;
	}
	pVerData = (CpaCyDsaVerifyOpData *) pOpData;

	if (CPA_STATUS_SUCCESS != status) {
		APRINTK("%s(): LAC DSA Verify operation failed - "
			"Operation Status = %d\n", __FUNCTION__, status);
		krp->krp_status = ECANCELED;
	} else {
		krp->krp_status = CRYPTO_OP_SUCCESS;

		if (CPA_TRUE != verifyStatus) {
			DPRINTK("%s(): DSA signature invalid\n", __FUNCTION__);
			krp->krp_status = EIO;
		}
	}

	/* Swap bytes only when the callback status is successful and
	   verifyStatus is set to true */
	/*Just swapping back the key values for now. Possibly all
	   swapped buffers need to be reverted */
	if (CPA_STATUS_SUCCESS == status && CPA_TRUE == verifyStatus) {
		icp_ocfDrvSwapBytes(pVerData->R.pData,
				    pVerData->R.dataLenInBytes);
		icp_ocfDrvSwapBytes(pVerData->S.pData,
				    pVerData->S.dataLenInBytes);
	}

	memset(pVerData, 0, sizeof(CpaCyDsaVerifyOpData));
	kmem_cache_free(drvDSAVerify_zone, pVerData);
	crypto_kdone(krp);

	return;
}