blob: d3ed2c39efc77b5a7fe869e189799df9fc73e056 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file padlock.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004 * \brief VIA PadLock ACE for HW encryption/decryption supported by some
5 * processors
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +01006 *
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +01007 * \warning These functions are only for internal use by other library
8 * functions; you must not call them directly.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_PADLOCK_H
27#define MBEDTLS_PADLOCK_H
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Bence Szépkútic662b362021-05-27 11:25:03 +020029#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020030
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/aes.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */
Paul Bakker70338f52011-05-23 10:19:31 +000034
Manuel Pégourié-Gonnardf659f0c2015-08-04 22:19:05 +020035#if defined(__has_feature)
36#if __has_feature(address_sanitizer)
37#define MBEDTLS_HAVE_ASAN
38#endif
39#endif
40
41/* Some versions of ASan result in errors about not enough registers */
Jerry Yu782b9662023-08-21 11:25:01 +080042#if defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X86) && \
43 defined(MBEDTLS_HAVE_ASM) && !defined(MBEDTLS_HAVE_ASAN)
Jerry Yud76ded02023-04-19 11:07:40 +080044
Jerry Yu9e628622023-08-17 11:20:09 +080045#define MBEDTLS_VIA_PADLOCK_HAVE_CODE
46
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020047#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_PADLOCK_RNG 0x000C
50#define MBEDTLS_PADLOCK_ACE 0x00C0
51#define MBEDTLS_PADLOCK_PHE 0x0C00
52#define MBEDTLS_PADLOCK_PMM 0x3000
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Hanno Becker1eeca412018-10-15 12:01:35 +010054#define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) (x) & ~15))
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010061 * \brief Internal PadLock detection routine
62 *
63 * \note This function is only for internal use by other library
64 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000065 *
Paul Bakkera36d23e2013-12-30 17:57:27 +010066 * \param feature The feature to detect
Paul Bakker13e2dfe2009-07-28 07:18:38 +000067 *
Dave Rodgmanc1ae30a2021-05-18 18:59:37 +010068 * \return non-zero if CPU has support for the feature, 0 otherwise
Paul Bakker5121ce52009-01-03 21:22:43 +000069 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_padlock_has_support(int feature);
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010073 * \brief Internal PadLock AES-ECB block en(de)cryption
74 *
75 * \note This function is only for internal use by other library
76 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000077 *
78 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +000080 * \param input 16-byte input block
81 * \param output 16-byte output block
82 *
83 * \return 0 if success, 1 if operation failed
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
86 int mode,
87 const unsigned char input[16],
88 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010091 * \brief Internal PadLock AES-CBC buffer en(de)cryption
92 *
93 * \note This function is only for internal use by other library
94 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000095 *
96 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +000098 * \param length length of the input data
99 * \param iv initialization vector (updated after use)
100 * \param input buffer holding the input data
101 * \param output buffer holding the output data
102 *
103 * \return 0 if success, 1 if operation failed
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
106 int mode,
107 size_t length,
108 unsigned char iv[16],
109 const unsigned char *input,
110 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112#ifdef __cplusplus
113}
114#endif
115
116#endif /* HAVE_X86 */
117
118#endif /* padlock.h */