Multos Forums

   

Secure Hash IV

Rank

Total Posts: 7

Joined 2013-04-17

PM

We are looking into the Secure Hash IV primitive, but don’t seem to get it to work. Could you please provide a small sample on how to use this primitive?

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

I’ve found an issue in some old code in SmartDeck that is probably the cause of your issues. I will get it fixed as soon as I can (hopefully this week) and e-mail you that along with an example (which I’ll also post here).

Thanks for reporting this.

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

The issues in the simulator (ver 2.3.1) with SECURE_HASH_IV primitive have been found and fixed. A patch for registered SmartDeck users is available on request.

Here is an example of using this primitive.

#include <multos.h>
#include <string.h>

#pragma attribute("aid", "F1000005")
#pragma attribute("dir", "611B4F04F1000005501343523330352053454355524520484153482020")

#pragma melstatic
BYTE bData[256];
BYTE bHash1[64];
BYTE bHash2[64];
BYTE bIMHash[64];
DWORD dwPrevHashedBytes;
WORD wLenMsgRem;
WORD pRemainder;

void main (void)
{
 
// Start a completely new hash - erase buffers  
 
memset(bIMHash,0,sizeof(bIMHash));  
 
pRemainder 0;
 
dwPrevHashedBytes 0;
 
wLenMsgRem 0;

 
// Set up some data at various points
 
bData[0] 0x5A;
 
bData[65] 0xA5;
 
bData[130] 0xFF;

 
// SHA-1 hash using the non IV primitive for range of data
 
multosSecureHash(14020bHash1bData);

 
// Use IV primitive to do the hash in two parts
 // Part 1: Bytes 0-119
 
multosSecureHashIV(12020bHash2bDatabIMHash, &dwPrevHashedBytes;, &wLenMsgRem;, &pRemainder;);

 
// Part 2: Bytes 120-139
 
multosSecureHashIV(2020bHash2bData+120bIMHash, &dwPrevHashedBytes;, &wLenMsgRem;, &pRemainder;);
 
 
// Compare hashes, should match
 
if(memcmp(bHash1bHash220) == 0)
  
multosExitSW(0x9000);
 else
  
multosExitSW(0x90FF);
     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

Here is a bit more info on usage of this primitive. If you are writing something to hash a long stream of data calling the APDU multiple times, this is how you could do it.

#pragma melsession
BYTE bRemain[64]
WORD wLenMsgRem;

void
main
(void)
{
   
// …
  
case CMD_HASHINIT:
      
pRemainder bRemain;
      
wLenMsgRem 0;
      
// etc.

  
case CMD_HASHIV:
     
// On entry, pRemainder points to the buffer storing the remainder from the previous call
     
multosSecureHashIV(Lc32bHash2pubbIMHash, &dwPrevHashedBytes;, &wLenMsgRem;, &pRemainder;);

     
// On exit, pRemainder points to the data in public that was not hashed. That data needs to be saved for the next calculation
     
memcpy(bRemain,pRemainder,wLenMsgRem);
     
//etc