Пример #1
0
/** \brief Initialize the unique encryption key for this platform
 *		Write the provided encryption key to the parent encryption key slot
 *		Function optionally lock the parent encryption key slot after it is written
 *  \param[in] enckeyin Pointer to a 32 byte encryption key that will be stored on the platform and in the device
 *  \param[in] enckeyId Slot id on the ECC508 to store the encryption key
 *  \param[in] lock If this is set to true, the slot that stores the encryption key will be locked
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_set_enckey(uint8_t* enckeyin, uint8_t enckeyId, bool lock)
{
	ATCA_STATUS status = ATCA_SUCCESS;
	uint8_t block = 0;
	uint8_t offset = 0;
	uint8_t lockSuccess = 0;
	uint8_t enckeyIdByte = (uint8_t)enckeyId;

	do {
		// Verify input parameters
		if (enckeyin == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Write the random number to specified slot
		if ((status = atcab_write_zone(ATCA_ZONE_DATA, enckeyIdByte, block, offset, enckeyin, ATCA_BLOCK_SIZE)) != ATCA_SUCCESS)
			BREAK(status, "Write parent encryption key failed");

		// Optionally lock the key
		if (lock)
			// Send the slot lock command for this slot, ignore the return status
			atcab_lock_data_slot(enckeyIdByte, &lockSuccess);

	} while (0);

	return status;
}
Пример #2
0
void sdcard_init(void)
{
    volatile int retry=10;

    /* SDIO initial configuration */
    SDHandle.Instance                 = SDIO;
    SDHandle.Init.ClockEdge           = SDIO_CLOCK_EDGE_RISING;
    SDHandle.Init.ClockBypass         = SDIO_CLOCK_BYPASS_DISABLE;
    SDHandle.Init.ClockPowerSave      = SDIO_CLOCK_POWER_SAVE_DISABLE; //TODO
    SDHandle.Init.BusWide             = SDIO_BUS_WIDE_1B;
    SDHandle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
    SDHandle.Init.ClockDiv            = SDIO_TRANSFER_CLK_DIV; //INIT_CLK_DIV will be used first

    /* DeInit in case of reboot */
    HAL_SD_DeInit(&SDHandle);

    /* Init the SD interface */
    HAL_SD_CardInfoTypedef cardinfo;
    while(HAL_SD_Init(&SDHandle, &cardinfo) != SD_OK && --retry) {
        systick_sleep(100);
    }

    if (retry == 0) {
        BREAK();
    }

    /* Configure the SD Card in wide bus mode. */
    if (HAL_SD_WideBusOperation_Config(&SDHandle, SDIO_BUS_WIDE_4B) != SD_OK) {
        BREAK();
    }
}
Пример #3
0
void On_simulation_begin()
//************************
// VMLAB informs you that the simulation is starting. Initialize pin values
// here Open files; allocate memory, etc.
// The first instance to enter this function is responsible for opening the
// single log file and initializing any global data. Although the log filename
// doesn't change with each simulation, it's better to open it here and close it
// in On_simulation_end(). This allows the user to delete or move the log file
// without having to close or rebuild the project.
{
   char strBuffer[MAXBUF];

   // Force the initial value of data input to be logged at time step 0
   VAR(Log_data) = -1;

   // Keep track of how many instances have already been created
   VAR(Instance_number) = Instance_count;
   Instance_count++;
   
   // Because the VCD file format uses a single ASCII character to identify each
   // signal, it limits the number of vcdlog instances that can be used in a
   // project
   if(VAR(Instance_number) + MIN_ID > MAX_ID) {
      snprintf(strBuffer, MAXBUF, "Too many instances (max %d)",
         MAX_ID - MIN_ID + 1);
      BREAK(strBuffer);
      Close_file();
   }

   // The first instance to have its On_simulation_begin() called is responsible
   // for opening the log file and initializing global variables.
   if(Instance_count == 1) {
      Total_time = 0;

      // Setting Log_time to -1 forces the first instance entering
      // On_time_step(), to finish writing the VCD header section.
      Log_time = -1;

      // Create or overwrite log file in current directory
      File = fopen(FILE_NAME, "w");

      // We can still run if the file won't open; we just can't log anything.
      if(!File) {
         snprintf(strBuffer, MAXBUF, "Could not create \"%s\" file: %s",
            FILE_NAME, strerror(errno));
         BREAK(strBuffer);
      }
      
      // Write out the global VCD file header
      Log_printf("$version VMLAB vcdlog component $end\n");
      Log_printf("$timescale 1 %s $end\n", TIME_UNITS);
      Log_printf("$scope module vmlab $end\n");
   }

   // Write out per instance part of the VCD header that contains the variable
   // name and the ASCII identifier.
   Log_printf("$var wire 1 %c %s $end\n",
      VAR(Instance_number) + MIN_ID, GET_INSTANCE());
}
Пример #4
0
int main(void)
{
	unsigned last_branch = 0;
	enum mips_exception err;
	Elf32_Sym *sym;
	const char *symname;
	int opcode;

	/* Prepare the code */

	l2_memory = sbrk(MEMSZ);
	if(!l2_memory)
		BREAK(1);
	mips_init();
	pcpu = mips_init_cpu(l2_memory, MEMSZ, STKSZ);
	if(mips_elf_load(pcpu, l2_elf, l2_elf_size) < 0)
		BREAK(2);

	/* Execute it */

	while(1) {
		if(pcpu->delay_slot)
			last_branch = pcpu->delay_slot-4;
		
		/* Print all labels as they are encountered. */
		if((sym = mips_elf_find_address(pcpu, pcpu->pc)) && 
		   (sym->st_value == pcpu->pc) &&
		   (symname = mips_elf_get_symname(pcpu, sym))) {
			printaddr("PC=", pcpu->pc);
			printaddr(",last_branch=", last_branch);
			print_char('\n');
		}
	
		if((err = mips_execute(pcpu)) == MIPS_E_OK)
			continue;
		if(err == MIPS_E_BREAK)
			break;
		
		/* Expected exceptions must exactly match PC. */
		if((sym = mips_elf_find_address(pcpu, pcpu->pc)) && 
		   (sym->st_value == pcpu->pc) &&
		   (symname = mips_elf_get_symname(pcpu, sym)) &&
		   (beginswith(symname, "EXN") == 0)) {
			pcpu->pc += 8;	
		} else {
			break;
		}
	}
	
	print_string("L1 FINISHED: exception="); print_int(err);
	print_string(", code="); print_int(mips_break_code(pcpu, &opcode));
	print_string(", "); printaddr("last_branch=", last_branch);
	print_char('\n');
	printaddr("PC=", pcpu->pc);
	print_char('\n');
	
	return 0;
}
Пример #5
0
bool DISABLE_OPT sdram_test()
{
    uint8_t pattern = 0xAA;
    uint8_t antipattern = 0x55;
    uint32_t mem_size = (16*1024*1024);
    uint8_t * const mem_base = (uint8_t*)0xC0000000;

    printf("sdram test...\n");
    /* test data bus */
    for (uint8_t i=1; i; i<<=1) {
        *mem_base = i;
        if (*mem_base != i) {
            printf("data bus lines test failed! data (%d)\n", i);
            BREAK();
        }
    }

    /* test address bus */
    /* Check individual address lines */
    for (uint32_t i=1; i<mem_size; i<<=1) {
        mem_base[i] = pattern;
        if (mem_base[i] != pattern) {
            printf("address bus lines test failed! address (%p)\n", &mem_base[i]);
            BREAK();
        }
    }

    /* Check for aliasing (overlaping addresses) */
    mem_base[0] = antipattern;
    for (uint32_t i=1; i<mem_size; i<<=1) {
        if (mem_base[i] != pattern) {
            printf("address bus overlap %p\n", &mem_base[i]);
            BREAK();
        }
    }

    /* test all ram cells */
    for (uint32_t i=0; i<mem_size; i++) {
        mem_base[i] = pattern;
        if (mem_base[i] != pattern) {
            printf("address bus test failed! address (%p)\n", &mem_base[i]);
            BREAK();
        }
    }

    printf("sdram test passed\n");
    return true;
}
Пример #6
0
void os_SCHEDULE( void )
{
    if ( gOS_FLAGS.g_tskCriticalExecution )    // One task is in a critical part of its execution which has to be concurrend... do not kill it
    {
        return;
    }

    //if ( gOS_FLAGS.g_needsScheduling == (uint8_t) 0x1u )    // scheduling needed according to interrupt nesting level?
    {
        if ( ( (uint32_t) p_cur_tsk_tcb != (uint32_t) NULL ) && ( p_cur_tsk_tcb->tskState
                == TSK_STATE_ACTIVE_RUNNING ) )
        {
            p_cur_tsk_tcb->tskState = TSK_STATE_ACTIVE_SUSPENDED;
        }

        tsk_GetNxtActvTsk( (PTskTCB *) &p_nxt_tsk_tcb );
        if ( !TSK_STATE_IS_ACTIVE( p_nxt_tsk_tcb ) )
        {
            BREAK();
        }
        //p_nxt_tsk_tcb->tskState = TSK_STATE_ACTIVE_RUNNING;    // set to running, because it will be scheduled next task anyway.

        // check if there is a current task or if it was already deleted; then check if the next task is also the current task
        if ( /*( p_cur_tsk_tcb == (TskTCB *) NULL ) || */( p_nxt_tsk_tcb
                != p_cur_tsk_tcb ) )
        {
            gOS_FLAGS.g_DispatchFlag = (uint8_t) ( OS_DISPATCH_NEEDED & 0x3u );
            SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;    // do it by hand
        }
    }
    gOS_FLAGS.g_needsScheduling = 0x0u;    // reset scheduling flag
}
Пример #7
0
void On_time_step(double pTime)
//*****************************
// The analysis at the given time has finished. DO NOT place further actions
// on pins (unless they are delayed). Pins values are stable at this point.
{
   // Check the initial pin state (at time 0) here since On_digial_in_edge()
   // doesn't get called at the beginning of the simulation.
   if(pTime == 0) {
      Check_trigger(0);
   }

   // If the CANCEL pin is asserted, clear any pending BREAK() */
   if(GET_LOGIC(CANCEL) == 1) {
      VAR(Edge_time) = -1;
   }
   
   // Trigger any previously scheduled breakpoints if their time has arrived
   if(VAR(Edge_time) != -1 && pTime >= VAR(Edge_time) + VAR(Break_delay)) {
      char strBuffer[MAXBUF];
      
      snprintf(strBuffer, MAXBUF, "Triggered at %.2f ms",
         VAR(Edge_time) * 1000);
      BREAK(strBuffer);
      
      // Clear pending breakpoint so Check_trigger() can schedule new ones
      VAR(Edge_time) = -1;
   }
}
Пример #8
0
void interrupt 20 RxInterrupt(void)
{
    DisableInterrupts;
    if( SCI0SR1 & 0x20 )
    {
        CaptureCommand = SCI0DRL;
    }
    switch(CaptureCommand)
    {
        case '0':
            BREAK();isbreak = 1;constant_speed = 0;
            break;
        case '9':
            RUN();debug = 0;isbreak = 0;constant_speed = 450;
            break;
        case 'b':
            STOP();constant_speed = 0;
            break;
        case 'c':
            STOP();constant_speed = 0;
            break;
        case 'd':
            debug = 1;STOP();constant_speed = 0;
            break;
        default:
            break;
    }
    EnableInterrupts;
}
Пример #9
0
void On_simulation_begin()
//************************
// VMLAB informs you that the simulation is starting. Initialize pin values
// here Open files; allocate memory, etc.
// Although the log filename doesn't change with each simulation, it's better to
// open it here and close it in On_simulation_end(). This allows the user to
// delete or move the log file without having to close or rebuild the project.
{
   char strBuffer[MAXBUF];

   // Initialize per instance simulation variables.
   VAR(Clock_delay) = 0;
   VAR(Log_time) = 0;
   VAR(Log_data) = 0;

   // Create or overwrite log file in current directory
   snprintf(strBuffer, MAXBUF, "%s.log", GET_INSTANCE());
   VAR(File) = fopen(strBuffer, "w");

   // We can still run if the file won't open; we just can't log anything
   if(!VAR(File)) {
      snprintf(strBuffer, MAXBUF, "Could not create \"%s.log\" file: %s",
         GET_INSTANCE(), strerror(errno));
      BREAK(strBuffer);
   }
}
Пример #10
0
static SEM_PC
SEM_FN_NAME (lm32bf,x_cti_chain) (SIM_CPU *current_cpu, SEM_ARG sem_arg)
{
#define FLD(f) abuf->fields.sfmt_empty.f
  ARGBUF *abuf = SEM_ARGBUF (sem_arg);
  int UNUSED written = 0;
  IADDR UNUSED pc = abuf->addr;
  SEM_PC vpc = SEM_NEXT_VPC (sem_arg, pc, 0);

  {
#if WITH_SCACHE_PBB_LM32BF
#ifdef DEFINE_SWITCH
    vpc = lm32bf_pbb_cti_chain (current_cpu, sem_arg,
			       pbb_br_type, pbb_br_npc);
    BREAK (sem);
#else
    /* FIXME: Allow provision of explicit ifmt spec in insn spec.  */
    vpc = lm32bf_pbb_cti_chain (current_cpu, sem_arg,
			       CPU_PBB_BR_TYPE (current_cpu),
			       CPU_PBB_BR_NPC (current_cpu));
#endif
#endif
  }

  return vpc;
#undef FLD
}
Пример #11
0
void Log_printf(char *fmt, ...)
//********************
// Wrapper around vfprintf that checks for any file I/O errors. Since the
// printf() style functions will occasionally flush the stdio buffers, they
// can return a system level error (such as disk full). Using a wrapper
// function that checks for errors immediately after each printf() guarantees
// that errno will still be valid and that strerror() wlll produce a useful
// message to the user.
{
   char strBuffer[MAXBUF];
   va_list args;

   // Do nothing if the log file could not be opened or was already closed
   // due to a previous error.
   if(!File) {
      return;
   }

   // Call the real fvprintf function to do the actual printing
   va_start(args, fmt);
   vfprintf(File, fmt, args);

   // If any I/O error occurred, break with error message and close log file
   if(ferror(File)) {
      snprintf(strBuffer, MAXBUF, "Could not write \"%s\" file: %s",
         FILE_NAME, strerror(errno));
      BREAK(strBuffer);
      Close_file();
   }

   va_end(args);
}
Пример #12
0
	void checkGLError()
	{
		GLenum error = glGetError();
		if(error != GL_NO_ERROR)
		{
			BREAK("GL error 0x%x\n", error);
		}
	}
Пример #13
0
/** \brief Sign the message with the specified slot and return the signature
 *  \param[in] slotid The private P256 key slot to use for signing
 *  \param[in] message A pointer to the 32 byte message to be signed
 *  \param[out] signature A pointer that will hold the 64 byte P256 signature
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_sign(uint8_t slotid, const uint8_t *message, uint8_t *signature)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Check the inputs
		if (message == NULL || signature == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "Bad input parameters");
		}
		// Sign the message
		if ((status = atcab_sign(slotid, message, signature)) != ATCA_SUCCESS) BREAK(status, "Sign Failed");

	} while (0);

	return status;
}
Пример #14
0
/** \brief reads a pub key from a readable data slot versus atcab_get_pubkey which generates a pubkey from a private key slot
 *  \param[in] slotid Slot number to read, expected value is 0x8 through 0xF
 *  \param[out] pubkey Pointer the public key bytes that were read from the slot
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_read_pubkey(uint8_t slotid, uint8_t *pubkey)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (pubkey == NULL || slotid < 8 || slotid > 0xF) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "Bad atcatls_read_pubkey() input parameters");
		}
		// Call the GenKey command to return the public key
		if ((status = atcab_read_pubkey(slotid, pubkey)) != ATCA_SUCCESS) BREAK(status, "Read public key failed");

	} while (0);

	return status;
}
Пример #15
0
/** \brief Get a random number
 *  \param[out] randout Pointer the 32 random bytes that were returned by the Random Command
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_random(uint8_t* randout)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (randout == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Call the random command
		if ((status = atcab_random(randout)) != ATCA_SUCCESS) BREAK(status, "Random command failed");

	} while (0);

	return status;
}
Пример #16
0
/** \brief Create a unique public-private key pair in the specified slot
 *  \param[in] slotid The slot id to create the ECC private key
 *  \param[out] pubkey Pointer the public key bytes that coorespond to the private key that was created
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_create_key(uint8_t slotid, uint8_t* pubkey)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (pubkey == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Call the Genkey command on the specified slot
		if ((status = atcab_genkey(slotid, pubkey)) != ATCA_SUCCESS) BREAK(status, "Create key failed");

	} while (0);

	return status;
}
Пример #17
0
/** \brief Get the public key from the specified private key slot
 *  \param[in] slotid The slot id containing the private key used to calculate the public key
 *  \param[out] pubkey Pointer the public key bytes that coorespond to the private key
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_calc_pubkey(uint8_t slotid, uint8_t *pubkey)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (pubkey == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Call the GenKey command to return the public key
		if ((status = atcab_get_pubkey(slotid, pubkey)) != ATCA_SUCCESS) BREAK(status, "Gen public key failed");

	} while (0);

	return status;
}
Пример #18
0
/**
 * \brief Verify a certificate against its certificate authority's public key using the host's ATECC device for crypto functions.
 * \param[in] cert_def       Certificate definition describing how to extract the TBS and signature components from the certificate specified.
 * \param[in] cert           Certificate to verify.
 * \param[in] cert_size      Size of the certificate (cert) in bytes.
 * \param[in] ca_public_key  The ECC P256 public key of the certificate authority that signed this
 *                           certificate. Formatted as the 32 byte X and Y integers concatenated
 *                           together (64 bytes total).
 * \return ATCA_SUCCESS if the verify succeeds, ATCACERT_VERIFY_FAILED or ATCA_EXECUTION_ERROR if it fails to verify.
 */
ATCA_STATUS atcatls_verify_cert(const atcacert_def_t* cert_def, const uint8_t* cert, size_t cert_size, const uint8_t* ca_public_key)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Check the inputs
		if (cert_def == NULL || cert == NULL || ca_public_key == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "Bad input parameters");
		}
		// Verify the certificate
		status = atcacert_verify_cert_hw(cert_def, cert, cert_size, ca_public_key);
		if (status != ATCA_SUCCESS) BREAK(status, "Verify Failed");

	} while (0);

	return status;
}
Пример #19
0
void On_simulation_begin()
//************************
// VMLAB informs you that the simulation is starting. Initialize pin values
// here Open files; allocate memory, etc.
// Although the wav filename doesn't change with each simulation, it's better to
// open it here and close it in On_simulation_end(). This allows the user to
// delete or move the log file without having to close or rebuild the project.
{
   char strBuffer[MAXBUF];

   // Open existing wav file in current directory. The libsndfile API requires
   // the SF_INFO.format field set to 0 before opening a file for read access.
   VAR(File_info).format = 0;
   snprintf(strBuffer, MAXBUF, "%s.wav", GET_INSTANCE());
   VAR(File) = sf_open(strBuffer, SFM_READ, &VAR(File_info));

   // We can still run if the file won't open; output stays at POWER()/2.
   if(!VAR(File)) {
      snprintf(strBuffer, MAXBUF, "Could not open \"%s.wav\" file: %s",
         GET_INSTANCE(), sf_strerror(VAR(File)));
      BREAK(strBuffer);
      return;
   }
   
   // If the WAV file contains more than one channel, print an error mssage
   // that the additional channels will be ignored.
   if(VAR(File_info).channels != 1) {
      snprintf(strBuffer, MAXBUF, "File \"%s.wav\" has multiple channels; "
         "only first (left) channel used", GET_INSTANCE());
      PRINT(strBuffer);
   }
   
   // Allocate buffer large enough to hold a single sample across all the
   // channels. The libsndfile library requires that all the channels are read
   // at once, and it provides no way to ignore unwanted channels.
   VAR(Sample_buffer) = (double *)
      malloc(sizeof(double) * VAR(File_info).channels);
   if(!VAR(Sample_buffer)) {
      BREAK("Error allocating memory buffer");
      Close_file();
      return;
   }
}
Пример #20
0
/** \brief Write a public key from the device.
 *  \param[in] slotid The slot ID to write to
 *  \param[in] pubkey The key bytes
 *  \param[in] lock   If true, lock the slot after writing these bytes.
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_write_pubkey(uint8_t slotid, uint8_t pubkey[PUB_KEY_SIZE], bool lock)
{
	ATCA_STATUS status = ATCA_SUCCESS;
	uint8_t lock_response = 0x00;

	do {
		// Write the buffer as a public key into the specified slot
		if ((status = atcab_write_pubkey(slotid, pubkey)) != ATCA_SUCCESS) BREAK(status, "Write of public key slot failed");


		// Lock the slot if indicated
		if (lock == true)
			if ((status = atcab_lock_data_slot(slotid, &lock_response)) != ATCA_SUCCESS) BREAK(status, "Lock public key slot failed");

	} while (0);

	return status;

}
Пример #21
0
/**
 * \brief Reads the certificate specified by the certificate definition from the ATECC508A device.
 *        This process involves reading the dynamic cert data from the device and combining it
 *        with the template found in the certificate definition. Return the certificate int der format
 * \param[in] cert_def Certificate definition describing where to find the dynamic certificate information
 *                     on the device and how to incorporate it into the template.
 * \param[in] ca_public_key The ECC P256 public key of the certificate authority that signed this certificate.
 *                          Formatted as the 32 byte X and Y integers concatenated together (64 bytes total).
 * \param[out] cert Buffer to received the certificate.
 * \param[inout] cert_size As input, the size of the cert buffer in bytes.
 *                         As output, the size of the certificate returned in cert in bytes.
 * \return ATCA_STATUS
 */
ATCA_STATUS atcatls_get_cert(const atcacert_def_t* cert_def, const uint8_t *ca_public_key, uint8_t *certout, size_t* certsize)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (certout == NULL || certsize == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}

		// Build a certificate with signature and public key
		status = atcacert_read_cert(cert_def, ca_public_key, certout, certsize);
		if (status != ATCACERT_E_SUCCESS)
			BREAK(status, "Failed to read certificate");

	} while (0);

	return status;
}
Пример #22
0
/** \brief Initialize the unique encryption key for this platform.
 *		Write a random number to the parent encryption key slot
 *		Return the random number for storage on platform
 *  \param[out] enckeyout Pointer to a random 32 byte encryption key that will be stored on the platform and in the device
 *  \param[in] enckeyId Slot id on the ECC508 to store the encryption key
 *  \param[in] lock If this is set to true, the slot that stores the encryption key will be locked
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_init_enckey(uint8_t* enckeyout, uint8_t enckeyId, bool lock)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Verify input parameters
		if (enckeyout == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Get a random number
		if ((status = atcatls_random(enckeyout)) != ATCA_SUCCESS) BREAK(status, "Random command failed");

		// Write the random number as the encryption key
		atcatls_set_enckey(enckeyout, enckeyId, lock);

	} while (0);

	return status;
}
Пример #23
0
/** \brief Write encrypted bytes to the specified slot
 *  \param[in]  slotid    The slot id for the encrypted write
 *  \param[in]  block     The block id in the specified slot
 *  \param[in]  enckeyid  The keyid of the parent encryption key
 *  \param[out] data      The 32 bytes of clear text data that will be encrypted to write to the slot.
 *  \param[in] bufsize    Size of data buffer.
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_enc_write(uint8_t slotid, uint8_t block, uint8_t enckeyId, uint8_t* data, int16_t bufsize)
{
	ATCA_STATUS status = ATCA_SUCCESS;
	uint8_t enckey[ATCA_KEY_SIZE] = { 0 };

	do {
		// Verify input parameters
		if (data == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		// Get the encryption key from the platform
		if ((status = atcatls_get_enckey(enckey)) != ATCA_SUCCESS) BREAK(status, "Get encryption key failed");

		// todo: implement to account for the correct block on the ECC508
		if ((status = atcab_write_enc(slotid, block, data, enckey, enckeyId)) != ATCA_SUCCESS) BREAK(status, "Write encrypted failed");

	} while (0);

	return status;
}
Пример #24
0
void intr_enable()
{
  uint8_t id = apic_get_id();
  if (intr_sema[id] == 0)
  {
    kprintf("Bug: intr_enable!\n");
    BREAK();
    asm("hlt");
  }
  if (--intr_sema[id] == 0)
    asm("sti");
}
Пример #25
0
/** \brief Get the serial number of this device
 *  \param[out] sn_out Pointer to the buffer that will hold the 9 byte serial number read from this device
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_get_sn(uint8_t sn_out[ATCA_SERIAL_NUM_SIZE])
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Call the basic API to get the serial number
		if ((status = atcab_read_serial_number(sn_out)) != ATCA_SUCCESS) BREAK(status, "Get serial number failed");

	} while (0);

	return status;
}
Пример #26
0
/** \brief Generate a pre-master key (pmk) given a private key slot and a public key that will be shared with.
 *         This version performs an encrypted read from (slotid + 1)
 *  \param[in] slotid Slot of key for ECDH computation
 *  \param[in] enckeyId Slot of key for the encryption parent
 *  \param[in] pubkey Public to shared with
 *  \param[out] pmk - Computed ECDH key - A buffer with size of ATCA_KEY_SIZE
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_ecdh_enc(uint8_t slotid, uint8_t enckeyId, const uint8_t* pubkey, uint8_t* pmk)
{
	ATCA_STATUS status = ATCA_SUCCESS;
	uint8_t encKey[ECDH_KEY_SIZE] = { 0 };

	do {
		// Check the inputs
		if (pubkey == NULL || pmk == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "Bad input parameters");
		}
		// Get the encryption key for this platform
		if ((status = atcatls_get_enckey(encKey)) != ATCA_SUCCESS) BREAK(status, "Get enckey Failed");

		// Send the encrypted version of the ECDH command with the public key provided
		if ((status = atcab_ecdh_enc(slotid, pubkey, pmk, encKey, enckeyId)) != ATCA_SUCCESS) BREAK(status, "ECDH Failed");

	} while (0);

	return status;
}
Пример #27
0
/** \brief Generate a pre-master key (pmk) given a private key slot to create and a public key that will be shared with
 *  \param[in] slotid Slot of key for ECDHE computation
 *  \param[in] pubkey Public to share with
 *  \param[out] pubkeyret Public that was created as part of the ECDHE operation
 *  \param[out] pmk - Computed ECDH key - A buffer with size of ATCA_KEY_SIZE
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_ecdhe(uint8_t slotid, const uint8_t* pubkey, uint8_t* pubkeyret, uint8_t* pmk)
{
	ATCA_STATUS status = ATCA_SUCCESS;

	do {
		// Check the inputs
		if ((pubkey == NULL) || (pubkeyret == NULL) || (pmk == NULL)) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "Bad input parameters");
		}
		// Create a new key in the ECDH slot
		if ((status = atcab_genkey(slotid, pubkeyret)) != ATCA_SUCCESS) BREAK(status, "Create key failed");

		// Send the ECDH command with the public key provided
		if ((status = atcab_ecdh(slotid, pubkey, pmk)) != ATCA_SUCCESS) BREAK(status, "ECDH failed");


	} while (0);

	return status;
}
Пример #28
0
/** \brief Write a public key from the device.
 *  \param[in] slotid   The slot ID to write to
 *  \param[in] caPubkey The key bytes
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_read_ca_pubkey(uint8_t slotid, uint8_t caPubkey[PUB_KEY_SIZE])
{
	ATCA_STATUS status = ATCA_GEN_FAIL;

	do {
		// Read public key from the specified slot and return it in the buffer provided
		if ((status = atcab_read_pubkey(slotid, caPubkey)) != ATCA_SUCCESS) BREAK(status, "Read of public key slot failed");


	} while (0);

	return status;
}
Пример #29
0
static void extclk_config(int frequency)
{
    /* TCLK (PCLK2 * 2) */
    int tclk  = HAL_RCC_GetPCLK2Freq() * 2;

    /* SYSCLK/TCLK = No prescaler */
    int prescaler = (uint16_t) (HAL_RCC_GetSysClockFreq()/ tclk) - 1;

    /* Period should be even */
    int period = (tclk / frequency)-1;

    /* Timer base configuration */
    TIMHandle.Instance          = DCMI_TIM;
    TIMHandle.Init.Period       = period;
    TIMHandle.Init.Prescaler    = prescaler;
    TIMHandle.Init.ClockDivision = 0;
    TIMHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;

    /* Timer channel configuration */
    TIM_OC_InitTypeDef TIMOCHandle;
    TIMOCHandle.Pulse       = period/2;
    TIMOCHandle.OCMode      = TIM_OCMODE_PWM1;
    TIMOCHandle.OCPolarity  = TIM_OCPOLARITY_HIGH;
    TIMOCHandle.OCFastMode  = TIM_OCFAST_DISABLE;
    TIMOCHandle.OCIdleState = TIM_OCIDLESTATE_RESET;
    if (HAL_TIM_PWM_Init(&TIMHandle) != HAL_OK) {
        /* Initialization Error */
        BREAK();
    }

    if (HAL_TIM_PWM_ConfigChannel(&TIMHandle, &TIMOCHandle, DCMI_TIM_CHANNEL) != HAL_OK) {
        BREAK();
    }

    if (HAL_TIM_PWM_Start(&TIMHandle, DCMI_TIM_CHANNEL) != HAL_OK) {
        BREAK();
    }
}
Пример #30
0
/** \brief Read a private RSA key from the device.  The read will be encrypted
 *  \param[in]  enckeyid  The keyid of the parent encryption key
 *  \param[out] rsakey    The RSA key bytes
 *  \param[inout] keysize Size of RSA key.
 *  \return ATCA_STATUS
 */
ATCA_STATUS atcatls_enc_rsakey_read(uint8_t enckeyId, uint8_t* rsakey, int16_t* keysize)
{
	ATCA_STATUS status = ATCA_SUCCESS;
	uint8_t enckey[ATCA_KEY_SIZE] = { 0 };
	uint8_t slotid = RSA_KEY_SLOT;
	uint8_t startBlock = RSA_KEY_START_BLOCK;
	uint8_t memBlock = 0;
	uint8_t numKeyBlocks = RSA2048_KEY_SIZE / ATCA_BLOCK_SIZE;
	uint8_t block = 0;
	uint8_t memLoc = 0;

	do {
		// Verify input parameters
		if (rsakey == NULL || keysize == NULL) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "NULL inputs");
		}
		if (*keysize < RSA2048_KEY_SIZE) {
			status = ATCA_BAD_PARAM;
			BREAK(status, "RSA key buffer too small");
		}

		// Get the encryption key from the platform
		if ((status = atcatls_get_enckey(enckey)) != ATCA_SUCCESS) BREAK(status, "Get encryption key failed");

		// Read the RSA key by blocks
		for (memBlock = 0; memBlock < numKeyBlocks; memBlock++) {
			block = startBlock + memBlock;
			memLoc = ATCA_BLOCK_SIZE * memBlock;
			if ((status = atcab_read_enc(slotid, block, &rsakey[memLoc], enckey, enckeyId)) != ATCA_SUCCESS) BREAK(status, "Read RSA failed");
		}
		*keysize = RSA2048_KEY_SIZE;

	} while (0);

	return status;
}