Line data Source code
1 : /* 2 : * This Source Code Form is subject to the terms of the Mozilla Public 3 : * License, v. 2.0. If a copy of the MPL was not distributed with this 4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 : * 6 : * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V. 7 : */ 8 : 9 : #include "monetdb_config.h" 10 : #include "rmd160.h" 11 : #include "ripemd160.h" 12 : 13 : void 14 6118 : RIPEMD160Reset(RIPEMD160Context *ctxt) 15 : { 16 6118 : MDinit(ctxt->digest); 17 6118 : ctxt->noverflow = 0; 18 6118 : ctxt->length = 0; 19 6118 : } 20 : 21 : void 22 12235 : RIPEMD160Input(RIPEMD160Context *ctxt, const uint8_t *bytes, unsigned bytecount) 23 : { 24 : dword X[16]; 25 : 26 12235 : ctxt->length += bytecount; 27 12235 : if (ctxt->noverflow > 0) { 28 0 : if (ctxt->noverflow + bytecount < 64) { 29 0 : memcpy(ctxt->overflow + ctxt->noverflow, bytes, bytecount); 30 0 : ctxt->noverflow += bytecount; 31 0 : return; 32 : } 33 0 : memcpy(ctxt->overflow + ctxt->noverflow, bytes, bytecount - ctxt->noverflow); 34 : const uint8_t *x = ctxt->overflow; 35 0 : for (int i = 0; i < 16; i++) { 36 0 : X[i] = BYTES_TO_DWORD(x); 37 0 : x += 4; 38 : } 39 0 : bytecount -= ctxt->noverflow; 40 0 : bytes += ctxt->noverflow; 41 0 : ctxt->noverflow = 0; 42 0 : MDcompress(ctxt->digest, X); 43 : } 44 24469 : while (bytecount >= 64) { 45 207978 : for (int i = 0; i < 16; i++) { 46 195744 : X[i] = BYTES_TO_DWORD(bytes); 47 195744 : bytes += 4; 48 : } 49 12234 : bytecount -= 64; 50 12234 : MDcompress(ctxt->digest, X); 51 : } 52 12235 : if (bytecount > 0) 53 6118 : memcpy(ctxt->overflow, bytes, bytecount); 54 12235 : ctxt->noverflow = bytecount; 55 : } 56 : 57 : void 58 6118 : RIPEMD160Result(RIPEMD160Context *ctxt, uint8_t digest[RIPEMD160_DIGEST_LENGTH]) 59 : { 60 6118 : MDfinish(ctxt->digest, ctxt->overflow, (dword) ctxt->length, 0); 61 36708 : for (int i = 0; i < RIPEMD160_DIGEST_LENGTH; i += 4) { 62 30590 : digest[i] = (uint8_t) ctxt->digest[i >> 2]; 63 30590 : digest[i + 1] = (uint8_t) (ctxt->digest[i >> 2] >> 8); 64 30590 : digest[i + 2] = (uint8_t) (ctxt->digest[i >> 2] >> 16); 65 30590 : digest[i + 3] = (uint8_t) (ctxt->digest[i >> 2] >> 24); 66 : } 67 6118 : }