From 314ad2d877dd945eac5ce71bc7c64fc0ff29f875 Mon Sep 17 00:00:00 2001 From: Frantisek Burian Date: Sun, 15 Dec 2013 21:47:56 +0100 Subject: [PATCH] [F24:CRYPTO] Add example using cryptographic module on F41x or F2x --- examples/stm32/f4/other/crypto-basic/Makefile | 25 ++++ examples/stm32/f4/other/crypto-basic/README | 6 + .../stm32/f4/other/crypto-basic/cryptobasic.c | 125 ++++++++++++++++++ examples/stm32/f4/other/stm32f4-discovery.ld | 32 +++++ 4 files changed, 188 insertions(+) create mode 100644 examples/stm32/f4/other/crypto-basic/Makefile create mode 100644 examples/stm32/f4/other/crypto-basic/README create mode 100644 examples/stm32/f4/other/crypto-basic/cryptobasic.c create mode 100644 examples/stm32/f4/other/stm32f4-discovery.ld diff --git a/examples/stm32/f4/other/crypto-basic/Makefile b/examples/stm32/f4/other/crypto-basic/Makefile new file mode 100644 index 0000000..fc1c547 --- /dev/null +++ b/examples/stm32/f4/other/crypto-basic/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cryptobasic + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/other/crypto-basic/README b/examples/stm32/f4/other/crypto-basic/README new file mode 100644 index 0000000..e9e3198 --- /dev/null +++ b/examples/stm32/f4/other/crypto-basic/README @@ -0,0 +1,6 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program is for demonstrating of use Crypto Controller on STM32F417 +board. diff --git a/examples/stm32/f4/other/crypto-basic/cryptobasic.c b/examples/stm32/f4/other/crypto-basic/cryptobasic.c new file mode 100644 index 0000000..df431b0 --- /dev/null +++ b/examples/stm32/f4/other/crypto-basic/cryptobasic.c @@ -0,0 +1,125 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void clock_setup(void) +{ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Enable clocks for CRYP. */ + rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_CRYPEN); +} + +static void usart_setup(void) +{ + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +static uint64_t key[4] = {0x11223344,0x44556677,0x77889900, 0x99005522}; +static uint64_t iv[4] = {0x01020304,0x02030405,0x09080706, 0x55245711}; + +static uint8_t plaintext[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +static uint8_t ciphertext[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +static uint8_t plaintext2[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + +int main(void) +{ + int i; + int iserr = 1; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PD12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOD, GPIO12); /* LED on/off */ + + /* encode the plaintext message into ciphertext */ + + crypto_set_key(CRYPTO_KEY_128BIT,key); + crypto_set_iv(iv); /* only in CBC or CTR mode */ + crypto_set_datatype(CRYPTO_DATA_32BIT); + crypto_set_algorithm(ENCRYPT_DES_ECB); + + crypto_start(); + crypto_process_block((uint32_t*)plaintext, + (uint32_t*)ciphertext, + 8 / sizeof(uint32_t)); + crypto_stop(); + + /* decode the previously encoded message + from ciphertext to plaintext2 */ + + crypto_set_key(CRYPTO_KEY_128BIT,key); + crypto_set_iv(iv); /* only in CBC or CTR mode */ + crypto_set_datatype(CRYPTO_DATA_32BIT); + crypto_set_algorithm(DECRYPT_DES_ECB); + + crypto_start(); + crypto_process_block((uint32_t*)ciphertext, + (uint32_t*)plaintext2, + 8 / sizeof(uint32_t)); + crypto_stop(); + + /* compare the two plaintexts if they are same */ + iserr = 0; + for (i=0; i<8; i++) + { + if (plaintext[i] != plaintext2[i]) + iserr = true; + } + + if (iserr) + gpio_toggle(GPIOD,GPIO12); /* something went wrong. */ + } + + return 0; +} diff --git a/examples/stm32/f4/other/stm32f4-discovery.ld b/examples/stm32/f4/other/stm32f4-discovery.ld new file mode 100644 index 0000000..927c786 --- /dev/null +++ b/examples/stm32/f4/other/stm32f4-discovery.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for ST STM32F4DISCOVERY (STM32F407VG, 1024K flash, 128K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 1024K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f4.ld +