Skip to content

Commit

Permalink
Add internal SHA-256 interface
Browse files Browse the repository at this point in the history
Add a simple private libk5crypto interface for computing SHA-256
hashes.
  • Loading branch information
greghudson committed Jan 26, 2016
1 parent 06ff3b5 commit e4c9d25
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/include/k5-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ krb5int_arcfour_gsscrypt(const krb5_keyblock *keyblock, krb5_keyusage usage,
const krb5_data *kd_data, krb5_crypto_iov *data,
size_t num_data);

#define K5_SHA256_HASHLEN (256 / 8)

/* Write the SHA-256 hash of in to out. */
krb5_error_code
k5_sha256(const krb5_data *in, uint8_t out[K5_SHA256_HASHLEN]);

/*
* Attempt to zero memory in a way that compilers won't optimize out.
*
Expand Down
11 changes: 11 additions & 0 deletions src/lib/crypto/builtin/sha2/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,14 @@ k5_sha256_final(void *res, SHA256_CTX *m)
}
}
}

krb5_error_code
k5_sha256(const krb5_data *in, uint8_t out[K5_SHA256_HASHLEN])
{
SHA256_CTX ctx;

k5_sha256_init(&ctx);
k5_sha256_update(&ctx, in->data, in->length);
k5_sha256_final(out, &ctx);
return 0;
}
2 changes: 2 additions & 0 deletions src/lib/crypto/krb/crypto_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ void k5_iov_cursor_put(struct iov_cursor *cursor, unsigned char *block);

/*** Crypto module declarations ***/

/* Modules must implement the k5_sha256() function prototyped in k5-int.h. */

/* Modules must implement the following enc_providers and hash_providers: */
extern const struct krb5_enc_provider krb5int_enc_des;
extern const struct krb5_enc_provider krb5int_enc_des3;
Expand Down
1 change: 1 addition & 0 deletions src/lib/crypto/libk5crypto.exports
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ krb5int_enc_camellia256
krb5int_derive_key
krb5int_aes_enc_blk
krb5int_aes_enc_key
k5_sha256
k5_sha256_final
k5_sha256_init
k5_sha256_update
Expand Down
3 changes: 3 additions & 0 deletions src/lib/crypto/openssl/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ STLIBOBJS=\
hmac.o \
init.o \
pbkdf2.o \
sha256.o \
stubs.o

OBJS=\
$(OUTPRE)hmac.$(OBJEXT) \
$(OUTPRE)init.$(OBJEXT) \
$(OUTPRE)pbkdf2.$(OBJEXT) \
$(OUTPRE)sha256.$(OBJEXT) \
$(OUTPRE)stubs.$(OBJEXT)

SRCS=\
$(srcdir)/hmac.c \
$(srcdir)/init.c \
$(srcdir)/pbkdf2.c \
$(srcdir)/sha256.c \
$(srcdir)/stubs.c

STOBJLISTS= des/OBJS.ST md4/OBJS.ST \
Expand Down
48 changes: 48 additions & 0 deletions src/lib/crypto/openssl/sha256.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/crypto/openssl/sha256.c - k5_sha256() implementation */
/*
* Copyright (C) 2016 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "crypto_int.h"
#include <openssl/evp.h>

krb5_error_code
k5_sha256(const krb5_data *in, uint8_t out[K5_SHA256_HASHLEN])
{
EVP_MD_CTX ctx;
int ok;

EVP_MD_CTX_init(&ctx);
ok = EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL);
ok = ok && EVP_DigestUpdate(&ctx, in->data, in->length);
ok = ok && EVP_DigestFinal_ex(&ctx, out, NULL);
EVP_MD_CTX_cleanup(&ctx);
return ok ? 0 : ENOMEM;
}

0 comments on commit e4c9d25

Please sign in to comment.