cube bite crypto lib
Dependents: CEBF746_Slave_withTPM
cube_crypto.h@4:0a155e03336e, 2016-10-31 (annotated)
- Committer:
- yoondoru
- Date:
- Mon Oct 31 08:25:48 2016 +0000
- Revision:
- 4:0a155e03336e
- Parent:
- 2:47e48f68e858
- Child:
- 6:77ed53bde81a
add TPM class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gandol2 | 0:f0000829c039 | 1 | |
gandol2 | 2:47e48f68e858 | 2 | #ifndef __CUBE_CRYPTO_H__ |
gandol2 | 2:47e48f68e858 | 3 | #define __CUBE_CRYPTO_H__ |
gandol2 | 0:f0000829c039 | 4 | |
gandol2 | 0:f0000829c039 | 5 | #include "mbed.h" |
gandol2 | 0:f0000829c039 | 6 | #include <Timer.h> |
gandol2 | 0:f0000829c039 | 7 | #include "mbedtls/platform.h" |
gandol2 | 0:f0000829c039 | 8 | #include <string.h> |
gandol2 | 0:f0000829c039 | 9 | #include "mbedtls/cipher.h" |
gandol2 | 0:f0000829c039 | 10 | #include "mbedtls/entropy.h" |
gandol2 | 0:f0000829c039 | 11 | #include "mbedtls/ctr_drbg.h" |
gandol2 | 2:47e48f68e858 | 12 | #include "lib_CEBF746.h" |
gandol2 | 0:f0000829c039 | 13 | |
gandol2 | 0:f0000829c039 | 14 | #if DEBUG_LEVEL > 0 |
gandol2 | 0:f0000829c039 | 15 | #include "mbedtls/debug.h" |
gandol2 | 0:f0000829c039 | 16 | #endif |
gandol2 | 0:f0000829c039 | 17 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
gandol2 | 0:f0000829c039 | 18 | #include "mbedtls/memory_buffer_alloc.h" |
gandol2 | 0:f0000829c039 | 19 | #endif |
gandol2 | 0:f0000829c039 | 20 | |
gandol2 | 0:f0000829c039 | 21 | /* |
gandol2 | 0:f0000829c039 | 22 | * random number generate seed |
gandol2 | 0:f0000829c039 | 23 | */ |
gandol2 | 0:f0000829c039 | 24 | |
gandol2 | 0:f0000829c039 | 25 | const uint8_t random_num_seed[16]={ |
gandol2 | 0:f0000829c039 | 26 | 0x74, 0x11, 0xF0, 0x45, 0xD6, 0xA4, 0x3F, 0x69, |
gandol2 | 0:f0000829c039 | 27 | 0x18, 0xC6, 0x75, 0x42, 0xDF, 0x4C, 0xA7, 0x84 |
gandol2 | 0:f0000829c039 | 28 | }; |
gandol2 | 0:f0000829c039 | 29 | |
gandol2 | 0:f0000829c039 | 30 | // 0:reset 1:encrypt 2:decrypt 3~5:MODE 6:WORK 7:NON |
gandol2 | 0:f0000829c039 | 31 | |
gandol2 | 0:f0000829c039 | 32 | |
gandol2 | 0:f0000829c039 | 33 | /* |
gandol2 | 0:f0000829c039 | 34 | * AES crypto Struct |
gandol2 | 0:f0000829c039 | 35 | */ |
gandol2 | 0:f0000829c039 | 36 | typedef struct |
gandol2 | 0:f0000829c039 | 37 | { |
gandol2 | 0:f0000829c039 | 38 | uint8_t cmd; |
gandol2 | 2:47e48f68e858 | 39 | //uint8_t data[16]; |
gandol2 | 2:47e48f68e858 | 40 | uint8_t* data; |
gandol2 | 0:f0000829c039 | 41 | uint16_t key_crc; |
gandol2 | 0:f0000829c039 | 42 | } |
gandol2 | 0:f0000829c039 | 43 | sec_spi_data; |
gandol2 | 0:f0000829c039 | 44 | |
gandol2 | 0:f0000829c039 | 45 | |
gandol2 | 0:f0000829c039 | 46 | typedef struct |
gandol2 | 0:f0000829c039 | 47 | { |
gandol2 | 0:f0000829c039 | 48 | uint8_t iv[16]; |
gandol2 | 0:f0000829c039 | 49 | uint8_t key[32]; |
gandol2 | 0:f0000829c039 | 50 | uint8_t key_size; |
gandol2 | 0:f0000829c039 | 51 | uint16_t key_crc; |
yoondoru | 4:0a155e03336e | 52 | uint8_t keyindex; |
gandol2 | 0:f0000829c039 | 53 | } |
gandol2 | 0:f0000829c039 | 54 | cube_sec_key; |
gandol2 | 0:f0000829c039 | 55 | |
gandol2 | 0:f0000829c039 | 56 | typedef struct |
gandol2 | 0:f0000829c039 | 57 | { |
gandol2 | 0:f0000829c039 | 58 | uint8_t input_data[16]; |
gandol2 | 0:f0000829c039 | 59 | uint8_t output_data[16]; |
gandol2 | 0:f0000829c039 | 60 | uint8_t input_data_size; |
gandol2 | 0:f0000829c039 | 61 | uint8_t output_data_size; |
gandol2 | 0:f0000829c039 | 62 | } |
gandol2 | 0:f0000829c039 | 63 | cube_sec_data; |
gandol2 | 0:f0000829c039 | 64 | |
gandol2 | 0:f0000829c039 | 65 | typedef struct |
gandol2 | 0:f0000829c039 | 66 | { |
gandol2 | 0:f0000829c039 | 67 | mbedtls_aes_context mbed_ctx; |
gandol2 | 0:f0000829c039 | 68 | uint8_t cmd; |
gandol2 | 0:f0000829c039 | 69 | cube_sec_data sec_data; |
gandol2 | 0:f0000829c039 | 70 | cube_sec_key sec_key; |
gandol2 | 0:f0000829c039 | 71 | } |
gandol2 | 0:f0000829c039 | 72 | cube_sec_context; |
gandol2 | 0:f0000829c039 | 73 | |
gandol2 | 0:f0000829c039 | 74 | enum sec_config |
gandol2 | 0:f0000829c039 | 75 | { |
gandol2 | 0:f0000829c039 | 76 | INIT = 0, |
gandol2 | 0:f0000829c039 | 77 | ENCRYPT, |
gandol2 | 0:f0000829c039 | 78 | DECRYPT, |
gandol2 | 0:f0000829c039 | 79 | MODE, |
gandol2 | 0:f0000829c039 | 80 | WORK=6, |
gandol2 | 0:f0000829c039 | 81 | EXTERN2=7, |
gandol2 | 0:f0000829c039 | 82 | }; |
gandol2 | 0:f0000829c039 | 83 | |
gandol2 | 0:f0000829c039 | 84 | // 0:reset 1:encrypt 2:decrypt 3~5:MODE 6:WORK 7:NON |
gandol2 | 0:f0000829c039 | 85 | |
gandol2 | 0:f0000829c039 | 86 | //SET |
gandol2 | 0:f0000829c039 | 87 | #define AES_INIT (0b01<<INIT) |
gandol2 | 0:f0000829c039 | 88 | #define AES_INIT_NON (0b00<<INIT) |
gandol2 | 0:f0000829c039 | 89 | #define AES_ENCRYPT_ON (0b01<<ENCRYPT) |
gandol2 | 0:f0000829c039 | 90 | #define AES_ENCRYPT_OFF (0b00<<ENCRYPT) |
gandol2 | 0:f0000829c039 | 91 | #define AES_DECRYPT_ON (0b01<<DECRYPT) |
gandol2 | 0:f0000829c039 | 92 | #define AES_DECRYPT_OFF (0b00<<DECRYPT) |
gandol2 | 0:f0000829c039 | 93 | #define AES_MODE_ECB (0b000<<MODE) |
gandol2 | 0:f0000829c039 | 94 | #define AES_MODE_CBC (0b001<<MODE) |
gandol2 | 0:f0000829c039 | 95 | #define AES_WORK_ON (0b01<<WORK) |
gandol2 | 0:f0000829c039 | 96 | #define AES_WORK_OFF (0b00<<WORK) |
gandol2 | 0:f0000829c039 | 97 | |
gandol2 | 0:f0000829c039 | 98 | //STATUS |
gandol2 | 0:f0000829c039 | 99 | #define INIT_STATUS (0b01<<INIT) |
gandol2 | 0:f0000829c039 | 100 | #define ENCRYPT_STATUS (0b01<<ENCRYPT) |
gandol2 | 0:f0000829c039 | 101 | #define DECRYPT_STATUS (0b01<<DECRYPT) |
gandol2 | 0:f0000829c039 | 102 | #define MODE_STATUS (0b111<<MODE) |
gandol2 | 0:f0000829c039 | 103 | #define WORK_STATUS (0b01<<WORK) |
gandol2 | 0:f0000829c039 | 104 | #define EXTERN2_STATUS (0b01<<EXTERN2) |
gandol2 | 0:f0000829c039 | 105 | |
gandol2 | 0:f0000829c039 | 106 | #define CONFIRM 0xff |
gandol2 | 0:f0000829c039 | 107 | |
gandol2 | 0:f0000829c039 | 108 | #define SET_STATUS(reg, status, set) reg = (((~status) & reg) | set) |
gandol2 | 0:f0000829c039 | 109 | #define GET_STATUS(reg, status) ( status & reg ) |
gandol2 | 0:f0000829c039 | 110 | |
gandol2 | 0:f0000829c039 | 111 | /* |
gandol2 | 0:f0000829c039 | 112 | * AES crypto func |
gandol2 | 0:f0000829c039 | 113 | */ |
gandol2 | 2:47e48f68e858 | 114 | spiDataStr getEncTxPacket(void); |
gandol2 | 2:47e48f68e858 | 115 | spiDataStr getDecTxPacket(void); |
gandol2 | 2:47e48f68e858 | 116 | uint8_t cube_get_status(void); |
gandol2 | 0:f0000829c039 | 117 | bool cube_tpmkey_read(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 118 | bool cube_tpmkey_write(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 119 | void cube_sec_init(void); |
gandol2 | 0:f0000829c039 | 120 | void cube_sec_struct_init(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 121 | bool cube_AES_setkey(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 122 | bool cube_sec_key_reset(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 123 | int32_t cube_AES_encrypt(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 124 | int32_t cube_AES_decrypt(cube_sec_context *cube_ctx); |
gandol2 | 0:f0000829c039 | 125 | bool cube_AES_read_data(cube_sec_context *cube_ctx, uint8_t *output_data, uint32_t size); |
yoondoru | 4:0a155e03336e | 126 | int32_t cube_random_number_generate(const uint8_t *seed, uint8_t *num, uint8_t size); |
gandol2 | 0:f0000829c039 | 127 | bool cube_crypt_spi_cmd_set(sec_spi_data *spi_data); |
gandol2 | 0:f0000829c039 | 128 | void cube_Thread_AES_decrypt(void const *argument); |
gandol2 | 0:f0000829c039 | 129 | void cube_Thread_AES_encrypt(void const *argument); |
gandol2 | 0:f0000829c039 | 130 | |
gandol2 | 0:f0000829c039 | 131 | /* |
gandol2 | 0:f0000829c039 | 132 | * print func |
gandol2 | 0:f0000829c039 | 133 | */ |
gandol2 | 0:f0000829c039 | 134 | |
gandol2 | 0:f0000829c039 | 135 | void print_mbedtls_aes_context(mbedtls_aes_context printStr); |
gandol2 | 0:f0000829c039 | 136 | void print_cube_sec_data(cube_sec_data printStr); |
gandol2 | 0:f0000829c039 | 137 | void print_cube_sec_key(cube_sec_key printStr); |
gandol2 | 0:f0000829c039 | 138 | void print_cube_sec_context(cube_sec_context printStr); |
gandol2 | 0:f0000829c039 | 139 | void print_hex(const char *title, const unsigned char buf[], size_t len); |
gandol2 | 0:f0000829c039 | 140 | |
gandol2 | 0:f0000829c039 | 141 | |
gandol2 | 0:f0000829c039 | 142 | |
gandol2 | 0:f0000829c039 | 143 | extern cube_sec_context Encrypt_ctx; |
gandol2 | 0:f0000829c039 | 144 | extern cube_sec_context Decrypt_ctx; |
gandol2 | 0:f0000829c039 | 145 | |
gandol2 | 2:47e48f68e858 | 146 | #endif /* __CUBE_CRYPTO_H__ */ |