Merge pull request #2654 from Antiklesys/master
[RRG-proxmark3.git] / client / src / mifare / lrpcrypto.h
blobe88780681832f6816155e41ce907de1cc0ee6f4a
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
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.
8 //
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 // description here: Leakage Resilient Primitive (LRP) Specification, https://www.nxp.com/docs/en/application-note/AN12304.pdf
17 //-----------------------------------------------------------------------------
19 #ifndef __LRPCRYPTO_H
20 #define __LRPCRYPTO_H
22 #include "common.h"
23 #include "crypto/libpcrypto.h"
25 #define LRP_MAX_PLAINTEXTS_SIZE 16
26 #define LRP_MAX_UPDATED_KEYS_SIZE 4
27 #define LRP_MAX_COUNTER_SIZE (CRYPTO_AES128_KEY_SIZE * 4)
29 typedef struct {
30 uint8_t key[CRYPTO_AES128_KEY_SIZE];
32 bool useBitPadding;
33 size_t plaintextsCount;
34 uint8_t plaintexts[LRP_MAX_PLAINTEXTS_SIZE][CRYPTO_AES128_KEY_SIZE];
35 size_t updatedKeysCount;
36 uint8_t updatedKeys[LRP_MAX_UPDATED_KEYS_SIZE][CRYPTO_AES128_KEY_SIZE];
37 size_t useUpdatedKeyNum;
39 uint8_t counter[LRP_MAX_COUNTER_SIZE];
40 size_t counterLenNibbles; // len in bytes * 2 (or * 2 - 1)
41 } LRPContext_t;
43 void LRPClearContext(LRPContext_t *ctx);
44 void LRPSetKey(LRPContext_t *ctx, uint8_t *key, size_t updatedKeyNum, bool useBitPadding);
45 void LRPSetKeyEx(LRPContext_t *ctx, uint8_t *key, uint8_t *counter, size_t counterLenNibbles, size_t updatedKeyNum, bool useBitPadding);
46 void LRPSetCounter(LRPContext_t *ctx, uint8_t *counter, size_t counterLenNibbles);
47 void LRPGeneratePlaintexts(LRPContext_t *ctx, size_t plaintextsCount);
48 void LRPGenerateUpdatedKeys(LRPContext_t *ctx, size_t updatedKeysCount);
49 void LRPEvalLRP(LRPContext_t *ctx, const uint8_t *iv, size_t ivlen, bool final, uint8_t *y);
50 void LRPIncCounter(uint8_t *ctr, size_t ctrlen);
51 void LRPEncode(LRPContext_t *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen);
52 void LRPDecode(LRPContext_t *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen);
53 void LRPEncDec(uint8_t *key, uint8_t *iv, bool encode, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen);
54 void LRPGenSubkeys(uint8_t *key, uint8_t *sk1, uint8_t *sk2);
55 void LRPCMAC(LRPContext_t *ctx, uint8_t *data, size_t datalen, uint8_t *cmac);
56 void LRPCMAC8(LRPContext_t *ctx, uint8_t *data, size_t datalen, uint8_t *cmac);
58 #endif // __LRPCRYPTO_H