Changeset 4460:65edaf8cf35f in roaraudio


Ignore:
Timestamp:
10/10/10 07:13:58 (8 years ago)
Author:
phi
Branch:
default
Message:

added support for store and restore of state, only supports storing of block aligned hashs for security resons

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/hash_tiger.c

    r4459 r4460  
    3737 
    3838#define STATE_LEN (3*8) 
     39#define PSTATE_LEN (4*8) 
    3940#define DIGEST_LEN STATE_LEN 
    4041#define BLOCK_LEN (64) 
     
    582583} 
    583584 
    584 int roar_hash_tiger_init_from_pstate(struct roar_hash_tiger * state, void * oldstate); 
     585int roar_hash_tiger_init_from_pstate(struct roar_hash_tiger * state, void * oldstate) { 
     586 uint64_t blocks; 
     587 unsigned char * p = oldstate; 
     588 
     589 if ( state == NULL || oldstate == NULL ) 
     590  return -1; 
     591 
     592 if ( roar_hash_tiger_init(state) == -1 ) 
     593  return -1; 
     594 
     595#if BYTE_ORDER == BIG_ENDIAN && !defined(ROAR_TARGET_WIN32) 
     596#define _in(m)  do { m = *(uint64_t*)p;  p += 8; } while(0) 
     597#elif BYTE_ORDER == LITTLE_ENDIAN 
     598#define _S(x, n) (((uint64_t)(p[(n)])) << (uint64_t)(x)) 
     599#define _in(m) do { m = (uint64_t)(_S(56, 0) | _S(48, 1) | _S(40, 2) | _S(32, 3) | \ 
     600                                   _S(24, 4) | _S(16, 5) | _S( 8, 6) | _S( 0, 7)   \ 
     601                        ); p += 8; } while (0) 
     602#else 
     603  ROAR_ERR("_export(state=%p, ...): Tiger not implemented for non little or big endian systems!", state); 
     604  return -1; 
     605#define _in 
     606#endif 
     607 _in(state->a); 
     608 _in(state->b); 
     609 _in(state->c); 
     610 _in(blocks); 
     611 
     612 state->blocks = blocks; 
     613#undef _in 
     614 
     615 return 0; 
     616} 
     617 
     618static int _export(struct roar_hash_tiger * state, void * newstate, size_t * len, int is_pstate) { 
     619 unsigned char * p = newstate; 
     620 size_t needlen; 
     621 uint64_t blocks; 
     622 
     623 if ( is_pstate ) { 
     624  needlen = PSTATE_LEN; 
     625 } else { 
     626  needlen = STATE_LEN; 
     627 } 
     628 
     629 if ( state == NULL || newstate == NULL || len == NULL ) 
     630  return -1; 
     631 
     632 if ( state->inlen != 0 ) 
     633  return -1; 
     634 
     635 if ( *len < needlen ) 
     636  return -1; 
     637 
     638 *len = needlen; 
     639 
     640#if BYTE_ORDER == BIG_ENDIAN && !defined(ROAR_TARGET_WIN32) 
     641#define _out(m) do { *(uint64_t*)p = m ; p += 8; } while(0) 
     642#elif BYTE_ORDER == LITTLE_ENDIAN 
     643#define _out(m) do { *p++ = m >> 56; *p++ = m >> 48; \ 
     644                     *p++ = m >> 40; *p++ = m >> 32; \ 
     645                     *p++ = m >> 24; *p++ = m >> 16; \ 
     646                     *p++ = m >>  8; *p++ = m;       } while(0) 
     647#else 
     648  ROAR_ERR("_export(state=%p, ...): Tiger not implemented for non little or big endian systems!", state); 
     649  return -1; 
     650#define _out 
     651#endif 
     652 _out(state->a); 
     653 _out(state->b); 
     654 _out(state->c); 
     655 if ( is_pstate ) { 
     656  blocks = state->blocks; 
     657  _out(blocks); 
     658 } 
     659#undef _out 
     660 
     661 return 0; 
     662} 
     663 
    585664int roar_hash_tiger_to_pstate(struct roar_hash_tiger * state, void * newstate, size_t * len) { 
    586  unsigned char * p = newstate; 
    587  
    588  if ( state == NULL || newstate == NULL || len == NULL ) 
    589   return -1; 
    590  
    591  if ( state->inlen != 0 ) 
    592   return -1; 
    593  
    594  if ( *len < STATE_LEN ) 
    595   return -1; 
    596  
    597  *len = STATE_LEN; 
    598  
    599 #define _out(m) do { *p++ = state->m >> 56; *p++ = state->m >> 48; \ 
    600                      *p++ = state->m >> 40; *p++ = state->m >> 32; \ 
    601                      *p++ = state->m >> 24; *p++ = state->m >> 16; \ 
    602                      *p++ = state->m >>  8; *p++ = state->m;       } while(0) 
    603  _out(a); 
    604  _out(b); 
    605  _out(c); 
    606 #undef _out 
    607  
    608  return 0; 
     665 return _export(state, newstate, len, 1); 
    609666} 
    610667 
    611668ssize_t roar_hash_tiger_statelen(struct roar_hash_tiger * state) { 
    612  return STATE_LEN; 
     669 return PSTATE_LEN; 
    613670} 
    614671 
     
    662719  return -1; 
    663720 
    664  return roar_hash_tiger_to_pstate(state, digest, len); 
     721 return _export(state, digest, len, 0); 
    665722} 
    666723 
Note: See TracChangeset for help on using the changeset viewer.