Path: blob/main/contrib/bearssl/src/hash/md5sha1.c
39536 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.h */27void28br_md5sha1_init(br_md5sha1_context *cc)29{30cc->vtable = &br_md5sha1_vtable;31memcpy(cc->val_md5, br_md5_IV, sizeof cc->val_md5);32memcpy(cc->val_sha1, br_sha1_IV, sizeof cc->val_sha1);33cc->count = 0;34}3536/* see bearssl.h */37void38br_md5sha1_update(br_md5sha1_context *cc, const void *data, size_t len)39{40const unsigned char *buf;41size_t ptr;4243buf = data;44ptr = (size_t)cc->count & 63;45while (len > 0) {46size_t clen;4748clen = 64 - ptr;49if (clen > len) {50clen = len;51}52memcpy(cc->buf + ptr, buf, clen);53ptr += clen;54buf += clen;55len -= clen;56cc->count += (uint64_t)clen;57if (ptr == 64) {58br_md5_round(cc->buf, cc->val_md5);59br_sha1_round(cc->buf, cc->val_sha1);60ptr = 0;61}62}63}6465/* see bearssl.h */66void67br_md5sha1_out(const br_md5sha1_context *cc, void *dst)68{69unsigned char buf[64];70uint32_t val_md5[4];71uint32_t val_sha1[5];72size_t ptr;73unsigned char *out;74uint64_t count;7576count = cc->count;77ptr = (size_t)count & 63;78memcpy(buf, cc->buf, ptr);79memcpy(val_md5, cc->val_md5, sizeof val_md5);80memcpy(val_sha1, cc->val_sha1, sizeof val_sha1);81buf[ptr ++] = 0x80;82if (ptr > 56) {83memset(buf + ptr, 0, 64 - ptr);84br_md5_round(buf, val_md5);85br_sha1_round(buf, val_sha1);86memset(buf, 0, 56);87} else {88memset(buf + ptr, 0, 56 - ptr);89}90count <<= 3;91br_enc64le(buf + 56, count);92br_md5_round(buf, val_md5);93br_enc64be(buf + 56, count);94br_sha1_round(buf, val_sha1);95out = dst;96br_range_enc32le(out, val_md5, 4);97br_range_enc32be(out + 16, val_sha1, 5);98}99100/* see bearssl.h */101uint64_t102br_md5sha1_state(const br_md5sha1_context *cc, void *dst)103{104unsigned char *out;105106out = dst;107br_range_enc32le(out, cc->val_md5, 4);108br_range_enc32be(out + 16, cc->val_sha1, 5);109return cc->count;110}111112/* see bearssl.h */113void114br_md5sha1_set_state(br_md5sha1_context *cc, const void *stb, uint64_t count)115{116const unsigned char *buf;117118buf = stb;119br_range_dec32le(cc->val_md5, 4, buf);120br_range_dec32be(cc->val_sha1, 5, buf + 16);121cc->count = count;122}123124/* see bearssl.h */125const br_hash_class br_md5sha1_vtable = {126sizeof(br_md5sha1_context),127BR_HASHDESC_ID(br_md5sha1_ID)128| BR_HASHDESC_OUT(36)129| BR_HASHDESC_STATE(36)130| BR_HASHDESC_LBLEN(6),131(void (*)(const br_hash_class **))&br_md5sha1_init,132(void (*)(const br_hash_class **, const void *, size_t))133&br_md5sha1_update,134(void (*)(const br_hash_class *const *, void *))135&br_md5sha1_out,136(uint64_t (*)(const br_hash_class *const *, void *))137&br_md5sha1_state,138(void (*)(const br_hash_class **, const void *, uint64_t))139&br_md5sha1_set_state140};141142143