1 /* $OpenBSD: digest-openssl.c,v 1.7 2017/05/08 22:57:38 djm Exp $ */ 2 /* 3 * Copyright (c) 2013 Damien Miller <djm@mindrot.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 #include "includes.h" 18 __RCSID("$NetBSD: digest-openssl.c,v 1.6 2017/10/07 19:39:19 christos Exp $"); 19 20 #include <sys/types.h> 21 #include <limits.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include <openssl/evp.h> 26 27 #include "sshbuf.h" 28 #include "digest.h" 29 #include "ssherr.h" 30 31 struct ssh_digest_ctx { 32 int alg; 33 EVP_MD_CTX mdctx; 34 }; 35 36 struct ssh_digest { 37 int id; 38 const char *name; 39 size_t digest_len; 40 const EVP_MD *(*mdfunc)(void); 41 }; 42 43 /* NB. Indexed directly by algorithm number */ 44 const struct ssh_digest digests[] = { 45 { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 }, 46 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 }, 47 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 }, 48 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 }, 49 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 }, 50 { -1, NULL, 0, NULL }, 51 }; 52 53 static const struct ssh_digest * 54 ssh_digest_by_alg(int alg) 55 { 56 if (alg < 0 || alg >= SSH_DIGEST_MAX) 57 return NULL; 58 if (digests[alg].id != alg) /* sanity */ 59 return NULL; 60 return &(digests[alg]); 61 } 62 63 int 64 ssh_digest_alg_by_name(const char *name) 65 { 66 int alg; 67 68 for (alg = 0; digests[alg].id != -1; alg++) { 69 if (strcasecmp(name, digests[alg].name) == 0) 70 return digests[alg].id; 71 } 72 return -1; 73 } 74 75 const char * 76 ssh_digest_alg_name(int alg) 77 { 78 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 79 80 return digest == NULL ? NULL : digest->name; 81 } 82 83 size_t 84 ssh_digest_bytes(int alg) 85 { 86 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 87 88 return digest == NULL ? 0 : digest->digest_len; 89 } 90 91 size_t 92 ssh_digest_blocksize(struct ssh_digest_ctx *ctx) 93 { 94 return EVP_MD_CTX_block_size(&ctx->mdctx); 95 } 96 97 struct ssh_digest_ctx * 98 ssh_digest_start(int alg) 99 { 100 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 101 struct ssh_digest_ctx *ret; 102 103 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL)) 104 return NULL; 105 ret->alg = alg; 106 EVP_MD_CTX_init(&ret->mdctx); 107 if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) { 108 free(ret); 109 return NULL; 110 } 111 return ret; 112 } 113 114 int 115 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) 116 { 117 if (from->alg != to->alg) 118 return SSH_ERR_INVALID_ARGUMENT; 119 /* we have bcopy-style order while openssl has memcpy-style */ 120 if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) 121 return SSH_ERR_LIBCRYPTO_ERROR; 122 return 0; 123 } 124 125 int 126 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 127 { 128 if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) 129 return SSH_ERR_LIBCRYPTO_ERROR; 130 return 0; 131 } 132 133 int 134 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b) 135 { 136 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); 137 } 138 139 int 140 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 141 { 142 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 143 u_int l = dlen; 144 145 if (digest == NULL || dlen > UINT_MAX) 146 return SSH_ERR_INVALID_ARGUMENT; 147 if (dlen < digest->digest_len) /* No truncation allowed */ 148 return SSH_ERR_INVALID_ARGUMENT; 149 if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) 150 return SSH_ERR_LIBCRYPTO_ERROR; 151 if (l != digest->digest_len) /* sanity */ 152 return SSH_ERR_INTERNAL_ERROR; 153 return 0; 154 } 155 156 void 157 ssh_digest_free(struct ssh_digest_ctx *ctx) 158 { 159 if (ctx != NULL) { 160 EVP_MD_CTX_cleanup(&ctx->mdctx); 161 explicit_bzero(ctx, sizeof(*ctx)); 162 free(ctx); 163 } 164 } 165 166 int 167 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) 168 { 169 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 170 u_int mdlen; 171 172 if (digest == NULL) 173 return SSH_ERR_INVALID_ARGUMENT; 174 if (dlen > UINT_MAX) 175 return SSH_ERR_INVALID_ARGUMENT; 176 if (dlen < digest->digest_len) 177 return SSH_ERR_INVALID_ARGUMENT; 178 mdlen = dlen; 179 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL)) 180 return SSH_ERR_LIBCRYPTO_ERROR; 181 return 0; 182 } 183 184 int 185 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 186 { 187 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen); 188 } 189