whirlpool/whirlpool_finalize.c

Go to the documentation of this file.
00001 // Copyright 2006 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 modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (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
00019 // Free Software Foundation, Inc.,
00020 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 
00022 #include "str.h"
00023 #include "whirlpool.h"
00024 
00025 void whirlpool_finalize(whirlpool_t * const context, 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                         str_zero(&buf[pos], 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                 str_zero(&buf[pos], (WBLOCKBYTES - LENGTHBYTES) - pos);
00052         
00053         pos = WBLOCKBYTES - LENGTHBYTES;
00054         
00055         /* append bit length of hashed data */
00056         str_cpyn(&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 Sun Dec 3 17:45:53 2006 for lucid by  doxygen 1.5.1