blob: e7ca31d72f5124799324e5afe04f9732922afe98 [file] [log] [blame]
Amaury Pouly76125592010-11-29 14:15:06 +00001/***************************************************************************
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 Pouly9fe029b2011-10-29 14:22:17 +000021#ifndef __CRYPTO_H__
22#define __CRYPTO_H__
23
Amaury Pouly76125592010-11-29 14:15:06 +000024#include <stdio.h>
25#include <stdint.h>
26#include <string.h>
Amaury Pouly2b200262017-01-01 20:48:05 +010027#include <stdbool.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
Amaury Pouly76125592010-11-29 14:15:06 +000032
33typedef uint8_t byte;
34
Amaury Pouly9fe029b2011-10-29 14:22:17 +000035/* crypto.c */
36enum crypto_method_t
37{
38 CRYPTO_NONE, /* disable */
39 CRYPTO_KEY, /* key */
Amaury Pouly4e95b722012-11-26 23:54:44 +010040 CRYPTO_XOR_KEY, /* XOR key */
Amaury Pouly9fe029b2011-10-29 14:22:17 +000041};
42
Amaury Pouly4e95b722012-11-26 23:54:44 +010043union xorcrypt_key_t
44{
45 uint8_t key[64];
46 uint32_t k[16];
47};
48
Amaury Pouly9fe029b2011-10-29 14:22:17 +000049/* all-in-one function */
50struct crypto_key_t
51{
52 enum crypto_method_t method;
53 union
54 {
55 byte key[16];
Amaury Pouly4e95b722012-11-26 23:54:44 +010056 union xorcrypt_key_t xor_key[2];
Amaury Pouly9fe029b2011-10-29 14:22:17 +000057 }u;
58};
59
Amaury Pouly2b200262017-01-01 20:48:05 +010060#define CRYPTO_ERROR_SUCCESS 0
61#define CRYPTO_ERROR_BADSETUP -1
Amaury Pouly8b3f5a82017-01-03 13:56:48 +010062#define CRYPTO_ERROR_INVALID_OP -2
Amaury Pouly2b200262017-01-01 20:48:05 +010063
64/* parameter can be:
65 * - CRYPTO_KEY: array of 16-bytes (the key)
66 * return 0 on success, <0 on error */
67int crypto_setup(struct crypto_key_t *key);
68
69/* return 0 on success, <0 on error */
70int crypto_apply(
Amaury Pouly9fe029b2011-10-29 14:22:17 +000071 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 Pouly9fe029b2011-10-29 14:22:17 +000074 byte iv[16], /* IV */
75 byte (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
Amaury Pouly2b200262017-01-01 20:48:05 +010076 bool encrypt);
Amaury Pouly9fe029b2011-10-29 14:22:17 +000077
Amaury Pouly76125592010-11-29 14:15:06 +000078/* crc.c */
79uint32_t crc(byte *data, int size);
Amaury Poulyb2c59542011-04-17 22:30:09 +000080uint32_t crc_continue(uint32_t previous_crc, byte *data, int size);
Amaury Pouly76125592010-11-29 14:15:06 +000081
82/* sha1.c */
83struct sha_1_params_t
84{
Amaury Pouly759a78e2017-01-03 16:09:34 +010085 byte hash[20]; /* final hash */
86 void *object; /* pointer to CryptoPP::SHA1 object */
Amaury Pouly76125592010-11-29 14:15:06 +000087};
88
89void sha_1_init(struct sha_1_params_t *params);
Amaury Pouly76125592010-11-29 14:15:06 +000090void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size);
91void sha_1_finish(struct sha_1_params_t *params);
92void sha_1_output(struct sha_1_params_t *params, byte *out);
Amaury Pouly9fe029b2011-10-29 14:22:17 +000093
Amaury Pouly4e95b722012-11-26 23:54:44 +010094/* xorcrypt.c */
95
96// WARNING those functions modifies the keys !!
97uint32_t xor_encrypt(union xorcrypt_key_t keys[2], void *data, int size);
98uint32_t xor_decrypt(union xorcrypt_key_t keys[2], void *data, int size);
Amaury Poulyb05b7622013-02-16 20:47:07 +010099void xor_generate_key(uint32_t laserfuse[3], union xorcrypt_key_t key[2]);
Amaury Pouly4e95b722012-11-26 23:54:44 +0100100
Amaury Pouly2b200262017-01-01 20:48:05 +0100101#ifdef __cplusplus
102}
103#endif
104
Amaury Pouly9fe029b2011-10-29 14:22:17 +0000105#endif /* __CRYPTO_H__ */