Пример #1
0
//----------------------------------------------------------------------
// Write Data Page for DS1963S.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'pagenum'     - page number to write to
// 'data'        - buffer to write into page
// 'resume'      - if true, device access is resumed using the RESUME
//                 ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                 used along with the device's entire address number.
//
// Return: TRUE - Write successfull
//         FALSE - error occurred during write.
//
SMALLINT WriteDataPageSHA18(int portnum, SMALLINT pagenum,
                          uchar* data, SMALLINT resume)
{
   uchar buffer[32];
   int addr = pagenum << 5, addr_buff;
   uchar es = 0;

   OWASSERT( EraseScratchpadSHA18(portnum, addr, resume),
             OWERROR_ERASE_SCRATCHPAD_FAILED, FALSE );

   OWASSERT( WriteScratchpadSHA18(portnum, addr, data, 32, TRUE),
              OWERROR_WRITE_SCRATCHPAD_FAILED, FALSE );

   OWASSERT( ReadScratchpadSHA18(portnum, &addr_buff, &es, buffer, TRUE),
             OWERROR_READ_SCRATCHPAD_FAILED, FALSE );

   // verify that what we read is exactly what we wrote
   OWASSERT( (addr == addr_buff) && (es == 0x1F)
                 && (memcmp(buffer, data, 32)==0),
             OWERROR_READ_SCRATCHPAD_FAILED, FALSE );

   OWASSERT( CopyScratchpadSHA18(portnum, addr, 32, TRUE),
             OWERROR_COPY_SCRATCHPAD_FAILED, FALSE );

   return TRUE;

}
Пример #2
0
//-------------------------------------------------------------------------
// Creates a random challenge using the SHA engine of the given
// coprocessor.
//
// 'copr'      - Structure for holding coprocessor information.
// 'pagenum'   - pagenumber to use for calculation.
// 'chlg'      - 3-byte return buffer for challenge.
// 'offset'    - offset into resulting MAC to pull challenge data from.
//
// Return: TRUE - create challenge succeeded.
//         FALSE - an error occurred.
//
SMALLINT CreateChallenge(SHACopr* copr, SMALLINT pageNum,
                         uchar* chlg, SMALLINT offset)
{
   uchar scratchpad[32];
   int   addr = pageNum << 5;
   uchar es = 0x1F;
   int   start = 8;

   if(offset>0 && offset<17)
      start += offset;

   //set the serial number
   owSerialNum(copr->portnum, copr->devAN, FALSE);

   OWASSERT( EraseScratchpadSHA18(copr->portnum, addr, FALSE),
             OWERROR_ERASE_SCRATCHPAD_FAILED, FALSE );

   OWASSERT( SHAFunction18(copr->portnum, SHA_COMPUTE_CHALLENGE, addr, TRUE),
             OWERROR_SHA_FUNCTION_FAILED, FALSE );

   OWASSERT( ReadScratchpadSHA18(copr->portnum, &addr, &es, scratchpad, TRUE),
             OWERROR_READ_SCRATCHPAD_FAILED, FALSE );

   memcpy(chlg,&scratchpad[start],3);

   return TRUE;
}
Пример #3
0
//-------------------------------------------------------------------------
// Answers a random challenge by performing an authenticated read of the
// user's account information.
//
// 'user'      - Structure for holding user token information.
// 'chlg'      - 3-byte buffer of challenge data.
//
// Return: the value of the write cycle counter for the account page.
//         or -1 if there is an error
//
int AnswerChallenge(SHAUser* user, uchar* chlg)
{
   int addr = user->accountPageNumber << 5;

   user->writeCycleCounter = -1;
   memcpy(&user->accountFile[20], chlg, 3);

   //set the serial number
   owSerialNum(user->portnum, user->devAN, FALSE);

   if(user->devAN[0]==0x18)
   {
      // for the DS1963S
      OWASSERT( EraseScratchpadSHA18(user->portnum, addr, FALSE),
                OWERROR_ERASE_SCRATCHPAD_FAILED, -1 );

      OWASSERT( WriteScratchpadSHA18(user->portnum, addr,
                                     user->accountFile, 32,
                                     TRUE),
                 OWERROR_WRITE_SCRATCHPAD_FAILED, -1 );

      user->writeCycleCounter =
         ReadAuthPageSHA18(user->portnum,
                           user->accountPageNumber,
                           user->accountFile,
                           user->responseMAC, TRUE);
   }
   else if(user->devAN[0]==0x33||user->devAN[0]==0xB3)
   {
      // for the DS1961S
      OWASSERT( WriteScratchpadSHA33(user->portnum, addr,
                                     &user->accountFile[16],
                                     FALSE),
                 OWERROR_WRITE_SCRATCHPAD_FAILED, -1 );

      user->writeCycleCounter =
         ReadAuthPageSHA33(user->portnum,
                           user->accountPageNumber,
                           user->accountFile,
                           user->responseMAC, TRUE);
   }
   return user->writeCycleCounter;
}