xref: /freebsd/crypto/krb5/src/lib/crypto/krb/verify_checksum.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
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 krb5_error_code KRB5_CALLCONV
krb5_k_verify_checksum(krb5_context context,krb5_key key,krb5_keyusage usage,const krb5_data * data,const krb5_checksum * cksum,krb5_boolean * valid)31 krb5_k_verify_checksum(krb5_context context, krb5_key key,
32                        krb5_keyusage usage, const krb5_data *data,
33                        const krb5_checksum *cksum, krb5_boolean *valid)
34 {
35     const struct krb5_cksumtypes *ctp;
36     krb5_cksumtype cksumtype;
37     krb5_crypto_iov iov;
38     krb5_error_code ret;
39     krb5_data cksum_data;
40     krb5_checksum computed;
41 
42     iov.flags = KRB5_CRYPTO_TYPE_DATA;
43     iov.data = *data;
44 
45     /* A 0 checksum type means use the mandatory checksum. */
46     cksumtype = cksum->checksum_type;
47     if (cksumtype == 0 && key != NULL) {
48         ret = krb5int_c_mandatory_cksumtype(context, key->keyblock.enctype,
49                                             &cksumtype);
50         if (ret)
51             return ret;
52     }
53     ctp = find_cksumtype(cksumtype);
54     if (ctp == NULL)
55         return KRB5_BAD_ENCTYPE;
56 
57     ret = verify_key(ctp, key);
58     if (ret != 0)
59         return ret;
60 
61     /* If there's actually a verify function, call it. */
62     cksum_data = make_data(cksum->contents, cksum->length);
63     if (ctp->verify != NULL)
64         return ctp->verify(ctp, key, usage, &iov, 1, &cksum_data, valid);
65 
66     /* Otherwise, make the checksum again, and compare. */
67     if (cksum->length != ctp->output_size)
68         return KRB5_BAD_MSIZE;
69 
70     ret = krb5_k_make_checksum(context, cksum->checksum_type, key, usage,
71                                data, &computed);
72     if (ret)
73         return ret;
74 
75     *valid = (k5_bcmp(computed.contents, cksum->contents,
76                       ctp->output_size) == 0);
77 
78     free(computed.contents);
79     return 0;
80 }
81 
82 krb5_error_code KRB5_CALLCONV
krb5_c_verify_checksum(krb5_context context,const krb5_keyblock * keyblock,krb5_keyusage usage,const krb5_data * data,const krb5_checksum * cksum,krb5_boolean * valid)83 krb5_c_verify_checksum(krb5_context context, const krb5_keyblock *keyblock,
84                        krb5_keyusage usage, const krb5_data *data,
85                        const krb5_checksum *cksum, krb5_boolean *valid)
86 {
87     krb5_key key = NULL;
88     krb5_error_code ret;
89 
90     if (keyblock != NULL) {
91         ret = krb5_k_create_key(context, keyblock, &key);
92         if (ret != 0)
93             return ret;
94     }
95     ret = krb5_k_verify_checksum(context, key, usage, data, cksum, valid);
96     krb5_k_free_key(context, key);
97     return ret;
98 }
99