blob: bdcf7e0da34eaf4fd0d0e630306094ebca89616b [file] [log] [blame]
Amol Jadi7d3ad062013-01-04 15:30:19 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
2 *
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -08003 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
Amol Jadi7d3ad062013-01-04 15:30:19 -08006 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080015 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __CRYPTO_HASH_H__
30#define __CRYPYO_HASH_H__
31
32#ifndef NULL
33#define NULL 0
34#endif
35
36#define TRUE 1
37#define FALSE 0
38
39#define SHA256_INIT_VECTOR_SIZE 8
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080040#define SHA1_INIT_VECTOR_SIZE 5
41
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080042#define CRYPTO_SHA_BLOCK_SIZE 64
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080043
Shashank Mittal1fcde7a2011-07-25 13:41:50 -070044#define CRYPTO_ERR_NONE 0x01
45#define CRYPTO_ERR_FAIL 0x02
46
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080047typedef enum {
Amol Jadi7d3ad062013-01-04 15:30:19 -080048 CRYPTO_FIRST_CHUNK = 1,
49 CRYPTO_LAST_CHUNK = 2,
50} crypto_flag_type;
51
52typedef enum {
Ajay Dudanib01e5062011-12-03 23:23:42 -080053 CRYPTO_SHA_ERR_NONE,
54 CRYPTO_SHA_ERR_BUSY,
55 CRYPTO_SHA_ERR_FAIL,
56 CRYPTO_SHA_ERR_INVALID_PARAM,
57} crypto_result_type;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080058
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080059typedef enum {
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080060 CRYPTO_ENGINE_TYPE_NONE,
61 CRYPTO_ENGINE_TYPE_SW,
62 CRYPTO_ENGINE_TYPE_HW,
63}crypto_engine_type;
64
65typedef enum {
Ajay Dudanib01e5062011-12-03 23:23:42 -080066 CRYPTO_AUTH_ALG_SHA1 = 1,
67 CRYPTO_AUTH_ALG_SHA256
68} crypto_auth_alg_type;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080069
70typedef struct {
Ajay Dudanib01e5062011-12-03 23:23:42 -080071 unsigned int auth_bytecnt[2];
72 unsigned char saved_buff[64];
73 unsigned char saved_buff_indx;
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080074 uint32_t bytes_to_write;
Amol Jadi7d3ad062013-01-04 15:30:19 -080075 uint32_t flags;
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080076 unsigned int auth_iv[5];
Ajay Dudanib01e5062011-12-03 23:23:42 -080077} crypto_SHA1_ctx;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080078
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080079typedef struct {
Ajay Dudanib01e5062011-12-03 23:23:42 -080080 unsigned int auth_bytecnt[2];
81 unsigned char saved_buff[64];
82 unsigned char saved_buff_indx;
Amol Jadi7d3ad062013-01-04 15:30:19 -080083 uint32_t bytes_to_write;
84 uint32_t flags;
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080085 unsigned int auth_iv[8];
Ajay Dudanib01e5062011-12-03 23:23:42 -080086} crypto_SHA256_ctx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080087
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080088extern void crypto_eng_reset(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080089
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080090extern void crypto_eng_init(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080091
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080092extern void crypto_set_sha_ctx(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 unsigned int bytes_to_write,
94 crypto_auth_alg_type auth_alg,
95 bool first, bool last);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080096
97extern void crypto_send_data(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -080098 unsigned char *data_ptr, unsigned int buff_size,
99 unsigned int bytes_to_write,
100 unsigned int *ret_status);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800101
102extern void crypto_get_digest(unsigned char *digest_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800103 unsigned int *ret_status,
104 crypto_auth_alg_type auth_alg, bool last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800105
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800106extern void crypto_get_ctx(void *ctx_ptr);
107
Amol Jadi7d3ad062013-01-04 15:30:19 -0800108extern uint32_t crypto_get_max_auth_blk_size();
109
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800110static void crypto_init(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800111
112static crypto_result_type do_sha(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800113 unsigned int buff_size,
114 unsigned char *digest_ptr,
115 crypto_auth_alg_type auth_alg);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800116
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800117static crypto_result_type do_sha_update(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 unsigned char *buff_ptr,
119 unsigned int buff_size,
120 crypto_auth_alg_type auth_alg,
121 bool first, bool last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800122
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800123static unsigned int calc_num_bytes_to_send(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124 unsigned int buff_size, bool last);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800125
126static crypto_result_type crypto_sha256(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800127 unsigned int buff_size,
128 unsigned char *digest_ptr);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800129
130static crypto_result_type crypto_sha1(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800131 unsigned int buff_size,
132 unsigned char *digest_ptr);
Channagoud Kadabi1d3e9b42013-12-04 12:35:46 -0800133
134bool crypto_initialized(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800135#endif