void sha1_hmac_init(struct sha1_hmac_context *ctx, const byte *key, uint keylen) { byte keybuf[SHA1_BLOCK_SIZE], buf[SHA1_BLOCK_SIZE]; /* Hash the key if necessary */ if (keylen <= SHA1_BLOCK_SIZE) { memcpy(keybuf, key, keylen); memset(keybuf + keylen, 0, SHA1_BLOCK_SIZE - keylen); } else { sha1_hash_buffer(keybuf, key, keylen); memset(keybuf + SHA1_SIZE, 0, SHA1_BLOCK_SIZE - SHA1_SIZE); } /* Initialize the inner digest */ sha1_init(&ctx->ictx); int i; for (i = 0; i < SHA1_BLOCK_SIZE; i++) buf[i] = keybuf[i] ^ 0x36; sha1_update(&ctx->ictx, buf, SHA1_BLOCK_SIZE); /* Initialize the outer digest */ sha1_init(&ctx->octx); for (i = 0; i < SHA1_BLOCK_SIZE; i++) buf[i] = keybuf[i] ^ 0x5c; sha1_update(&ctx->octx, buf, SHA1_BLOCK_SIZE); }
static gpg_error_t my_hash_buffer (void *arg, const char *oid, const void *buffer, size_t length, size_t resultsize, unsigned char *result, size_t *resultlen) { (void)arg; /* Not used. */ if (oid && strcmp (oid, "1.3.14.3.2.26")) return gpg_error (GPG_ERR_NOT_SUPPORTED); /* We only support SHA-1. */ if (resultsize < 20) return gpg_error (GPG_ERR_BUFFER_TOO_SHORT); sha1_hash_buffer (result, buffer, length); *resultlen = 20; return 0; }