xref: /freebsd/crypto/krb5/src/lib/crypto/builtin/hash_provider/hash_sha2.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/builtin/hash_provider/sha2.c - SHA-2 hash providers */
3 /*
4  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "crypto_int.h"
34 #include "sha2.h"
35 
36 #ifdef K5_BUILTIN_SHA2
37 
38 static krb5_error_code
k5_sha256_hash(const krb5_crypto_iov * data,size_t num_data,krb5_data * output)39 k5_sha256_hash(const krb5_crypto_iov *data, size_t num_data, krb5_data *output)
40 {
41     SHA256_CTX ctx;
42     size_t i;
43     const krb5_crypto_iov *iov;
44 
45     if (output->length != SHA256_DIGEST_LENGTH)
46         return KRB5_CRYPTO_INTERNAL;
47 
48     k5_sha256_init(&ctx);
49     for (i = 0; i < num_data; i++) {
50         iov = &data[i];
51         if (SIGN_IOV(iov))
52             k5_sha256_update(&ctx, iov->data.data, iov->data.length);
53     }
54     k5_sha256_final(output->data, &ctx);
55     return 0;
56 }
57 
58 static krb5_error_code
k5_sha384_hash(const krb5_crypto_iov * data,size_t num_data,krb5_data * output)59 k5_sha384_hash(const krb5_crypto_iov *data, size_t num_data, krb5_data *output)
60 {
61     SHA384_CTX ctx;
62     size_t i;
63     const krb5_crypto_iov *iov;
64 
65     if (output->length != SHA384_DIGEST_LENGTH)
66         return KRB5_CRYPTO_INTERNAL;
67 
68     k5_sha384_init(&ctx);
69     for (i = 0; i < num_data; i++) {
70         iov = &data[i];
71         if (SIGN_IOV(iov))
72             k5_sha384_update(&ctx, iov->data.data, iov->data.length);
73     }
74     k5_sha384_final(output->data, &ctx);
75     return 0;
76 }
77 
78 const struct krb5_hash_provider krb5int_hash_sha256 = {
79     "SHA-256",
80     SHA256_DIGEST_LENGTH,
81     SHA256_BLOCK_SIZE,
82     k5_sha256_hash
83 };
84 
85 const struct krb5_hash_provider krb5int_hash_sha384 = {
86     "SHA-384",
87     SHA384_DIGEST_LENGTH,
88     SHA384_BLOCK_SIZE,
89     k5_sha384_hash
90 };
91 
92 #endif /* K5_BUILTIN_SHA2 */
93