]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - common/polarssl/libpcrypto.c
9be9fd266e32105a6ce0bbc8021dad37eff03610
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
11 #include "polarssl/libpcrypto.h"
12 #include <polarssl/aes.h>
13 #include <polarssl/aes_cmac128.h>
15 // NIST Special Publication 800-38A \97 Recommendation for block cipher modes of operation: methods and techniques, 2001.
16 int aes_encode(uint8_t *iv
, uint8_t *key
, uint8_t *input
, uint8_t *output
, int length
){
17 uint8_t iiv
[16] = {0};
23 if (aes_setkey_enc(&aes
, key
, 128))
25 if (aes_crypt_cbc(&aes
, AES_ENCRYPT
, length
, iiv
, input
, output
))
32 int aes_decode(uint8_t *iv
, uint8_t *key
, uint8_t *input
, uint8_t *output
, int length
){
33 uint8_t iiv
[16] = {0};
39 if (aes_setkey_dec(&aes
, key
, 128))
41 if (aes_crypt_cbc(&aes
, AES_DECRYPT
, length
, iiv
, input
, output
))
48 // NIST Special Publication 800-38B \97 Recommendation for block cipher modes of operation: The CMAC mode for authentication.
49 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf
50 int aes_cmac(uint8_t *iv
, uint8_t *key
, uint8_t *input
, uint8_t *mac
, int length
) {
51 memset(mac
, 0x00, 16);
52 uint8_t iiv
[16] = {0};
56 // padding: ISO/IEC 9797-1 Message Authentication Codes (MACs) - Part 1: Mechanisms using a block cipher
57 uint8_t data
[2049] = {0}; // length + 16
58 memcpy(data
, input
, length
);
60 int datalen
= (length
& 0xfffffff0) + 0x10;
63 aes_cmac128_context ctx
;
64 aes_cmac128_starts(&ctx
, key
);
65 aes_cmac128_update(&ctx
, data
, datalen
);
66 aes_cmac128_final(&ctx
, mac
);
71 int aes_cmac8(uint8_t *iv
, uint8_t *key
, uint8_t *input
, uint8_t *mac
, int length
) {
72 uint8_t cmac
[16] = {0};
75 int res
= aes_cmac(iv
, key
, input
, cmac
, length
);
79 for(int i
= 0; i
< 8; i
++)
80 mac
[i
] = cmac
[i
* 2 + 1];