CM_PASSWORD.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Verify Password
  2. //
  3. // CryptoMemory Library Include Files
  4. #include "CM_LIB.h"
  5. #include "CM_I2C.h"
  6. #include "CM_I2C_L.h"
  7. #include "CM_GPA.h"
  8. #include <stdio.h>
  9. uint8_t ucCmdPassword[4] = {0xba, 0x00, 0x00, 0x03};
  10. uint8_t ucPSW[3];
  11. // Verify Password
  12. uint8_t cm_VerifyPassword(uint8_t * pucPassword, uint8_t ucSet, uint8_t ucRW)
  13. {
  14. uint8_t i, j;
  15. uint8_t ucReturn;
  16. uint8_t ucAddr;
  17. // Build command and PAC address
  18. ucAddr = CM_PSW + (ucSet<<3);
  19. ucCmdPassword[1] = ucSet;// to choose which sets of passwords
  20. if (ucRW != CM_PWWRITE) {
  21. ucCmdPassword[1] |= 0x10;
  22. ucAddr += 4;
  23. }
  24. // Deal with encryption if in authenticate mode
  25. for (j = 0; j<3; j++) {
  26. // Encrypt the password
  27. if(ucCM_Authenticate) {
  28. for(i = 0; i<5; i++) cm_GPAGen(pucPassword[j]);
  29. ucPSW[j] = Gpa_byte;
  30. }
  31. // Else just copy it
  32. else ucPSW[j] = pucPassword[j];
  33. }
  34. // Send the command
  35. ucReturn = cm_WriteCommand(ucCmdPassword, ucPSW, 3);
  36. // Wait for chip to process password
  37. CM_LOW_LEVEL.WaitClock(30); //3
  38. // Read Password attempts counter to determine if the password was accepted
  39. if (ucReturn == SUCCESS) {
  40. ucReturn = cm_ReadConfigZone(ucAddr, ucPSW, 1);
  41. if (ucPSW[0]!= 0xFF) ucReturn = FAILED;
  42. }
  43. if (ucCM_Authenticate && (ucReturn != SUCCESS)) cm_ResetCrypto();
  44. // Done
  45. return ucReturn;
  46. }