Branch data Line data Source code
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 : :
37 : 0 : 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 : :
42 : 0 : uint8_t buffer[TC_AES_BLOCK_SIZE];
43 : 0 : unsigned int n, m;
44 : :
45 : : /* input sanity check: */
46 : 0 : if (out == (uint8_t *) 0 ||
47 [ # # ]: 0 : in == (const uint8_t *) 0 ||
48 : 0 : sched == (TCAesKeySched_t) 0 ||
49 [ # # # # ]: 0 : inlen == 0 ||
50 : 0 : outlen == 0 ||
51 [ # # ]: 0 : (inlen % TC_AES_BLOCK_SIZE) != 0 ||
52 [ # # ]: 0 : (outlen % TC_AES_BLOCK_SIZE) != 0 ||
53 [ # # ]: 0 : outlen != inlen + TC_AES_BLOCK_SIZE) {
54 : : return TC_CRYPTO_FAIL;
55 : : }
56 : :
57 : : /* copy iv to the buffer */
58 : 0 : (void)_copy(buffer, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
59 : : /* copy iv to the output buffer */
60 : 0 : (void)_copy(out, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
61 : 0 : out += TC_AES_BLOCK_SIZE;
62 : :
63 [ # # ]: 0 : for (n = m = 0; n < inlen; ++n) {
64 : 0 : buffer[m++] ^= *in++;
65 [ # # ]: 0 : if (m == TC_AES_BLOCK_SIZE) {
66 : 0 : (void)tc_aes_encrypt(buffer, buffer, sched);
67 : 0 : (void)_copy(out, TC_AES_BLOCK_SIZE,
68 : : buffer, TC_AES_BLOCK_SIZE);
69 : 0 : out += TC_AES_BLOCK_SIZE;
70 : 0 : m = 0;
71 : : }
72 : : }
73 : :
74 : : return TC_CRYPTO_SUCCESS;
75 : : }
76 : :
77 : 0 : int tc_cbc_mode_decrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
78 : : unsigned int inlen, const uint8_t *iv,
79 : : const TCAesKeySched_t sched)
80 : : {
81 : :
82 : 0 : uint8_t buffer[TC_AES_BLOCK_SIZE];
83 : 0 : const uint8_t *p;
84 : 0 : unsigned int n, m;
85 : :
86 : : /* sanity check the inputs */
87 : 0 : if (out == (uint8_t *) 0 ||
88 [ # # ]: 0 : in == (const uint8_t *) 0 ||
89 : 0 : sched == (TCAesKeySched_t) 0 ||
90 [ # # # # ]: 0 : inlen == 0 ||
91 : 0 : outlen == 0 ||
92 [ # # ]: 0 : (inlen % TC_AES_BLOCK_SIZE) != 0 ||
93 [ # # # # ]: 0 : (outlen % TC_AES_BLOCK_SIZE) != 0 ||
94 : : outlen != inlen) {
95 : : return TC_CRYPTO_FAIL;
96 : : }
97 : :
98 : : /*
99 : : * Note that in == iv + ciphertext, i.e. the iv and the ciphertext are
100 : : * contiguous. This allows for a very efficient decryption algorithm
101 : : * that would not otherwise be possible.
102 : : */
103 : : p = iv;
104 [ # # ]: 0 : for (n = m = 0; n < outlen; ++n) {
105 [ # # ]: 0 : if ((n % TC_AES_BLOCK_SIZE) == 0) {
106 : 0 : (void)tc_aes_decrypt(buffer, in, sched);
107 : 0 : in += TC_AES_BLOCK_SIZE;
108 : 0 : m = 0;
109 : : }
110 : 0 : *out++ = buffer[m++] ^ *p++;
111 : : }
112 : :
113 : : return TC_CRYPTO_SUCCESS;
114 : : }
|