1*36e94dc5SPeter Avalos /* $OpenBSD: hmac.c,v 1.10 2014/01/31 16:39:19 tedu Exp $ */ 2*36e94dc5SPeter Avalos /* 3*36e94dc5SPeter Avalos * Copyright (c) 2014 Markus Friedl. All rights reserved. 4*36e94dc5SPeter Avalos * 5*36e94dc5SPeter Avalos * Permission to use, copy, modify, and distribute this software for any 6*36e94dc5SPeter Avalos * purpose with or without fee is hereby granted, provided that the above 7*36e94dc5SPeter Avalos * copyright notice and this permission notice appear in all copies. 8*36e94dc5SPeter Avalos * 9*36e94dc5SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10*36e94dc5SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*36e94dc5SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12*36e94dc5SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*36e94dc5SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*36e94dc5SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15*36e94dc5SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*36e94dc5SPeter Avalos */ 17*36e94dc5SPeter Avalos 18*36e94dc5SPeter Avalos #include "includes.h" 19*36e94dc5SPeter Avalos 20*36e94dc5SPeter Avalos #include <sys/types.h> 21*36e94dc5SPeter Avalos #include <string.h> 22*36e94dc5SPeter Avalos 23*36e94dc5SPeter Avalos #include "buffer.h" 24*36e94dc5SPeter Avalos #include "digest.h" 25*36e94dc5SPeter Avalos #include "hmac.h" 26*36e94dc5SPeter Avalos 27*36e94dc5SPeter Avalos struct ssh_hmac_ctx { 28*36e94dc5SPeter Avalos int alg; 29*36e94dc5SPeter Avalos struct ssh_digest_ctx *ictx; 30*36e94dc5SPeter Avalos struct ssh_digest_ctx *octx; 31*36e94dc5SPeter Avalos struct ssh_digest_ctx *digest; 32*36e94dc5SPeter Avalos u_char *buf; 33*36e94dc5SPeter Avalos size_t buf_len; 34*36e94dc5SPeter Avalos }; 35*36e94dc5SPeter Avalos 36*36e94dc5SPeter Avalos size_t 37*36e94dc5SPeter Avalos ssh_hmac_bytes(int alg) 38*36e94dc5SPeter Avalos { 39*36e94dc5SPeter Avalos return ssh_digest_bytes(alg); 40*36e94dc5SPeter Avalos } 41*36e94dc5SPeter Avalos 42*36e94dc5SPeter Avalos struct ssh_hmac_ctx * 43*36e94dc5SPeter Avalos ssh_hmac_start(int alg) 44*36e94dc5SPeter Avalos { 45*36e94dc5SPeter Avalos struct ssh_hmac_ctx *ret; 46*36e94dc5SPeter Avalos 47*36e94dc5SPeter Avalos if ((ret = calloc(1, sizeof(*ret))) == NULL) 48*36e94dc5SPeter Avalos return NULL; 49*36e94dc5SPeter Avalos ret->alg = alg; 50*36e94dc5SPeter Avalos if ((ret->ictx = ssh_digest_start(alg)) == NULL || 51*36e94dc5SPeter Avalos (ret->octx = ssh_digest_start(alg)) == NULL || 52*36e94dc5SPeter Avalos (ret->digest = ssh_digest_start(alg)) == NULL) 53*36e94dc5SPeter Avalos goto fail; 54*36e94dc5SPeter Avalos ret->buf_len = ssh_digest_blocksize(ret->ictx); 55*36e94dc5SPeter Avalos if ((ret->buf = calloc(1, ret->buf_len)) == NULL) 56*36e94dc5SPeter Avalos goto fail; 57*36e94dc5SPeter Avalos return ret; 58*36e94dc5SPeter Avalos fail: 59*36e94dc5SPeter Avalos ssh_hmac_free(ret); 60*36e94dc5SPeter Avalos return NULL; 61*36e94dc5SPeter Avalos } 62*36e94dc5SPeter Avalos 63*36e94dc5SPeter Avalos int 64*36e94dc5SPeter Avalos ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen) 65*36e94dc5SPeter Avalos { 66*36e94dc5SPeter Avalos size_t i; 67*36e94dc5SPeter Avalos 68*36e94dc5SPeter Avalos /* reset ictx and octx if no is key given */ 69*36e94dc5SPeter Avalos if (key != NULL) { 70*36e94dc5SPeter Avalos /* truncate long keys */ 71*36e94dc5SPeter Avalos if (klen <= ctx->buf_len) 72*36e94dc5SPeter Avalos memcpy(ctx->buf, key, klen); 73*36e94dc5SPeter Avalos else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf, 74*36e94dc5SPeter Avalos ctx->buf_len) < 0) 75*36e94dc5SPeter Avalos return -1; 76*36e94dc5SPeter Avalos for (i = 0; i < ctx->buf_len; i++) 77*36e94dc5SPeter Avalos ctx->buf[i] ^= 0x36; 78*36e94dc5SPeter Avalos if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0) 79*36e94dc5SPeter Avalos return -1; 80*36e94dc5SPeter Avalos for (i = 0; i < ctx->buf_len; i++) 81*36e94dc5SPeter Avalos ctx->buf[i] ^= 0x36 ^ 0x5c; 82*36e94dc5SPeter Avalos if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0) 83*36e94dc5SPeter Avalos return -1; 84*36e94dc5SPeter Avalos explicit_bzero(ctx->buf, ctx->buf_len); 85*36e94dc5SPeter Avalos } 86*36e94dc5SPeter Avalos /* start with ictx */ 87*36e94dc5SPeter Avalos if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0) 88*36e94dc5SPeter Avalos return -1; 89*36e94dc5SPeter Avalos return 0; 90*36e94dc5SPeter Avalos } 91*36e94dc5SPeter Avalos 92*36e94dc5SPeter Avalos int 93*36e94dc5SPeter Avalos ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen) 94*36e94dc5SPeter Avalos { 95*36e94dc5SPeter Avalos return ssh_digest_update(ctx->digest, m, mlen); 96*36e94dc5SPeter Avalos } 97*36e94dc5SPeter Avalos 98*36e94dc5SPeter Avalos int 99*36e94dc5SPeter Avalos ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const Buffer *b) 100*36e94dc5SPeter Avalos { 101*36e94dc5SPeter Avalos return ssh_digest_update_buffer(ctx->digest, b); 102*36e94dc5SPeter Avalos } 103*36e94dc5SPeter Avalos 104*36e94dc5SPeter Avalos int 105*36e94dc5SPeter Avalos ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen) 106*36e94dc5SPeter Avalos { 107*36e94dc5SPeter Avalos size_t len; 108*36e94dc5SPeter Avalos 109*36e94dc5SPeter Avalos len = ssh_digest_bytes(ctx->alg); 110*36e94dc5SPeter Avalos if (dlen < len || 111*36e94dc5SPeter Avalos ssh_digest_final(ctx->digest, ctx->buf, len)) 112*36e94dc5SPeter Avalos return -1; 113*36e94dc5SPeter Avalos /* switch to octx */ 114*36e94dc5SPeter Avalos if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 || 115*36e94dc5SPeter Avalos ssh_digest_update(ctx->digest, ctx->buf, len) < 0 || 116*36e94dc5SPeter Avalos ssh_digest_final(ctx->digest, d, dlen) < 0) 117*36e94dc5SPeter Avalos return -1; 118*36e94dc5SPeter Avalos return 0; 119*36e94dc5SPeter Avalos } 120*36e94dc5SPeter Avalos 121*36e94dc5SPeter Avalos void 122*36e94dc5SPeter Avalos ssh_hmac_free(struct ssh_hmac_ctx *ctx) 123*36e94dc5SPeter Avalos { 124*36e94dc5SPeter Avalos if (ctx != NULL) { 125*36e94dc5SPeter Avalos ssh_digest_free(ctx->ictx); 126*36e94dc5SPeter Avalos ssh_digest_free(ctx->octx); 127*36e94dc5SPeter Avalos ssh_digest_free(ctx->digest); 128*36e94dc5SPeter Avalos if (ctx->buf) { 129*36e94dc5SPeter Avalos explicit_bzero(ctx->buf, ctx->buf_len); 130*36e94dc5SPeter Avalos free(ctx->buf); 131*36e94dc5SPeter Avalos } 132*36e94dc5SPeter Avalos explicit_bzero(ctx, sizeof(*ctx)); 133*36e94dc5SPeter Avalos free(ctx); 134*36e94dc5SPeter Avalos } 135*36e94dc5SPeter Avalos } 136*36e94dc5SPeter Avalos 137*36e94dc5SPeter Avalos #ifdef TEST 138*36e94dc5SPeter Avalos 139*36e94dc5SPeter Avalos /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */ 140*36e94dc5SPeter Avalos static void 141*36e94dc5SPeter Avalos hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen) 142*36e94dc5SPeter Avalos { 143*36e94dc5SPeter Avalos struct ssh_hmac_ctx *ctx; 144*36e94dc5SPeter Avalos size_t i; 145*36e94dc5SPeter Avalos u_char digest[16]; 146*36e94dc5SPeter Avalos 147*36e94dc5SPeter Avalos if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL) 148*36e94dc5SPeter Avalos printf("ssh_hmac_start failed"); 149*36e94dc5SPeter Avalos if (ssh_hmac_init(ctx, key, klen) < 0 || 150*36e94dc5SPeter Avalos ssh_hmac_update(ctx, m, mlen) < 0 || 151*36e94dc5SPeter Avalos ssh_hmac_final(ctx, digest, sizeof(digest)) < 0) 152*36e94dc5SPeter Avalos printf("ssh_hmac_xxx failed"); 153*36e94dc5SPeter Avalos ssh_hmac_free(ctx); 154*36e94dc5SPeter Avalos 155*36e94dc5SPeter Avalos if (memcmp(e, digest, elen)) { 156*36e94dc5SPeter Avalos for (i = 0; i < elen; i++) 157*36e94dc5SPeter Avalos printf("[%zd] %2.2x %2.2x\n", i, e[i], digest[i]); 158*36e94dc5SPeter Avalos printf("mismatch\n"); 159*36e94dc5SPeter Avalos } else 160*36e94dc5SPeter Avalos printf("ok\n"); 161*36e94dc5SPeter Avalos } 162*36e94dc5SPeter Avalos 163*36e94dc5SPeter Avalos int 164*36e94dc5SPeter Avalos main(int argc, char **argv) 165*36e94dc5SPeter Avalos { 166*36e94dc5SPeter Avalos /* try test vectors from RFC 2104 */ 167*36e94dc5SPeter Avalos 168*36e94dc5SPeter Avalos u_char key1[16] = { 169*36e94dc5SPeter Avalos 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 170*36e94dc5SPeter Avalos 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb }; 171*36e94dc5SPeter Avalos u_char *data1 = "Hi There"; 172*36e94dc5SPeter Avalos u_char dig1[16] = { 173*36e94dc5SPeter Avalos 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 174*36e94dc5SPeter Avalos 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d }; 175*36e94dc5SPeter Avalos 176*36e94dc5SPeter Avalos u_char *key2 = "Jefe"; 177*36e94dc5SPeter Avalos u_char *data2 = "what do ya want for nothing?"; 178*36e94dc5SPeter Avalos u_char dig2[16] = { 179*36e94dc5SPeter Avalos 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 180*36e94dc5SPeter Avalos 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 }; 181*36e94dc5SPeter Avalos 182*36e94dc5SPeter Avalos u_char key3[16]; 183*36e94dc5SPeter Avalos u_char data3[50]; 184*36e94dc5SPeter Avalos u_char dig3[16] = { 185*36e94dc5SPeter Avalos 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 186*36e94dc5SPeter Avalos 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 }; 187*36e94dc5SPeter Avalos memset(key3, 0xaa, sizeof(key3)); 188*36e94dc5SPeter Avalos memset(data3, 0xdd, sizeof(data3)); 189*36e94dc5SPeter Avalos 190*36e94dc5SPeter Avalos hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1)); 191*36e94dc5SPeter Avalos hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2)); 192*36e94dc5SPeter Avalos hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3)); 193*36e94dc5SPeter Avalos 194*36e94dc5SPeter Avalos return 0; 195*36e94dc5SPeter Avalos } 196*36e94dc5SPeter Avalos 197*36e94dc5SPeter Avalos #endif 198