1 /*
2  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright (C) 1998 by the FundsXpress, INC.
10  *
11  * All rights reserved.
12  *
13  * Export of this software from the United States of America may require
14  * a specific license from the United States Government.  It is the
15  * responsibility of any person or organization contemplating export to
16  * obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19  * distribute this software and its documentation for any purpose and
20  * without fee is hereby granted, provided that the above copyright
21  * notice appear in all copies and that both that copyright notice and
22  * this permission notice appear in supporting documentation, and that
23  * the name of FundsXpress. not be used in advertising or publicity pertaining
24  * to distribution of the software without specific, written prior
25  * permission.  FundsXpress makes no representations about the suitability of
26  * this software for any purpose.  It is provided "as is" without express
27  * or implied warranty.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  */
33 
34 #include <k5-int.h>
35 #include <cksumtypes.h>
36 
37 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
38 krb5_c_verify_checksum(context, key, usage, data, cksum, valid)
39      krb5_context context;
40      krb5_const krb5_keyblock *key;
41      krb5_keyusage usage;
42      krb5_const krb5_data *data;
43      krb5_const krb5_checksum *cksum;
44      krb5_boolean *valid;
45 {
46     int i;
47     size_t hashsize;
48     krb5_error_code ret;
49     krb5_data indata;
50     krb5_checksum computed;
51 
52     for (i=0; i<krb5_cksumtypes_length; i++) {
53 	if (krb5_cksumtypes_list[i].ctype == cksum->checksum_type)
54 	    break;
55     }
56 
57     if (i == krb5_cksumtypes_length)
58 	return(KRB5_BAD_ENCTYPE);
59 
60     /* if there's actually a verify function, call it */
61 
62     indata.length = cksum->length;
63     indata.data = (char *) cksum->contents;
64     *valid = 0;
65 
66     if (krb5_cksumtypes_list[i].keyhash &&
67 	krb5_cksumtypes_list[i].keyhash->verify)
68 	return((*(krb5_cksumtypes_list[i].keyhash->verify))(
69 		context, key, usage, 0, data, &indata, valid));
70 
71     /* otherwise, make the checksum again, and compare */
72 
73     if ((ret = krb5_c_checksum_length(context, cksum->checksum_type, &hashsize)))
74 	return(ret);
75 
76     if (cksum->length != hashsize)
77 	return(KRB5_BAD_MSIZE);
78 
79     computed.length = hashsize;
80 
81     if ((ret = krb5_c_make_checksum(context, cksum->checksum_type, key, usage,
82 				   data, &computed))) {
83 	FREE(computed.contents, computed.length);
84 	return(ret);
85     }
86 
87     *valid = (memcmp(computed.contents, cksum->contents, hashsize) == 0);
88 
89     FREE(computed.contents, computed.length);
90 
91     return(0);
92 }
93