1 /**
2  *
3  * \file
4  *
5  * \brief SAM Advanced Encryption Standard driver.
6  *
7  * This file defines a useful set of functions for the AES on SAM devices.
8  *
9  * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
10  *
11  * \asf_license_start
12  *
13  * \page License
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  *    this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright notice,
22  *    this list of conditions and the following disclaimer in the documentation
23  *    and/or other materials provided with the distribution.
24  *
25  * 3. The name of Atmel may not be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * 4. This software may only be redistributed and used in connection with an
29  *    Atmel microcontroller product.
30  *
31  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
34  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
35  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  *
43  * \asf_license_stop
44  *
45  */
46 /*
47  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
48  */
49 
50 #ifndef AES_CALLBACK_H_INCLUDED
51 #define AES_CALLBACK_H_INCLUDED
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 #include <compiler.h>
58 #include <system_interrupt.h>
59 /**
60  * \addtogroup asfdoc_sam0_drivers_aes_group
61  *
62  * @{
63  */
64 
65 /** AES interrupt callback function type. */
66 typedef void (*aes_callback_t)(void);
67 
68 /** AES callback type. */
69 enum aes_callback_type {
70 	/** Encryption complete callback */
71 	AES_CALLBACK_ENCRYPTION_COMPLETE = 0,
72 	/** GF Multiplication Complete callback */
73 	AES_CALLBACK_GF_MULTI_COMPLETE = 1,
74 };
75 
76 /** \name Callback Configuration and Initialization
77  * @{
78  */
79 
80 enum status_code aes_register_callback(
81 	const aes_callback_t callback,
82 	const enum aes_callback_type type);
83 
84 enum status_code aes_unregister_callback(
85 	const aes_callback_t callback,
86 	const enum aes_callback_type type);
87 
88 /** @} */
89 
90 
91 /** \name Callback Enabling and Disabling
92  * @{
93  */
94 
95 /**
96  * \brief Enable an AES callback.
97  *
98  * \param[in,out] module  Pointer to the software instance struct
99  * \param[in] type Callback source type
100  *
101  * \return Status of the callback enable operation.
102  * \retval STATUS_OK The callback was enabled successfully
103  * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
104  */
aes_enable_callback(struct aes_module * const module,const enum aes_callback_type type)105 static inline enum status_code aes_enable_callback(struct aes_module *const module,
106 		const enum aes_callback_type type)
107 {
108 	system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_AES);
109 	if (type == AES_CALLBACK_ENCRYPTION_COMPLETE){
110 		module->hw->INTENSET.reg = AES_INTENSET_ENCCMP;
111 	} else if (type == AES_CALLBACK_GF_MULTI_COMPLETE){
112 		module->hw->INTENSET.reg = AES_INTENSET_GFMCMP;
113 	} else {
114 		Assert(false);
115 		return STATUS_ERR_INVALID_ARG;
116 	}
117 
118 	return STATUS_OK;
119 }
120 
121 /**
122  * \brief Disable an AES callback.
123  *
124  * \param[in,out] module  Pointer to the software instance struct
125  * \param[in]  type Callback source type
126  *
127  * \return Status of the callback enable operation.
128  * \retval STATUS_OK The callback was enabled successfully
129  * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
130  */
aes_disable_callback(struct aes_module * const module,const enum aes_callback_type type)131 static inline enum status_code aes_disable_callback(struct aes_module *const module,
132 		 const enum aes_callback_type type)
133 {
134 	system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_AES);
135 	if (type == AES_CALLBACK_ENCRYPTION_COMPLETE){
136 		module->hw->INTENCLR.reg = AES_INTENCLR_ENCCMP;
137 	} else if (type == AES_CALLBACK_GF_MULTI_COMPLETE){
138 		module->hw->INTENCLR.reg = AES_INTENCLR_GFMCMP;
139 	} else {
140 		Assert(false);
141 		return STATUS_ERR_INVALID_ARG;
142 	}
143 
144 	return STATUS_OK;
145 }
146 
147 /** @} */
148 
149 /** @} */
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif  /* AES_CALLBACK_H_INCLUDED */
156