]> pd.if.org Git - zpackage/blob - crypto/hash.c
remove stray debug fprintf
[zpackage] / crypto / hash.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include "tlse.h"
4
5 void tls_destroy_hash(struct TLSContext *context) {
6         if (context) {
7                 context->hs_index = -1;
8         }
9         return;
10 }
11
12 static void create_hash(struct TLSContext *context) {
13         int index;
14
15         if (!context) {
16                 return;
17         }
18
19         int hash_size = tls_mac_length(context);
20
21         if (hash_size == TLS_SHA384_MAC_SIZE) {
22                 index = find_hash("sha384");
23         } else {
24                 index = find_hash("sha256");
25         }
26         context->hs_index = index;
27         context->handshake_init = hash_descriptor[index].init;
28         context->handshake_process = hash_descriptor[index].process;
29         context->handshake_done = hash_descriptor[index].done;
30
31         context->handshake_init(&context->hs_hash);
32         context->hs_created = 1;
33 }
34
35 int tls_update_hash(struct TLSContext *context, const unsigned char *in,
36                 unsigned int len) {
37         if (!context) {
38                 return 0;
39         }
40
41         if (!len) {
42                 return 0;
43         }
44
45         if (!context->hs_created) {
46                 create_hash(context);
47         }
48
49         context->handshake_process(&context->hs_hash, in, len);
50
51         if (context->request_client_certificate) {
52                 tls_buffer_append(&context->cached_handshake, in, len);
53         }
54         return 0;
55 }
56
57 int tls_done_hash(struct TLSContext *context, unsigned char *hout) {
58         if (!context) {
59                 return 0;
60         }
61
62         if (!context->hs_created) {
63                 return 0;
64         }
65
66         if (hout) {
67                 context->handshake_done(&context->hs_hash, hout);
68         }
69         /* TODO zero memory? */
70         context->hs_created = 0;
71
72         tls_buffer_free(&context->cached_handshake);
73         return tls_mac_length(context);
74 }
75
76 int tls_get_hash_idx(struct TLSContext *context) {
77         if (!context) {
78                 return -1;
79         }
80
81         switch (tls_mac_length(context)) {
82                 case TLS_SHA256_MAC_SIZE:
83                         return find_hash("sha256");
84                 case TLS_SHA384_MAC_SIZE:
85                         return find_hash("sha384");
86                 case TLS_SHA1_MAC_SIZE:
87                         return find_hash("sha1");
88         }
89         return -1;
90 }
91
92 int tls_get_hash(struct TLSContext *context, unsigned char *hout) {
93         hash_state prec;
94
95         if (!context) {
96                 return 0;
97         }
98
99         if (!context->hs_created) {
100                 return 0;
101         }
102
103         prec = context->hs_hash;
104
105         context->handshake_done(&prec, hout);
106
107         return tls_mac_length(context);
108 }