Add basic aria and camellia ciphers modes to default provider
[openssl.git] / providers / common / ciphers / cipher_locl.h
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 <stdio.h>
11 #include <openssl/opensslconf.h>
12 #include <openssl/params.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17 #include "internal/cryptlib.h"
18 #include "internal/modes_int.h"
19 #include "internal/provider_algs.h"
20 #include "internal/providercommonerr.h"
21 #include "internal/ciphermode_platform.h"
22
23 #define GENERIC_BLOCK_SIZE 16
24 #define IV_STATE_UNINITIALISED 0  /* initial state is not initialized */
25 #define IV_STATE_BUFFERED      1  /* iv has been copied to the iv buffer */
26 #define IV_STATE_COPIED        2  /* iv has been copied from the iv buffer */
27 #define IV_STATE_FINISHED      3  /* the iv has been used - so don't reuse it */
28
29 #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
30
31 typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
32 typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
33
34 typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
35                                 const unsigned char *in, size_t len);
36
37 struct prov_cipher_ctx_st {
38     block128_f block;
39     union {
40         cbc128_f cbc;
41         ctr128_f ctr;
42     } stream;
43
44     /*
45      * num contains the number of bytes of |iv| which are valid for modes that
46      * manage partial blocks themselves.
47      */
48     size_t num;
49
50     int mode;
51     int enc;              /* Set to 1 for encrypt, or 0 otherwise */
52     size_t bufsz;         /* Number of bytes in buf */
53     size_t keylen;        /* key size (in bytes) */
54     size_t blocksize;
55     uint64_t flags;
56     unsigned int pad : 1; /* Whether padding should be used or not */
57
58     /* Buffer of partial blocks processed via update calls */
59     unsigned char buf[GENERIC_BLOCK_SIZE];
60     unsigned char iv[GENERIC_BLOCK_SIZE];
61     const PROV_CIPHER_HW *hw; /* hardware specific functions */
62     const void *ks; /* Pointer to algorithm specific key data */
63 };
64
65 struct prov_cipher_hw_st {
66     int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
67     PROV_CIPHER_HW_FN *cipher;
68 };
69
70 OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit;
71 OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit;
72 OSSL_OP_cipher_update_fn cipher_generic_block_update;
73 OSSL_OP_cipher_final_fn cipher_generic_block_final;
74 OSSL_OP_cipher_update_fn cipher_generic_stream_update;
75 OSSL_OP_cipher_final_fn cipher_generic_stream_final;
76 OSSL_OP_cipher_cipher_fn cipher_generic_cipher;
77 OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params;
78 OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params;
79 OSSL_OP_cipher_gettable_params_fn     cipher_default_gettable_params;
80 OSSL_OP_cipher_gettable_ctx_params_fn cipher_default_gettable_ctx_params;
81 OSSL_OP_cipher_settable_ctx_params_fn cipher_default_settable_ctx_params;
82 OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params;
83 OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params;
84 int cipher_default_get_params(OSSL_PARAM params[], int md, unsigned long flags,
85                               int kbits, int blkbits, int ivbits);
86 void cipher_generic_initkey(void *vctx, int kbits, int blkbits, int mode,
87                             const PROV_CIPHER_HW *ciph);
88
89 size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
90                  const unsigned char **in, size_t *inlen);
91 int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
92                  const unsigned char **in, size_t *inlen);
93 void padblock(unsigned char *buf, size_t *buflen, size_t blocksize);
94 int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize);
95
96 #include "cipher_aes.h"
97 #include "cipher_aria.h"
98 #include "cipher_camellia.h"
99 #include "cipher_gcm.h"
100 #include "cipher_ccm.h"
101
102 #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,     \
103                                  blkbits, ivbits, typ)                         \
104 static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;     \
105 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
106 {                                                                              \
107     return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags,  \
108                                      kbits, blkbits, ivbits);                  \
109 }                                                                              \
110 static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;             \
111 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
112 {                                                                              \
113      PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));                   \
114      if (ctx != NULL) {                                                        \
115          cipher_generic_initkey(ctx, kbits, blkbits, EVP_CIPH_##UCMODE##_MODE, \
116                                 PROV_CIPHER_HW_##alg##_##lcmode(kbits));       \
117      }                                                                         \
118      return ctx;                                                               \
119 }                                                                              \
120 const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = {                       \
121     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
122       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
123     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
124     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
125     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit },   \
126     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit },   \
127     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
128     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final },  \
129     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher },        \
130     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
131       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
132     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
133       (void (*)(void))cipher_generic_get_ctx_params },                         \
134     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
135       (void (*)(void))cipher_generic_set_ctx_params },                         \
136     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
137       (void (*)(void))cipher_default_gettable_params },                        \
138     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
139       (void (*)(void))cipher_default_gettable_ctx_params },                    \
140     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
141      (void (*)(void))cipher_default_settable_ctx_params },                     \
142     { 0, NULL }                                                                \
143 };
144
145 #define IMPLEMENT_aead_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits)  \
146 static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lc##_get_params;         \
147 static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[])              \
148 {                                                                              \
149     return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE,         \
150                                      flags, kbits, blkbits, ivbits);           \
151 }                                                                              \
152 static OSSL_OP_cipher_newctx_fn alg##kbits##lc##_newctx;                       \
153 static void * alg##kbits##lc##_newctx(void *provctx)                           \
154 {                                                                              \
155     return alg##_##lc##_newctx(provctx, kbits);                                \
156 }                                                                              \
157 const OSSL_DISPATCH alg##kbits##lc##_functions[] = {                           \
158     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx },      \
159     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx },        \
160     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) lc##_einit },            \
161     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) lc##_dinit },            \
162     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void)) lc##_stream_update },          \
163     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void)) lc##_stream_final },            \
164     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void)) lc##_cipher },                 \
165     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
166       (void (*)(void)) alg##_##kbits##_##lc##_get_params },                    \
167     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
168       (void (*)(void)) lc##_get_ctx_params },                                  \
169     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
170       (void (*)(void)) lc##_set_ctx_params },                                  \
171     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
172       (void (*)(void))cipher_default_gettable_params },                        \
173     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
174       (void (*)(void))cipher_aead_gettable_ctx_params },                       \
175     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
176       (void (*)(void))cipher_aead_settable_ctx_params },                       \
177     { 0, NULL }                                                                \
178 }
179
180 PROV_CIPHER_HW_FN cipher_hw_generic_cbc;
181 PROV_CIPHER_HW_FN cipher_hw_generic_ecb;
182 PROV_CIPHER_HW_FN cipher_hw_generic_ofb128;
183 PROV_CIPHER_HW_FN cipher_hw_generic_cfb128;
184 PROV_CIPHER_HW_FN cipher_hw_generic_cfb8;
185 PROV_CIPHER_HW_FN cipher_hw_generic_cfb1;
186 PROV_CIPHER_HW_FN cipher_hw_generic_ctr;
187 PROV_CIPHER_HW_FN cipher_hw_chunked_cbc;
188 PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8;
189 PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128;
190 PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128;
191 #define cipher_hw_chunked_ecb  cipher_hw_generic_ecb
192 #define cipher_hw_chunked_ctr  cipher_hw_generic_ctr
193 #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1