Path: blob/main/contrib/bearssl/src/ssl/ssl_rec_gcm.c
39483 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/*27* GCM initialisation. This does everything except setting the vtable,28* which depends on whether this is a context for encrypting or for29* decrypting.30*/31static void32gen_gcm_init(br_sslrec_gcm_context *cc,33const br_block_ctr_class *bc_impl,34const void *key, size_t key_len,35br_ghash gh_impl,36const void *iv)37{38unsigned char tmp[12];3940cc->seq = 0;41bc_impl->init(&cc->bc.vtable, key, key_len);42cc->gh = gh_impl;43memcpy(cc->iv, iv, sizeof cc->iv);44memset(cc->h, 0, sizeof cc->h);45memset(tmp, 0, sizeof tmp);46bc_impl->run(&cc->bc.vtable, tmp, 0, cc->h, sizeof cc->h);47}4849static void50in_gcm_init(br_sslrec_gcm_context *cc,51const br_block_ctr_class *bc_impl,52const void *key, size_t key_len,53br_ghash gh_impl,54const void *iv)55{56cc->vtable.in = &br_sslrec_in_gcm_vtable;57gen_gcm_init(cc, bc_impl, key, key_len, gh_impl, iv);58}5960static int61gcm_check_length(const br_sslrec_gcm_context *cc, size_t rlen)62{63/*64* GCM adds a fixed overhead:65* 8 bytes for the nonce_explicit (before the ciphertext)66* 16 bytes for the authentication tag (after the ciphertext)67*/68(void)cc;69return rlen >= 24 && rlen <= (16384 + 24);70}7172/*73* Compute the authentication tag. The value written in 'tag' must still74* be CTR-encrypted.75*/76static void77do_tag(br_sslrec_gcm_context *cc,78int record_type, unsigned version,79void *data, size_t len, void *tag)80{81unsigned char header[13];82unsigned char footer[16];8384/*85* Compute authentication tag. Three elements must be injected in86* sequence, each possibly 0-padded to reach a length multiple87* of the block size: the 13-byte header (sequence number, record88* type, protocol version, record length), the cipher text, and89* the word containing the encodings of the bit lengths of the two90* other elements.91*/92br_enc64be(header, cc->seq ++);93header[8] = (unsigned char)record_type;94br_enc16be(header + 9, version);95br_enc16be(header + 11, len);96br_enc64be(footer, (uint64_t)(sizeof header) << 3);97br_enc64be(footer + 8, (uint64_t)len << 3);98memset(tag, 0, 16);99cc->gh(tag, cc->h, header, sizeof header);100cc->gh(tag, cc->h, data, len);101cc->gh(tag, cc->h, footer, sizeof footer);102}103104/*105* Do CTR encryption. This also does CTR encryption of a single block at106* address 'xortag' with the counter value appropriate for the final107* processing of the authentication tag.108*/109static void110do_ctr(br_sslrec_gcm_context *cc, const void *nonce, void *data, size_t len,111void *xortag)112{113unsigned char iv[12];114115memcpy(iv, cc->iv, 4);116memcpy(iv + 4, nonce, 8);117cc->bc.vtable->run(&cc->bc.vtable, iv, 2, data, len);118cc->bc.vtable->run(&cc->bc.vtable, iv, 1, xortag, 16);119}120121static unsigned char *122gcm_decrypt(br_sslrec_gcm_context *cc,123int record_type, unsigned version, void *data, size_t *data_len)124{125unsigned char *buf;126size_t len, u;127uint32_t bad;128unsigned char tag[16];129130buf = (unsigned char *)data + 8;131len = *data_len - 24;132do_tag(cc, record_type, version, buf, len, tag);133do_ctr(cc, data, buf, len, tag);134135/*136* Compare the computed tag with the value from the record. It137* is possibly useless to do a constant-time comparison here,138* but it does not hurt.139*/140bad = 0;141for (u = 0; u < 16; u ++) {142bad |= tag[u] ^ buf[len + u];143}144if (bad) {145return NULL;146}147*data_len = len;148return buf;149}150151/* see bearssl_ssl.h */152const br_sslrec_in_gcm_class br_sslrec_in_gcm_vtable = {153{154sizeof(br_sslrec_gcm_context),155(int (*)(const br_sslrec_in_class *const *, size_t))156&gcm_check_length,157(unsigned char *(*)(const br_sslrec_in_class **,158int, unsigned, void *, size_t *))159&gcm_decrypt160},161(void (*)(const br_sslrec_in_gcm_class **,162const br_block_ctr_class *, const void *, size_t,163br_ghash, const void *))164&in_gcm_init165};166167static void168out_gcm_init(br_sslrec_gcm_context *cc,169const br_block_ctr_class *bc_impl,170const void *key, size_t key_len,171br_ghash gh_impl,172const void *iv)173{174cc->vtable.out = &br_sslrec_out_gcm_vtable;175gen_gcm_init(cc, bc_impl, key, key_len, gh_impl, iv);176}177178static void179gcm_max_plaintext(const br_sslrec_gcm_context *cc,180size_t *start, size_t *end)181{182size_t len;183184(void)cc;185*start += 8;186len = *end - *start - 16;187if (len > 16384) {188len = 16384;189}190*end = *start + len;191}192193static unsigned char *194gcm_encrypt(br_sslrec_gcm_context *cc,195int record_type, unsigned version, void *data, size_t *data_len)196{197unsigned char *buf;198size_t u, len;199unsigned char tmp[16];200201buf = (unsigned char *)data;202len = *data_len;203memset(tmp, 0, sizeof tmp);204br_enc64be(buf - 8, cc->seq);205do_ctr(cc, buf - 8, buf, len, tmp);206do_tag(cc, record_type, version, buf, len, buf + len);207for (u = 0; u < 16; u ++) {208buf[len + u] ^= tmp[u];209}210len += 24;211buf -= 13;212buf[0] = (unsigned char)record_type;213br_enc16be(buf + 1, version);214br_enc16be(buf + 3, len);215*data_len = len + 5;216return buf;217}218219/* see bearssl_ssl.h */220const br_sslrec_out_gcm_class br_sslrec_out_gcm_vtable = {221{222sizeof(br_sslrec_gcm_context),223(void (*)(const br_sslrec_out_class *const *,224size_t *, size_t *))225&gcm_max_plaintext,226(unsigned char *(*)(const br_sslrec_out_class **,227int, unsigned, void *, size_t *))228&gcm_encrypt229},230(void (*)(const br_sslrec_out_gcm_class **,231const br_block_ctr_class *, const void *, size_t,232br_ghash, const void *))233&out_gcm_init234};235236237