diff options
Diffstat (limited to 'digest-openssl.c')
-rw-r--r-- | digest-openssl.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/digest-openssl.c b/digest-openssl.c index 863d37d03..de0380135 100644 --- a/digest-openssl.c +++ b/digest-openssl.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: digest-openssl.c,v 1.2 2014/02/02 03:44:31 djm Exp $ */ | 1 | /* $OpenBSD: digest-openssl.c,v 1.3 2014/06/24 01:13:21 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2013 Damien Miller <djm@mindrot.org> | 3 | * Copyright (c) 2013 Damien Miller <djm@mindrot.org> |
4 | * | 4 | * |
@@ -26,8 +26,9 @@ | |||
26 | 26 | ||
27 | #include "openbsd-compat/openssl-compat.h" | 27 | #include "openbsd-compat/openssl-compat.h" |
28 | 28 | ||
29 | #include "buffer.h" | 29 | #include "sshbuf.h" |
30 | #include "digest.h" | 30 | #include "digest.h" |
31 | #include "ssherr.h" | ||
31 | 32 | ||
32 | struct ssh_digest_ctx { | 33 | struct ssh_digest_ctx { |
33 | int alg; | 34 | int alg; |
@@ -98,9 +99,11 @@ ssh_digest_start(int alg) | |||
98 | int | 99 | int |
99 | ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) | 100 | ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) |
100 | { | 101 | { |
102 | if (from->alg != to->alg) | ||
103 | return SSH_ERR_INVALID_ARGUMENT; | ||
101 | /* we have bcopy-style order while openssl has memcpy-style */ | 104 | /* we have bcopy-style order while openssl has memcpy-style */ |
102 | if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) | 105 | if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) |
103 | return -1; | 106 | return SSH_ERR_LIBCRYPTO_ERROR; |
104 | return 0; | 107 | return 0; |
105 | } | 108 | } |
106 | 109 | ||
@@ -108,14 +111,14 @@ int | |||
108 | ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) | 111 | ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) |
109 | { | 112 | { |
110 | if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) | 113 | if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) |
111 | return -1; | 114 | return SSH_ERR_LIBCRYPTO_ERROR; |
112 | return 0; | 115 | return 0; |
113 | } | 116 | } |
114 | 117 | ||
115 | int | 118 | int |
116 | ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const Buffer *b) | 119 | ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b) |
117 | { | 120 | { |
118 | return ssh_digest_update(ctx, buffer_ptr(b), buffer_len(b)); | 121 | return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); |
119 | } | 122 | } |
120 | 123 | ||
121 | int | 124 | int |
@@ -125,13 +128,13 @@ ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) | |||
125 | u_int l = dlen; | 128 | u_int l = dlen; |
126 | 129 | ||
127 | if (dlen > UINT_MAX) | 130 | if (dlen > UINT_MAX) |
128 | return -1; | 131 | return SSH_ERR_INVALID_ARGUMENT; |
129 | if (dlen < digest->digest_len) /* No truncation allowed */ | 132 | if (dlen < digest->digest_len) /* No truncation allowed */ |
130 | return -1; | 133 | return SSH_ERR_INVALID_ARGUMENT; |
131 | if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) | 134 | if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) |
132 | return -1; | 135 | return SSH_ERR_LIBCRYPTO_ERROR; |
133 | if (l != digest->digest_len) /* sanity */ | 136 | if (l != digest->digest_len) /* sanity */ |
134 | return -1; | 137 | return SSH_ERR_INTERNAL_ERROR; |
135 | return 0; | 138 | return 0; |
136 | } | 139 | } |
137 | 140 | ||
@@ -149,18 +152,19 @@ int | |||
149 | ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) | 152 | ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) |
150 | { | 153 | { |
151 | struct ssh_digest_ctx *ctx = ssh_digest_start(alg); | 154 | struct ssh_digest_ctx *ctx = ssh_digest_start(alg); |
155 | int r; | ||
152 | 156 | ||
153 | if (ctx == NULL) | 157 | if (ctx == NULL) |
154 | return -1; | 158 | return SSH_ERR_INVALID_ARGUMENT; |
155 | if (ssh_digest_update(ctx, m, mlen) != 0 || | 159 | if ((r = ssh_digest_update(ctx, m, mlen) != 0) || |
156 | ssh_digest_final(ctx, d, dlen) != 0) | 160 | (r = ssh_digest_final(ctx, d, dlen) != 0)) |
157 | return -1; | 161 | return r; |
158 | ssh_digest_free(ctx); | 162 | ssh_digest_free(ctx); |
159 | return 0; | 163 | return 0; |
160 | } | 164 | } |
161 | 165 | ||
162 | int | 166 | int |
163 | ssh_digest_buffer(int alg, const Buffer *b, u_char *d, size_t dlen) | 167 | ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen) |
164 | { | 168 | { |
165 | return ssh_digest_memory(alg, buffer_ptr(b), buffer_len(b), d, dlen); | 169 | return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen); |
166 | } | 170 | } |