summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/krb/crypto_length.c
blob: 00de30c7cff3437d005325f0dc0fe7603224ecd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * lib/crypto/crypto_length.c
 *
 * Copyright 2008 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "k5-int.h"
#include "etypes.h"
#include "aead.h"

krb5_error_code KRB5_CALLCONV
krb5_c_crypto_length(krb5_context context, krb5_enctype enctype,
		     krb5_cryptotype type, unsigned int *size)
{
    const struct krb5_keytypes *ktp;
    krb5_error_code ret;

    ktp = find_enctype(enctype);
    if (ktp == NULL || ktp->aead == NULL)
	return KRB5_BAD_ENCTYPE;

    switch (type) {
    case KRB5_CRYPTO_TYPE_EMPTY:
    case KRB5_CRYPTO_TYPE_SIGN_ONLY:
	*size = 0;
	ret = 0;
	break;
    case KRB5_CRYPTO_TYPE_DATA:
	*size = (size_t)~0; /* match Heimdal */
	ret = 0;
	break;
    case KRB5_CRYPTO_TYPE_HEADER:
    case KRB5_CRYPTO_TYPE_PADDING:
    case KRB5_CRYPTO_TYPE_TRAILER:
    case KRB5_CRYPTO_TYPE_CHECKSUM:
	ret = (*ktp->aead->crypto_length)(ktp->aead, ktp->enc, ktp->hash,
					  type, size);
	break;
    default:
	ret = EINVAL;
	break;
    }

    return ret;
}

krb5_error_code KRB5_CALLCONV
krb5_c_padding_length(krb5_context context, krb5_enctype enctype,
		      size_t data_length, unsigned int *pad_length)
{
    const struct krb5_keytypes *ktp;

    ktp = find_enctype(enctype);
    if (ktp == NULL || ktp->aead == NULL)
	return KRB5_BAD_ENCTYPE;

    return krb5int_c_padding_length(ktp->aead, ktp->enc, ktp->hash,
				    data_length, pad_length);
}

krb5_error_code KRB5_CALLCONV
krb5_c_crypto_length_iov(krb5_context context, krb5_enctype enctype,
			 krb5_crypto_iov *data, size_t num_data)
{
    krb5_error_code ret = 0;
    size_t i;
    const struct krb5_keytypes *ktp;
    unsigned int data_length = 0, pad_length;
    krb5_crypto_iov *padding = NULL;

    /*
     * XXX need to rejig internal interface so we can accurately
     * report variable header lengths.
     */

    ktp = find_enctype(enctype);
    if (ktp == NULL || ktp->aead == NULL)
	return KRB5_BAD_ENCTYPE;

    for (i = 0; i < num_data; i++) {
	krb5_crypto_iov *iov = &data[i];

	switch (iov->flags) {
	case KRB5_CRYPTO_TYPE_DATA:
	    data_length += iov->data.length;
	    break;
	case KRB5_CRYPTO_TYPE_PADDING:
	    if (padding != NULL)
		return EINVAL;

	    padding = iov;
	    break;
	case KRB5_CRYPTO_TYPE_HEADER:
	case KRB5_CRYPTO_TYPE_TRAILER:
	case KRB5_CRYPTO_TYPE_CHECKSUM:
	    ret = (*ktp->aead->crypto_length)(ktp->aead, ktp->enc, ktp->hash,
					      iov->flags, &iov->data.length);
	    break;
	case KRB5_CRYPTO_TYPE_EMPTY:
	case KRB5_CRYPTO_TYPE_SIGN_ONLY:
	default:
	    break;
	}

	if (ret != 0)
	    break;
    }

    if (ret != 0)
	return ret;

    ret = krb5int_c_padding_length(ktp->aead, ktp->enc, ktp->hash,
				   data_length, &pad_length);
    if (ret != 0)
	return ret;

    if (pad_length != 0 && padding == NULL)
	return EINVAL;

    if (padding != NULL)
	padding->data.length = pad_length;

    return 0;
}