blob: 800ebaf520e1f1086ce6435010fd20f0a35bfa7a [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;
74 unsigned int auth_iv[5];
Amol Jadi7d3ad062013-01-04 15:30:19 -080075 uint32_t flags;
Ajay Dudanib01e5062011-12-03 23:23:42 -080076} crypto_SHA1_ctx;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080077
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080078typedef struct {
Ajay Dudanib01e5062011-12-03 23:23:42 -080079 unsigned int auth_bytecnt[2];
80 unsigned char saved_buff[64];
81 unsigned char saved_buff_indx;
82 unsigned int auth_iv[8];
Amol Jadi7d3ad062013-01-04 15:30:19 -080083 uint32_t bytes_to_write;
84 uint32_t flags;
Ajay Dudanib01e5062011-12-03 23:23:42 -080085} crypto_SHA256_ctx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080086
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080087extern void crypto_eng_reset(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080088
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080089extern void crypto_eng_init(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080090
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080091extern void crypto_set_sha_ctx(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -080092 unsigned int bytes_to_write,
93 crypto_auth_alg_type auth_alg,
94 bool first, bool last);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080095
96extern void crypto_send_data(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -080097 unsigned char *data_ptr, unsigned int buff_size,
98 unsigned int bytes_to_write,
99 unsigned int *ret_status);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800100
101extern void crypto_get_digest(unsigned char *digest_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800102 unsigned int *ret_status,
103 crypto_auth_alg_type auth_alg, bool last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800104
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800105extern void crypto_get_ctx(void *ctx_ptr);
106
Amol Jadi7d3ad062013-01-04 15:30:19 -0800107extern uint32_t crypto_get_max_auth_blk_size();
108
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800109static void crypto_init(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800110
111static crypto_result_type do_sha(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800112 unsigned int buff_size,
113 unsigned char *digest_ptr,
114 crypto_auth_alg_type auth_alg);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800115
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800116static crypto_result_type do_sha_update(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800117 unsigned char *buff_ptr,
118 unsigned int buff_size,
119 crypto_auth_alg_type auth_alg,
120 bool first, bool last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800121
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800122static unsigned int calc_num_bytes_to_send(void *ctx_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800123 unsigned int buff_size, bool last);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800124
125static crypto_result_type crypto_sha256(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800126 unsigned int buff_size,
127 unsigned char *digest_ptr);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800128
129static crypto_result_type crypto_sha1(unsigned char *buff_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 unsigned int buff_size,
131 unsigned char *digest_ptr);
Channagoud Kadabi1d3e9b42013-12-04 12:35:46 -0800132
133bool crypto_initialized(void);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800134#endif