-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpsp-dev-ccp-v5.c
2362 lines (2099 loc) · 84.9 KB
/
psp-dev-ccp-v5.c
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
/** @file
* PSP Emulator - CCPv5 device.
*/
/*
* Copyright (C) 2020 Alexander Eichner <alexander.eichner@campus.tu-berlin.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/** @page pg_dev_ccp_v5 CCPv5 - Cryptographic Co-Processor version 5
*
* @todo Write something here.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/ec.h>
#include <openssl/err.h>
/* OpenSSL version 1.0.x support (see https://www.openssl.org/docs/man1.1.0/man3/EVP_MD_CTX_new.html#HISTORY) */
# if OPENSSL_VERSION_NUMBER < 0x10100000 // = OpenSSL 1.1.0
# define EVP_MD_CTX_new EVP_MD_CTX_create
# define EVP_MD_CTX_free EVP_MD_CTX_destroy
# endif
#include <zlib.h>
/* Missing in zlib.h */
# ifndef Z_DEF_WBITS
# define Z_DEF_WBITS MAX_WBITS
# endif
#include <common/cdefs.h>
#include <common/status.h>
#include <psp/ccp.h>
#include <psp-devs.h>
#include <psp-trace.h>
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** Address type the CCP uses (created from low and high parts). */
typedef uint64_t CCPADDR;
/** Create a CCP address from the given low and high parts. */
#define CCP_ADDR_CREATE_FROM_HI_LO(a_High, a_Low) (((CCPADDR)(a_High) << 32) | (a_Low))
/**
* A single CCP queue.
*/
typedef struct CCPQUEUE
{
/** Control register. */
uint32_t u32RegCtrl;
/** Request descriptor tail pointer. */
uint32_t u32RegReqTail;
/** Request descriptor head pointer. */
uint32_t u32RegReqHead;
/** Request status register. */
uint32_t u32RegSts;
/** Interrupt enable register. */
uint32_t u32RegIen;
/** Interrupt status register. */
uint32_t u32RegIsts;
/** Flag whether the queue was enabled by setting the run bit. */
bool fEnabled;
} CCPQUEUE;
/** Pointer to a single CCP queue. */
typedef CCPQUEUE *PCCPQUEUE;
/** Pointer to a single const CCP queue. */
typedef const CCPQUEUE *PCCCPQUEUE;
/**
* A single local storage buffer.
*/
typedef struct CCPLSB
{
/** View dependent data. */
union
{
/** A single slot. */
struct
{
/** 32byte data. */
uint8_t abData[32];
} aSlots[128];
/* Contiguous view of the complete LSB. */
uint8_t abLsb[1];
} u;
} CCPLSB;
/** Pointer to a local storage buffer. */
typedef CCPLSB *PCCPLSB;
/** Pointer to a const local storage buffer. */
typedef const CCPLSB *PCCCPLSB;
/**
* CCP device instance data.
*/
typedef struct PSPDEVCCP
{
/** Pointer to device instance. */
PPSPDEV pDev;
/** MMIO region handle. */
PSPIOMREGIONHANDLE hMmio;
/** MMIO2 region handle. */
PSPIOMREGIONHANDLE hMmio2;
/** The CCP queues. */
CCPQUEUE aQueues[2];
/** The local storage buffer. */
CCPLSB Lsb;
/** The openssl SHA context currently in use. This doesn't really belong here
* as the state is contained in an LSB but for use with openssl and to support
* multi-part messages we have to store it here, luckily the PSP is single threaded
* so the code will only every process one SHA operation at a time.
*/
EVP_MD_CTX *pOsslShaCtx;
/** The openssl AES context currently in use, same note as above applies. */
EVP_CIPHER_CTX *pOsslAesCtx;
/** The zlib decompression state. */
z_stream Zlib;
/** Size of the last transfer in bytes (written to local PSP memory). */
size_t cbWrittenLast;
} PSPDEVCCP;
/** Pointer to the device instance data. */
typedef PSPDEVCCP *PPSPDEVCCP;
/**
* Data transfer context.
*/
typedef struct CCPXFERCTX
{
/** The read callback. */
int (*pfnRead) (PPSPDEVCCP pThis, CCPADDR CcpAddr, void *pvDst, size_t cbRead);
/** The write callback. */
int (*pfnWrite) (PPSPDEVCCP pThis, CCPADDR CcpAddr, const void *pvSrc, size_t cbWrite);
/** The CCP device instance the context is for. */
PPSPDEVCCP pThis;
/** Current source address. */
CCPADDR CcpAddrSrc;
/** Amount of data to read left. */
size_t cbReadLeft;
/** Current destination address. */
CCPADDR CcpAddrDst;
/** Amount of data to write left. */
size_t cbWriteLeft;
/** Flag whether to write in reverse order. */
bool fWriteRev;
} CCPXFERCTX;
/** Pointer to an xfer context. */
typedef CCPXFERCTX *PCCPXFERCTX;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
/**
* Transfer data from system memory to a local buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to read from (x86 physical address).
* @param pvDst Where to store the read data.
* @param cbRead How much to read.
*/
static int pspDevCcpXferMemSysRead(PPSPDEVCCP pThis, CCPADDR CcpAddr, void *pvDst, size_t cbRead)
{
return -1;
}
/**
* Transfer data from a local buffer to system memory.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to write to (x86 physical address).
* @param pvSrc The data to write.
* @param cbWrite How much to write.
*/
static int pspDevCcpXferMemSysWrite(PPSPDEVCCP pThis, CCPADDR CcpAddr, const void *pvSrc, size_t cbWrite)
{
return -1;
}
/**
* Transfer data from a local storage buffer to a local buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to read from (LSB address).
* @param pvDst Where to store the read data.
* @param cbRead How much to read.
*/
static int pspDevCcpXferMemLsbRead(PPSPDEVCCP pThis, CCPADDR CcpAddr, void *pvDst, size_t cbRead)
{
int rc = 0;
if ( CcpAddr < sizeof(pThis->Lsb)
&& CcpAddr + cbRead <= sizeof(pThis->Lsb))
memcpy(pvDst, &pThis->Lsb.u.abLsb[CcpAddr], cbRead);
else
{
printf("CCP: Invalid LSB read offset=%#x cbRead=%zu\n", (uint32_t)CcpAddr, cbRead);
rc = -1;
}
return rc;
}
/**
* Transfer data from a local buffer to a local storage buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to write to (LSB address).
* @param pvSrc The data to write.
* @param cbWrite How much to write.
*/
static int pspDevCcpXferMemLsbWrite(PPSPDEVCCP pThis, CCPADDR CcpAddr, const void *pvSrc, size_t cbWrite)
{
int rc = 0;
if ( CcpAddr < sizeof(pThis->Lsb)
&& CcpAddr + cbWrite <= sizeof(pThis->Lsb))
memcpy(&pThis->Lsb.u.abLsb[CcpAddr], pvSrc, cbWrite);
else
{
printf("CCP: Invalid LSB write offset=%#x cbWrite=%zu\n", (uint32_t)CcpAddr, cbWrite);
rc = -1;
}
return rc;
}
/**
* Transfer data from a local PSP memory address (SRAM,MMIO) to a local buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to read from (PSP address).
* @param pvDst Where to store the read data.
* @param cbRead How much to read.
*/
static int pspDevCcpXferMemLocalRead(PPSPDEVCCP pThis, CCPADDR CcpAddr, void *pvDst, size_t cbRead)
{
return PSPEmuIoMgrPspAddrRead(pThis->pDev->hIoMgr, (uint32_t)CcpAddr, pvDst, cbRead);
}
/**
* Transfer data from a local buffer to a local PSP memory address (SRAM,MMIO).
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddr The address to write to (PSP address).
* @param pvSrc The data to write.
* @param cbWrite How much to write.
*/
static int pspDevCcpXferMemLocalWrite(PPSPDEVCCP pThis, CCPADDR CcpAddr, const void *pvSrc, size_t cbWrite)
{
int rc = PSPEmuIoMgrPspAddrWrite(pThis->pDev->hIoMgr, (uint32_t)CcpAddr, pvSrc, cbWrite);
if (!rc)
pThis->cbWrittenLast += cbWrite;
return rc;
}
/**
* Initializes a data transfer context.
*
* @returns Status code.
* @param pCtx The transfer context to initialize.
* @param pThis The CCP device instance data.
* @param pReq The CCP request to take memory types from.
* @param fSha Flag whether this context is for the SHA engine.
* @param cbWrite Amount of bytes to write in total.
* @param fWriteRev Flag whether to write the data in reverse order.
*/
static int pspDevCcpXferCtxInit(PCCPXFERCTX pCtx, PPSPDEVCCP pThis, PCCCP5REQ pReq, bool fSha, size_t cbWrite,
bool fWriteRev)
{
pThis->cbWrittenLast = 0;
pCtx->pThis = pThis;
pCtx->CcpAddrSrc = CCP_ADDR_CREATE_FROM_HI_LO(pReq->u16AddrSrcHigh, pReq->u32AddrSrcLow);
pCtx->cbReadLeft = pReq->cbSrc;
pCtx->fWriteRev = fWriteRev;
switch (CCP_V5_MEM_TYPE_GET(pReq->u16SrcMemType))
{
case CCP_V5_MEM_TYPE_SYSTEM:
pCtx->pfnRead = pspDevCcpXferMemSysRead;
break;
case CCP_V5_MEM_TYPE_SB:
pCtx->pfnRead = pspDevCcpXferMemLsbRead;
break;
case CCP_V5_MEM_TYPE_LOCAL:
pCtx->pfnRead = pspDevCcpXferMemLocalRead;
break;
default:
return -1;
}
pCtx->cbWriteLeft = cbWrite;
if (!fSha)
{
pCtx->CcpAddrDst = CCP_ADDR_CREATE_FROM_HI_LO(pReq->Op.NonSha.u16AddrDstHigh, pReq->Op.NonSha.u32AddrDstLow);
switch (CCP_V5_MEM_TYPE_GET(pReq->Op.NonSha.u16DstMemType))
{
case CCP_V5_MEM_TYPE_SYSTEM:
pCtx->pfnWrite = pspDevCcpXferMemSysWrite;
break;
case CCP_V5_MEM_TYPE_SB:
pCtx->pfnWrite = pspDevCcpXferMemLsbWrite;
break;
case CCP_V5_MEM_TYPE_LOCAL:
pCtx->pfnWrite = pspDevCcpXferMemLocalWrite;
break;
default:
return -1;
}
}
else /* SHA always writes to the LSB. */
{
uint8_t uLsbCtxId = CCP_V5_MEM_LSB_CTX_ID_GET(pReq->u16SrcMemType);
if (uLsbCtxId < ELEMENTS(pThis->Lsb.u.aSlots))
{
pCtx->pfnWrite = pspDevCcpXferMemLsbWrite;
pCtx->CcpAddrDst = uLsbCtxId * sizeof(pThis->Lsb.u.aSlots[0].abData);
}
else
return -1;
}
if (pCtx->fWriteRev)
pCtx->CcpAddrDst += pCtx->cbWriteLeft;
return 0;
}
/**
* Executes a read pass using the given transfer context.
*
* @returns Status code.
* @param pCtx The transfer context to use.
* @param pvDst Where to store the read data.
* @param cbRead How much to read.
* @param pcbRead Where to store the amount of data actually read, optional.
*/
static int pspDevCcpXferCtxRead(PCCPXFERCTX pCtx, void *pvDst, size_t cbRead, size_t *pcbRead)
{
int rc = 0;
size_t cbThisRead = MIN(cbRead, pCtx->cbReadLeft);
if ( cbThisRead
&& ( pcbRead
|| cbThisRead == cbRead))
{
rc = pCtx->pfnRead(pCtx->pThis, pCtx->CcpAddrSrc, pvDst, cbThisRead);
if (!rc)
{
pCtx->cbReadLeft -= cbThisRead;
pCtx->CcpAddrSrc += cbThisRead;
if (pcbRead)
*pcbRead = cbThisRead;
}
}
else
rc = -1;
return rc;
}
/**
* Executes a write pass using the given transfer context.
*
* @returns Status code.
* @param pCtx The transfer context to use.
* @param pvSrc The data to write.
* @param cbWrite How much to write.
* @param pcbWritten Where to store the amount of data actually written, optional.
*/
static int pspDevCcpXferCtxWrite(PCCPXFERCTX pCtx, const void *pvSrc, size_t cbWrite, size_t *pcbWritten)
{
int rc = 0;
size_t cbThisWrite = MIN(cbWrite, pCtx->cbWriteLeft);
if ( cbThisWrite
&& ( pcbWritten
|| cbThisWrite == cbWrite))
{
if (pCtx->fWriteRev)
{
const uint8_t *pbSrc = (const uint8_t *)pvSrc;
/** @todo Unoptimized single byte writes... */
while ( cbThisWrite
&& !rc)
{
pCtx->CcpAddrDst--;
rc = pCtx->pfnWrite(pCtx->pThis, pCtx->CcpAddrDst, pbSrc, 1);
cbThisWrite--;
pbSrc++;
}
if ( !rc
&& pcbWritten)
*pcbWritten = cbThisWrite;
}
else
{
rc = pCtx->pfnWrite(pCtx->pThis, pCtx->CcpAddrDst, pvSrc, cbThisWrite);
if (!rc)
{
pCtx->cbWriteLeft -= cbThisWrite;
pCtx->CcpAddrDst += cbThisWrite;
if (pcbWritten)
*pcbWritten = cbThisWrite;
}
}
}
else
rc = -1;
return rc;
}
/**
* Reverses the data in the given buffer.
*
* @returns nothing.
* @param pbBuf The buffer to reverse the data in.
* @param cbBuf Size of the buffer to reverse.
*/
static void pspDevCcpReverseBuf(uint8_t *pbBuf, size_t cbBuf)
{
uint8_t *pbBufTop = pbBuf + cbBuf - 1;
while (pbBuf < pbBufTop)
{
uint8_t bTmp = *pbBuf;
*pbBuf = *pbBufTop;
*pbBufTop = bTmp;
pbBuf++;
pbBufTop--;
}
}
/**
* Copies the key material pointed to by the request into a supplied buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param pReq The request to get the key address from.
* @param cbKey Size of the key buffer.
* @param pvKey Where to store the key material.
*/
static int pspDevCcpKeyCopyFromReq(PPSPDEVCCP pThis, PCCCP5REQ pReq, size_t cbKey, void *pvKey)
{
int rc = 0;
if (CCP_V5_MEM_TYPE_GET(pReq->u16KeyMemType) == CCP_V5_MEM_TYPE_LOCAL)
{
CCPADDR CcpAddrKey = CCP_ADDR_CREATE_FROM_HI_LO(pReq->u16AddrKeyHigh, pReq->u32AddrKeyLow);
rc = pspDevCcpXferMemLocalRead(pThis, CcpAddrKey, pvKey, cbKey);
}
else if (CCP_V5_MEM_TYPE_GET(pReq->u16KeyMemType) == CCP_V5_MEM_TYPE_SB)
{
CCPADDR CcpAddrKey = CCP_ADDR_CREATE_FROM_HI_LO(pReq->u16AddrKeyHigh, pReq->u32AddrKeyLow);
if ( CcpAddrKey < sizeof(pThis->Lsb)
&& CcpAddrKey + cbKey <= sizeof(pThis->Lsb))
memcpy(pvKey, &pThis->Lsb.u.abLsb[CcpAddrKey], cbKey);
else
rc = -1;
}
return rc;
}
/**
* Copies data from an LSB into a supplied buffer.
*
* @returns Status code.
* @param pThis The CCP device instance data.
* @param CcpAddrLsb CCP LSB address to copy from.
* @param cb Amount of bytes to copy.
* @param pv Where to store the data.
*/
static int pspDevCcpCopyFromLsb(PPSPDEVCCP pThis, CCPADDR CcpAddrLsb, size_t cb, void *pv)
{
int rc = 0;
if ( CcpAddrLsb < sizeof(pThis->Lsb)
&& CcpAddrLsb + cb <= sizeof(pThis->Lsb))
memcpy(pv, &pThis->Lsb.u.abLsb[CcpAddrLsb], cb);
else
rc = -1;
return rc;
}
/**
* Returns the string representation of the given CCP request engine field.
*
* @returns Engine description.
* @param uEngine The engine to convert to a stirng.
*/
static const char *pspDevCcpReqEngineToStr(uint32_t uEngine)
{
switch (uEngine)
{
case CCP_V5_ENGINE_AES:
return "AES";
case CCP_V5_ENGINE_XTS_AES128:
return "XTS_AES_128";
case CCP_V5_ENGINE_DES3:
return "DES3";
case CCP_V5_ENGINE_SHA:
return "SHA";
case CCP_V5_ENGINE_RSA:
return "RSA";
case CCP_V5_ENGINE_PASSTHRU:
return "PASSTHROUGH";
case CCP_V5_ENGINE_ZLIB_DECOMP:
return "ZLIB_DECOMPRESS";
case CCP_V5_ENGINE_ECC:
return "ECC";
}
return "<INVALID>";
}
/**
* Extracts and dumps information about the given AES function.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param uFunc The function part of dword 0.
* @param u32Dw0Raw The raw dw0 value used for dumping.
* @param pszEngine The used engine string.
*/
static void pspDevCcpReqDumpAesFunction(char *pszBuf, size_t cbBuf, uint32_t uFunc,
uint32_t u32Dw0Raw, const char *pszEngine)
{
uint8_t uSz = CCP_V5_ENGINE_AES_SZ_GET(uFunc);
uint8_t fEncrypt = CCP_V5_ENGINE_AES_ENCRYPT_GET(uFunc);
uint8_t uMode = CCP_V5_ENGINE_AES_MODE_GET(uFunc);
uint8_t uAesType = CCP_V5_ENGINE_AES_TYPE_GET(uFunc);
const char *pszMode = "<INVALID>";
const char *pszAesType = "<INVALID>";
switch (uMode)
{
case CCP_V5_ENGINE_AES_MODE_ECB:
pszMode = "ECB";
break;
case CCP_V5_ENGINE_AES_MODE_CBC:
pszMode = "CBC";
break;
case CCP_V5_ENGINE_AES_MODE_OFB:
pszMode = "OFB";
break;
case CCP_V5_ENGINE_AES_MODE_CFB:
pszMode = "CFB";
break;
case CCP_V5_ENGINE_AES_MODE_CTR:
pszMode = "CTR";
break;
case CCP_V5_ENGINE_AES_MODE_CMAC:
pszMode = "CMAC";
break;
case CCP_V5_ENGINE_AES_MODE_GHASH:
pszMode = "GHASH";
break;
case CCP_V5_ENGINE_AES_MODE_GCTR:
pszMode = "GCTR";
break;
case CCP_V5_ENGINE_AES_MODE_GCM:
pszMode = "GCM";
break;
case CCP_V5_ENGINE_AES_MODE_GMAC:
pszMode = "GMAC";
break;
}
switch (uAesType)
{
case CCP_V5_ENGINE_AES_TYPE_128:
pszAesType = "AES128";
break;
case CCP_V5_ENGINE_AES_TYPE_192:
pszAesType = "AES192";
break;
case CCP_V5_ENGINE_AES_TYPE_256:
pszAesType = "AES256";
break;
}
snprintf(pszBuf, cbBuf, "u32Dw0: 0x%08x (Engine: %s, AES Type: %s, Mode: %s, Encrypt: %u, Size: %u)",
u32Dw0Raw, pszEngine, pszAesType, pszMode, fEncrypt, uSz);
}
/**
* Extracts and dumps information about the given SHA function.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param uFunc The function part of dword 0.
* @param u32Dw0Raw The raw dw0 value used for dumping.
* @param pszEngine The used engine string.
*/
static void pspDevCcpReqDumpShaFunction(char *pszBuf, size_t cbBuf, uint32_t uFunc,
uint32_t u32Dw0Raw, const char *pszEngine, bool fInit, bool fEom)
{
uint32_t uShaType = CCP_V5_ENGINE_SHA_TYPE_GET(uFunc);
const char *pszShaType = "<INVALID>";
switch (uShaType)
{
case CCP_V5_ENGINE_SHA_TYPE_1:
pszShaType = "SHA1";
break;
case CCP_V5_ENGINE_SHA_TYPE_224:
pszShaType = "SHA224";
break;
case CCP_V5_ENGINE_SHA_TYPE_256:
pszShaType = "SHA256";
break;
case CCP_V5_ENGINE_SHA_TYPE_384:
pszShaType = "SHA384";
break;
case CCP_V5_ENGINE_SHA_TYPE_512:
pszShaType = "SHA512";
break;
}
snprintf(pszBuf, cbBuf, "u32Dw0: 0x%08x (Engine: %s, Init: %u, Eom: %u, SHA type: %s)",
u32Dw0Raw, pszEngine, fInit, fEom, pszShaType);
}
/**
* Extracts and dumps information about the given PASSTHRU function.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param uFunc The function part of dword 0.
* @param u32Dw0Raw The raw dw0 value used for dumping.
* @param pszEngine The used engine string.
*/
static void pspDevCcpReqDumpPassthruFunction(char *pszBuf, size_t cbBuf, uint32_t uFunc,
uint32_t u32Dw0Raw, const char *pszEngine)
{
uint8_t uByteSwap = CCP_V5_ENGINE_PASSTHRU_BYTESWAP_GET(uFunc);
uint8_t uBitwise = CCP_V5_ENGINE_PASSTHRU_BITWISE_GET(uFunc);
uint8_t uReflect = CCP_V5_ENGINE_PASSTHRU_REFLECT_GET(uFunc);
const char *pszByteSwap = "<INVALID>";
const char *pszBitwise = "<INVALID>";
switch (uByteSwap)
{
case CCP_V5_ENGINE_PASSTHRU_BYTESWAP_NOOP:
pszByteSwap = "NOOP";
break;
case CCP_V5_ENGINE_PASSTHRU_BYTESWAP_32BIT:
pszByteSwap = "32BIT";
break;
case CCP_V5_ENGINE_PASSTHRU_BYTESWAP_256BIT:
pszByteSwap = "256BIT";
break;
}
switch (uBitwise)
{
case CCP_V5_ENGINE_PASSTHRU_BITWISE_NOOP:
pszBitwise = "NOOP";
break;
case CCP_V5_ENGINE_PASSTHRU_BITWISE_AND:
pszBitwise = "AND";
break;
case CCP_V5_ENGINE_PASSTHRU_BITWISE_OR:
pszBitwise = "OR";
break;
case CCP_V5_ENGINE_PASSTHRU_BITWISE_XOR:
pszBitwise = "XOR";
break;
case CCP_V5_ENGINE_PASSTHRU_BITWISE_MASK:
pszBitwise = "MASK";
break;
}
snprintf(pszBuf, cbBuf, "u32Dw0: 0x%08x (Engine: %s, ByteSwap: %s, Bitwise: %s, Reflect: %#x)",
u32Dw0Raw, pszEngine, pszByteSwap, pszBitwise, uReflect);
}
/**
* Extracts and dumps information about the given RSA function.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param uFunc The function part of dword 0.
* @param u32Dw0Raw The raw dw0 value used for dumping.
* @param pszEngine The used engine string.
*/
static void pspDevCcpReqDumpRsaFunction(char *pszBuf, size_t cbBuf, uint32_t uFunc,
uint32_t u32Dw0Raw, const char *pszEngine)
{
uint16_t uSz = CCP_V5_ENGINE_RSA_SZ_GET(uFunc);
uint8_t uMode = CCP_V5_ENGINE_RSA_MODE_GET(uFunc);
snprintf(pszBuf, cbBuf, "u32Dw0: 0x%08x (Engine: %s, Mode: %u, Size: %u)",
u32Dw0Raw, pszEngine, uMode, uSz);
}
/**
* Extracts and dumps information about the given ECC function.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param uFunc The function part of dword 0.
* @param u32Dw0Raw The raw dw0 value used for dumping.
* @param pszEngine The used engine string.
*/
static void pspDevCcpReqDumpEccFunction(char *pszBuf, size_t cbBuf, uint32_t uFunc,
uint32_t u32Dw0Raw, const char *pszEngine)
{
uint8_t uOp = CCP_V5_ENGINE_ECC_OP_GET(uFunc);
uint16_t uBits = CCP_V5_ENGINE_ECC_BIT_COUNT_GET(uFunc);
snprintf(pszBuf, cbBuf, "u32Dw0: 0x%08x (Engine: %s, Op: %u, Bits: %u)",
u32Dw0Raw, pszEngine, uOp, uBits);
}
/**
* Dumps the CCP5 request descriptor.
*
* @returns nothing.
* @param pReq The request to dump.
*/
static void pspDevCcpDumpReq(PCCCP5REQ pReq, PSPADDR PspAddrReq)
{
uint32_t uEngine = CCP_V5_ENGINE_GET(pReq->u32Dw0);
uint32_t uFunction = CCP_V5_ENGINE_FUNC_GET(pReq->u32Dw0);
bool fInit = CCP_V5_ENGINE_INIT_GET(pReq->u32Dw0);
bool fEom = CCP_V5_ENGINE_EOM_GET(pReq->u32Dw0);
const char *pszEngine = pspDevCcpReqEngineToStr(uEngine);
char szDw0[512];
if (uEngine == CCP_V5_ENGINE_AES)
pspDevCcpReqDumpAesFunction(&szDw0[0], sizeof(szDw0), uFunction, pReq->u32Dw0, pszEngine);
else if (uEngine == CCP_V5_ENGINE_SHA)
pspDevCcpReqDumpShaFunction(&szDw0[0], sizeof(szDw0), uFunction, pReq->u32Dw0, pszEngine, fInit, fEom);
else if (uEngine == CCP_V5_ENGINE_PASSTHRU)
pspDevCcpReqDumpPassthruFunction(&szDw0[0], sizeof(szDw0), uFunction, pReq->u32Dw0, pszEngine);
else if (uEngine == CCP_V5_ENGINE_RSA)
pspDevCcpReqDumpRsaFunction(&szDw0[0], sizeof(szDw0), uFunction, pReq->u32Dw0, pszEngine);
else if (uEngine == CCP_V5_ENGINE_ECC)
pspDevCcpReqDumpEccFunction(&szDw0[0], sizeof(szDw0), uFunction, pReq->u32Dw0, pszEngine);
else
snprintf(&szDw0[0], sizeof(szDw0), "u32Dw0: 0x%08x (Engine: %s)",
pReq->u32Dw0, pszEngine);
if (uEngine != CCP_V5_ENGINE_SHA)
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP Request 0x%08x:\n"
" %s\n"
" cbSrc: %u\n"
" u32AddrSrcLow: 0x%08x\n"
" u16AddrSrcHigh: 0x%08x\n"
" u16SrcMemType: 0x%08x (MemType: %u, LsbCtxId: %u, Fixed: %u)\n"
" u32AddrDstLow: 0x%08x\n"
" u16AddrDstHigh: 0x%08x\n"
" u16DstMemType: 0x%08x (MemType: %u, Fixed: %u)\n"
" u32AddrKeyLow: 0x%08x\n"
" u16AddrKeyHigh: 0x%08x\n"
" u16KeyMemType: 0x%08x\n",
PspAddrReq, &szDw0[0], pReq->cbSrc, pReq->u32AddrSrcLow, pReq->u16AddrSrcHigh,
pReq->u16SrcMemType, CCP_V5_MEM_TYPE_GET(pReq->u16SrcMemType),
CCP_V5_MEM_LSB_CTX_ID_GET(pReq->u16SrcMemType), CCP_V5_MEM_LSB_FIXED_GET(pReq->u16SrcMemType),
pReq->Op.NonSha.u32AddrDstLow, pReq->Op.NonSha.u16AddrDstHigh,
pReq->Op.NonSha.u16DstMemType, CCP_V5_MEM_TYPE_GET(pReq->Op.NonSha.u16DstMemType),
CCP_V5_MEM_LSB_FIXED_GET(pReq->Op.NonSha.u16DstMemType),
pReq->u32AddrKeyLow, pReq->u16AddrKeyHigh, pReq->u16KeyMemType);
else
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP Request 0x%08x:\n"
" %s\n"
" cbSrc: %u\n"
" u32AddrSrcLow: 0x%08x\n"
" u16AddrSrcHigh: 0x%08x\n"
" u16SrcMemType: 0x%08x (MemType: %u, LsbCtxId: %u, Fixed: %u)\n"
" u32ShaBitsLow: 0x%08x\n"
" u32ShaBitsHigh: 0x%08x\n"
" u32AddrKeyLow: 0x%08x\n"
" u16AddrKeyHigh: 0x%08x\n"
" u16KeyMemType: 0x%08x\n",
PspAddrReq, &szDw0[0], pReq->cbSrc, pReq->u32AddrSrcLow, pReq->u16AddrSrcHigh,
pReq->u16SrcMemType, CCP_V5_MEM_TYPE_GET(pReq->u16SrcMemType),
CCP_V5_MEM_LSB_CTX_ID_GET(pReq->u16SrcMemType), CCP_V5_MEM_LSB_FIXED_GET(pReq->u16SrcMemType),
pReq->Op.Sha.u32ShaBitsLow, pReq->Op.Sha.u32ShaBitsHigh,
pReq->u32AddrKeyLow, pReq->u16AddrKeyHigh, pReq->u16KeyMemType);
}
/**
* Dump ecc number.
*
* @returns nothing.
* @param pszBuf The buffer to dump into.
* @param cbBuf Size of the buffer in bytes.
* @param pNum The ecc number to dump.
*/
static void pspDevCcpDumpEccNumber(char *pszBuf, size_t cbBuf, PCCCP5ECCNUM pNum)
{
const uint64_t *pu64Num = (const uint64_t *)pNum;
snprintf(pszBuf, cbBuf,
"0x%016llx_%016llx_%016llx_%016llx"
"_%016llx_%016llx_%016llx_%016llx"
"_%016llx",
pu64Num[8], pu64Num[7], pu64Num[6], pu64Num[5],
pu64Num[4], pu64Num[3], pu64Num[2], pu64Num[1],
pu64Num[0]
);
}
/**
* Dumps the ecc request data for a request.
*
* @returns nothing.
* @param uOp The ECC operation.
* @param pEccReq The ECC request data.
*/
static void pspDevCcpDumpEccReq(uint8_t uOp, PCCCP5ECCREQ pEccReq)
{
char szPrime[256];
pspDevCcpDumpEccNumber(szPrime, sizeof(szPrime), &pEccReq->Prime);
switch (uOp)
{
case CCP_V5_ENGINE_ECC_OP_MUL_FIELD:
{
char szFactor1[256], szFactor2[256];
pspDevCcpDumpEccNumber(szFactor1, sizeof(szFactor1), &pEccReq->Op.FieldMul.Factor1);
pspDevCcpDumpEccNumber(szFactor2, sizeof(szFactor2), &pEccReq->Op.FieldMul.Factor2);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Field Multiplication):\n"
" Prime: %s\n"
" Factor1: %s\n"
" Factor2: %s\n",
szPrime, szFactor1, szFactor2);
break;
}
case CCP_V5_ENGINE_ECC_OP_ADD_FIELD:
{
char szSummand1[256], szSummand2[256];
pspDevCcpDumpEccNumber(szSummand1, sizeof(szSummand1), &pEccReq->Op.FieldAdd.Summand1);
pspDevCcpDumpEccNumber(szSummand2, sizeof(szSummand2), &pEccReq->Op.FieldAdd.Summand2);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Field Addition):\n"
" Prime: %s\n"
" Summand1: %s\n"
" Summand2: %s\n",
szPrime, szSummand1, szSummand2);
break;
}
case CCP_V5_ENGINE_ECC_OP_INV_FIELD:
{
char szNumber[256];
pspDevCcpDumpEccNumber(szNumber, sizeof(szNumber),
&pEccReq->Op.FieldInv.Num);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Field Inversion):\n"
" Prime: %s\n"
" Number: %s\n",
szPrime, szNumber);
break;
}
case CCP_V5_ENGINE_ECC_OP_MUL_CURVE:
{
char szFactor[256];
char szPointX[256];
char szPointY[256];
char szCoefficient[256];
pspDevCcpDumpEccNumber(szFactor, sizeof(szFactor), &pEccReq->Op.CurveMul.Factor);
pspDevCcpDumpEccNumber(szPointX, sizeof(szPointX), &pEccReq->Op.CurveMul.Point.X);
pspDevCcpDumpEccNumber(szPointY, sizeof(szPointY), &pEccReq->Op.CurveMul.Point.Y);
pspDevCcpDumpEccNumber(szCoefficient, sizeof(szCoefficient), &pEccReq->Op.CurveMul.Coefficient);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Curve Multiplication):\n"
" Prime: %s\n"
" Factor: %s\n"
" PointX: %s\n"
" PointY: %s\n"
" CurveCoefficient: %s\n",
szPrime,
szFactor, szPointX, szPointY,
szCoefficient);
break;
}
case CCP_V5_ENGINE_ECC_OP_MUL_ADD_CURVE:
{
char szFactor1[256];
char szPoint1X[256];
char szPoint1Y[256];
char szFactor2[256];
char szPoint2X[256];
char szPoint2Y[256];
char szCoefficient[256];
pspDevCcpDumpEccNumber(szFactor1, sizeof(szFactor1), &pEccReq->Op.CurveMulAdd.Factor1);
pspDevCcpDumpEccNumber(szPoint1X, sizeof(szPoint1X), &pEccReq->Op.CurveMulAdd.Point1.X);
pspDevCcpDumpEccNumber(szPoint1Y, sizeof(szPoint1Y), &pEccReq->Op.CurveMulAdd.Point1.Y);
pspDevCcpDumpEccNumber(szFactor2, sizeof(szFactor2), &pEccReq->Op.CurveMulAdd.Factor2);
pspDevCcpDumpEccNumber(szPoint2X, sizeof(szPoint2X), &pEccReq->Op.CurveMulAdd.Point2.X);
pspDevCcpDumpEccNumber(szPoint2Y, sizeof(szPoint2Y), &pEccReq->Op.CurveMulAdd.Point2.Y);
pspDevCcpDumpEccNumber(szCoefficient, sizeof(szCoefficient), &pEccReq->Op.CurveMulAdd.Coefficient);
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Curve Multiplication and Addition):\n"
" Prime: %s\n"
" Factor1: %s\n"
" Point1X: %s\n"
" Point1Y: %s\n"
" Factor2: %s\n"
" Point2X: %s\n"
" Point2Y: %s\n"
" CurveCoefficient: %s\n",
szPrime,
szFactor1, szPoint1X, szPoint1Y,
szFactor2, szPoint2X, szPoint2Y,
szCoefficient);
}
break;
default:
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_CCP,
"CCP ECC Data (Unkown Operation):\n"
" Prime: %s\n"
" Unknown Parameters ...\n",
szPrime);
break;
}