Path: blob/main/contrib/bearssl/src/symcipher/chacha20_ct.c
39482 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see bearssl_block.h */27uint32_t28br_chacha20_ct_run(const void *key,29const void *iv, uint32_t cc, void *data, size_t len)30{31unsigned char *buf;32uint32_t kw[8], ivw[3];33size_t u;3435static const uint32_t CW[] = {360x61707865, 0x3320646e, 0x79622d32, 0x6b20657437};3839buf = data;40for (u = 0; u < 8; u ++) {41kw[u] = br_dec32le((const unsigned char *)key + (u << 2));42}43for (u = 0; u < 3; u ++) {44ivw[u] = br_dec32le((const unsigned char *)iv + (u << 2));45}46while (len > 0) {47uint32_t state[16];48int i;49size_t clen;50unsigned char tmp[64];5152memcpy(&state[0], CW, sizeof CW);53memcpy(&state[4], kw, sizeof kw);54state[12] = cc;55memcpy(&state[13], ivw, sizeof ivw);56for (i = 0; i < 10; i ++) {5758#define QROUND(a, b, c, d) do { \59state[a] += state[b]; \60state[d] ^= state[a]; \61state[d] = (state[d] << 16) | (state[d] >> 16); \62state[c] += state[d]; \63state[b] ^= state[c]; \64state[b] = (state[b] << 12) | (state[b] >> 20); \65state[a] += state[b]; \66state[d] ^= state[a]; \67state[d] = (state[d] << 8) | (state[d] >> 24); \68state[c] += state[d]; \69state[b] ^= state[c]; \70state[b] = (state[b] << 7) | (state[b] >> 25); \71} while (0)7273QROUND( 0, 4, 8, 12);74QROUND( 1, 5, 9, 13);75QROUND( 2, 6, 10, 14);76QROUND( 3, 7, 11, 15);77QROUND( 0, 5, 10, 15);78QROUND( 1, 6, 11, 12);79QROUND( 2, 7, 8, 13);80QROUND( 3, 4, 9, 14);8182#undef QROUND8384}85for (u = 0; u < 4; u ++) {86br_enc32le(&tmp[u << 2], state[u] + CW[u]);87}88for (u = 4; u < 12; u ++) {89br_enc32le(&tmp[u << 2], state[u] + kw[u - 4]);90}91br_enc32le(&tmp[48], state[12] + cc);92for (u = 13; u < 16; u ++) {93br_enc32le(&tmp[u << 2], state[u] + ivw[u - 13]);94}9596clen = len < 64 ? len : 64;97for (u = 0; u < clen; u ++) {98buf[u] ^= tmp[u];99}100buf += clen;101len -= clen;102cc ++;103}104return cc;105}106107108