X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fcrypto.c;h=1983a55705391646e16f2637cf1f2b50ebebcaa7;hb=f29a1574c94ad8875626d4d707cc712a6f68ee29;hp=a5dea45700ae0ebc4a0ddccd23c587e207068120;hpb=2b95bf742f097b419b4e63ef74f33fc121c91bf0;p=mod_auth_gssapi.git diff --git a/src/crypto.c b/src/crypto.c index a5dea45..1983a55 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -72,25 +72,47 @@ apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey, ret = 0; done: - if (ret) { - free(n->ekey); - free(n->hkey); - free(n); - } else { + if (ret == 0) { *skey = n; } return ret; } +apr_status_t HMAC_BUFFER(struct seal_key *skey, struct databuf *buffer, + struct databuf *result) +{ + HMAC_CTX hmac_ctx = { 0 }; + unsigned int len; + int ret; + + /* now MAC the buffer */ + HMAC_CTX_init(&hmac_ctx); + + ret = HMAC_Init_ex(&hmac_ctx, skey->hkey, + skey->cipher->key_len, skey->md, NULL); + if (ret == 0) goto done; + + ret = HMAC_Update(&hmac_ctx, buffer->value, buffer->length); + if (ret == 0) goto done; + + ret = HMAC_Final(&hmac_ctx, result->value, &len); + +done: + HMAC_CTX_cleanup(&hmac_ctx); + if (ret == 0) return EFAULT; + + result->length = len; + return 0; +} + apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey, struct databuf *plain, struct databuf *cipher) { int blksz = skey->cipher->block_size; apr_status_t err = EFAULT; EVP_CIPHER_CTX ctx = { 0 }; - HMAC_CTX hmac_ctx = { 0 }; uint8_t rbuf[blksz]; - unsigned int len; + struct databuf hmacbuf; int outlen, totlen; int ret; @@ -132,24 +154,16 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey, totlen += outlen; /* now MAC the buffer */ - HMAC_CTX_init(&hmac_ctx); - - ret = HMAC_Init_ex(&hmac_ctx, skey->hkey, - skey->cipher->key_len, skey->md, NULL); - if (ret == 0) goto done; - - ret = HMAC_Update(&hmac_ctx, cipher->value, totlen); - if (ret == 0) goto done; - - ret = HMAC_Final(&hmac_ctx, &cipher->value[totlen], &len); - if (ret == 0) goto done; + cipher->length = totlen; + hmacbuf.value = &cipher->value[totlen]; + ret = HMAC_BUFFER(skey, cipher, &hmacbuf); + if (ret != 0) goto done; - cipher->length = totlen + len; + cipher->length += hmacbuf.length; err = 0; done: EVP_CIPHER_CTX_cleanup(&ctx); - HMAC_CTX_cleanup(&hmac_ctx); return err; } @@ -158,29 +172,19 @@ apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey, { apr_status_t err = EFAULT; EVP_CIPHER_CTX ctx = { 0 }; - HMAC_CTX hmac_ctx = { 0 }; unsigned char mac[skey->md->md_size]; - unsigned int len; + struct databuf hmacbuf; int outlen, totlen; volatile bool equal = true; int ret, i; /* check MAC first */ - HMAC_CTX_init(&hmac_ctx); - - ret = HMAC_Init_ex(&hmac_ctx, skey->hkey, - skey->cipher->key_len, skey->md, NULL); - if (ret == 0) goto done; - cipher->length -= skey->md->md_size; + hmacbuf.value = mac; + ret = HMAC_BUFFER(skey, cipher, &hmacbuf); + if (ret != 0) goto done; - ret = HMAC_Update(&hmac_ctx, cipher->value, cipher->length); - if (ret == 0) goto done; - - ret = HMAC_Final(&hmac_ctx, mac, &len); - if (ret == 0) goto done; - - if (len != skey->md->md_size) goto done; + if (hmacbuf.length != skey->md->md_size) goto done; for (i = 0; i < skey->md->md_size; i++) { if (cipher->value[cipher->length + i] != mac[i]) equal = false; /* not breaking intentionally, @@ -223,6 +227,14 @@ apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey, done: EVP_CIPHER_CTX_cleanup(&ctx); - HMAC_CTX_cleanup(&hmac_ctx); return err; } + +int get_mac_size(struct seal_key *skey) +{ + if (skey) { + return skey->md->md_size; + } else { + return 0; + } +}