blob: 5769a0cb11309da30c6f2973c9b2d7ed5cd4336c [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file psa/crypto_struct.h
3 *
4 * \brief PSA cryptography module: Mbed Crypto structured type implementations
5 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
9 * This file contains the definitions of some data structures with
10 * implementation-specific definitions.
11 *
12 * In implementations with isolation between the application and the
13 * cryptography module, it is expected that the front-end and the back-end
14 * would have different versions of this file.
15 */
16/*
17 * Copyright (C) 2018, ARM Limited, All Rights Reserved
18 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 *
32 * This file is part of Mbed Crypto (https://tls.mbed.org)
33 */
34
35#ifndef PSA_CRYPTO_STRUCT_H
36#define PSA_CRYPTO_STRUCT_H
37
38/* Include the Mbed Crypto configuration file, the way Mbed Crypto does it
39 * in each of its header files. */
40#if !defined(MBEDCRYPTO_CONFIG_FILE)
41#include "../mbedcrypto/config.h"
42#else
43#include MBEDCRYPTO_CONFIG_FILE
44#endif
45
46#include "mbedcrypto/cipher.h"
47#include "mbedcrypto/cmac.h"
48#include "mbedcrypto/gcm.h"
49#include "mbedcrypto/md.h"
50#include "mbedcrypto/md2.h"
51#include "mbedcrypto/md4.h"
52#include "mbedcrypto/md5.h"
53#include "mbedcrypto/ripemd160.h"
54#include "mbedcrypto/sha1.h"
55#include "mbedcrypto/sha256.h"
56#include "mbedcrypto/sha512.h"
57
58struct psa_hash_operation_s
59{
60 psa_algorithm_t alg;
61 union
62 {
63 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
64#if defined(MBEDCRYPTO_MD2_C)
65 mbedcrypto_md2_context md2;
66#endif
67#if defined(MBEDCRYPTO_MD4_C)
68 mbedcrypto_md4_context md4;
69#endif
70#if defined(MBEDCRYPTO_MD5_C)
71 mbedcrypto_md5_context md5;
72#endif
73#if defined(MBEDCRYPTO_RIPEMD160_C)
74 mbedcrypto_ripemd160_context ripemd160;
75#endif
76#if defined(MBEDCRYPTO_SHA1_C)
77 mbedcrypto_sha1_context sha1;
78#endif
79#if defined(MBEDCRYPTO_SHA256_C)
80 mbedcrypto_sha256_context sha256;
81#endif
82#if defined(MBEDCRYPTO_SHA512_C)
83 mbedcrypto_sha512_context sha512;
84#endif
85 } ctx;
86};
87
88
89typedef struct
90{
91 /** The hash context. */
92 struct psa_hash_operation_s hash_ctx;
93 /** The HMAC part of the context. */
94 uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
95} psa_hmac_internal_data;
96
97
98struct psa_mac_operation_s
99{
100 psa_algorithm_t alg;
101 unsigned int key_set : 1;
102 unsigned int iv_required : 1;
103 unsigned int iv_set : 1;
104 unsigned int has_input : 1;
105 unsigned int is_sign : 1;
106 uint8_t mac_size;
107 union
108 {
109 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
110#if defined(MBEDCRYPTO_MD_C)
111 psa_hmac_internal_data hmac;
112#endif
113#if defined(MBEDCRYPTO_CMAC_C)
114 mbedcrypto_cipher_context_t cmac;
115#endif
116 } ctx;
117};
118
119struct psa_cipher_operation_s
120{
121 psa_algorithm_t alg;
122 unsigned int key_set : 1;
123 unsigned int iv_required : 1;
124 unsigned int iv_set : 1;
125 uint8_t iv_size;
126 uint8_t block_size;
127 union
128 {
129 mbedcrypto_cipher_context_t cipher;
130 } ctx;
131};
132
133typedef struct
134{
135 uint8_t *info;
136 size_t info_length;
137 psa_hmac_internal_data hmac;
138 uint8_t prk[PSA_HASH_MAX_SIZE];
139 uint8_t output_block[PSA_HASH_MAX_SIZE];
140#if PSA_HASH_MAX_SIZE > 0xff
141#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
142#endif
143 uint8_t offset_in_block;
144 uint8_t block_number;
145} psa_hkdf_generator_t;
146
147struct psa_crypto_generator_s
148{
149 psa_algorithm_t alg;
150 size_t capacity;
151 union
152 {
153 struct
154 {
155 uint8_t *data;
156 size_t size;
157 } buffer;
158#if defined(MBEDCRYPTO_MD_C)
159 psa_hkdf_generator_t hkdf;
160#endif
161 } ctx;
162};
163
164#define PSA_CRYPTO_GENERATOR_INIT {0, 0, {{0, 0}}}
165static inline struct psa_crypto_generator_s psa_crypto_generator_init( void )
166{
167 const struct psa_crypto_generator_s v = PSA_CRYPTO_GENERATOR_INIT;
168 return( v );
169}
170
171struct psa_key_policy_s
172{
173 psa_key_usage_t usage;
174 psa_algorithm_t alg;
175};
176
177#endif /* PSA_CRYPTO_STRUCT_H */