Path: blob/main/contrib/bearssl/src/symcipher/aes_small_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_small_ctr_init(br_aes_small_ctr_keys *ctx,29const void *key, size_t len)30{31ctx->vtable = &br_aes_small_ctr_vtable;32ctx->num_rounds = br_aes_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_small_ctr_run(const br_aes_small_ctr_keys *ctx,51const void *iv, uint32_t cc, void *data, size_t len)52{53unsigned char *buf;5455buf = data;56while (len > 0) {57unsigned char tmp[16];5859memcpy(tmp, iv, 12);60br_enc32be(tmp + 12, cc ++);61br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);62if (len <= 16) {63xorbuf(buf, tmp, len);64break;65}66xorbuf(buf, tmp, 16);67buf += 16;68len -= 16;69}70return cc;71}7273/* see bearssl_block.h */74const br_block_ctr_class br_aes_small_ctr_vtable = {75sizeof(br_aes_small_ctr_keys),7616,774,78(void (*)(const br_block_ctr_class **, const void *, size_t))79&br_aes_small_ctr_init,80(uint32_t (*)(const br_block_ctr_class *const *,81const void *, uint32_t, void *, size_t))82&br_aes_small_ctr_run83};848586