File: cipher-padding.c

package info (click to toggle)
gnutls28 3.8.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 82,372 kB
  • sloc: ansic: 391,853; asm: 117,804; sh: 18,780; makefile: 6,801; yacc: 1,858; python: 1,399; cpp: 1,243; perl: 995; sed: 39
file content (153 lines) | stat: -rw-r--r-- 3,949 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * Author: Daiki Ueno
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#include "config.h"

#include <gnutls/crypto.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include "utils.h"

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "<%d>| %s", level, str);
}

#define CLAMP(x, b) (((x) + (b)) / (b)) * (b)

static void start(gnutls_cipher_algorithm_t algo, size_t plaintext_size,
		  unsigned int flags)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t plaintext[128];
	uint8_t ciphertext[128];
	size_t block_size;
	size_t size;
	gnutls_datum_t key, iv;

	success("%s %zu %u\n", gnutls_cipher_get_name(algo), plaintext_size,
		flags);

	block_size = gnutls_cipher_get_block_size(algo);

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(plaintext, 0xfa, sizeof(plaintext));

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0) {
		fail("gnutls_cipher_init failed\n");
	}

	/* Check overflow if PKCS#7 is requested */
	if (flags & GNUTLS_CIPHER_PADDING_PKCS7) {
		ret = gnutls_cipher_encrypt3(ch, plaintext, SIZE_MAX, NULL,
					     &size, flags);
		if (ret != GNUTLS_E_INVALID_REQUEST) {
			fail("gnutls_cipher_encrypt3 succeeded\n");
		}
	}

	/* Get the ciphertext size */
	ret = gnutls_cipher_encrypt3(ch, plaintext, plaintext_size, NULL, &size,
				     flags);
	if (ret < 0) {
		fail("gnutls_cipher_encrypt3 failed\n");
	}

	if (flags & GNUTLS_CIPHER_PADDING_PKCS7) {
		if (size <= plaintext_size) {
			fail("no padding appended\n");
		}
		if (size != CLAMP(plaintext_size, block_size)) {
			fail("size does not match: %zu (expected %zu)\n", size,
			     CLAMP(plaintext_size, block_size));
		}
	} else {
		if (size != plaintext_size) {
			fail("size does not match: %zu (expected %zu)\n", size,
			     plaintext_size);
		}
	}

	/* Encrypt with padding */
	ret = gnutls_cipher_encrypt3(ch, plaintext, plaintext_size, ciphertext,
				     &size, flags);
	if (ret < 0) {
		fail("gnutls_cipher_encrypt3 failed\n");
	}

	/* Decrypt with padding */
	ret = gnutls_cipher_decrypt3(ch, ciphertext, size, ciphertext, &size,
				     flags);
	if (ret < 0) {
		fail("gnutls_cipher_encrypt3 failed\n");
	}

	if (size != plaintext_size) {
		fail("size does not match: %zu (expected %zu)\n", size,
		     plaintext_size);
	}

	if (memcmp(ciphertext, plaintext, size) != 0) {
		fail("plaintext does not match\n");
	}

	gnutls_cipher_deinit(ch);
}

void doit(void)
{
	int ret;

	gnutls_global_set_log_function(tls_log_func);
	if (debug) {
		gnutls_global_set_log_level(4711);
	}

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n");
	}

	start(GNUTLS_CIPHER_AES_128_CBC, 0, GNUTLS_CIPHER_PADDING_PKCS7);
	start(GNUTLS_CIPHER_AES_128_CBC, 11, GNUTLS_CIPHER_PADDING_PKCS7);
	start(GNUTLS_CIPHER_AES_128_CBC, 77, GNUTLS_CIPHER_PADDING_PKCS7);
	start(GNUTLS_CIPHER_AES_128_CBC, 80, GNUTLS_CIPHER_PADDING_PKCS7);

	start(GNUTLS_CIPHER_AES_128_CBC, 0, 0);
	start(GNUTLS_CIPHER_AES_128_CBC, 80, 0);

	gnutls_global_deinit();
}