Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pana_sha1.cxx

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
00033 
00034 #include "ace/OS.h"
00035 #include "pana_sha1.h"
00036 
00037 PANA_Sha1::PANA_Sha1()
00038 {
00039     m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
00040 
00041     Reset();
00042 }
00043 
00044 PANA_Sha1::~PANA_Sha1()
00045 {
00046     Reset();
00047 }
00048 
00049 void PANA_Sha1::Reset()
00050 {
00051     // SHA1 initialization constants
00052     m_state[0] = 0x67452301;
00053     m_state[1] = 0xEFCDAB89;
00054     m_state[2] = 0x98BADCFE;
00055     m_state[3] = 0x10325476;
00056     m_state[4] = 0xC3D2E1F0;
00057 
00058     m_count[0] = 0;
00059     m_count[1] = 0;
00060 }
00061 
00062 void PANA_Sha1::Transform(unsigned long state[5], unsigned char buffer[64])
00063 {
00064     unsigned long a = 0, b = 0, c = 0, d = 0, e = 0;
00065 
00066     ACE_OS::memcpy(m_block, buffer, 64);
00067 
00068     // Copy state[] to working vars
00069     a = state[0];
00070     b = state[1];
00071     c = state[2];
00072     d = state[3];
00073     e = state[4];
00074 
00075     // 4 rounds of 20 operations each. Loop unrolled.
00076     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
00077     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
00078     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
00079     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
00080     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
00081     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
00082     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
00083     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
00084     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
00085     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
00086     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
00087     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
00088     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
00089     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
00090     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
00091     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
00092     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
00093     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
00094     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
00095     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
00096 
00097     // Add the working vars back into state[]
00098     state[0] += a;
00099     state[1] += b;
00100     state[2] += c;
00101     state[3] += d;
00102     state[4] += e;
00103 
00104     // Wipe variables
00105     a = 0; b = 0; c = 0; d = 0; e = 0;
00106 }
00107 
00108 // Use this function to hash in binary data and strings
00109 void PANA_Sha1::Update(unsigned char* data, unsigned int len)
00110 {
00111     unsigned long i = 0, j = 0;
00112 
00113     j = (m_count[0] >> 3) & 63;
00114 
00115     if ((m_count[0] += len << 3) < (len << 3)) {
00116         m_count[1]++;
00117     }
00118 
00119     m_count[1] += (len >> 29);
00120 
00121     if((j + len) > 63) {
00122 
00123         ACE_OS::memcpy(&m_buffer[j], data, (i = 64 - j));
00124         Transform(m_state, m_buffer);
00125 
00126         for (; i+63 < len; i += 64)  {
00127             Transform(m_state, &data[i]);
00128         }
00129 
00130         j = 0;
00131     }
00132     else {
00133         i = 0;
00134     }
00135 
00136     ACE_OS::memcpy(&m_buffer[j], &data[i], len - i);
00137 }
00138 
00139 void PANA_Sha1::Final()
00140 {
00141     unsigned long i = 0, j = 0;
00142     unsigned char finalcount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
00143 
00144     for (i = 0; i < 8; i++) {
00145         finalcount[i] = (unsigned char)((m_count[(i >= 4 ? 0 : 1)]
00146             >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
00147     }
00148 
00149     Update((unsigned char *)"\200", 1);
00150 
00151     while ((m_count[0] & 504) != 448) {
00152         Update((unsigned char *)"\0", 1);
00153     }
00154 
00155     Update(finalcount, 8); // Cause a SHA1Transform()
00156 
00157     for (i = 0; i < 20; i++) {
00158         m_digest[i] = (unsigned char)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
00159     }
00160 
00161     // Wipe variables for security reasons
00162     i = 0; j = 0;
00163     memset(m_buffer, 0, 64);
00164     memset(m_state, 0, 20);
00165     memset(m_count, 0, 8);
00166     memset(finalcount, 0, 8);
00167 
00168     Transform(m_state, m_buffer);
00169 }
00170 
00171 void PANA_Sha1::ReportHash(char *report, unsigned char type)
00172 {
00173     unsigned char i = 0;
00174     char szTemp[5];
00175 
00176     if(type == REPORT_HEX) {
00177 
00178         ACE_OS::sprintf(szTemp, "%02X", m_digest[0]);
00179         ACE_OS::strcat(report, szTemp);
00180 
00181         for(i = 1; i < 20; i++) {
00182             ACE_OS::sprintf(szTemp, " %02X", m_digest[i]);
00183             ACE_OS::strcat(report, szTemp);
00184         }
00185     }
00186     else if(type == REPORT_DIGIT) {
00187 
00188         ACE_OS::sprintf(szTemp, "%u", m_digest[0]);
00189         ACE_OS::strcat(report, szTemp);
00190 
00191         for(i = 1; i < 20; i++) {
00192             ACE_OS::sprintf(szTemp, " %u", m_digest[i]);
00193             ACE_OS::strcat(report, szTemp);
00194         }
00195     }
00196     else {
00197         ACE_OS::strcpy(report, "Error: Unknown report type!");
00198     }
00199 }
00200 
00201 void PANA_Sha1::GetHash(unsigned char *uDest)
00202 {
00203     ACE_OS::memcpy(uDest, m_digest, 20);
00204 }

Generated on Fri Jun 25 19:18:30 2004 for PANA by doxygen 1.3.5