1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // CIPURSE crypto primitives
17 //-----------------------------------------------------------------------------
19 #ifndef __CIPURSECRYPTO_H__
20 #define __CIPURSECRYPTO_H__
23 #include "iso7816/apduinfo.h" // sAPDU_t
25 #define CIPURSE_KVV_LENGTH 4
26 #define CIPURSE_AES_KEY_LENGTH 16
27 #define CIPURSE_AES_BLOCK_LENGTH 16
28 #define CIPURSE_SECURITY_PARAM_N 6
29 #define CIPURSE_MAC_LENGTH 8
30 #define CIPURSE_MIC_LENGTH 4
31 #define CIPURSE_POLY 0x35b088cce172UL
33 #define member_size(type, member) sizeof(((type *)0)->member)
40 } CipurseChannelSecurityLevel
;
42 typedef struct CipurseContext_tS
{
44 uint8_t key
[CIPURSE_AES_KEY_LENGTH
];
46 uint8_t RP
[CIPURSE_AES_KEY_LENGTH
];
47 uint8_t rP
[CIPURSE_SECURITY_PARAM_N
];
48 uint8_t RT
[CIPURSE_AES_KEY_LENGTH
];
49 uint8_t rT
[CIPURSE_SECURITY_PARAM_N
];
51 uint8_t k0
[CIPURSE_AES_KEY_LENGTH
];
52 uint8_t cP
[CIPURSE_AES_KEY_LENGTH
];
53 uint8_t CT
[CIPURSE_AES_KEY_LENGTH
];
55 uint8_t frameKey
[CIPURSE_AES_KEY_LENGTH
];
56 uint8_t frameKeyNext
[CIPURSE_AES_KEY_LENGTH
];
58 CipurseChannelSecurityLevel RequestSecurity
;
59 CipurseChannelSecurityLevel ResponseSecurity
;
62 uint8_t CipurseCSecurityLevelEnc(CipurseChannelSecurityLevel lvl
);
64 void CipurseCClearContext(CipurseContext_t
*ctx
);
65 void CipurseCSetKey(CipurseContext_t
*ctx
, uint8_t keyId
, uint8_t *key
);
66 void CipurseCSetRandomFromPICC(CipurseContext_t
*ctx
, uint8_t *random
);
67 void CipurseCSetRandomHost(CipurseContext_t
*ctx
);
68 uint8_t CipurseCGetSMI(CipurseContext_t
*ctx
, bool LePresent
);
70 void CipurseCAuthenticateHost(CipurseContext_t
*ctx
, uint8_t *authdata
);
71 bool CipurseCCheckCT(CipurseContext_t
*ctx
, uint8_t *CT
);
73 void CipurseCChannelSetSecurityLevels(CipurseContext_t
*ctx
, CipurseChannelSecurityLevel req
, CipurseChannelSecurityLevel resp
);
74 bool isCipurseCChannelSecuritySet(CipurseContext_t
*ctx
);
76 void CipurseCGenerateMAC(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *mac
);
77 void CipurseCCalcMACPadded(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *mac
);
78 bool CipurseCCheckMACPadded(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *mac
);
79 void CipurseCGenerateMIC(uint8_t *data
, size_t datalen
, uint8_t *mic
);
80 bool CipurseCCheckMIC(uint8_t *data
, size_t datalen
, uint8_t *mic
);
81 void CipurseCEncryptDecrypt(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *dstdata
, bool isEncrypt
);
82 void CipurseCChannelEncrypt(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *encdata
, size_t *encdatalen
);
83 void CipurseCChannelDecrypt(CipurseContext_t
*ctx
, uint8_t *data
, size_t datalen
, uint8_t *plaindata
, size_t *plaindatalen
);
84 void CipurseCGetKVV(uint8_t *key
, uint8_t *kvv
);
86 void CipurseCAPDUReqEncode(CipurseContext_t
*ctx
, sAPDU_t
*srcapdu
, sAPDU_t
*dstapdu
, uint8_t *dstdatabuf
, bool includeLe
, uint8_t Le
);
87 void CipurseCAPDURespDecode(CipurseContext_t
*ctx
, uint8_t *srcdata
, size_t srcdatalen
, uint8_t *dstdata
, size_t *dstdatalen
, uint16_t *sw
);
90 #endif /* __CIPURSECRYPTO_H__ */