Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2010 Amaury Pouly |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 21 | #ifndef __CRYPTO_H__ |
| 22 | #define __CRYPTO_H__ |
| 23 | |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
Amaury Pouly | 2b20026 | 2017-01-01 20:48:05 +0100 | [diff] [blame] | 27 | #include <stdbool.h> |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 32 | |
| 33 | typedef uint8_t byte; |
| 34 | |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 35 | /* crypto.c */ |
| 36 | enum crypto_method_t |
| 37 | { |
| 38 | CRYPTO_NONE, /* disable */ |
| 39 | CRYPTO_KEY, /* key */ |
Amaury Pouly | 4e95b72 | 2012-11-26 23:54:44 +0100 | [diff] [blame] | 40 | CRYPTO_XOR_KEY, /* XOR key */ |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Amaury Pouly | 4e95b72 | 2012-11-26 23:54:44 +0100 | [diff] [blame] | 43 | union xorcrypt_key_t |
| 44 | { |
| 45 | uint8_t key[64]; |
| 46 | uint32_t k[16]; |
| 47 | }; |
| 48 | |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 49 | /* all-in-one function */ |
| 50 | struct crypto_key_t |
| 51 | { |
| 52 | enum crypto_method_t method; |
| 53 | union |
| 54 | { |
| 55 | byte key[16]; |
Amaury Pouly | 4e95b72 | 2012-11-26 23:54:44 +0100 | [diff] [blame] | 56 | union xorcrypt_key_t xor_key[2]; |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 57 | }u; |
| 58 | }; |
| 59 | |
Amaury Pouly | 2b20026 | 2017-01-01 20:48:05 +0100 | [diff] [blame] | 60 | #define CRYPTO_ERROR_SUCCESS 0 |
| 61 | #define CRYPTO_ERROR_BADSETUP -1 |
Amaury Pouly | 8b3f5a8 | 2017-01-03 13:56:48 +0100 | [diff] [blame] | 62 | #define CRYPTO_ERROR_INVALID_OP -2 |
Amaury Pouly | 2b20026 | 2017-01-01 20:48:05 +0100 | [diff] [blame] | 63 | |
| 64 | /* parameter can be: |
| 65 | * - CRYPTO_KEY: array of 16-bytes (the key) |
| 66 | * return 0 on success, <0 on error */ |
| 67 | int crypto_setup(struct crypto_key_t *key); |
| 68 | |
| 69 | /* return 0 on success, <0 on error */ |
| 70 | int crypto_apply( |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 71 | byte *in_data, /* Input data */ |
| 72 | byte *out_data, /* Output data (or NULL) */ |
| 73 | int nr_blocks, /* Number of blocks (one block=16 bytes) */ |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 74 | byte iv[16], /* IV */ |
| 75 | byte (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ |
Amaury Pouly | 2b20026 | 2017-01-01 20:48:05 +0100 | [diff] [blame] | 76 | bool encrypt); |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 77 | |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 78 | /* crc.c */ |
| 79 | uint32_t crc(byte *data, int size); |
Amaury Pouly | b2c5954 | 2011-04-17 22:30:09 +0000 | [diff] [blame] | 80 | uint32_t crc_continue(uint32_t previous_crc, byte *data, int size); |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 81 | |
| 82 | /* sha1.c */ |
| 83 | struct sha_1_params_t |
| 84 | { |
Amaury Pouly | 759a78e | 2017-01-03 16:09:34 +0100 | [diff] [blame] | 85 | byte hash[20]; /* final hash */ |
| 86 | void *object; /* pointer to CryptoPP::SHA1 object */ |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | void sha_1_init(struct sha_1_params_t *params); |
Amaury Pouly | 7612559 | 2010-11-29 14:15:06 +0000 | [diff] [blame] | 90 | void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size); |
| 91 | void sha_1_finish(struct sha_1_params_t *params); |
| 92 | void sha_1_output(struct sha_1_params_t *params, byte *out); |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 93 | |
Amaury Pouly | 4e95b72 | 2012-11-26 23:54:44 +0100 | [diff] [blame] | 94 | /* xorcrypt.c */ |
| 95 | |
| 96 | // WARNING those functions modifies the keys !! |
| 97 | uint32_t xor_encrypt(union xorcrypt_key_t keys[2], void *data, int size); |
| 98 | uint32_t xor_decrypt(union xorcrypt_key_t keys[2], void *data, int size); |
Amaury Pouly | b05b762 | 2013-02-16 20:47:07 +0100 | [diff] [blame] | 99 | void xor_generate_key(uint32_t laserfuse[3], union xorcrypt_key_t key[2]); |
Amaury Pouly | 4e95b72 | 2012-11-26 23:54:44 +0100 | [diff] [blame] | 100 | |
Amaury Pouly | 2b20026 | 2017-01-01 20:48:05 +0100 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | |
Amaury Pouly | 9fe029b | 2011-10-29 14:22:17 +0000 | [diff] [blame] | 105 | #endif /* __CRYPTO_H__ */ |