]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/ciphers/ciphercommon_block.c
Improve small block cipher performance
[thirdparty/openssl.git] / providers / implementations / ciphers / ciphercommon_block.c
1 /*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <assert.h>
11 #include "ciphercommon_local.h"
12 #include "prov/providercommonerr.h"
13
14 /*
15 * Fills a single block of buffered data from the input, and returns the amount
16 * of data remaining in the input that is a multiple of the blocksize. The buffer
17 * is only filled if it already has some data in it, isn't full already or we
18 * don't have at least one block in the input.
19 *
20 * buf: a buffer of blocksize bytes
21 * buflen: contains the amount of data already in buf on entry. Updated with the
22 * amount of data in buf at the end. On entry *buflen must always be
23 * less than the blocksize
24 * blocksize: size of a block. Must be greater than 0 and a power of 2
25 * in: pointer to a pointer containing the input data
26 * inlen: amount of input data available
27 *
28 * On return buf is filled with as much data as possible up to a full block,
29 * *buflen is updated containing the amount of data in buf. *in is updated to
30 * the new location where input data should be read from, *inlen is updated with
31 * the remaining amount of data in *in. Returns the largest value <= *inlen
32 * which is a multiple of the blocksize.
33 */
34 size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
35 const unsigned char **in, size_t *inlen)
36 {
37 size_t blockmask = ~(blocksize - 1);
38 size_t bufremain = blocksize - *buflen;
39
40 assert(*buflen <= blocksize);
41 assert(blocksize > 0 && (blocksize & (blocksize - 1)) == 0);
42
43 if (*inlen < bufremain)
44 bufremain = *inlen;
45 memcpy(buf + *buflen, *in, bufremain);
46 *in += bufremain;
47 *inlen -= bufremain;
48 *buflen += bufremain;
49
50 return *inlen & blockmask;
51 }
52
53 /*
54 * Fills the buffer with trailing data from an encryption/decryption that didn't
55 * fit into a full block.
56 */
57 int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
58 const unsigned char **in, size_t *inlen)
59 {
60 if (*inlen == 0)
61 return 1;
62
63 if (*buflen + *inlen > blocksize) {
64 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
65 return 0;
66 }
67
68 memcpy(buf + *buflen, *in, *inlen);
69 *buflen += *inlen;
70 *inlen = 0;
71
72 return 1;
73 }
74
75 /* Pad the final block for encryption */
76 void padblock(unsigned char *buf, size_t *buflen, size_t blocksize)
77 {
78 size_t i;
79 unsigned char pad = (unsigned char)(blocksize - *buflen);
80
81 for (i = *buflen; i < blocksize; i++)
82 buf[i] = pad;
83 }
84
85 int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize)
86 {
87 size_t pad, i;
88 size_t len = *buflen;
89
90 if(len != blocksize) {
91 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
92 return 0;
93 }
94
95 /*
96 * The following assumes that the ciphertext has been authenticated.
97 * Otherwise it provides a padding oracle.
98 */
99 pad = buf[blocksize - 1];
100 if (pad == 0 || pad > blocksize) {
101 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT);
102 return 0;
103 }
104 for (i = 0; i < pad; i++) {
105 if (buf[--len] != pad) {
106 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT);
107 return 0;
108 }
109 }
110 *buflen = len;
111 return 1;
112 }