]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/bio_md.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "crypto/evp.h"
17 #include "internal/bio.h"
20 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
23 static int md_write(BIO
*h
, char const *buf
, int num
);
24 static int md_read(BIO
*h
, char *buf
, int size
);
25 static int md_gets(BIO
*h
, char *str
, int size
);
26 static long md_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
27 static int md_new(BIO
*h
);
28 static int md_free(BIO
*data
);
29 static long md_callback_ctrl(BIO
*h
, int cmd
, BIO_info_cb
*fp
);
31 static const BIO_METHOD methods_md
= {
34 /* TODO: Convert to new style write function */
37 /* TODO: Convert to new style read function */
48 const BIO_METHOD
*BIO_f_md(void)
53 static int md_new(BIO
*bi
)
57 ctx
= EVP_MD_CTX_new();
62 BIO_set_data(bi
, ctx
);
67 static int md_free(BIO
*a
)
71 EVP_MD_CTX_free(BIO_get_data(a
));
72 BIO_set_data(a
, NULL
);
78 static int md_read(BIO
*b
, char *out
, int outl
)
87 ctx
= BIO_get_data(b
);
90 if ((ctx
== NULL
) || (next
== NULL
))
93 ret
= BIO_read(next
, out
, outl
);
94 if (BIO_get_init(b
)) {
96 if (EVP_DigestUpdate(ctx
, (unsigned char *)out
,
97 (unsigned int)ret
) <= 0)
101 BIO_clear_retry_flags(b
);
102 BIO_copy_next_retry(b
);
106 static int md_write(BIO
*b
, const char *in
, int inl
)
112 if ((in
== NULL
) || (inl
<= 0))
115 ctx
= BIO_get_data(b
);
117 if ((ctx
!= NULL
) && (next
!= NULL
))
118 ret
= BIO_write(next
, in
, inl
);
120 if (BIO_get_init(b
)) {
122 if (!EVP_DigestUpdate(ctx
, (const unsigned char *)in
,
123 (unsigned int)ret
)) {
124 BIO_clear_retry_flags(b
);
130 BIO_clear_retry_flags(b
);
131 BIO_copy_next_retry(b
);
136 static long md_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
138 EVP_MD_CTX
*ctx
, *dctx
, **pctx
;
145 ctx
= BIO_get_data(b
);
151 ret
= EVP_DigestInit_ex(ctx
, ctx
->digest
, NULL
);
155 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
158 if (BIO_get_init(b
)) {
164 case BIO_C_GET_MD_CTX
:
169 case BIO_C_SET_MD_CTX
:
171 BIO_set_data(b
, ptr
);
175 case BIO_C_DO_STATE_MACHINE
:
176 BIO_clear_retry_flags(b
);
177 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
178 BIO_copy_next_retry(b
);
183 ret
= EVP_DigestInit_ex(ctx
, md
, NULL
);
189 dctx
= BIO_get_data(dbio
);
190 if (!EVP_MD_CTX_copy_ex(dctx
, ctx
))
195 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
201 static long md_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
213 ret
= BIO_callback_ctrl(next
, cmd
, fp
);
219 static int md_gets(BIO
*bp
, char *buf
, int size
)
224 ctx
= BIO_get_data(bp
);
226 if (size
< ctx
->digest
->md_size
)
229 if (EVP_DigestFinal_ex(ctx
, (unsigned char *)buf
, &ret
) <= 0)