1 /* $OpenBSD: digest-openssl.c,v 1.5 2014/12/21 22:27:56 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.4 2016/12/25 00:07:47 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_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 }, 47 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 }, 48 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 }, 49 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 }, 50 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 }, 51 { -1, NULL, 0, NULL }, 52 }; 53 54 static const struct ssh_digest * 55 ssh_digest_by_alg(int alg) 56 { 57 if (alg < 0 || alg >= SSH_DIGEST_MAX) 58 return NULL; 59 if (digests[alg].id != alg) /* sanity */ 60 return NULL; 61 return &(digests[alg]); 62 } 63 64 int 65 ssh_digest_alg_by_name(const char *name) 66 { 67 int alg; 68 69 for (alg = 0; digests[alg].id != -1; alg++) { 70 if (strcasecmp(name, digests[alg].name) == 0) 71 return digests[alg].id; 72 } 73 return -1; 74 } 75 76 const char * 77 ssh_digest_alg_name(int alg) 78 { 79 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 80 81 return digest == NULL ? NULL : digest->name; 82 } 83 84 size_t 85 ssh_digest_bytes(int alg) 86 { 87 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 88 89 return digest == NULL ? 0 : digest->digest_len; 90 } 91 92 size_t 93 ssh_digest_blocksize(struct ssh_digest_ctx *ctx) 94 { 95 return EVP_MD_CTX_block_size(&ctx->mdctx); 96 } 97 98 struct ssh_digest_ctx * 99 ssh_digest_start(int alg) 100 { 101 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 102 struct ssh_digest_ctx *ret; 103 104 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL)) 105 return NULL; 106 ret->alg = alg; 107 EVP_MD_CTX_init(&ret->mdctx); 108 if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) { 109 free(ret); 110 return NULL; 111 } 112 return ret; 113 } 114 115 int 116 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) 117 { 118 if (from->alg != to->alg) 119 return SSH_ERR_INVALID_ARGUMENT; 120 /* we have bcopy-style order while openssl has memcpy-style */ 121 if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) 122 return SSH_ERR_LIBCRYPTO_ERROR; 123 return 0; 124 } 125 126 int 127 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 128 { 129 if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) 130 return SSH_ERR_LIBCRYPTO_ERROR; 131 return 0; 132 } 133 134 int 135 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b) 136 { 137 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); 138 } 139 140 int 141 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 142 { 143 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 144 u_int l = dlen; 145 146 if (dlen > UINT_MAX) 147 return SSH_ERR_INVALID_ARGUMENT; 148 if (dlen < digest->digest_len) /* No truncation allowed */ 149 return SSH_ERR_INVALID_ARGUMENT; 150 if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) 151 return SSH_ERR_LIBCRYPTO_ERROR; 152 if (l != digest->digest_len) /* sanity */ 153 return SSH_ERR_INTERNAL_ERROR; 154 return 0; 155 } 156 157 void 158 ssh_digest_free(struct ssh_digest_ctx *ctx) 159 { 160 if (ctx != NULL) { 161 EVP_MD_CTX_cleanup(&ctx->mdctx); 162 explicit_bzero(ctx, sizeof(*ctx)); 163 free(ctx); 164 } 165 } 166 167 int 168 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) 169 { 170 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 171 u_int mdlen; 172 173 if (digest == NULL) 174 return SSH_ERR_INVALID_ARGUMENT; 175 if (dlen > UINT_MAX) 176 return SSH_ERR_INVALID_ARGUMENT; 177 if (dlen < digest->digest_len) 178 return SSH_ERR_INVALID_ARGUMENT; 179 mdlen = dlen; 180 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL)) 181 return SSH_ERR_LIBCRYPTO_ERROR; 182 return 0; 183 } 184 185 int 186 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 187 { 188 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen); 189 } 190