|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2024 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Igor Misic <[email protected]> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * 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 |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +// Chacha20 definition document : https://datatracker.ietf.org/doc/html/rfc8439 |
| 36 | + |
| 37 | +#include "chacha20.h" |
| 38 | + |
| 39 | +#include "bit_manipulation.h" |
| 40 | +#include "utils.h" |
| 41 | + |
| 42 | +#define KEY_STREAM_SIZE 64U |
| 43 | +#define BLOCK_SIZE 64U |
| 44 | + |
| 45 | +// Hex representation of Magic number: "expand 32-byte k" |
| 46 | +#define CHACHA_MAGIC_NUMBER_PART_1 (0x61707865U) |
| 47 | +#define CHACHA_MAGIC_NUMBER_PART_2 (0x3320646eU) |
| 48 | +#define CHACHA_MAGIC_NUMBER_PART_3 (0x79622d32U) |
| 49 | +#define CHACHA_MAGIC_NUMBER_PART_4 (0x6b206574U) |
| 50 | + |
| 51 | +#define ARRAY_64_ZERO_VALUES { \ |
| 52 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 53 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 54 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 55 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 56 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 57 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 58 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ |
| 59 | + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U \ |
| 60 | + } |
| 61 | + |
| 62 | +static inline void quarterround(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d); |
| 63 | +static void chacha20_block(const byte_t key[CHACHA20_KEY_SIZE], const uint32_t counter, |
| 64 | + const byte_t nonce[CHACHA20_NONCE_SIZE], |
| 65 | + byte_t out[KEY_STREAM_SIZE]); |
| 66 | + |
| 67 | +static inline void |
| 68 | +quarterround(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d) { |
| 69 | + *a += *b; |
| 70 | + *d = BitManipulation_rotl32(*d ^ *a, 16); |
| 71 | + *c += *d; |
| 72 | + *b = BitManipulation_rotl32(*b ^ *c, 12); |
| 73 | + *a += *b; |
| 74 | + *d = BitManipulation_rotl32(*d ^ *a, 8); |
| 75 | + *c += *d; |
| 76 | + *b = BitManipulation_rotl32(*b ^ *c, 7); |
| 77 | +} |
| 78 | + |
| 79 | +static void |
| 80 | +chacha20_block(const byte_t key[CHACHA20_KEY_SIZE], const uint32_t counter, const byte_t nonce[CHACHA20_NONCE_SIZE], |
| 81 | + byte_t out[KEY_STREAM_SIZE]) { |
| 82 | + |
| 83 | + uint32_t x0 = CHACHA_MAGIC_NUMBER_PART_1; |
| 84 | + uint32_t x1 = CHACHA_MAGIC_NUMBER_PART_2; |
| 85 | + uint32_t x2 = CHACHA_MAGIC_NUMBER_PART_3; |
| 86 | + uint32_t x3 = CHACHA_MAGIC_NUMBER_PART_4; |
| 87 | + uint32_t x4 = Utils_Deserialize32LE(&key[0]); |
| 88 | + uint32_t x5 = Utils_Deserialize32LE(&key[4]); |
| 89 | + uint32_t x6 = Utils_Deserialize32LE(&key[8]); |
| 90 | + uint32_t x7 = Utils_Deserialize32LE(&key[12]); |
| 91 | + uint32_t x8 = Utils_Deserialize32LE(&key[16]); |
| 92 | + uint32_t x9 = Utils_Deserialize32LE(&key[20]); |
| 93 | + uint32_t x10 = Utils_Deserialize32LE(&key[24]); |
| 94 | + uint32_t x11 = Utils_Deserialize32LE(&key[28]); |
| 95 | + uint32_t x12 = counter; |
| 96 | + uint32_t x13 = Utils_Deserialize32LE(&nonce[0]); |
| 97 | + uint32_t x14 = Utils_Deserialize32LE(&nonce[4]); |
| 98 | + uint32_t x15 = Utils_Deserialize32LE(&nonce[8]); |
| 99 | + |
| 100 | + for (uint8_t i = 0; i < 10U; ++i) { |
| 101 | + quarterround(&x0, &x4, &x8, &x12); |
| 102 | + quarterround(&x1, &x5, &x9, &x13); |
| 103 | + quarterround(&x2, &x6, &x10, &x14); |
| 104 | + quarterround(&x3, &x7, &x11, &x15); |
| 105 | + quarterround(&x0, &x5, &x10, &x15); |
| 106 | + quarterround(&x1, &x6, &x11, &x12); |
| 107 | + quarterround(&x2, &x7, &x8, &x13); |
| 108 | + quarterround(&x3, &x4, &x9, &x14); |
| 109 | + } |
| 110 | + |
| 111 | + x0 += CHACHA_MAGIC_NUMBER_PART_1; |
| 112 | + x1 += CHACHA_MAGIC_NUMBER_PART_2; |
| 113 | + x2 += CHACHA_MAGIC_NUMBER_PART_3; |
| 114 | + x3 += CHACHA_MAGIC_NUMBER_PART_4; |
| 115 | + x4 += Utils_Deserialize32LE(&key[0]); |
| 116 | + x5 += Utils_Deserialize32LE(&key[4]); |
| 117 | + x6 += Utils_Deserialize32LE(&key[8]); |
| 118 | + x7 += Utils_Deserialize32LE(&key[12]); |
| 119 | + x8 += Utils_Deserialize32LE(&key[16]); |
| 120 | + x9 += Utils_Deserialize32LE(&key[20]); |
| 121 | + x10 += Utils_Deserialize32LE(&key[24]); |
| 122 | + x11 += Utils_Deserialize32LE(&key[28]); |
| 123 | + x12 += counter; |
| 124 | + x13 += Utils_Deserialize32LE(&nonce[0]); |
| 125 | + x14 += Utils_Deserialize32LE(&nonce[4]); |
| 126 | + x15 += Utils_Deserialize32LE(&nonce[8]); |
| 127 | + |
| 128 | + /* -E> compliant MC3R1.R18.6 16 automatic storage pointed with "out" is not copied to values from this function */ |
| 129 | + Utils_Serialize32LE(&out[0], x0); |
| 130 | + Utils_Serialize32LE(&out[4], x1); |
| 131 | + Utils_Serialize32LE(&out[8], x2); |
| 132 | + Utils_Serialize32LE(&out[12], x3); |
| 133 | + Utils_Serialize32LE(&out[16], x4); |
| 134 | + Utils_Serialize32LE(&out[20], x5); |
| 135 | + Utils_Serialize32LE(&out[24], x6); |
| 136 | + Utils_Serialize32LE(&out[28], x7); |
| 137 | + Utils_Serialize32LE(&out[32], x8); |
| 138 | + Utils_Serialize32LE(&out[36], x9); |
| 139 | + Utils_Serialize32LE(&out[40], x10); |
| 140 | + Utils_Serialize32LE(&out[44], x11); |
| 141 | + Utils_Serialize32LE(&out[48], x12); |
| 142 | + Utils_Serialize32LE(&out[52], x13); |
| 143 | + Utils_Serialize32LE(&out[56], x14); |
| 144 | + Utils_Serialize32LE(&out[60], x15); |
| 145 | +} |
| 146 | + |
| 147 | +void |
| 148 | +chacha20(const byte_t* plaintext, |
| 149 | + uint32_t plaintext_len, |
| 150 | + const byte_t key[CHACHA20_KEY_SIZE], |
| 151 | + const byte_t nonce[CHACHA20_NONCE_SIZE], |
| 152 | + uint32_t inc, |
| 153 | + byte_t* encrypted_message) { |
| 154 | + |
| 155 | + uint32_t blocks = plaintext_len / BLOCK_SIZE; |
| 156 | + uint32_t remaining_bytes = plaintext_len % BLOCK_SIZE; |
| 157 | + |
| 158 | + for (uint32_t j = 0; j < blocks; ++j) { |
| 159 | + byte_t key_stream[KEY_STREAM_SIZE] = ARRAY_64_ZERO_VALUES; |
| 160 | + chacha20_block(key, j + inc, nonce, key_stream); |
| 161 | + |
| 162 | + for (uint8_t i = 0; i < BLOCK_SIZE; ++i) { |
| 163 | + encrypted_message[(j * BLOCK_SIZE) + i] = plaintext[(j * BLOCK_SIZE) + i] ^ key_stream[i]; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (0U != remaining_bytes) { |
| 168 | + byte_t key_stream[KEY_STREAM_SIZE] = ARRAY_64_ZERO_VALUES; |
| 169 | + chacha20_block(key, blocks + inc, nonce, key_stream); |
| 170 | + |
| 171 | + for (uint32_t i = 0; i < remaining_bytes; ++i) { |
| 172 | + encrypted_message[(blocks * BLOCK_SIZE) + i] = plaintext[(blocks * BLOCK_SIZE) + i] ^ key_stream[i]; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments