cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
ctx_md5.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib MD5 Hash Routines *
4 * Copyright Peter Gutmann 1992-2005 *
5 * *
6 ****************************************************************************/
7 
8 #if defined( INC_ALL )
9  #include "crypt.h"
10  #include "context.h"
11  #include "md5.h"
12 #else
13  #include "crypt.h"
14  #include "context/context.h"
15  #include "crypt/md5.h"
16 #endif /* Compiler-specific includes */
17 
18 #ifdef USE_MD5
19 
20 #define HASH_STATE_SIZE sizeof( MD5_CTX )
21 
22 /****************************************************************************
23 * *
24 * MD5 Self-test Routines *
25 * *
26 ****************************************************************************/
27 
28 #ifndef CONFIG_NO_SELFTEST
29 
30 /* Test the MD5 output against the test vectors given in RFC 1321 */
31 
32 static const struct {
33  const char *data; /* Data to hash */
34  const int length; /* Length of data */
35  const BYTE digest[ MD5_DIGEST_LENGTH ]; /* Digest of data */
36  } FAR_BSS digestValues[] = {
37  { NULL, 0,
38  { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
39  0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E } },
40  { "a", 1,
41  { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
42  0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 } },
43  { "abc", 3,
44  { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
45  0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 } },
46  { "message digest", 14,
47  { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
48  0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 } },
49  { "abcdefghijklmnopqrstuvwxyz", 26,
50  { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
51  0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B } },
52  { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
53  { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
54  0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F } },
55  { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
56  { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
57  0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } },
58  { NULL, 0, { 0 } }
59  };
60 
61 static int selfTest( void )
62  {
63  const CAPABILITY_INFO *capabilityInfo = getMD5Capability();
64  BYTE hashState[ HASH_STATE_SIZE + 8 ];
65  int i, status;
66 
67  /* Test MD5 against the test vectors given in RFC 1320 */
68  for( i = 0; digestValues[ i ].data != NULL; i++ )
69  {
70  status = testHash( capabilityInfo, hashState, digestValues[ i ].data,
71  digestValues[ i ].length, digestValues[ i ].digest );
72  if( cryptStatusError( status ) )
73  return( status );
74  }
75 
76  return( CRYPT_OK );
77  }
78 #else
79  #define selfTest NULL
80 #endif /* !CONFIG_NO_SELFTEST */
81 
82 /****************************************************************************
83 * *
84 * Control Routines *
85 * *
86 ****************************************************************************/
87 
88 /* Return context subtype-specific information */
89 
91 static int getInfo( IN_ENUM( CAPABILITY_INFO ) const CAPABILITY_INFO_TYPE type,
93  OUT void *data,
94  IN_INT_Z const int length )
95  {
96  assert( contextInfoPtr == NULL || \
97  isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
98  assert( ( length == 0 && isWritePtr( data, sizeof( int ) ) ) || \
99  ( length > 0 && isWritePtr( data, length ) ) );
100 
102 
103  if( type == CAPABILITY_INFO_STATESIZE )
104  {
105  int *valuePtr = ( int * ) data;
106 
107  *valuePtr = HASH_STATE_SIZE;
108 
109  return( CRYPT_OK );
110  }
111 
112  return( getDefaultInfo( type, contextInfoPtr, data, length ) );
113  }
114 
115 /****************************************************************************
116 * *
117 * MD5 Hash Routines *
118 * *
119 ****************************************************************************/
120 
121 /* Hash data using MD5 */
122 
123 static int hash( CONTEXT_INFO *contextInfoPtr, BYTE *buffer, int noBytes )
124  {
125  MD5_CTX *md5Info = ( MD5_CTX * ) contextInfoPtr->ctxHash->hashInfo;
126 
127  /* If the hash state was reset to allow another round of hashing,
128  reinitialise things */
129  if( !( contextInfoPtr->flags & CONTEXT_FLAG_HASH_INITED ) )
130  MD5_Init( md5Info );
131 
132  if( noBytes > 0 )
133  {
134  MD5_Update( md5Info, buffer, noBytes );
135  }
136  else
137  {
138  MD5_Final( contextInfoPtr->ctxHash->hash, md5Info );
139  }
140 
141  return( CRYPT_OK );
142  }
143 
144 /* Internal API: Hash a single block of memory without the overhead of
145  creating an encryption context */
146 
147 void md5HashBuffer( HASHINFO hashInfo, BYTE *outBuffer,
148  const int outBufMaxLength, const void *inBuffer,
149  const int inLength, const HASH_STATE hashState )
150  {
151  MD5_CTX *md5Info = ( MD5_CTX * ) hashInfo;
152 
153  assert( isWritePtr( hashInfo, sizeof( HASHINFO ) ) );
154  assert( ( hashState != HASH_STATE_END && \
155  outBuffer == NULL && outBufMaxLength == 0 ) || \
156  ( hashState == HASH_STATE_END && \
157  isWritePtr( outBuffer, outBufMaxLength ) && \
158  outBufMaxLength >= 16 ) );
159  assert( inBuffer == NULL || isReadPtr( inBuffer, inLength ) );
160 
161  if( ( hashState == HASH_STATE_END && outBufMaxLength < 16 ) || \
162  ( hashState != HASH_STATE_END && inLength <= 0 ) )
164 
165  switch( hashState )
166  {
167  case HASH_STATE_START:
168  MD5_Init( md5Info );
169  /* Drop through */
170 
171  case HASH_STATE_CONTINUE:
172  MD5_Update( md5Info, ( BYTE * ) inBuffer, inLength );
173  break;
174 
175  case HASH_STATE_END:
176  if( inBuffer != NULL )
177  MD5_Update( md5Info, ( BYTE * ) inBuffer, inLength );
178  MD5_Final( outBuffer, md5Info );
179  break;
180 
181  default:
183  }
184  }
185 
186 void md5HashBufferAtomic( BYTE *outBuffer, const int outBufMaxLength,
187  const void *inBuffer, const int inLength )
188  {
189  MD5_CTX md5Info;
190 
191  assert( isWritePtr( outBuffer, outBufMaxLength ) && \
192  outBufMaxLength >= 16 );
193  assert( isReadPtr( inBuffer, inLength ) );
194 
195  if( outBufMaxLength < 16 || inLength <= 0 )
197 
198  MD5_Init( &md5Info );
199  MD5_Update( &md5Info, ( BYTE * ) inBuffer, inLength );
200  MD5_Final( outBuffer, &md5Info );
201  zeroise( &md5Info, sizeof( MD5_CTX ) );
202  }
203 
204 /****************************************************************************
205 * *
206 * Capability Access Routines *
207 * *
208 ****************************************************************************/
209 
210 static const CAPABILITY_INFO FAR_BSS capabilityInfo = {
211  CRYPT_ALGO_MD5, bitsToBytes( 128 ), "MD5", 3,
212  bitsToBytes( 0 ), bitsToBytes( 0 ), bitsToBytes( 0 ),
213  selfTest, getInfo, NULL, NULL, NULL, NULL, hash, hash
214  };
215 
216 const CAPABILITY_INFO *getMD5Capability( void )
217  {
218  return( &capabilityInfo );
219  }
220 
221 #endif /* USE_MD5 */