Add gcm ciphers (aes and aria) to providers.
[openssl.git] / providers / common / ciphers / ciphers_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 <openssl/opensslconf.h>
11 #include <openssl/aes.h>
12 #include <openssl/params.h>
13 #include "internal/cryptlib.h"
14 #include "internal/modes_int.h"
15
16 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
17 /*-
18  * KMA-GCM-AES parameter block - begin
19  * (see z/Architecture Principles of Operation >= SA22-7832-11)
20  */
21 typedef struct S390X_kma_params_st {
22     unsigned char reserved[12];
23     union {
24         unsigned int w;
25         unsigned char b[4];
26     } cv; /* 32 bit counter value */
27     union {
28         unsigned long long g[2];
29         unsigned char b[16];
30     } t; /* tag */
31     unsigned char h[16]; /* hash subkey */
32     unsigned long long taadl; /* total AAD length */
33     unsigned long long tpcl; /* total plaintxt/ciphertxt len */
34     union {
35         unsigned long long g[2];
36         unsigned int w[4];
37     } j0;                   /* initial counter value */
38     unsigned char k[32];    /* key */
39 } S390X_KMA_PARAMS;
40
41 #endif
42
43 typedef struct prov_aes_cipher_st PROV_AES_CIPHER;
44
45 #define IV_STATE_UNINITIALISED 0  /* initial state is not initialized */
46 #define IV_STATE_BUFFERED      1  /* iv has been copied to the iv buffer */
47 #define IV_STATE_COPIED        2  /* iv has been copied from the iv buffer */
48 #define IV_STATE_FINISHED      3  /* the iv has been used - so don't reuse it */
49
50 typedef struct prov_aes_key_st {
51     union {
52         OSSL_UNION_ALIGN;
53         AES_KEY ks;
54     } ks;
55     block128_f block;
56     union {
57         cbc128_f cbc;
58         ctr128_f ctr;
59     } stream;
60
61     /* Platform specific data */
62     union {
63         int dummy;
64 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
65         struct {
66             union {
67                 OSSL_UNION_ALIGN;
68                 /*-
69                  * KM-AES parameter block - begin
70                  * (see z/Architecture Principles of Operation >= SA22-7832-06)
71                  */
72                 struct {
73                     unsigned char k[32];
74                 } km;
75                 /* KM-AES parameter block - end */
76                 /*-
77                  * KMO-AES/KMF-AES parameter block - begin
78                  * (see z/Architecture Principles of Operation >= SA22-7832-08)
79                  */
80                 struct {
81                     unsigned char cv[16];
82                     unsigned char k[32];
83                 } kmo_kmf;
84                 /* KMO-AES/KMF-AES parameter block - end */
85             } param;
86             unsigned int fc;
87             int res;
88         } s390x;
89 #endif /* defined(OPENSSL_CPUID_OBJ) && defined(__s390__) */
90     } plat;
91
92     /* The cipher functions we are going to use */
93     const PROV_AES_CIPHER *ciph;
94
95     /* The mode that we are using */
96     int mode;
97
98     /* Set to 1 if we are encrypting or 0 otherwise */
99     int enc;
100
101     unsigned char iv[AES_BLOCK_SIZE];
102
103     /*
104      * num contains the number of bytes of |iv| which are valid for modes that
105      * manage partial blocks themselves.
106      */
107     size_t num;
108
109     /* Buffer of partial blocks processed via update calls */
110     unsigned char buf[AES_BLOCK_SIZE];
111
112     /* Number of bytes in buf */
113     size_t bufsz;
114
115     uint64_t flags;
116
117     size_t keylen;
118
119     /* Whether padding should be used or not */
120     unsigned int pad : 1;
121 } PROV_AES_KEY;
122
123 struct prov_aes_cipher_st {
124   int (*init)(PROV_AES_KEY *dat, const uint8_t *key, size_t keylen);
125   int (*cipher)(PROV_AES_KEY *dat, uint8_t *out, const uint8_t *in,
126                 size_t inl);
127 };
128
129 #define OSSL_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
130
131 #include "ciphers_gcm.h"
132
133 const PROV_AES_CIPHER *PROV_AES_CIPHER_ecb(size_t keylen);
134 const PROV_AES_CIPHER *PROV_AES_CIPHER_cbc(size_t keylen);
135 const PROV_AES_CIPHER *PROV_AES_CIPHER_ofb(size_t keylen);
136 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb(size_t keylen);
137 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb1(size_t keylen);
138 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb8(size_t keylen);
139 const PROV_AES_CIPHER *PROV_AES_CIPHER_ctr(size_t keylen);
140
141 size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
142                  const unsigned char **in, size_t *inlen);
143 int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
144                  const unsigned char **in, size_t *inlen);
145 void padblock(unsigned char *buf, size_t *buflen, size_t blocksize);
146 int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize);
147 int aes_get_params(OSSL_PARAM params[], int md, unsigned long flags,
148                    int kbits, int blkbits, int ivbits);