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 
42 	uint8_t buffer[TC_AES_BLOCK_SIZE];
43 	unsigned int 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 
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)77 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 	uint8_t buffer[TC_AES_BLOCK_SIZE];
83 	const uint8_t *p;
84 	unsigned int n, m;
85 
86 	/* sanity check the inputs */
87 	if (out == (uint8_t *) 0 ||
88 	    in == (const uint8_t *) 0 ||
89 	    sched == (TCAesKeySched_t) 0 ||
90 	    inlen == 0 ||
91 	    outlen == 0 ||
92 	    (inlen % TC_AES_BLOCK_SIZE) != 0 ||
93 	    (outlen % TC_AES_BLOCK_SIZE) != 0 ||
94 	    outlen != inlen - TC_AES_BLOCK_SIZE) {
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 	for (n = m = 0; n < inlen; ++n) {
105 		if ((n % TC_AES_BLOCK_SIZE) == 0) {
106 			(void)tc_aes_decrypt(buffer, in, sched);
107 			in += TC_AES_BLOCK_SIZE;
108 			m = 0;
109 		}
110 		*out++ = buffer[m++] ^ *p++;
111 	}
112 
113 	return TC_CRYPTO_SUCCESS;
114 }
115