This was originally written by Ulrich Drepper for glibc. However, this particular version is copied from libgcrypt (simpler to adapt).