1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifdef FREEBL_NO_DEPEND
6 #include "stubs.h"
7 #endif
8 
9 #include "nspr.h"
10 #include "hasht.h"
11 #include "blapi.h" /* below the line */
12 #include "secerr.h"
13 
14 static void *
null_hash_new_context(void)15 null_hash_new_context(void)
16 {
17     return NULL;
18 }
19 
20 static void *
null_hash_clone_context(void * v)21 null_hash_clone_context(void *v)
22 {
23     PORT_Assert(v == NULL);
24     return NULL;
25 }
26 
27 static void
null_hash_begin(void * v)28 null_hash_begin(void *v)
29 {
30 }
31 
32 static void
null_hash_update(void * v,const unsigned char * input,unsigned int length)33 null_hash_update(void *v, const unsigned char *input, unsigned int length)
34 {
35 }
36 
37 static void
null_hash_end(void * v,unsigned char * output,unsigned int * outLen,unsigned int maxOut)38 null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
39               unsigned int maxOut)
40 {
41     *outLen = 0;
42 }
43 
44 static void
null_hash_destroy_context(void * v,PRBool b)45 null_hash_destroy_context(void *v, PRBool b)
46 {
47     PORT_Assert(v == NULL);
48 }
49 
50 const SECHashObject SECRawHashObjects[] = {
51     { 0,
52       (void *(*)(void))null_hash_new_context,
53       (void *(*)(void *))null_hash_clone_context,
54       (void (*)(void *, PRBool))null_hash_destroy_context,
55       (void (*)(void *))null_hash_begin,
56       (void (*)(void *, const unsigned char *, unsigned int))null_hash_update,
57       (void (*)(void *, unsigned char *, unsigned int *,
58                 unsigned int))null_hash_end,
59       0,
60       HASH_AlgNULL,
61       (void (*)(void *, unsigned char *, unsigned int *,
62                 unsigned int))null_hash_end },
63     {
64         MD2_LENGTH,
65         (void *(*)(void))MD2_NewContext,
66         (void *(*)(void *))null_hash_clone_context,
67         (void (*)(void *, PRBool))MD2_DestroyContext,
68         (void (*)(void *))MD2_Begin,
69         (void (*)(void *, const unsigned char *, unsigned int))MD2_Update,
70         (void (*)(void *, unsigned char *, unsigned int *, unsigned int))MD2_End,
71         MD2_BLOCK_LENGTH,
72         HASH_AlgMD2,
73         NULL /* end_raw */
74     },
75     { MD5_LENGTH,
76       (void *(*)(void))MD5_NewContext,
77       (void *(*)(void *))null_hash_clone_context,
78       (void (*)(void *, PRBool))MD5_DestroyContext,
79       (void (*)(void *))MD5_Begin,
80       (void (*)(void *, const unsigned char *, unsigned int))MD5_Update,
81       (void (*)(void *, unsigned char *, unsigned int *, unsigned int))MD5_End,
82       MD5_BLOCK_LENGTH,
83       HASH_AlgMD5,
84       (void (*)(void *, unsigned char *, unsigned int *, unsigned int))MD5_EndRaw },
85     { SHA1_LENGTH,
86       (void *(*)(void))SHA1_NewContext,
87       (void *(*)(void *))null_hash_clone_context,
88       (void (*)(void *, PRBool))SHA1_DestroyContext,
89       (void (*)(void *))SHA1_Begin,
90       (void (*)(void *, const unsigned char *, unsigned int))SHA1_Update,
91       (void (*)(void *, unsigned char *, unsigned int *, unsigned int))SHA1_End,
92       SHA1_BLOCK_LENGTH,
93       HASH_AlgSHA1,
94       (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
95           SHA1_EndRaw },
96     { SHA256_LENGTH,
97       (void *(*)(void))SHA256_NewContext,
98       (void *(*)(void *))null_hash_clone_context,
99       (void (*)(void *, PRBool))SHA256_DestroyContext,
100       (void (*)(void *))SHA256_Begin,
101       (void (*)(void *, const unsigned char *, unsigned int))SHA256_Update,
102       (void (*)(void *, unsigned char *, unsigned int *,
103                 unsigned int))SHA256_End,
104       SHA256_BLOCK_LENGTH,
105       HASH_AlgSHA256,
106       (void (*)(void *, unsigned char *, unsigned int *,
107                 unsigned int))SHA256_EndRaw },
108     { SHA384_LENGTH,
109       (void *(*)(void))SHA384_NewContext,
110       (void *(*)(void *))null_hash_clone_context,
111       (void (*)(void *, PRBool))SHA384_DestroyContext,
112       (void (*)(void *))SHA384_Begin,
113       (void (*)(void *, const unsigned char *, unsigned int))SHA384_Update,
114       (void (*)(void *, unsigned char *, unsigned int *,
115                 unsigned int))SHA384_End,
116       SHA384_BLOCK_LENGTH,
117       HASH_AlgSHA384,
118       (void (*)(void *, unsigned char *, unsigned int *,
119                 unsigned int))SHA384_EndRaw },
120     { SHA512_LENGTH,
121       (void *(*)(void))SHA512_NewContext,
122       (void *(*)(void *))null_hash_clone_context,
123       (void (*)(void *, PRBool))SHA512_DestroyContext,
124       (void (*)(void *))SHA512_Begin,
125       (void (*)(void *, const unsigned char *, unsigned int))SHA512_Update,
126       (void (*)(void *, unsigned char *, unsigned int *,
127                 unsigned int))SHA512_End,
128       SHA512_BLOCK_LENGTH,
129       HASH_AlgSHA512,
130       (void (*)(void *, unsigned char *, unsigned int *,
131                 unsigned int))SHA512_EndRaw },
132     { SHA224_LENGTH,
133       (void *(*)(void))SHA224_NewContext,
134       (void *(*)(void *))null_hash_clone_context,
135       (void (*)(void *, PRBool))SHA224_DestroyContext,
136       (void (*)(void *))SHA224_Begin,
137       (void (*)(void *, const unsigned char *, unsigned int))SHA224_Update,
138       (void (*)(void *, unsigned char *, unsigned int *,
139                 unsigned int))SHA224_End,
140       SHA224_BLOCK_LENGTH,
141       HASH_AlgSHA224,
142       (void (*)(void *, unsigned char *, unsigned int *,
143                 unsigned int))SHA224_EndRaw },
144 };
145 
146 const SECHashObject *
HASH_GetRawHashObject(HASH_HashType hashType)147 HASH_GetRawHashObject(HASH_HashType hashType)
148 {
149     if (hashType <= HASH_AlgNULL || hashType >= HASH_AlgTOTAL) {
150         PORT_SetError(SEC_ERROR_INVALID_ARGS);
151         return NULL;
152     }
153     return &SECRawHashObjects[hashType];
154 }
155