]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/polarssl/libpcrypto.c
13e37f00a4ceed844014d38a546dae7eeead7930
[proxmark3-svn] / common / polarssl / libpcrypto.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
3 // Copyright (C) 2018 drHatson
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // crypto commands
10 //-----------------------------------------------------------------------------
11
12 #include "polarssl/libpcrypto.h"
13 #include <polarssl/aes.h>
14 #include <polarssl/aes_cmac128.h>
15
16 // NIST Special Publication 800-38A \97 Recommendation for block cipher modes of operation: methods and techniques, 2001.
17 int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){
18 uint8_t iiv[16] = {0};
19 if (iv)
20 memcpy(iiv, iv, 16);
21
22 aes_context aes;
23 aes_init(&aes);
24 if (aes_setkey_enc(&aes, key, 128))
25 return 1;
26 if (aes_crypt_cbc(&aes, AES_ENCRYPT, length, iiv, input, output))
27 return 2;
28 aes_free(&aes);
29
30 return 0;
31 }
32
33 int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){
34 uint8_t iiv[16] = {0};
35 if (iv)
36 memcpy(iiv, iv, 16);
37
38 aes_context aes;
39 aes_init(&aes);
40 if (aes_setkey_dec(&aes, key, 128))
41 return 1;
42 if (aes_crypt_cbc(&aes, AES_DECRYPT, length, iiv, input, output))
43 return 2;
44 aes_free(&aes);
45
46 return 0;
47 }
48
49 // NIST Special Publication 800-38B \97 Recommendation for block cipher modes of operation: The CMAC mode for authentication.
50 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf
51 int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
52 memset(mac, 0x00, 16);
53 uint8_t iiv[16] = {0};
54 if (iv)
55 memcpy(iiv, iv, 16);
56
57 // NIST 800-38B
58 aes_cmac128_context ctx;
59 aes_cmac128_starts(&ctx, key);
60 aes_cmac128_update(&ctx, input, length);
61 aes_cmac128_final(&ctx, mac);
62
63 return 0;
64 }
65
66 int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
67 uint8_t cmac[16] = {0};
68 memset(mac, 0x00, 8);
69
70 int res = aes_cmac(iv, key, input, cmac, length);
71 if (res)
72 return res;
73
74 for(int i = 0; i < 8; i++)
75 mac[i] = cmac[i * 2 + 1];
76
77 return 0;
78 }
Impressum, Datenschutz