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