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