blob: 8163e0d38d7f7fa4e5bfc4cd4f914aebb7fa004f [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* cbc_mode.c - TinyCrypt implementation of CBC mode encryption & decryption */
2
3/*
4 * Copyright (C) 2015 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
37int32_t tc_cbc_mode_encrypt(uint8_t *out, uint32_t outlen, const uint8_t *in,
38 uint32_t inlen, const uint8_t *iv,
39 const TCAesKeySched_t sched)
40{
41
42 uint8_t buffer[TC_AES_BLOCK_SIZE];
43 uint32_t n, m;
44
45 /* input sanity check: */
46 if (out == (uint8_t *) 0 ||
47 in == (const uint8_t *) 0 ||
48 sched == (TCAesKeySched_t) 0 ||
49 inlen == 0 ||
50 outlen == 0 ||
51 (inlen % TC_AES_BLOCK_SIZE) != 0 ||
52 (outlen % TC_AES_BLOCK_SIZE) != 0 ||
53 outlen != inlen + TC_AES_BLOCK_SIZE) {
54 return TC_CRYPTO_FAIL;
55 }
56
57 /* copy iv to the buffer */
58 (void)_copy(buffer, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
59 /* copy iv to the output buffer */
60 (void)_copy(out, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
61 out += TC_AES_BLOCK_SIZE;
62
63 for (n = m = 0; n < inlen; ++n) {
64 buffer[m++] ^= *in++;
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
74 return TC_CRYPTO_SUCCESS;
75}
76
77int32_t tc_cbc_mode_decrypt(uint8_t *out, uint32_t outlen, const uint8_t *in,
78 uint32_t inlen, const uint8_t *iv,
79 const TCAesKeySched_t sched)
80{
81 uint8_t buffer[TC_AES_BLOCK_SIZE];
82 const uint8_t *p;
83 uint32_t n, m;
84
85 /* sanity check the inputs */
86 if (out == (uint8_t *) 0 ||
87 in == (const uint8_t *) 0 ||
88 sched == (TCAesKeySched_t) 0 ||
89 inlen == 0 ||
90 outlen == 0 ||
91 (inlen % TC_AES_BLOCK_SIZE) != 0 ||
92 (outlen % TC_AES_BLOCK_SIZE) != 0 ||
93 outlen != inlen - TC_AES_BLOCK_SIZE) {
94 return TC_CRYPTO_FAIL;
95 }
96
97 /*
98 * Note that in == iv + ciphertext, i.e. the iv and the ciphertext are
99 * contiguous. This allows for a very efficient decryption algorithm
100 * that would not otherwise be possible.
101 */
102 p = iv;
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 *out++ = buffer[m++] ^ *p++;
110 }
111
112 return TC_CRYPTO_SUCCESS;
113}