1 /* cbc_mode.c - TinyCrypt implementation of CBC mode encryption & decryption */
2
3 /*
4 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <tinycrypt/cbc_mode.h>
34 #include <tinycrypt/constants.h>
35 #include <tinycrypt/utils.h>
36
tc_cbc_mode_encrypt(uint8_t * out,unsigned int outlen,const uint8_t * in,unsigned int inlen,const uint8_t * iv,const TCAesKeySched_t sched)37 int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
38 unsigned int inlen, const uint8_t *iv,
39 const TCAesKeySched_t sched)
40 {
41 uint8_t buffer[TC_AES_BLOCK_SIZE];
42 unsigned int n, m;
43
44 /* input sanity check: */
45 if (out == (uint8_t *) 0 ||
46 in == (const uint8_t *) 0 ||
47 sched == (TCAesKeySched_t) 0 ||
48 inlen == 0 ||
49 outlen == 0 ||
50 (inlen % TC_AES_BLOCK_SIZE) != 0 ||
51 (outlen % TC_AES_BLOCK_SIZE) != 0 ||
52 outlen != inlen + TC_AES_BLOCK_SIZE) {
53 return TC_CRYPTO_FAIL;
54 }
55
56 /* copy iv to the buffer */
57 (void)_copy(buffer, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
58 /* copy iv to the output buffer */
59 (void)_copy(out, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
60 out += TC_AES_BLOCK_SIZE;
61
62 for (n = m = 0; n < inlen; ++n) {
63 buffer[m++] ^= *in++;
64
65 if (m == TC_AES_BLOCK_SIZE) {
66 (void)tc_aes_encrypt(buffer, buffer, sched);
67 (void)_copy(out, TC_AES_BLOCK_SIZE,
68 buffer, TC_AES_BLOCK_SIZE);
69 out += TC_AES_BLOCK_SIZE;
70 m = 0;
71 }
72 }
73 return TC_CRYPTO_SUCCESS;
74 }
75
tc_cbc_mode_decrypt(uint8_t * out,unsigned int outlen,const uint8_t * in,unsigned int inlen,const uint8_t * iv,const TCAesKeySched_t sched)76 int tc_cbc_mode_decrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
77 unsigned int inlen, const uint8_t *iv,
78 const TCAesKeySched_t sched)
79 {
80 uint8_t buffer[TC_AES_BLOCK_SIZE];
81 const uint8_t *p;
82 unsigned int n, m;
83
84 /* sanity check the inputs */
85 if (out == (uint8_t *) 0 ||
86 in == (const uint8_t *) 0 ||
87 sched == (TCAesKeySched_t) 0 ||
88 inlen == 0 ||
89 outlen == 0 ||
90 (inlen % TC_AES_BLOCK_SIZE) != 0 ||
91 (outlen % TC_AES_BLOCK_SIZE) != 0 ||
92 outlen != inlen - TC_AES_BLOCK_SIZE) {
93 return TC_CRYPTO_FAIL;
94 }
95
96 /*
97 * Note that in == iv + ciphertext, i.e. the iv and the ciphertext are
98 * contiguous. This allows for a very efficient decryption algorithm
99 * that would not otherwise be possible.
100 */
101 p = iv;
102
103 for (n = m = 0; n < inlen; ++n) {
104 if ((n % TC_AES_BLOCK_SIZE) == 0) {
105 (void)tc_aes_decrypt(buffer, in, sched);
106 in += TC_AES_BLOCK_SIZE;
107 m = 0;
108 }
109
110 *out++ = buffer[m++] ^ *p++;
111 }
112
113 return TC_CRYPTO_SUCCESS;
114 }