xref: /freebsd/crypto/krb5/src/lib/crypto/krb/make_checksum.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1998 by the FundsXpress, INC.
4  *
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may require
8  * a specific license from the United States Government.  It is the
9  * responsibility of any person or organization contemplating export to
10  * obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of FundsXpress. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  FundsXpress makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27 
28 #include "crypto_int.h"
29 
30 /* A 0 checksum type means use the mandatory checksum. */
31 
32 krb5_error_code KRB5_CALLCONV
krb5_k_make_checksum(krb5_context context,krb5_cksumtype cksumtype,krb5_key key,krb5_keyusage usage,const krb5_data * input,krb5_checksum * cksum)33 krb5_k_make_checksum(krb5_context context, krb5_cksumtype cksumtype,
34                      krb5_key key, krb5_keyusage usage,
35                      const krb5_data *input, krb5_checksum *cksum)
36 {
37     const struct krb5_cksumtypes *ctp;
38     krb5_crypto_iov iov;
39     krb5_data cksum_data;
40     krb5_octet *trunc;
41     krb5_error_code ret;
42 
43     if (cksumtype == 0 && key != NULL) {
44         ret = krb5int_c_mandatory_cksumtype(context, key->keyblock.enctype,
45                                             &cksumtype);
46         if (ret != 0)
47             return ret;
48     }
49     ctp = find_cksumtype(cksumtype);
50     if (ctp == NULL)
51         return KRB5_BAD_ENCTYPE;
52 
53     ret = verify_key(ctp, key);
54     if (ret != 0)
55         return ret;
56 
57     ret = alloc_data(&cksum_data, ctp->compute_size);
58     if (ret != 0)
59         return ret;
60 
61     iov.flags = KRB5_CRYPTO_TYPE_DATA;
62     iov.data = *input;
63     ret = ctp->checksum(ctp, key, usage, &iov, 1, &cksum_data);
64     if (ret != 0)
65         goto cleanup;
66 
67     cksum->magic = KV5M_CHECKSUM;
68     cksum->checksum_type = cksumtype;
69     cksum->length = ctp->output_size;
70     cksum->contents = (krb5_octet *) cksum_data.data;
71     cksum_data.data = NULL;
72     if (ctp->output_size < ctp->compute_size) {
73         trunc = realloc(cksum->contents, ctp->output_size);
74         if (trunc != NULL)
75             cksum->contents = trunc;
76     }
77 
78 cleanup:
79     zapfree(cksum_data.data, ctp->compute_size);
80     return ret;
81 }
82 
83 krb5_error_code KRB5_CALLCONV
krb5_c_make_checksum(krb5_context context,krb5_cksumtype cksumtype,const krb5_keyblock * keyblock,krb5_keyusage usage,const krb5_data * input,krb5_checksum * cksum)84 krb5_c_make_checksum(krb5_context context, krb5_cksumtype cksumtype,
85                      const krb5_keyblock *keyblock, krb5_keyusage usage,
86                      const krb5_data *input, krb5_checksum *cksum)
87 {
88     krb5_key key = NULL;
89     krb5_error_code ret;
90 
91     if (keyblock != NULL) {
92         ret = krb5_k_create_key(context, keyblock, &key);
93         if (ret != 0)
94             return ret;
95     }
96     ret = krb5_k_make_checksum(context, cksumtype, key, usage, input, cksum);
97     krb5_k_free_key(context, key);
98     return ret;
99 }
100