blob: 92d72af516e7c1b851eb8f5d075ff0a808c5d15d [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
Dave Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#ifndef MBEDTLS_PADLOCK_H
15#define MBEDTLS_PADLOCK_H
Paul Bakker5121ce52009-01-03 21:22:43 +000016
Bence Szépkútic662b362021-05-27 11:25:03 +020017#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020018
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010019#include "mbedtls/aes.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */
Paul Bakker70338f52011-05-23 10:19:31 +000022
Manuel Pégourié-Gonnardf659f0c2015-08-04 22:19:05 +020023#if defined(__has_feature)
24#if __has_feature(address_sanitizer)
25#define MBEDTLS_HAVE_ASAN
26#endif
27#endif
28
Jerry Yuf65f71e2023-08-28 10:58:24 +080029/*
30 * - `padlock` is implements with GNUC assembly for x86 target.
31 * - Some versions of ASan result in errors about not enough registers.
32 */
33#if defined(MBEDTLS_PADLOCK_C) && \
34 defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X86) && \
35 defined(MBEDTLS_HAVE_ASM) && \
36 !defined(MBEDTLS_HAVE_ASAN)
Jerry Yud76ded02023-04-19 11:07:40 +080037
Jerry Yu9e628622023-08-17 11:20:09 +080038#define MBEDTLS_VIA_PADLOCK_HAVE_CODE
39
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020040#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_PADLOCK_RNG 0x000C
43#define MBEDTLS_PADLOCK_ACE 0x00C0
44#define MBEDTLS_PADLOCK_PHE 0x0C00
45#define MBEDTLS_PADLOCK_PMM 0x3000
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Hanno Becker1eeca412018-10-15 12:01:35 +010047#define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) (x) & ~15))
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010054 * \brief Internal PadLock detection routine
55 *
56 * \note This function is only for internal use by other library
57 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000058 *
Paul Bakkera36d23e2013-12-30 17:57:27 +010059 * \param feature The feature to detect
Paul Bakker13e2dfe2009-07-28 07:18:38 +000060 *
Dave Rodgmanc1ae30a2021-05-18 18:59:37 +010061 * \return non-zero if CPU has support for the feature, 0 otherwise
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_padlock_has_support(int feature);
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010066 * \brief Internal PadLock AES-ECB block en(de)cryption
67 *
68 * \note This function is only for internal use by other library
69 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000070 *
71 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +000073 * \param input 16-byte input block
74 * \param output 16-byte output block
75 *
76 * \return 0 if success, 1 if operation failed
77 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
79 int mode,
80 const unsigned char input[16],
81 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010084 * \brief Internal PadLock AES-CBC buffer en(de)cryption
85 *
86 * \note This function is only for internal use by other library
87 * functions; you must not call it directly.
Paul Bakker5121ce52009-01-03 21:22:43 +000088 *
89 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +000091 * \param length length of the input data
92 * \param iv initialization vector (updated after use)
93 * \param input buffer holding the input data
94 * \param output buffer holding the output data
95 *
96 * \return 0 if success, 1 if operation failed
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
99 int mode,
100 size_t length,
101 unsigned char iv[16],
102 const unsigned char *input,
103 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105#ifdef __cplusplus
106}
107#endif
108
109#endif /* HAVE_X86 */
110
111#endif /* padlock.h */