OpenCoverage

digest-openssl.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/digest-openssl.c
Source codeSwitch to Preprocessed file
LineSourceCount
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-
18#include "includes.h"-
19-
20#ifdef WITH_OPENSSL-
21-
22#include <sys/types.h>-
23#include <limits.h>-
24#include <stdlib.h>-
25#include <string.h>-
26-
27#include <openssl/evp.h>-
28-
29#include "openbsd-compat/openssl-compat.h"-
30-
31#include "sshbuf.h"-
32#include "digest.h"-
33#include "ssherr.h"-
34-
35#ifndef HAVE_EVP_RIPEMD160-
36# define EVP_ripemd160 NULL-
37#endif /* HAVE_EVP_RIPEMD160 */-
38#ifndef HAVE_EVP_SHA256-
39# define EVP_sha256 NULL-
40# define EVP_sha384 NULL-
41# define EVP_sha512 NULL-
42#endif /* HAVE_EVP_SHA256 */-
43-
44struct ssh_digest_ctx {-
45 int alg;-
46 EVP_MD_CTX *mdctx;-
47};-
48-
49struct ssh_digest {-
50 int id;-
51 const char *name;-
52 size_t digest_len;-
53 const EVP_MD *(*mdfunc)(void);-
54};-
55-
56/* NB. Indexed directly by algorithm number */-
57const struct ssh_digest digests[] = {-
58 { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 },-
59 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 },-
60 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 },-
61 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 },-
62 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 },-
63 { -1, NULL, 0, NULL },-
64};-
65-
66static const struct ssh_digest *-
67ssh_digest_by_alg(int alg)-
68{-
69 if (alg < 0 || alg >= SSH_DIGEST_MAX)
alg < 0Description
TRUEnever evaluated
FALSEevaluated 212817 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
alg >= 5Description
TRUEnever evaluated
FALSEevaluated 212817 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
0-212817
70 return NULL;
never executed: return ((void *)0) ;
0
71 if (digests[alg].id != alg) /* sanity */
digests[alg].id != algDescription
TRUEnever evaluated
FALSEevaluated 212817 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
0-212817
72 return NULL;
never executed: return ((void *)0) ;
0
73 if (digests[alg].mdfunc == NULL)
digests[alg].m...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 212817 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
0-212817
74 return NULL;
never executed: return ((void *)0) ;
0
75 return &(digests[alg]);
executed 212817 times by 5 tests: return &(digests[alg]);
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
212817
76}-
77-
78int-
79ssh_digest_alg_by_name(const char *name)-
80{-
81 int alg;-
82-
83 for (alg = 0; digests[alg].id != -1; alg++) {
digests[alg].id != -1Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • ssh-keygen
FALSEnever evaluated
0-4
84 if (strcasecmp(name, digests[alg].name) == 0)
strcasecmp(nam...lg].name) == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • ssh-keygen
FALSEevaluated 2 times by 1 test
Evaluated by:
  • ssh-keygen
2
85 return digests[alg].id;
executed 2 times by 1 test: return digests[alg].id;
Executed by:
  • ssh-keygen
2
86 }
executed 2 times by 1 test: end of block
Executed by:
  • ssh-keygen
2
87 return -1;
never executed: return -1;
0
88}-
89-
90const char *-
91ssh_digest_alg_name(int alg)-
92{-
93 const struct ssh_digest *digest = ssh_digest_by_alg(alg);-
94-
95 return digest == NULL ? NULL : digest->name;
executed 31 times by 3 tests: return digest == ((void *)0) ? ((void *)0) : digest->name;
Executed by:
  • ssh-keygen
  • sshd
  • test_sshkey
digest == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 31 times by 3 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_sshkey
0-31
96}-
97-
98size_t-
99ssh_digest_bytes(int alg)-
100{-
101 const struct ssh_digest *digest = ssh_digest_by_alg(alg);-
102-
103 return digest == NULL ? 0 : digest->digest_len;
executed 125439 times by 5 tests: return digest == ((void *)0) ? 0 : digest->digest_len;
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
digest == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 125439 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshkey
0-125439
104}-
105-
106size_t-
107ssh_digest_blocksize(struct ssh_digest_ctx *ctx)-
108{-
109 return EVP_MD_CTX_block_size(ctx->mdctx);
executed 416 times by 1 test: return EVP_MD_block_size(EVP_MD_CTX_md( ctx->mdctx )) ;
Executed by:
  • test_hostkeys
416
110}-
111-
112struct ssh_digest_ctx *-
113ssh_digest_start(int alg)-
114{-
115 const struct ssh_digest *digest = ssh_digest_by_alg(alg);-
116 struct ssh_digest_ctx *ret;-
117-
118 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
digest == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 6290 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
((ret = calloc... ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 6290 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-6290
119 return NULL;
never executed: return ((void *)0) ;
0
120 ret->alg = alg;-
121 if ((ret->mdctx = EVP_MD_CTX_new()) == NULL) {
(ret->mdctx = ...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 6290 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-6290
122 free(ret);-
123 return NULL;
never executed: return ((void *)0) ;
0
124 }-
125 if (EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
EVP_DigestInit...id *)0) ) != 1Description
TRUEnever evaluated
FALSEevaluated 6290 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-6290
126 ssh_digest_free(ret);-
127 return NULL;
never executed: return ((void *)0) ;
0
128 }-
129 return ret;
executed 6290 times by 3 tests: return ret;
Executed by:
  • sshd
  • test_hostkeys
  • test_kex
6290
130}-
131-
132int-
133ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)-
134{-
135 if (from->alg != to->alg)
from->alg != to->algDescription
TRUEnever evaluated
FALSEevaluated 832 times by 1 test
Evaluated by:
  • test_hostkeys
0-832
136 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
137 /* we have bcopy-style order while openssl has memcpy-style */-
138 if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
!EVP_MD_CTX_co..., from->mdctx)Description
TRUEnever evaluated
FALSEevaluated 832 times by 1 test
Evaluated by:
  • test_hostkeys
0-832
139 return SSH_ERR_LIBCRYPTO_ERROR;
never executed: return -22;
0
140 return 0;
executed 832 times by 1 test: return 0;
Executed by:
  • test_hostkeys
832
141}-
142-
143int-
144ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)-
145{-
146 if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
EVP_DigestUpda... m, mlen) != 1Description
TRUEnever evaluated
FALSEevaluated 18710 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-18710
147 return SSH_ERR_LIBCRYPTO_ERROR;
never executed: return -22;
0
148 return 0;
executed 18710 times by 3 tests: return 0;
Executed by:
  • sshd
  • test_hostkeys
  • test_kex
18710
149}-
150-
151int-
152ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)-
153{-
154 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
executed 5040 times by 1 test: return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
Executed by:
  • test_kex
5040
155}-
156-
157int-
158ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)-
159{-
160 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);-
161 u_int l = dlen;-
162-
163 if (digest == NULL || dlen > UINT_MAX)
digest == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 5874 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
dlen > (0x7fffffff * 2U + 1U)Description
TRUEnever evaluated
FALSEevaluated 5874 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-5874
164 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
165 if (dlen < digest->digest_len) /* No truncation allowed */
dlen < digest->digest_lenDescription
TRUEnever evaluated
FALSEevaluated 5874 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-5874
166 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
167 if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
EVP_DigestFina...x, d, &l) != 1Description
TRUEnever evaluated
FALSEevaluated 5874 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-5874
168 return SSH_ERR_LIBCRYPTO_ERROR;
never executed: return -22;
0
169 if (l != digest->digest_len) /* sanity */
l != digest->digest_lenDescription
TRUEnever evaluated
FALSEevaluated 5874 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
0-5874
170 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
171 return 0;
executed 5874 times by 3 tests: return 0;
Executed by:
  • sshd
  • test_hostkeys
  • test_kex
5874
172}-
173-
174void-
175ssh_digest_free(struct ssh_digest_ctx *ctx)-
176{-
177 if (ctx == NULL)
ctx == ((void *)0)Description
TRUEevaluated 1920 times by 1 test
Evaluated by:
  • test_kex
FALSEevaluated 6290 times by 3 tests
Evaluated by:
  • sshd
  • test_hostkeys
  • test_kex
1920-6290
178 return;
executed 1920 times by 1 test: return;
Executed by:
  • test_kex
1920
179 EVP_MD_CTX_free(ctx->mdctx);-
180 freezero(ctx, sizeof(*ctx));-
181}
executed 6290 times by 3 tests: end of block
Executed by:
  • sshd
  • test_hostkeys
  • test_kex
6290
182-
183int-
184ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)-
185{-
186 const struct ssh_digest *digest = ssh_digest_by_alg(alg);-
187 u_int mdlen;-
188-
189 if (digest == NULL)
digest == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 75183 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshkey
0-75183
190 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
191 if (dlen > UINT_MAX)
dlen > (0x7fffffff * 2U + 1U)Description
TRUEnever evaluated
FALSEevaluated 75183 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshkey
0-75183
192 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
193 if (dlen < digest->digest_len)
dlen < digest->digest_lenDescription
TRUEnever evaluated
FALSEevaluated 75183 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshkey
0-75183
194 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
195 mdlen = dlen;-
196 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
!EVP_Digest(m,... ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 75183 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshkey
0-75183
197 return SSH_ERR_LIBCRYPTO_ERROR;
never executed: return -22;
0
198 return 0;
executed 75183 times by 4 tests: return 0;
Executed by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshkey
75183
199}-
200-
201int-
202ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)-
203{-
204 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
executed 320 times by 1 test: return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
Executed by:
  • test_kex
320
205}-
206#endif /* WITH_OPENSSL */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2