1 /* $OpenBSD: digest-openssl.c,v 1.4 2014/07/03 03:26:43 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 18 #include <sys/types.h> 19 #include <limits.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include <openssl/evp.h> 24 25 #include "sshbuf.h" 26 #include "digest.h" 27 #include "ssherr.h" 28 29 struct ssh_digest_ctx { 30 int alg; 31 EVP_MD_CTX mdctx; 32 }; 33 34 struct ssh_digest { 35 int id; 36 const char *name; 37 size_t digest_len; 38 const EVP_MD *(*mdfunc)(void); 39 }; 40 41 /* NB. Indexed directly by algorithm number */ 42 const struct ssh_digest digests[] = { 43 { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 }, 44 { SSH_DIGEST_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 }, 45 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 }, 46 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 }, 47 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 }, 48 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 }, 49 { -1, NULL, 0, NULL }, 50 }; 51 52 static const struct ssh_digest * 53 ssh_digest_by_alg(int alg) 54 { 55 if (alg < 0 || alg >= SSH_DIGEST_MAX) 56 return NULL; 57 if (digests[alg].id != alg) /* sanity */ 58 return NULL; 59 return &(digests[alg]); 60 } 61 62 size_t 63 ssh_digest_bytes(int alg) 64 { 65 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 66 67 return digest == NULL ? 0 : digest->digest_len; 68 } 69 70 size_t 71 ssh_digest_blocksize(struct ssh_digest_ctx *ctx) 72 { 73 return EVP_MD_CTX_block_size(&ctx->mdctx); 74 } 75 76 struct ssh_digest_ctx * 77 ssh_digest_start(int alg) 78 { 79 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 80 struct ssh_digest_ctx *ret; 81 82 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL)) 83 return NULL; 84 ret->alg = alg; 85 EVP_MD_CTX_init(&ret->mdctx); 86 if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) { 87 free(ret); 88 return NULL; 89 } 90 return ret; 91 } 92 93 int 94 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) 95 { 96 if (from->alg != to->alg) 97 return SSH_ERR_INVALID_ARGUMENT; 98 /* we have bcopy-style order while openssl has memcpy-style */ 99 if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) 100 return SSH_ERR_LIBCRYPTO_ERROR; 101 return 0; 102 } 103 104 int 105 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) 106 { 107 if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) 108 return SSH_ERR_LIBCRYPTO_ERROR; 109 return 0; 110 } 111 112 int 113 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b) 114 { 115 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); 116 } 117 118 int 119 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) 120 { 121 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); 122 u_int l = dlen; 123 124 if (dlen > UINT_MAX) 125 return SSH_ERR_INVALID_ARGUMENT; 126 if (dlen < digest->digest_len) /* No truncation allowed */ 127 return SSH_ERR_INVALID_ARGUMENT; 128 if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) 129 return SSH_ERR_LIBCRYPTO_ERROR; 130 if (l != digest->digest_len) /* sanity */ 131 return SSH_ERR_INTERNAL_ERROR; 132 return 0; 133 } 134 135 void 136 ssh_digest_free(struct ssh_digest_ctx *ctx) 137 { 138 if (ctx != NULL) { 139 EVP_MD_CTX_cleanup(&ctx->mdctx); 140 explicit_bzero(ctx, sizeof(*ctx)); 141 free(ctx); 142 } 143 } 144 145 int 146 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) 147 { 148 const struct ssh_digest *digest = ssh_digest_by_alg(alg); 149 u_int mdlen; 150 151 if (digest == NULL) 152 return SSH_ERR_INVALID_ARGUMENT; 153 if (dlen > UINT_MAX) 154 return SSH_ERR_INVALID_ARGUMENT; 155 if (dlen < digest->digest_len) 156 return SSH_ERR_INVALID_ARGUMENT; 157 mdlen = dlen; 158 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL)) 159 return SSH_ERR_LIBCRYPTO_ERROR; 160 return 0; 161 } 162 163 int 164 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) 165 { 166 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen); 167 } 168