whirlpool/whirlpool_finalize.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // The Whirlpool algorithm was developed by
00004 //                Paulo S. L. M. Barreto <pbarreto@scopus.com.br> and
00005 //                Vincent Rijmen <vincent.rijmen@cryptomathic.com>
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00020 
00021 #include "mem.h"
00022 #include "whirlpool.h"
00023 
00024 void whirlpool_finalize(whirlpool_t * const context,
00025                 unsigned char * const result)
00026 {
00027         int i;
00028         uint8_t *buf    = context->buf;
00029         uint8_t *len    = context->len;
00030         int bits        = context->bits;
00031         int pos         = context->pos;
00032         uint8_t *digest = result;
00033 
00034         /* append a '1'-bit */
00035         buf[pos] |= 0x80U >> (bits & 7);
00036         pos++; /* all remaining bits on the current uint8_t are set to zero. */
00037 
00038         /* pad with zero bits to complete (N*WBLOCKBITS - LENGTHBITS) bits */
00039         if (pos > WBLOCKBYTES - LENGTHBYTES) {
00040                 if (pos < WBLOCKBYTES)
00041                         mem_set(&buf[pos], 0, WBLOCKBYTES - pos);
00042 
00043                 /* process data block */
00044                 whirlpool_transform(context);
00045 
00046                 /* reset buffer */
00047                 pos = 0;
00048         }
00049 
00050         if (pos < WBLOCKBYTES - LENGTHBYTES)
00051                 mem_set(&buf[pos], 0, (WBLOCKBYTES - LENGTHBYTES) - pos);
00052 
00053         pos = WBLOCKBYTES - LENGTHBYTES;
00054 
00055         /* append bit length of hashed data */
00056         mem_cpy(&buf[WBLOCKBYTES - LENGTHBYTES], len, LENGTHBYTES);
00057 
00058         /* process data block */
00059         whirlpool_transform(context);
00060 
00061         /* return the completed message digest */
00062         for (i = 0; i < DIGESTBYTES/8; i++) {
00063                 digest[0] = (uint8_t)(context->hash[i] >> 56);
00064                 digest[1] = (uint8_t)(context->hash[i] >> 48);
00065                 digest[2] = (uint8_t)(context->hash[i] >> 40);
00066                 digest[3] = (uint8_t)(context->hash[i] >> 32);
00067                 digest[4] = (uint8_t)(context->hash[i] >> 24);
00068                 digest[5] = (uint8_t)(context->hash[i] >> 16);
00069                 digest[6] = (uint8_t)(context->hash[i] >>  8);
00070                 digest[7] = (uint8_t)(context->hash[i]      );
00071                 digest += 8;
00072         }
00073 
00074         context->bits = bits;
00075         context->pos  = pos;
00076 }

Generated on Tue Jun 19 20:38:27 2007 for lucid by  doxygen 1.5.2