Sans Pareil Technologies, Inc.

Key To Your Business

MD5Engine



A DigestEngine implementation for generating MD5 digest values.

MD5Engine.h

The definition of the MD5 engine.

    1: /*
    2: * MD5Engine.h
    3: *
    4: * Definition of class MD5Engine.
    5: *
    6: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
    7: * and Contributors.
    8: *
    9: * Permission is hereby granted, free of charge, to any person or organization
   10: * obtaining a copy of the software and accompanying documentation covered by
   11: * this license (the "Software") to use, reproduce, display, distribute,
   12: * execute, and transmit the Software, and to prepare derivative works of the
   13: * Software, and to permit third-parties to whom the Software is furnished to
   14: * do so, all subject to the following:
   15: *
   16: * The copyright notices in the Software and this entire statement, including
   17: * the above license grant, this restriction and the following disclaimer,
   18: * must be included in all copies of the Software, in whole or in part, and
   19: * all derivative works of the Software, unless such copies or derivative
   20: * works are solely in the form of machine-executable object code generated by
   21: * a source language processor.
   22: *
   23: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   24: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   25: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   26: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   27: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   28: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   29: * DEALINGS IN THE SOFTWARE.
   30: *
   31: *
   32: * MD5 (RFC 1321) algorithm:
   33: * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
   34: * rights reserved.
   35: *
   36: * License to copy and use this software is granted provided that it
   37: * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
   38: * Algorithm" in all material mentioning or referencing this software
   39: * or this function.
   40: *
   41: * License is also granted to make and use derivative works provided
   42: * that such works are identified as "derived from the RSA Data
   43: * Security, Inc. MD5 Message-Digest Algorithm" in all material
   44: * mentioning or referencing the derived work.
   45: *
   46: * RSA Data Security, Inc. makes no representations concerning either
   47: * the merchantability of this software or the suitability of this
   48: * software for any particular purpose. It is provided "as is"
   49: * without express or implied warranty of any kind.
   50: *
   51: * These notices must be retained in any copies of any part of this
   52: * documentation and/or software.
   53: */
   54:
   55: #ifndef SPT_DIGEST_MD5ENGINE
   56: #define SPT_DIGEST_MD5ENGINE
   57:
   58:
   59: #include "DigestEngine.h"
   60:
   61:
   62: namespace spt
   63: {
   64:   namespace digest
   65:   {
   66:     /// This class implementes the MD5 message digest algorithm,
   67:     /// described in RFC 1321.
   68:     class MD5Engine: public DigestEngine
   69:     {
   70:     public:
   71:       enum
   72:       {
   73:         BLOCK_SIZE  = 64,
   74:         DIGEST_SIZE = 16
   75:       };
   76:
   77:       MD5Engine();
   78:       ~MD5Engine();
   79:
   80:       uint32_t digestLength() const;
   81:       void reset();
   82:       const DigestEngine::Digest& digest();
   83:
   84:     protected:
   85:       void updateImpl( const void* data, uint32_t length );
   86:
   87:     private:
   88:       static void transform( uint32_t state[4], const Byte block[64] );
   89:       static void encode( Byte* output, const uint32_t* input, uint32_t len );
   90:       static void decode( uint32_t* output, const Byte* input, uint32_t len );
   91:
   92:       struct Context
   93:       {
   94:         uint32_t state[4];  // state (ABCD)
   95:         uint32_t count[2];  // number of bits, modulo 2^64 (lsb first)
   96:         Byte buffer[64];    // input buffer
   97:       };
   98:
   99:       Context context;
  100:       DigestEngine::Digest digestValue;
  101:
  102:       MD5Engine( const MD5Engine& );
  103:       MD5Engine& operator = ( const MD5Engine& );
  104:     };
  105:
  106:   } // namespace digest
  107:
  108: } // namespace spt
  109:
  110:
  111: #endif // SPT_DIGEST_MD5ENGINE


MD5Engine.cpp

The implementation of the MD5 digest algorithm.

    1: /*
    2: * MD5Engine.cpp
    3: *
    4: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
    5: * and Contributors.
    6: *
    7: * Permission is hereby granted, free of charge, to any person or organization
    8: * obtaining a copy of the software and accompanying documentation covered by
    9: * this license (the "Software") to use, reproduce, display, distribute,
   10: * execute, and transmit the Software, and to prepare derivative works of the
   11: * Software, and to permit third-parties to whom the Software is furnished to
   12: * do so, all subject to the following:
   13: *
   14: * The copyright notices in the Software and this entire statement, including
   15: * the above license grant, this restriction and the following disclaimer,
   16: * must be included in all copies of the Software, in whole or in part, and
   17: * all derivative works of the Software, unless such copies or derivative
   18: * works are solely in the form of machine-executable object code generated by
   19: * a source language processor.
   20: *
   21: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   22: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   23: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   24: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   25: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   26: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   27: * DEALINGS IN THE SOFTWARE.
   28: *
   29: *
   30: * MD5 (RFC 1321) algorithm:
   31: * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
   32: * rights reserved.
   33: *
   34: * License to copy and use this software is granted provided that it
   35: * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
   36: * Algorithm" in all material mentioning or referencing this software
   37: * or this function.
   38: *
   39: * License is also granted to make and use derivative works provided
   40: * that such works are identified as "derived from the RSA Data
   41: * Security, Inc. MD5 Message-Digest Algorithm" in all material
   42: * mentioning or referencing the derived work.
   43: *
   44: * RSA Data Security, Inc. makes no representations concerning either
   45: * the merchantability of this software or the suitability of this
   46: * software for any particular purpose. It is provided "as is"
   47: * without express or implied warranty of any kind.
   48: *
   49: * These notices must be retained in any copies of any part of this
   50: * documentation and/or software.
   51: */
   52:
   53:
   54: #include "MD5Engine.h"
   55:
   56: #if defined( ARDUINO )
   57: #include "../StandardCplusplus/cstring"
   58: #else
   59: #include <cstring>
   60: #endif
   61:
   62:
   63: using spt::Byte;
   64: using spt::digest::DigestEngine;
   65: using spt::digest::MD5Engine;
   66:
   67:
   68: MD5Engine::MD5Engine()
   69: {
   70:   digestValue.reserve(16);
   71:   reset();
   72: }
   73:
   74:
   75: MD5Engine::~MD5Engine()
   76: {
   77:   reset();
   78: }
   79:
   80:  
   81: void MD5Engine::updateImpl( const void* input_, uint32_t inputLen )
   82: {
   83:   const Byte* input = static_cast<const Byte*>( input_ );
   84:   uint32_t i, index, partLen;
   85:
   86:   /* Compute number of bytes mod 64 */
   87:   index = static_cast<uint32_t>( ( context.count[0] >> 3 ) & 0x3F );
   88:
   89:   /* Update number of bits */
   90:   if ( ( context.count[0] += static_cast<uint32_t>( inputLen << 3 ) ) <
   91:       static_cast<uint32_t>( inputLen << 3 ) ) context.count[1]++;
   92:
   93:   context.count[1] += static_cast<uint32_t>( inputLen >> 29 );
   94:
   95:   partLen = 64 - index;
   96:
   97:   /* Transform as many times as possible. */
   98:   if ( inputLen >= partLen )
   99:   {
  100:     std::memcpy( &context.buffer[index], input, partLen );
  101:     transform( context.state, context.buffer );
  102:
  103:     for ( i = partLen; i + 63 < inputLen; i += 64 ) transform( context.state, &input[i] );
  104:
  105:     index = 0;
  106:   }
  107:   else i = 0;
  108:
  109:   /* Buffer remaining input */
  110:   std::memcpy( &context.buffer[index], &input[i], inputLen - i );
  111: }
  112:
  113:
  114: uint32_t MD5Engine::digestLength() const
  115: {
  116:   return DIGEST_SIZE;
  117: }
  118:
  119:
  120: void MD5Engine::reset()
  121: {
  122:   std::memset( &context, 0, sizeof(context) );
  123:   context.count[0] = context.count[1] = 0;
  124:   context.state[0] = 0x67452301;
  125:   context.state[1] = 0xefcdab89;
  126:   context.state[2] = 0x98badcfe;
  127:   context.state[3] = 0x10325476;
  128: }
  129:
  130:
  131: const DigestEngine::Digest& MD5Engine::digest()
  132: {
  133:   static const Byte PADDING[64] =
  134:   {
  135:     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  136:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  138:   };
  139:
  140:   Byte bits[8];
  141:   uint32_t index, padLen;
  142:
  143:   /* Save number of bits */
  144:   encode(bits, context.count, 8);
  145:
  146:   /* Pad out to 56 mod 64. */
  147:   index = static_cast<uint32_t>( ( context.count[0] >> 3 ) & 0x3f );
  148:   padLen = ( index < 56 ) ? ( 56 - index ) : ( 120 - index );
  149:   update( PADDING, padLen );
  150:
  151:   /* Append length (before padding) */
  152:   update( bits, 8 );
  153:
  154:   /* Store state in digest */
  155:   Byte digest[16];
  156:   encode( digest, context.state, 16 );
  157:   digestValue.clear();
  158:   digestValue.insert( digestValue.begin(), digest, digest + sizeof( digest ) );
  159:
  160:   /* Zeroize sensitive information. */
  161:   std::memset( &context, 0, sizeof( context ) );
  162:   reset();
  163:   return digestValue;
  164: }
  165:
  166:
  167: namespace spt
  168: {
  169:   namespace digest
  170:   {
  171:
  172: /* Constants for MD5Transform routine. */
  173: #define S11 7
  174: #define S12 12
  175: #define S13 17
  176: #define S14 22
  177: #define S21 5
  178: #define S22 9
  179: #define S23 14
  180: #define S24 20
  181: #define S31 4
  182: #define S32 11
  183: #define S33 16
  184: #define S34 23
  185: #define S41 6
  186: #define S42 10
  187: #define S43 15
  188: #define S44 21
  189:
  190:
  191: /* F, G, H and I are basic MD5 functions. */
  192: #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  193: #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  194: #define H(x, y, z) ((x) ^ (y) ^ (z))
  195: #define I(x, y, z) ((y) ^ ((x) | (~z)))
  196:
  197:
  198: /* ROTATE_LEFT rotates x left n bits. */
  199: #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  200:
  201:
  202: /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  203:    Rotation is separate from addition to prevent recomputation. */
  204: #define FF(a, b, c, d, x, s, ac) { \
  205:  (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  206:  (a) = ROTATE_LEFT ((a), (s)); \
  207:  (a) += (b); \
  208:   }
  209: #define GG(a, b, c, d, x, s, ac) { \
  210:  (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  211:  (a) = ROTATE_LEFT ((a), (s)); \
  212:  (a) += (b); \
  213:   }
  214: #define HH(a, b, c, d, x, s, ac) { \
  215:  (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  216:  (a) = ROTATE_LEFT ((a), (s)); \
  217:  (a) += (b); \
  218:   }
  219: #define II(a, b, c, d, x, s, ac) { \
  220:  (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  221:  (a) = ROTATE_LEFT ((a), (s)); \
  222:  (a) += (b); \
  223:   }
  224:
  225:   }
  226: }
  227:
  228:
  229: void MD5Engine::transform( uint32_t state[4], const Byte block[64] )
  230: {
  231:   uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  232:
  233:   decode( x, block, 64 );
  234:
  235:   /* Round 1 */
  236:   FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  237:   FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  238:   FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  239:   FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  240:   FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  241:   FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  242:   FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  243:   FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  244:   FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  245:   FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  246:   FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  247:   FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  248:   FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  249:   FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  250:   FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  251:   FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  252:
  253:   /* Round 2 */
  254:   GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  255:   GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  256:   GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  257:   GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  258:   GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  259:   GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  260:   GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  261:   GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  262:   GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  263:   GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  264:   GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  265:   GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  266:   GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  267:   GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  268:   GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  269:   GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  270:
  271:   /* Round 3 */
  272:   HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  273:   HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  274:   HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  275:   HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  276:   HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  277:   HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  278:   HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  279:   HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  280:   HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  281:   HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  282:   HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  283:   HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  284:   HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  285:   HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  286:   HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  287:   HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  288:
  289:   /* Round 4 */
  290:   II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  291:   II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  292:   II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  293:   II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  294:   II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  295:   II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  296:   II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  297:   II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  298:   II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  299:   II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  300:   II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  301:   II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  302:   II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  303:   II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  304:   II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  305:   II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  306:
  307:   state[0] += a;
  308:   state[1] += b;
  309:   state[2] += c;
  310:   state[3] += d;
  311:
  312:   /* Zeroize sensitive information. */
  313:   std::memset( x, 0, sizeof( x ) );
  314: }
  315:
  316:
  317: void MD5Engine::encode( Byte* output, const uint32_t* input, uint32_t len)
  318: {
  319:   uint32_t i, j;
  320:
  321:   for ( i = 0, j = 0; j < len; i++, j += 4 )
  322:   {
  323:     output[j]   = static_cast<Byte>( input[i] & 0xff );
  324:     output[j+1] = static_cast<Byte>( ( input[i] >> 8 ) & 0xff );
  325:     output[j+2] = static_cast<Byte>( ( input[i] >> 16 ) & 0xff );
  326:     output[j+3] = static_cast<Byte>( ( input[i] >> 24 ) & 0xff );
  327:   }
  328: }
  329:
  330:
  331: void MD5Engine::decode( uint32_t* output, const Byte* input, uint32_t len )
  332: {
  333:   uint32_t i, j;
  334:
  335:   for ( i = 0, j = 0; j < len; i++, j += 4 )
  336:   {
  337:     output[i] = static_cast<uint32_t>( input[j] ) |
  338:         ( static_cast<uint32_t>( input[j+1] ) << 8 ) |
  339:         ( static_cast<uint32_t>( input[j+2] ) << 16 ) |
  340:         ( static_cast<uint32_t>( input[j+3] ) << 24 );
  341:   }
  342: }


MD5EngineTest.cpp

    1: Unit tests for the MD5 digest computation.
    2:
    3: #if defined( ARDUINO )
    4: #include "tut.hpp"
    5: #include "MD5Engine.h"
    6: #else
    7: #include <tut/tut.hpp>
    8: #include <SPT.h>
    9: #include <digest/MD5Engine.h>
   10: #endif
   11:
   12: using spt::digest::DigestEngine;
   13: using spt::digest::MD5Engine;
   14:
   15: namespace tut
   16: {
   17:   struct MD5EngineTestData {};
   18:
   19:   typedef test_group<MD5EngineTestData> MD5EngineTestGroup;
   20:   typedef MD5EngineTestGroup::object MD5EngineTest;
   21:   MD5EngineTestGroup md5EngineTestGroup( "MD5Engine test suite" );
   22:
   23:
   24:   template<>
   25:   template<>
   26:   void MD5EngineTest::test<1>()
   27:   {
   28:     set_test_name( "basic" );
   29:
   30:     MD5Engine engine;
   31:
   32:     // test vectors from RFC 1321
   33:
   34:     engine.update( "" );
   35:     ensure( "Empty string",
   36:       DigestEngine::digestToHex(engine.digest()) == "d41d8cd98f00b204e9800998ecf8427e" );
   37:
   38:     engine.update("a");
   39:     ensure( "Add a",
   40:         DigestEngine::digestToHex(engine.digest()) == "0cc175b9c0f1b6a831c399e269772661" );
   41:
   42:     engine.update("abc");
   43:     ensure( "Add abc",
   44:         DigestEngine::digestToHex(engine.digest()) == "900150983cd24fb0d6963f7d28e17f72");
   45:
   46:     engine.update("message digest");
   47:     ensure( "Add message digest",
   48:         DigestEngine::digestToHex(engine.digest()) == "f96b697d7cb7938d525a2f31aaf161d0");
   49:
   50:     engine.update("abcdefghijklmnopqrstuvwxyz");
   51:     ensure( "Add lower case",
   52:         DigestEngine::digestToHex(engine.digest()) == "c3fcd3d76192e4007dfb496cca67e13b");
   53:
   54:     engine.update("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
   55:     engine.update("abcdefghijklmnopqrstuvwxyz0123456789");
   56:     ensure( "Added upper and lower",
   57:         DigestEngine::digestToHex(engine.digest()) == "d174ab98d277d9f5a5611c2c9f419d9f");
   58:
   59:     engine.update("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
   60:     ensure( "Added numbers",
   61:         DigestEngine::digestToHex(engine.digest()) == "57edf4a22be3c955ac49da2e2107b67a");
   62:     std::cout << "Free SRAM: " << spt::freeRam() << std::endl;
   63:   }
   64: }