1 /*
2   ROM functions for hardware SHA support.
3 
4   It is not recommended to use these functions directly.  If using
5   them from esp-idf then use the esp_sha_lock_engine() and
6   esp_sha_lock_memory_block() functions in hwcrypto/sha.h to ensure
7   exclusive access.
8  */
9 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 
15 //     http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // Unless required by applicable law or agreed to in writing, software
18 // distributed under the License is distributed on an "AS IS" BASIS,
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 // See the License for the specific language governing permissions and
21 // limitations under the License.
22 #ifndef _ROM_SHA_H_
23 #define _ROM_SHA_H_
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include "ets_sys.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef enum {
34     SHA1 = 0,
35     SHA2_224,
36     SHA2_256,
37     SHA2_384,
38     SHA2_512,
39     SHA2_512224,
40     SHA2_512256,
41     SHA2_512T,
42     SHA_TYPE_MAX
43 } SHA_TYPE;
44 
45 typedef struct SHAContext {
46     bool start;
47     bool in_hardware;               // Is this context currently in peripheral? Needs to be manually cleared if multiple SHAs are interleaved
48     SHA_TYPE type;
49     uint32_t state[16];             // For SHA1/SHA224/SHA256, used 8, other used 16
50     unsigned char buffer[128];      // For SHA1/SHA224/SHA256, used 64, other used 128
51     uint32_t total_bits[4];
52 } SHA_CTX;
53 
54 void ets_sha_enable(void);
55 
56 void ets_sha_disable(void);
57 
58 ets_status_t ets_sha_init(SHA_CTX *ctx, SHA_TYPE type);
59 
60 ets_status_t ets_sha_starts(SHA_CTX *ctx, uint16_t sha512_t);
61 
62 void ets_sha_get_state(SHA_CTX *ctx);
63 
64 void ets_sha_process(SHA_CTX *ctx, const unsigned char *input);
65 
66 void ets_sha_update(SHA_CTX *ctx, const unsigned char *input, uint32_t input_bytes, bool update_ctx);
67 
68 ets_status_t ets_sha_finish(SHA_CTX *ctx, unsigned char *output);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #endif /* _ROM_SHA_H_ */
75