2 * Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_NSS_H
18 #define H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_NSS_H
24 #include "util-cast.h"
26 typedef struct SECHashObjectStr ensc_hash_method;
27 typedef struct HASHContextStr *ensc_hash_context;
30 ensc_crypto_init(void)
36 inline static ensc_hash_method const *
37 ensc_crypto_hash_get_default(void)
39 return HASH_GetHashObject(SEC_OID_SHA1);
42 inline static ensc_hash_method const *
43 ensc_crypto_hash_find(char const *id_c)
48 char *id = strdupa(id_c);
57 while ((ptr=strchr(ptr, '-'))!=NULL)
58 memmove(ptr, ptr+1, strlen(ptr));
60 if (strcmp(id, "md2")==0)
62 else if (strcmp(id, "md5")==0)
64 else if (strcmp(id, "sha1")==0)
66 else if (strcasecmp(id, "sha256")==0)
68 else if (strcasecmp(id, "sha384")==0)
70 else if (strcasecmp(id, "sha512")==0)
73 oid = SEC_OID_UNKNOWN;
76 struct SECItemStr const item = {
78 .data = const_cast(unsigned char *)(static_cast(unsigned char const *)(id)),
83 oid = SECOID_FindOIDTag(&item);
86 return HASH_GetHashObjectByOidTag(oid);
89 inline static char const *
90 ensc_crypto_hash_get_name(ensc_hash_method const *m)
92 char const * const NAMES[] = {
93 [HASH_AlgNULL] = "null",
94 [HASH_AlgMD2] = "md2",
95 [HASH_AlgMD5] = "md5",
96 [HASH_AlgSHA1] = "sha1",
97 [HASH_AlgSHA256] = "sha256",
98 [HASH_AlgSHA384] = "sha384",
99 [HASH_AlgSHA512] = "sha512",
101 size_t idx = static_cast(size_t)(m->type);
103 if (idx >= sizeof(NAMES)/sizeof(NAMES[0]))
107 /* TODO: use SECOID_FindOIDTagDescription()? */
111 ensc_crypto_hash_get_digestsize(ensc_hash_method const *m)
113 size_t const SIZES[] = {
114 [HASH_AlgMD2] = MD2_LENGTH,
115 [HASH_AlgMD5] = MD5_LENGTH,
116 [HASH_AlgSHA1] = SHA1_LENGTH,
117 [HASH_AlgSHA256] = SHA256_LENGTH,
118 [HASH_AlgSHA384] = SHA384_LENGTH,
119 [HASH_AlgSHA512] = SHA512_LENGTH,
121 size_t idx = static_cast(size_t)(m->type);
123 if (idx >= sizeof(SIZES)/sizeof(SIZES[0]))
130 ensc_crypto_hashctx_get_digestsize(ensc_hash_context const *ctx)
132 return ensc_crypto_hash_get_digestsize((*ctx)->hashobj);
136 ensc_crypto_hashctx_get_digest(ensc_hash_context *ctx, void *result,
137 size_t UNUSED *res_len_a, size_t UNUSED max_res_len)
139 unsigned int res_len;
141 HASH_End(*ctx, result, &res_len, max_res_len);
143 *res_len_a = res_len;
149 ensc_crypto_hashctx_init(ensc_hash_context *ctx, ensc_hash_method const *m)
151 *ctx = HASH_Create(m->type);
152 return *ctx==NULL ? -1 : 0;
156 ensc_crypto_hashctx_update(ensc_hash_context *ctx, void const *src, size_t len)
158 HASH_Update(*ctx, src, len);
163 ensc_crypto_hashctx_reset(ensc_hash_context *ctx)
170 ensc_crypto_hashctx_free(ensc_hash_context *ctx)
176 #endif /* H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_NSS_H */