---
name: "libressl-BIO_f_md"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-BIO_f_md"
---

# libressl-BIO\_f\_md(3)

message digest BIO filter

## NAME

**BIO\_f\_md**, **BIO\_set\_md**, **BIO\_get\_md**, **BIO\_get\_md\_ctx**, **BIO\_set\_md\_ctx** – message digest BIO filter

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/bio.h\>**

**#include \<openssl/evp.h\>**

*const* *BIO\_METHOD* *\**\

**BIO\_f\_md**(*void*);

*long*\

**BIO\_set\_md**(*BIO \*b*, *EVP\_MD \*md*);

*long*\

**BIO\_get\_md**(*BIO \*b*, *EVP\_MD \*\*mdp*);

*long*\

**BIO\_get\_md\_ctx**(*BIO \*b*, *EVP\_MD\_CTX \*\*mdcp*);

*long*\

**BIO\_set\_md\_ctx**(*BIO \*b*, *EVP\_MD\_CTX \*mdc*);

## DESCRIPTION

**BIO\_f\_md**() returns the message digest BIO method. This is a filter BIO that digests any data passed through it. It is a BIO wrapper for the digest routines EVP\_DigestInit(3), EVP\_DigestUpdate(3), and EVP\_DigestFinal(3).

**BIO\_set\_md**() sets the message digest of *b* to *md* and initializes it using EVP\_DigestInit\_ex(3). Calling this function is required before any data is passed through *b*.

**BIO\_get\_md**() places a pointer to the digest method of *b* into \**mdp*.

Any data written or read through a digest BIO using BIO\_read(3) and BIO\_write(3) is digested.

BIO\_gets(3), if its **size** parameter is large enough, finishes the digest calculation and returns the digest value. BIO\_puts(3) is not supported. If an application needs to call BIO\_gets(3) or BIO\_puts(3) through a chain containing digest BIOs, this can be done by prepending a buffering BIO.

After the digest has been retrieved from a digest BIO, call BIO\_reset(3) to reinitialize it and any BIOs following it in its chain before passing any more data through it. If no subsequent BIOs require reinitialization, **BIO\_set\_md**() can be used instead of BIO\_reset(3).

**BIO\_get\_md\_ctx**() places a pointer to the digest context of *b* into \**mdcp* and marks the BIO as initialized without actually initializing it. Unless **BIO\_set\_md**() was already called on *b*, the caller becomes responsible for initializing the digest context with EVP\_DigestInit\_ex(3).

The context returned by **BIO\_get\_md\_ctx**() can be used in calls to EVP\_DigestFinal(3) and also in the signature routines EVP\_SignFinal(3) and EVP\_VerifyFinal(3).

The context returned by **BIO\_get\_md\_ctx**() is an internal context structure. Changes made to this context will affect the digest BIO itself, and the context pointer will become invalid when the digest BIO is freed.

**BIO\_set\_md\_ctx**() replaces the digest context of *b* with *mdc*. Calling this function is usually not necessary because creating a digest BIO with BIO\_new(3) automatically creates a digest context and stores it internally. Before calling **BIO\_set\_md\_ctx**(), the caller has to retrieve the old context using **BIO\_get\_md\_ctx**(), and the caller also becomes responsible for calling EVP\_MD\_CTX\_free(3) on the old context. Unless *mdc* is already initialized, the caller needs to initialize it after calling **BIO\_set\_md\_ctx**() using either **BIO\_set\_md**() or EVP\_DigestInit(3).

When a chain containing a message digest BIO is copied with BIO\_dup\_chain(3), EVP\_MD\_CTX\_copy\_ex(3) is called internally to automatically copy the message digest context from the existing BIO object to the new one, and the init flag that can be retrieved with BIO\_get\_init(3) is set to 1.

BIO\_ctrl(3) *cmd* arguments correspond to macros as follows:

| | |
| --- | --- |
| *cmd* constant | corresponding macro |
| BIO\_C\_GET\_MD | **BIO\_get\_md**() |
| BIO\_C\_GET\_MD\_CTX | **BIO\_get\_md\_ctx**() |
| BIO\_C\_SET\_MD | **BIO\_set\_md**() |
| BIO\_C\_SET\_MD\_CTX | **BIO\_set\_md\_ctx**() |
| BIO\_CTRL\_RESET | BIO\_reset(3) |

## RETURN VALUES

**BIO\_f\_md**() returns the digest BIO method.

When called on a message digest BIO object, BIO\_method\_type(3) returns the constant BIO\_TYPE\_MD and BIO\_method\_name(3) returns a pointer to the static string "message digest".

**BIO\_set\_md**() returns 1 on success or 0 if EVP\_DigestInit\_ex(3) fails.

**BIO\_get\_md**() and **BIO\_set\_md\_ctx**() return 1 on success or 0 if *b* is not initialized.

**BIO\_get\_md\_ctx**() returns 1 on success or 0 on failure, but the current implementation cannot actually fail.

## EXAMPLES

The following example creates a BIO chain containing a SHA-1 and MD5 digest BIO and passes the string "Hello World" through it. Error checking has been omitted for clarity.

```
BIO *bio, *mdtmp;
const char message[] = "Hello World";
bio = BIO_new(BIO_s_null());
mdtmp = BIO_new(BIO_f_md());
BIO_set_md(mdtmp, EVP_sha1());
/*
 * For BIO_push() we want to append the sink BIO
 * and keep a note of the start of the chain.
 */
bio = BIO_push(mdtmp, bio);
mdtmp = BIO_new(BIO_f_md());
BIO_set_md(mdtmp, EVP_md5());
bio = BIO_push(mdtmp, bio);
/* Note: mdtmp can now be discarded */
BIO_write(bio, message, strlen(message));
```

The next example digests data by reading through a chain instead:

```
BIO *bio, *mdtmp;
char buf[1024];
int rdlen;
bio = BIO_new_file(file, "rb");
mdtmp = BIO_new(BIO_f_md());
BIO_set_md(mdtmp, EVP_sha1());
bio = BIO_push(mdtmp, bio);
mdtmp = BIO_new(BIO_f_md());
BIO_set_md(mdtmp, EVP_md5());
bio = BIO_push(mdtmp, bio);
do {
	rdlen = BIO_read(bio, buf, sizeof(buf));
	/* Might want to do something with the data here */
} while (rdlen > 0);
```

This next example retrieves the message digests from a BIO chain and outputs them. This could be used with the examples above.

```
BIO *mdtmp;
unsigned char mdbuf[EVP_MAX_MD_SIZE];
int mdlen;
int i;
mdtmp = bio;	/* Assume bio has previously been set up */
do {
	EVP_MD *md;
	mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
	if (!mdtmp)
		break;
	BIO_get_md(mdtmp, &md);
	printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
	mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
	for(i = 0; i < mdlen; i++)
		printf(":%02X", mdbuf[i]);
	printf("\n");
	mdtmp = BIO_next(mdtmp);
} while(mdtmp);
BIO_free_all(bio);
```

## SEE ALSO

BIO\_new(3), EVP\_DigestInit(3)

## HISTORY

**BIO\_f\_md**(), **BIO\_set\_md**(), and **BIO\_get\_md**() first appeared in SSLeay 0.6.0. **BIO\_get\_md\_ctx**() first appeared in SSLeay 0.8.1. These functions have been available since OpenBSD 2.4.

**BIO\_set\_md\_ctx**() first appeared in OpenSSL 0.9.7e and has been available since OpenBSD 3.8.

Before OpenSSL 1.0.0, the call to **BIO\_get\_md\_ctx**() would only work if the *BIO* had been initialized, for example by calling **BIO\_set\_md**().

## BUGS

The lack of support for BIO\_puts(3) and the non-standard behaviour of BIO\_gets(3) could be regarded as anomalous. It could be argued that BIO\_gets(3) and BIO\_puts(3) should be passed to the next BIO in the chain and digest the data passed through and that digests should be retrieved using a separate BIO\_ctrl(3) call.
