Path: blob/main/contrib/bearssl/src/symcipher/aes_ct_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_ct_ctr_init(br_aes_ct_ctr_keys *ctx,29const void *key, size_t len)30{31ctx->vtable = &br_aes_ct_ctr_vtable;32ctx->num_rounds = br_aes_ct_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_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,51const void *iv, uint32_t cc, void *data, size_t len)52{53unsigned char *buf;54const unsigned char *ivbuf;55uint32_t iv0, iv1, iv2;56uint32_t sk_exp[120];5758br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);59ivbuf = iv;60iv0 = br_dec32le(ivbuf);61iv1 = br_dec32le(ivbuf + 4);62iv2 = br_dec32le(ivbuf + 8);63buf = data;64while (len > 0) {65uint32_t q[8];66unsigned char tmp[32];6768/*69* TODO: see if we can save on the first br_aes_ct_ortho()70* call, since iv0/iv1/iv2 are constant for the whole run.71*/72q[0] = q[1] = iv0;73q[2] = q[3] = iv1;74q[4] = q[5] = iv2;75q[6] = br_swap32(cc);76q[7] = br_swap32(cc + 1);77br_aes_ct_ortho(q);78br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);79br_aes_ct_ortho(q);80br_enc32le(tmp, q[0]);81br_enc32le(tmp + 4, q[2]);82br_enc32le(tmp + 8, q[4]);83br_enc32le(tmp + 12, q[6]);84br_enc32le(tmp + 16, q[1]);85br_enc32le(tmp + 20, q[3]);86br_enc32le(tmp + 24, q[5]);87br_enc32le(tmp + 28, q[7]);8889if (len <= 32) {90xorbuf(buf, tmp, len);91cc ++;92if (len > 16) {93cc ++;94}95break;96}97xorbuf(buf, tmp, 32);98buf += 32;99len -= 32;100cc += 2;101}102return cc;103}104105/* see bearssl_block.h */106const br_block_ctr_class br_aes_ct_ctr_vtable = {107sizeof(br_aes_ct_ctr_keys),10816,1094,110(void (*)(const br_block_ctr_class **, const void *, size_t))111&br_aes_ct_ctr_init,112(uint32_t (*)(const br_block_ctr_class *const *,113const void *, uint32_t, void *, size_t))114&br_aes_ct_ctr_run115};116117118