This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Digest-SHA to CPAN version 5.50
[perl5.git] / cpan / Digest-SHA / src / hmac.c
CommitLineData
05128928
NC
1/*
2 * hmac.c: routines to compute HMAC-SHA-1/224/256/384/512 digests
3 *
4 * Ref: FIPS PUB 198 The Keyed-Hash Message Authentication Code
5 *
dfe1edcb 6 * Copyright (C) 2003-2010 Mark Shelor, All Rights Reserved
05128928 7 *
9cc8ef8a
CBW
8 * Version: 5.50
9 * Tue Dec 14 06:20:08 MST 2010
05128928
NC
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include "hmac.h"
17#include "sha.h"
18
19/* hmacopen: creates a new HMAC-SHA digest object */
14bcf1fb 20HMAC *hmacopen(int alg, unsigned char *key, unsigned int keylen)
05128928
NC
21{
22 unsigned int i;
23 HMAC *h;
24
25 SHA_newz(0, h, 1, HMAC);
26 if (h == NULL)
27 return(NULL);
28 if ((h->isha = shaopen(alg)) == NULL) {
29 SHA_free(h);
30 return(NULL);
31 }
32 if ((h->osha = shaopen(alg)) == NULL) {
33 shaclose(h->isha);
34 SHA_free(h);
35 return(NULL);
36 }
37 if (keylen <= h->osha->blocksize / 8)
38 memcpy(h->key, key, keylen);
39 else {
40 if ((h->ksha = shaopen(alg)) == NULL) {
41 shaclose(h->isha);
42 shaclose(h->osha);
43 SHA_free(h);
44 return(NULL);
45 }
46 shawrite(key, keylen * 8, h->ksha);
47 shafinish(h->ksha);
48 memcpy(h->key, shadigest(h->ksha), h->ksha->digestlen);
49 shaclose(h->ksha);
50 }
51 for (i = 0; i < h->osha->blocksize / 8; i++)
52 h->key[i] ^= 0x5c;
53 shawrite(h->key, h->osha->blocksize, h->osha);
54 for (i = 0; i < h->isha->blocksize / 8; i++)
55 h->key[i] ^= (0x5c ^ 0x36);
56 shawrite(h->key, h->isha->blocksize, h->isha);
57 memset(h->key, 0, sizeof(h->key));
58 return(h);
59}
60
61/* hmacwrite: triggers a state update using data in bitstr/bitcnt */
14bcf1fb 62unsigned long hmacwrite(unsigned char *bitstr, unsigned long bitcnt, HMAC *h)
05128928
NC
63{
64 return(shawrite(bitstr, bitcnt, h->isha));
65}
66
67/* hmacfinish: computes final digest state */
14bcf1fb 68void hmacfinish(HMAC *h)
05128928
NC
69{
70 shafinish(h->isha);
71 shawrite(shadigest(h->isha), h->isha->digestlen * 8, h->osha);
72 shaclose(h->isha);
73 shafinish(h->osha);
74}
75
76/* hmacdigest: returns pointer to digest (binary) */
14bcf1fb 77unsigned char *hmacdigest(HMAC *h)
05128928
NC
78{
79 return(shadigest(h->osha));
80}
81
82/* hmachex: returns pointer to digest (hexadecimal) */
14bcf1fb 83char *hmachex(HMAC *h)
05128928
NC
84{
85 return(shahex(h->osha));
86}
87
88/* hmacbase64: returns pointer to digest (Base 64) */
14bcf1fb 89char *hmacbase64(HMAC *h)
05128928
NC
90{
91 return(shabase64(h->osha));
92}
93
94/* hmacclose: de-allocates digest object */
14bcf1fb 95int hmacclose(HMAC *h)
05128928 96{
747da336 97 if (h != NULL) {
0bc2b4f8 98 shaclose(h->osha);
747da336
RGS
99 memset(h, 0, sizeof(HMAC));
100 SHA_free(h);
101 }
05128928
NC
102 return(0);
103}