1 /* $OpenBSD: digest-libc.c,v 1.7 2020/02/26 13:40:09 jsg Exp $ */ 2 /* 3 * Copyright (c) 2013 Damien Miller <djm@mindrot.org> 4 * Copyright (c) 2014 Markus Friedl. All rights reserved. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "includes.h" 20 21 #ifndef WITH_OPENSSL 22 23 #include <sys/types.h> 24 #include <limits.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #if 0 29 #include <md5.h> 30 #include <rmd160.h> 31 #endif 32 #ifdef HAVE_SHA1_H 33 #include <sha1.h> 34 #endif 35 #ifdef HAVE_SHA2_H 36 #include <sha2.h> 37 #endif 38 39 #if !defined(SHA256_BLOCK_LENGTH) && defined(SHA256_HMAC_BLOCK_SIZE) 40 #define SHA256_BLOCK_LENGTH SHA256_HMAC_BLOCK_SIZE 41 #endif 42 #if !defined(SHA384_BLOCK_LENGTH) && defined(SHA512_HMAC_BLOCK_SIZE) 43 #define SHA384_BLOCK_LENGTH SHA512_HMAC_BLOCK_SIZE 44 #endif 45 #if !defined(SHA512_BLOCK_LENGTH) && defined(SHA512_HMAC_BLOCK_SIZE) 46 #define SHA512_BLOCK_LENGTH SHA512_HMAC_BLOCK_SIZE 47 #endif 48 49 #include "ssherr.h" 50 #include "sshbuf.h" 51 #include "digest.h" 52 53 typedef void md_init_fn(void *mdctx); 54 typedef void md_update_fn(void *mdctx, const u_int8_t *m, size_t mlen); 55 typedef void md_final_fn(u_int8_t[], void *mdctx); 56 57 struct ssh_digest_ctx { 58 int alg; 59 void *mdctx; 60 }; 61 62 struct ssh_digest { 63 int id; 64 const char *name; 65 size_t block_len; 66 size_t digest_len; 67 size_t ctx_len; 68 md_init_fn *md_init; 69 md_update_fn *md_update; 70 md_final_fn *md_final; 71 }; 72 73 /* NB. Indexed directly by algorithm number */ 74 const struct ssh_digest digests[SSH_DIGEST_MAX] = { 75 { 76 SSH_DIGEST_MD5, 77 "MD5", 78 MD5_BLOCK_LENGTH, 79 MD5_DIGEST_LENGTH, 80 sizeof(MD5_CTX), 81 (md_init_fn *) MD5Init, 82 (md_update_fn *) MD5Update, 83 (md_final_fn *) MD5Final 84 }, 85 { 86 SSH_DIGEST_SHA1, 87 "SHA1", 88 SHA1_BLOCK_LENGTH, 89 SHA1_DIGEST_LENGTH, 90 sizeof(SHA1_CTX), 91 (md_init_fn *) SHA1Init, 92 (md_update_fn *) SHA1Update, 93 (md_final_fn *) SHA1Final 94 }, 95 { 96 SSH_DIGEST_SHA256, 97 "SHA256", 98 SHA256_BLOCK_LENGTH, 99 SHA256_DIGEST_LENGTH, 100 sizeof(SHA2_CTX), 101 (md_init_fn *) SHA256Init, 102 (md_update_fn *) SHA256Update, 103 (md_final_fn *) SHA256Final 104 }, 105 { 106 SSH_DIGEST_SHA384, 107 "SHA384", 108 SHA384_BLOCK_LENGTH, 109 SHA384_DIGEST_LENGTH, 110 sizeof(SHA2_CTX), 111 (md_init_fn *) SHA384Init, 112 (md_update_fn *) SHA384Update, 113 (md_final_fn *) SHA384Final 114 }, 115 { 116 SSH_DIGEST_SHA512, 117 "SHA512", 118 SHA512_BLOCK_LENGTH, 119 SHA512_DIGEST_LENGTH, 120 sizeof(SHA2_CTX), 121 (md_init_fn *) SHA512Init, 122 (md_update_fn *) SHA512Update, 123 (md_final_fn *) SHA512Final 124 } 125 }; 126 127 static const struct ssh_digest * 128 ssh_digest_by_alg(int alg) 129 { 130 if (alg < 0 || alg >= SSH_DIGEST_MAX) 131 return NULL; 132 if (digests[alg].id != alg) /* sanity */ 133 return NULL; 134 return &(digests[alg]); 135 } 136 137 int 138 ssh_digest_alg_by_name(const char *name) 139 { 140 int alg; 141 142 for (alg = 0; alg < SSH_DIGEST_MAX; alg++) { 143 if (strcasecmp(name, digests[alg].name) == 0) 144 return digests[alg].id; 145 } 146 return -1; 147 } 148 149 const char * 150 ssh_digest_alg_name(int alg) 151 { 152 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 153 154 return digest == NULL ? NULL : digest->name; 155 } 156 157 size_t 158 ssh_digest_bytes(int alg) 159 { 160 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 161 162 return digest == NULL ? 0 : digest->digest_len; 163 } 164 165 size_t 166 ssh_digest_blocksize(struct ssh_digest_ctx *ctx) 167 { 168 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 169 170 return digest == NULL ? 0 : digest->block_len; 171 } 172 173 struct ssh_digest_ctx * 174 ssh_digest_start(int alg) 175 { 176 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 177 struct ssh_digest_ctx *ret; 178 179 if (digest == NULL || (ret = calloc(1, sizeof(*ret))) == NULL) 180 return NULL; 181 if ((ret->mdctx = calloc(1, digest->ctx_len)) == NULL) { 182 free(ret); 183 return NULL; 184 } 185 ret->alg = alg; 186 digest->md_init(ret->mdctx); 187 return ret; 188 } 189 190 int 191 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) 192 { 193 const struct ssh_digest *digest = ssh_digest_by_alg(from->alg); 194 195 if (digest == NULL || from->alg != to->alg) 196 return SSH_ERR_INVALID_ARGUMENT; 197 memcpy(to->mdctx, from->mdctx, digest->ctx_len); 198 return 0; 199 } 200 201 int 202 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 203 { 204 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 205 206 if (digest == NULL) 207 return SSH_ERR_INVALID_ARGUMENT; 208 digest->md_update(ctx->mdctx, m, mlen); 209 return 0; 210 } 211 212 int 213 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b) 214 { 215 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); 216 } 217 218 int 219 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 220 { 221 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 222 223 if (digest == NULL) 224 return SSH_ERR_INVALID_ARGUMENT; 225 if (dlen > UINT_MAX) 226 return SSH_ERR_INVALID_ARGUMENT; 227 if (dlen < digest->digest_len) /* No truncation allowed */ 228 return SSH_ERR_INVALID_ARGUMENT; 229 digest->md_final(d, ctx->mdctx); 230 return 0; 231 } 232 233 void 234 ssh_digest_free(struct ssh_digest_ctx *ctx) 235 { 236 const struct ssh_digest *digest; 237 238 if (ctx != NULL) { 239 digest = ssh_digest_by_alg(ctx->alg); 240 if (digest) { 241 explicit_bzero(ctx->mdctx, digest->ctx_len); 242 free(ctx->mdctx); 243 freezero(ctx, sizeof(*ctx)); 244 } 245 } 246 } 247 248 int 249 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) 250 { 251 struct ssh_digest_ctx *ctx = ssh_digest_start(alg); 252 253 if (ctx == NULL) 254 return SSH_ERR_INVALID_ARGUMENT; 255 if (ssh_digest_update(ctx, m, mlen) != 0 || 256 ssh_digest_final(ctx, d, dlen) != 0) 257 return SSH_ERR_INVALID_ARGUMENT; 258 ssh_digest_free(ctx); 259 return 0; 260 } 261 262 int 263 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 264 { 265 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen); 266 } 267 #endif /* !WITH_OPENSSL */ 268