summaryrefslogtreecommitdiff
path: root/utils/imxtools/sbtools/crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/imxtools/sbtools/crypto.c')
-rw-r--r--utils/imxtools/sbtools/crypto.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/utils/imxtools/sbtools/crypto.c b/utils/imxtools/sbtools/crypto.c
new file mode 100644
index 0000000000..d3ec18bd11
--- /dev/null
+++ b/utils/imxtools/sbtools/crypto.c
@@ -0,0 +1,126 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "crypto.h"
22#include "misc.h"
23
24#include "tomcrypt.h"
25
26
27
28enum crypto_method_t g_cur_method = CRYPTO_NONE;
29uint8_t g_key[16];
30
31int cbc_mac2(
32 const uint8_t *in_data, /* Input data */
33 uint8_t *out_data, /* Output data (or NULL) */
34 int nr_blocks, /* Number of blocks to encrypt/decrypt (one block=16 bytes) */
35 uint8_t key[16], /* Key */
36 uint8_t iv[16], /* Initialisation Vector */
37 uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
38 bool encrypt /* 1 to encrypt, 0 to decrypt */
39 )
40{
41 int cipher = register_cipher(&aes_desc);
42 symmetric_CBC cbc;
43 cbc_start(cipher, iv, key, 16, 0, &cbc);
44
45 /* encrypt */
46 if(encrypt)
47 {
48 uint8_t tmp[16];
49 /* we need some output buffer, either a temporary one if we are CBC-MACing
50 * only, or use output buffer if available */
51 uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data;
52 while(nr_blocks-- > 0)
53 {
54 cbc_encrypt(in_data, out_ptr, 16, &cbc);
55 /* if this is the last block, copy CBC-MAC */
56 if(nr_blocks == 0 && out_cbc_mac)
57 memcpy(out_cbc_mac, out_ptr, 16);
58 /* if we are writing data to the output buffer, advance output pointer */
59 if(out_data != NULL)
60 out_ptr += 16;
61 in_data += 16;
62 }
63 return CRYPTO_ERROR_SUCCESS;
64 }
65 /* decrypt */
66 else
67 {
68 cbc_decrypt(in_data, out_data, nr_blocks * 16, &cbc);
69
70 /* update keys if neeeded */
71 /* we cannot produce a CBC-MAC in decrypt mode, output buffer exists */
72 if(out_cbc_mac || out_data == NULL)
73 return CRYPTO_ERROR_INVALID_OP;
74
75 return CRYPTO_ERROR_SUCCESS;
76 }
77}
78
79
80int crypto_setup(struct crypto_key_t *key)
81{
82 g_cur_method = key->method;
83 switch(g_cur_method)
84 {
85 case CRYPTO_KEY:
86 memcpy(g_key, key->u.key, 16);
87
88 return CRYPTO_ERROR_SUCCESS;
89 default:
90 return CRYPTO_ERROR_BADSETUP;
91 }
92}
93
94int crypto_apply(
95 uint8_t *in_data, /* Input data */
96 uint8_t *out_data, /* Output data (or NULL) */
97 int nr_blocks, /* Number of blocks (one block=16 bytes) */
98 uint8_t iv[16], /* Key */
99 uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
100 bool encrypt)
101{
102 if(g_cur_method == CRYPTO_KEY)
103 return cbc_mac2(in_data, out_data, nr_blocks, g_key, iv, out_cbc_mac, encrypt);
104 else
105 return CRYPTO_ERROR_BADSETUP;
106}
107
108void sha_1_init(struct sha_1_params_t *params)
109{
110 sha1_init(&params->state);
111}
112
113void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size)
114{
115 sha1_process(&params->state, buffer, size);
116}
117
118void sha_1_finish(struct sha_1_params_t *params)
119{
120 sha1_done(&params->state, params->hash);
121}
122
123void sha_1_output(struct sha_1_params_t *params, uint8_t *out)
124{
125 memcpy(out, params->hash, 20);
126}