bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SHA1Digest


From: Elizabeth Barham
Subject: SHA1Digest
Date: 16 Nov 2002 20:14:17 -0600

Dear CommonC++ Maintainers,

   I really enjoy your fine work and added an SHA1Digest
implementation that adheres to the Digest interface. I tested it and
it works. I would like to contribute it to the project. Thank you,
Elizabeth

/* $Id: sha1.cpp,v 1.6 2002/11/17 02:05:26 lizzy Exp $ */
/*
  
  Digest pseudo abstract class, Copyright (C) 1999-2002 Open Source
  Telecom Corporation

  sha1.cpp, C++ implementation of the SHA-1 digest algorithm, as
    according to FIPS 180-1, <http://www.itl.nist.gov/fipspubs/fip180-1.htm>
  Copyright (C) 2002 Elizabeth Barham

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

  As a special exception to the GNU General Public License, permission is
  granted for additional uses of the text contained in its release
  Common C++.

  The exception is that, if you link the Common C++ library with other
  files to produce an executable, this does not by itself cause the
  resulting executable to be covered by the GNU General Public
  License.  Your use of that executable is in no way restricted on
  account of linking the Common C++ library code into it.

  This exception does not however invalidate any other reasons why the
  executable file might be covered by the GNU General Public License.

  This exception applies only to the code released under the name
  Common C++.  If you copy code from other releases into a copy of
  Common C++, as the General Public License permits, the exception
  does not apply to the code that you add in this way.  To avoid
  misleading anyone as to the status of such modified files, you must
  delete this exception notice from them.

  If you write modifications of your own for Common C++, it is your
  choice whether to permit this exception to apply to your
  modifications.  If you do not wish that, delete this exception
  notice.

*/

#include <iostream>
#include <stdint.h>
#include <string.h>
#include <iomanip.h>

class Digest : protected std::streambuf, public std::ostream
{
protected:
        Digest();

        /**
         * Reset the digest table to an initial default value.
         */
        virtual void initDigest(void) = 0;

        /**
         * Get the size of a digest in octets.
         *
         * @return number of bytes in digest.
         */
        virtual unsigned getSize(void) = 0;

        /**
         * Copy the binary digest buffer to user memory.
         *
         * @return number of bytes in digest.
         * @param buffer to write into.
         */
        virtual unsigned getDigest(unsigned char *buffer) = 0;

        /**
         * Put data into the digest bypassing the stream subsystem.
         *
         * @param buffer to read from.
         * @param length of data.
         */
        virtual void putDigest(const unsigned char *buffer, unsigned length) = 
0;

        /**
         * print a digest string for export.
         *
         * @return string representation of digest.
         */
         virtual std::ostream &strDigest(std::ostream &os) = 0;

         friend std::ostream &operator<<(std::ostream &os, Digest &ia)
           {return ia.strDigest(os);};

};

Digest::Digest() :
streambuf()
#ifdef  HAVE_OLD_IOSTREAM
,ostream()
#else
,ostream((streambuf *)this)
#endif
{ 
#ifdef  HAVE_OLD_IOSTREAM
        init((streambuf *)this);
#endif
}



class SHA1Digest : public Digest {
private:
  uint32_t h[5];
  uint64_t totalLength;
  unsigned char tempBuffer[64];

  enum {
    a = 0, b, c, d, e
  };

 protected:
  std::ostream &strDigest(std::ostream &os);
  void processSingleBlock(const unsigned char * buffer, uint64_t begin = 0);
  uint32_t fK(unsigned t, const uint32_t ae[]);

 public:
  SHA1Digest();

  void initDigest(void);

  inline unsigned getSize()
    { return 20; };

  unsigned getDigest(unsigned char *buffer);

  void putDigest(const unsigned char *buffer, unsigned len);

};

#define S(n,X) ( (  X  << n) | (  X  >> 32 - n) )

SHA1Digest::SHA1Digest(): Digest() {
  initDigest();
}

void SHA1Digest::initDigest(void) {
  h[0] = 0x67452301;
  h[1] = 0xefcdab89;
  h[2] = 0x98badcfe;
  h[3] = 0x10325476;
  h[4] = 0xc3d2e1f0;
  totalLength = 0;
}

uint32_t SHA1Digest::fK(unsigned t, const uint32_t ae[]) {

  if(t < 20)
    return( ( ( ae[b] & ae[c] ) | ( ~ae[b] & ae[d] )  ) + 0x5A827999);
  if(t < 40)
    return( (ae[b] ^ ae[c] ^ ae[d]) + 0x6ED9EBA1 );
  if(t < 60)
    return( ( (ae[b] & ae[c]) | (ae[b] & ae[d]) | (ae[c] & ae[d]) )+ 
0x8F1BBCDC);
  return( (ae[b] ^ ae[c] ^ ae[d]) + 0xCA62C1D6);
}

#define MASK 0x0000000F

void SHA1Digest::processSingleBlock(const unsigned char * buffer, uint64_t 
begin) {
  uint32_t W[16];
  uint32_t temp;
  int t;
  uint32_t ae[5];

  memcpy(&W, buffer + begin, sizeof(W));
  memcpy(ae, h, sizeof(h));
  
  for(t = 0; t < 80; t++) {
    uint32_t s = t & MASK;
    if(t >= 16) {
      W[s] = S(1, (W[(s + 13) & MASK] ^  W[(s + 8) & MASK] ^  W[(s + 2) & MASK] 
^  W[s]));
    }
    temp = S(5, ae[a]) + ae[e] + W[s] + fK(t, ae);

    ae[e] = ae[d];
    ae[d] = ae[c];
    ae[c] = S(30, ae[b]);
    ae[b] = ae[a];
    ae[a] = temp;

  }

  for(t = 0; t < 5; t++) {
    // h[0] += ae[a] etc...
    h[t] += ae[t];
  }
}

void SHA1Digest::putDigest(const unsigned char * buffer, unsigned len) {
  if(len == 0) {
    return;
  }

  // first check and see if this is a continuation in that there is
  // still data in tempBuffer from a previous data entry:

  int remainder;
  int pos = 0;

  if( remainder = ( totalLength % 512 ) != 0 ) {
    // this is a continuation, so we fill up tempbuff with
    // what ever is in buffer and use it first
    
    int cpyAmount = (512 - remainder) / 8;
    if(len < cpyAmount) {
      cpyAmount = len;
    }

    memcpy(tempBuffer + remainder, buffer, cpyAmount);
    totalLength += cpyAmount * 8;
    pos = cpyAmount;

    // if there is more data on the way, then we need to process
    // tempBuffer

    if( (len - pos) > 0 ) {
      processSingleBlock(tempBuffer);
    }
  }
    

  // now we just loop through the incoming stream, buffer, 64 bytes at
  // a time. Any left overs are placed into tempBuffer:
  
  while(unsigned diff = len - pos) {
    if(diff >= 64) {
      processSingleBlock(buffer, pos);
      pos += 64;
      totalLength += 512;
    } else {
      memcpy(tempBuffer, buffer + pos, diff);
      pos += diff;
      totalLength += ( diff * 8 );
    }
  }
}
        
unsigned SHA1Digest::getDigest(unsigned char * buffer) {
  unsigned char myTempBuffer[64];
  int remainder = totalLength % 512;
  int pos       = remainder / 8;
  int fillToBytes;

  memcpy((void*)myTempBuffer, tempBuffer, sizeof(tempBuffer));

  if(remainder <= 448) {
    fillToBytes = 56;
  } else {
    fillToBytes = 64;
  }

  // add the 1:

  myTempBuffer[pos++] = 0x80;
  
  do {
    bzero(myTempBuffer + pos, fillToBytes - pos);
    if(fillToBytes == 56) {
      // add the length onto the end
      memcpy((void*)(myTempBuffer + 56), (void*)&totalLength, 
sizeof(totalLength));
    }
    processSingleBlock(myTempBuffer);
    pos = 0;
    fillToBytes -= 8;
  } while(fillToBytes > 48);
  
  memcpy((void*)buffer, (void*)h, 20);
  return 20;
}

std::ostream & SHA1Digest::strDigest(std::ostream &os) {
  unsigned char buff[20];
  getDigest(buff);
  int tmp;

  for(int i = 0; i < 20; i++) {
    tmp = buff[i];
    os << std::setbase(16) << std::setw(2) << std::setfill('0') << tmp;
  }
  return os;
}

#ifdef TEST_SHA1

#include <stdio.h>

#define TEST_BUFFER_SIZE 1024

int main(int argc, const char * argv[]) {
  SHA1Digest digest;

  for(int i = 1 ; i < argc; i++) {
    digest.initDigest();
    FILE *fp = fopen(argv[i], "rb");
    if(fp) {
      int length;
      unsigned char buff[TEST_BUFFER_SIZE];
      while(length = fread(buff, sizeof(unsigned char), TEST_BUFFER_SIZE, fp)) {
        digest.putDigest(buff, length);
      }
      std::cout << digest << "  " << argv[i] << std::endl;
    } else {
      std::cerr << "Error reading file: " << argv[i] << std::endl;
    }
    fclose(fp);
  }
}

#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]