Path: blob/main/contrib/bearssl/src/symcipher/aes_ct64_ctr.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 */27void28br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,29const void *key, size_t len)30{31ctx->vtable = &br_aes_ct64_ctr_vtable;32ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);33}3435static void36xorbuf(void *dst, const void *src, size_t len)37{38unsigned char *d;39const unsigned char *s;4041d = dst;42s = src;43while (len -- > 0) {44*d ++ ^= *s ++;45}46}4748/* see bearssl_block.h */49uint32_t50br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,51const void *iv, uint32_t cc, void *data, size_t len)52{53unsigned char *buf;54uint32_t ivw[16];55uint64_t sk_exp[120];5657br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);58br_range_dec32le(ivw, 3, iv);59memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t));60memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t));61memcpy(ivw + 12, ivw, 3 * sizeof(uint32_t));62buf = data;63while (len > 0) {64uint64_t q[8];65uint32_t w[16];66unsigned char tmp[64];67int i;6869/*70* TODO: see if we can save on the first br_aes_ct64_ortho()71* call, since iv0/iv1/iv2 are constant for the whole run.72*/73memcpy(w, ivw, sizeof ivw);74w[3] = br_swap32(cc);75w[7] = br_swap32(cc + 1);76w[11] = br_swap32(cc + 2);77w[15] = br_swap32(cc + 3);78for (i = 0; i < 4; i ++) {79br_aes_ct64_interleave_in(80&q[i], &q[i + 4], w + (i << 2));81}82br_aes_ct64_ortho(q);83br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);84br_aes_ct64_ortho(q);85for (i = 0; i < 4; i ++) {86br_aes_ct64_interleave_out(87w + (i << 2), q[i], q[i + 4]);88}89br_range_enc32le(tmp, w, 16);90if (len <= 64) {91xorbuf(buf, tmp, len);92cc += (uint32_t)len >> 4;93break;94}95xorbuf(buf, tmp, 64);96buf += 64;97len -= 64;98cc += 4;99}100return cc;101}102103/* see bearssl_block.h */104const br_block_ctr_class br_aes_ct64_ctr_vtable = {105sizeof(br_aes_ct64_ctr_keys),10616,1074,108(void (*)(const br_block_ctr_class **, const void *, size_t))109&br_aes_ct64_ctr_init,110(uint32_t (*)(const br_block_ctr_class *const *,111const void *, uint32_t, void *, size_t))112&br_aes_ct64_ctr_run113};114115116