cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
ctx_ripe.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib RIPEMD-160 Hash Routines *
4 * Copyright Peter Gutmann 1996-2005 *
5 * *
6 ****************************************************************************/
7 
8 #if defined( INC_ALL )
9  #include "crypt.h"
10  #include "context.h"
11  #include "ripemd.h"
12 #else
13  #include "crypt.h"
14  #include "context/context.h"
15  #include "crypt/ripemd.h"
16 #endif /* Compiler-specific includes */
17 
18 #ifdef USE_RIPEMD160
19 
20 #define HASH_STATE_SIZE sizeof( RIPEMD160_CTX )
21 
22 /****************************************************************************
23 * *
24 * RIPEMD160 Self-test Routines *
25 * *
26 ****************************************************************************/
27 
28 #ifndef CONFIG_NO_SELFTEST
29 
30 /* Test the RIPEMD160 output against the test vectors given in the RIPEMD-160
31  paper */
32 
33 static const struct {
34  const char FAR_BSS *data; /* Data to hash */
35  const int length; /* Length of data */
36  const BYTE digest[ RIPEMD160_DIGEST_LENGTH ]; /* Digest of data */
37  } FAR_BSS digestValues[] = {
38  { NULL, 0,
39  { 0x9C, 0x11, 0x85, 0xA5, 0xC5, 0xE9, 0xFC, 0x54,
40  0x61, 0x28, 0x08, 0x97, 0x7E, 0xE8, 0xF5, 0x48,
41  0xB2, 0x25, 0x8D, 0x31 } },
42  { "a", 1,
43  { 0x0B, 0xDC, 0x9D, 0x2D, 0x25, 0x6B, 0x3E, 0xE9,
44  0xDA, 0xAE, 0x34, 0x7B, 0xE6, 0xF4, 0xDC, 0x83,
45  0x5A, 0x46, 0x7F, 0xFE } },
46  { "abc", 3,
47  { 0x8E, 0xB2, 0x08, 0xF7, 0xE0, 0x5D, 0x98, 0x7A,
48  0x9B, 0x04, 0x4A, 0x8E, 0x98, 0xC6, 0xB0, 0x87,
49  0xF1, 0x5A, 0x0B, 0xFC } },
50  { "message digest", 14,
51  { 0x5D, 0x06, 0x89, 0xEF, 0x49, 0xD2, 0xFA, 0xE5,
52  0x72, 0xB8, 0x81, 0xB1, 0x23, 0xA8, 0x5F, 0xFA,
53  0x21, 0x59, 0x5F, 0x36 } },
54  { "abcdefghijklmnopqrstuvwxyz", 26,
55  { 0xF7, 0x1C, 0x27, 0x10, 0x9C, 0x69, 0x2C, 0x1B,
56  0x56, 0xBB, 0xDC, 0xEB, 0x5B, 0x9D, 0x28, 0x65,
57  0xB3, 0x70, 0x8D, 0xBC } },
58  { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
59  { 0x12, 0xA0, 0x53, 0x38, 0x4A, 0x9C, 0x0C, 0x88,
60  0xE4, 0x05, 0xA0, 0x6C, 0x27, 0xDC, 0xF4, 0x9A,
61  0xDA, 0x62, 0xEB, 0x2B } },
62  { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
63  { 0xB0, 0xE2, 0x0B, 0x6E, 0x31, 0x16, 0x64, 0x02,
64  0x86, 0xED, 0x3A, 0x87, 0xA5, 0x71, 0x30, 0x79,
65  0xB2, 0x1F, 0x51, 0x89 } },
66  { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
67  { 0x9B, 0x75, 0x2E, 0x45, 0x57, 0x3D, 0x4B, 0x39,
68  0xF4, 0xDB, 0xD3, 0x32, 0x3C, 0xAB, 0x82, 0xBF,
69  0x63, 0x32, 0x6B, 0xFB } },
70  { NULL, 0, { 0 } }
71  };
72 
73 static int selfTest( void )
74  {
75  const CAPABILITY_INFO *capabilityInfo = getRipemd160Capability();
76  BYTE hashState[ HASH_STATE_SIZE + 8 ];
77  int i, status;
78 
79  /* Test RIPEMD160 against the test vectors from the RIPEMD-160 paper */
80  for( i = 0; digestValues[ i ].data != NULL; i++ )
81  {
82  status = testHash( capabilityInfo, hashState, digestValues[ i ].data,
83  digestValues[ i ].length, digestValues[ i ].digest );
84  if( cryptStatusError( status ) )
85  return( status );
86  }
87 
88  return( CRYPT_OK );
89  }
90 #else
91  #define selfTest NULL
92 #endif /* !CONFIG_NO_SELFTEST */
93 
94 /****************************************************************************
95 * *
96 * Control Routines *
97 * *
98 ****************************************************************************/
99 
100 /* Return context subtype-specific information */
101 
103 static int getInfo( IN_ENUM( CAPABILITY_INFO ) const CAPABILITY_INFO_TYPE type,
105  OUT void *data,
106  IN_INT_Z const int length )
107  {
108  assert( contextInfoPtr == NULL || \
109  isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
110  assert( ( length == 0 && isWritePtr( data, sizeof( int ) ) ) || \
111  ( length > 0 && isWritePtr( data, length ) ) );
112 
114 
115  if( type == CAPABILITY_INFO_STATESIZE )
116  {
117  int *valuePtr = ( int * ) data;
118 
119  *valuePtr = HASH_STATE_SIZE;
120 
121  return( CRYPT_OK );
122  }
123 
124  return( getDefaultInfo( type, contextInfoPtr, data, length ) );
125  }
126 
127 /****************************************************************************
128 * *
129 * RIPEMD160 Hash Routines *
130 * *
131 ****************************************************************************/
132 
133 /* Hash data using RIPEMD160 */
134 
135 static int hash( CONTEXT_INFO *contextInfoPtr, BYTE *buffer, int noBytes )
136  {
137  RIPEMD160_CTX *ripemd160Info = ( RIPEMD160_CTX * ) contextInfoPtr->ctxHash->hashInfo;
138 
139  /* If the hash state was reset to allow another round of hashing,
140  reinitialise things */
141  if( !( contextInfoPtr->flags & CONTEXT_FLAG_HASH_INITED ) )
142  RIPEMD160_Init( ripemd160Info );
143 
144  if( noBytes > 0 )
145  RIPEMD160_Update( ripemd160Info, buffer, noBytes );
146  else
147  RIPEMD160_Final( contextInfoPtr->ctxHash->hash, ripemd160Info );
148 
149  return( CRYPT_OK );
150  }
151 
152 /* Internal API: Hash a single block of memory without the overhead of
153  creating an encryption context */
154 
155 void ripemd160HashBuffer( HASHINFO hashInfo, BYTE *outBuffer,
156  const int outBufMaxLength, const void *inBuffer,
157  const int inLength, const HASH_STATE hashState )
158  {
159  RIPEMD160_CTX *ripemd160Info = ( RIPEMD160_CTX * ) hashInfo;
160 
161  assert( isWritePtr( hashInfo, sizeof( HASHINFO ) ) );
162  assert( ( hashState != HASH_STATE_END && \
163  outBuffer == NULL && outBufMaxLength == 0 ) || \
164  ( hashState == HASH_STATE_END && \
165  isWritePtr( outBuffer, outBufMaxLength ) && \
166  outBufMaxLength >= 20 ) );
167  assert( inBuffer == NULL || isReadPtr( inBuffer, inLength ) );
168 
169  if( ( hashState == HASH_STATE_END && outBufMaxLength < 20 ) || \
170  ( hashState != HASH_STATE_END && inLength <= 0 ) )
172 
173  switch( hashState )
174  {
175  case HASH_STATE_START:
176  RIPEMD160_Init( ripemd160Info );
177  /* Drop through */
178 
179  case HASH_STATE_CONTINUE:
180  RIPEMD160_Update( ripemd160Info, ( BYTE * ) inBuffer, inLength );
181  break;
182 
183  case HASH_STATE_END:
184  if( inBuffer != NULL )
185  RIPEMD160_Update( ripemd160Info, ( BYTE * ) inBuffer, inLength );
186  RIPEMD160_Final( outBuffer, ripemd160Info );
187  break;
188 
189  default:
191  }
192  }
193 
194 void ripemd160HashBufferAtomic( BYTE *outBuffer, const int outBufMaxLength,
195  const void *inBuffer, const int inLength )
196  {
197  RIPEMD160_CTX ripemd160Info;
198 
199  assert( isWritePtr( outBuffer, outBufMaxLength ) && \
200  outBufMaxLength >= 20 );
201  assert( isReadPtr( inBuffer, inLength ) );
202 
203  if( outBufMaxLength < 20 || inLength <= 0 )
205 
206  RIPEMD160_Init( &ripemd160Info );
207  RIPEMD160_Update( &ripemd160Info, ( BYTE * ) inBuffer, inLength );
208  RIPEMD160_Final( outBuffer, &ripemd160Info );
209  zeroise( &ripemd160Info, sizeof( RIPEMD160_CTX ) );
210  }
211 
212 /****************************************************************************
213 * *
214 * Capability Access Routines *
215 * *
216 ****************************************************************************/
217 
218 static const CAPABILITY_INFO FAR_BSS capabilityInfo = {
219  CRYPT_ALGO_RIPEMD160, bitsToBytes( 160 ), "RIPEMD-160", 10,
220  bitsToBytes( 0 ), bitsToBytes( 0 ), bitsToBytes( 0 ),
221  selfTest, getInfo, NULL, NULL, NULL, NULL, hash, hash
222  };
223 
225  {
226  return( &capabilityInfo );
227  }
228 #endif /* USE_RIPEMD160 */