]>
git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/ciphers/block.c
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "cipher_locl.h"
12 #include "internal/providercommonerr.h"
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.
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
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.
34 size_t fillblock(unsigned char *buf
, size_t *buflen
, size_t blocksize
,
35 const unsigned char **in
, size_t *inlen
)
37 size_t blockmask
= ~(blocksize
- 1);
39 assert(*buflen
<= blocksize
);
40 assert(blocksize
> 0 && (blocksize
& (blocksize
- 1)) == 0);
42 if (*buflen
!= blocksize
&& (*buflen
!= 0 || *inlen
< blocksize
)) {
43 size_t bufremain
= blocksize
- *buflen
;
45 if (*inlen
< bufremain
)
47 memcpy(buf
+ *buflen
, *in
, bufremain
);
53 return *inlen
& blockmask
;
57 * Fills the buffer with trailing data from an encryption/decryption that didn't
58 * fit into a full block.
60 int trailingdata(unsigned char *buf
, size_t *buflen
, size_t blocksize
,
61 const unsigned char **in
, size_t *inlen
)
66 if (*buflen
+ *inlen
> blocksize
) {
67 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
71 memcpy(buf
+ *buflen
, *in
, *inlen
);
78 /* Pad the final block for encryption */
79 void padblock(unsigned char *buf
, size_t *buflen
, size_t blocksize
)
82 unsigned char pad
= (unsigned char)(blocksize
- *buflen
);
84 for (i
= *buflen
; i
< blocksize
; i
++)
88 int unpadblock(unsigned char *buf
, size_t *buflen
, size_t blocksize
)
93 if(len
!= blocksize
) {
94 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
99 * The following assumes that the ciphertext has been authenticated.
100 * Otherwise it provides a padding oracle.
102 pad
= buf
[blocksize
- 1];
103 if (pad
== 0 || pad
> blocksize
) {
104 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_DECRYPT
);
107 for (i
= 0; i
< pad
; i
++) {
108 if (buf
[--len
] != pad
) {
109 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_DECRYPT
);