Search Code
About crypto

Published 29 Dec 2009.

Last change message: N/A

Import this program

crypto

Published 29 Dec 2009, by   user Anders Rundgren   tag No tags
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMACCore.cpp Source File

HMACCore.cpp

00001 /* ====================================================================
00002  * Copyright (c) 1998-2010 The OpenSSL Project.  All rights reserved.
00003  *
00004  * This product includes cryptographic software written by Eric Young
00005  * (eay@cryptsoft.com).  This product includes software written by Tim
00006  * Hudson (tjh@cryptsoft.com).
00007  *
00008  * ====================================================================
00009  * C++ adoption was made by Anders Rundgren (anders.rundgren@telia.com)
00010  * ====================================================================
00011  */
00012 
00013 #include <string.h>
00014 #include <stdlib.h>
00015 
00016 #include "crypto.h"
00017 
00018 
00019 namespace webpki
00020 {
00021 
00022 HMACCore::HMACCore (SHACore& outer, SHACore& inner)
00023   {
00024     m_outer_save = &outer;
00025     m_inner_save = &inner;
00026     m_error = NULL;
00027   }
00028 
00029 
00030 void HMACCore::init (const unsigned char* key, int key_length)
00031   {
00032     unsigned char padded_key[SHACore::SHA_CBLOCK];
00033     if (key_length > SHACore::SHA_CBLOCK)
00034       {
00035         m_inner_save->doFinal (padded_key, key, key_length);
00036         key_length = m_inner_save->m_sha_ctx.digest_length;
00037       }
00038     else
00039       {
00040         memcpy (padded_key, key, key_length);
00041       }
00042     for (int i = 0; i < SHACore::SHA_CBLOCK; i++)
00043       {
00044         padded_key[i] = i < key_length ? padded_key[i] ^ 0x36 : 0x36;
00045       }
00046     m_inner_save->update (padded_key, SHACore::SHA_CBLOCK);
00047     for (int i = 0; i < SHACore::SHA_CBLOCK; i++)
00048       {
00049         padded_key[i] = i < key_length ? padded_key[i] ^ (0x36 ^ 0x5c) : 0x5c;
00050       }
00051     m_outer_save->update (padded_key, SHACore::SHA_CBLOCK);
00052   }
00053 
00054 
00055 void HMACCore::update (const unsigned char* data, int length)
00056   {
00057     m_inner_save->update (data, length);
00058   }
00059 
00060 
00061 const char* HMACCore::doFinal (unsigned char* digest)
00062   {
00063     unsigned char inner_digest[SHA256Provider::DIGEST_LENGTH];
00064     m_inner_save->doFinal (inner_digest);
00065     return m_outer_save->doFinal (digest, inner_digest, m_inner_save->m_sha_ctx.digest_length);
00066   }
00067 
00068 
00069 const char* HMACCore::doFinal (unsigned char* digest, const unsigned char* data, int length)
00070   {
00071     update (data, length);
00072     return doFinal (digest);
00073   }
00074 
00075 }