-
Notifications
You must be signed in to change notification settings - Fork 1
/
cast6.h
94 lines (79 loc) · 2.49 KB
/
cast6.h
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
/* Copyright (c) 2009, Markus Peloquin <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#ifndef CAST6_H
#define CAST6_H
#include <features.h>
#ifdef __cplusplus
# include <cstddef>
# include <cstdint>
/** Size of a CAST-256 block (bytes) */
const size_t CAST6_BLOCK = 16;
/** Minimum size of a CAST-256 key (bytes) */
const size_t CAST6_KEY_MIN = 16;
/** Maximum size of a CAST-256 key (bytes) */
const size_t CAST6_KEY_MAX = 32;
/** CAST-256 keys are all
* <code>CAST6_KEY_MIN + n * CAST6_KEY_STEP</code> bytes */
const size_t CAST6_KEY_STEP = 4;
#else
# include <stdbool.h>
# include <stddef.h>
# include <stdint.h>
# define CAST6_BLOCK 16
# define CAST6_KEY_MIN 16
# define CAST6_KEY_MAX 32
# define CAST6_KEY_STEP 4
#endif
struct cast6_ctx {
uint32_t Km[12][4];
uint8_t Kr[12][4];
};
#ifdef __cplusplus
extern "C" {
#endif
/** Initialize a CAST-256 context
*
* \param ctx Uninitialized structure
* \param key Private key
* \param sz The size in bytes of the key. Valid sizes are
* 16, 20, 24, 28, 32
* \retval false The key size is invalid
*/
bool cast6_init(struct cast6_ctx *ctx, const uint8_t *key, uint8_t sz);
/** Encrypt a block of data
*
* It is fine if the two buffers are the same.
*
* \param[in] ctx Context
* \param[in] plaintext Data to encrypt
* \param[out] ciphertext Encrypted data
*/
void cast6_encrypt(const struct cast6_ctx *ctx,
const uint8_t plaintext[CAST6_BLOCK],
uint8_t ciphertext[CAST6_BLOCK]);
/** Decrypt a block of data
*
* It is fine if the two buffers are the same.
*
* \param[in] ctx Context
* \param[in] ciphertext Data to decrypt
* \param[out] plaintext Decrypted data
*/
void cast6_decrypt(const struct cast6_ctx *ctx,
const uint8_t ciphertext[CAST6_BLOCK],
uint8_t plaintext[CAST6_BLOCK]);
#ifdef __cplusplus
}
#endif
#endif