2020-07-06 01:10:07 +01:00
/*
* aes . h
*
2024-04-12 10:47:36 +01:00
* Copyright ( c ) 2020 - 2024 , DarkMatterCore < pabloacurielz @ gmail . com > .
2020-07-06 01:10:07 +01:00
*
* This file is part of nxdumptool ( https : //github.com/DarkMatterCore/nxdumptool).
*
2021-03-25 19:26:58 +00:00
* nxdumptool is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
2020-07-06 01:10:07 +01:00
*
2021-03-25 19:26:58 +00:00
* nxdumptool is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
2020-07-06 01:10:07 +01:00
*
* You should have received a copy of the GNU General Public License
2021-03-25 19:26:58 +00:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
2020-07-06 01:10:07 +01:00
*/
# pragma once
2021-03-24 17:25:19 +00:00
# ifndef __AES_H__
# define __AES_H__
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
extern " C " {
# endif
2023-04-08 12:34:53 +01:00
/// One-shot function to perform AES-128-ECB crypto.
/// 'dst', 'src' and 'key' must all have a size of at least AES_BLOCK_SIZE bytes.
/// 'dst' and 'src' can both point to the same address.
void aes128EcbCrypt ( void * dst , const void * src , const void * key , bool encrypt ) ;
2020-07-06 01:10:07 +01:00
/// Performs an AES-128-XTS crypto operation using the non-standard Nintendo XTS tweak.
/// The Aes128XtsContext element should have been previously initialized with aes128XtsContextCreate(). 'encrypt' should match the value of 'is_encryptor' used with that call.
/// 'dst' and 'src' can both point to the same address.
size_t aes128XtsNintendoCrypt ( Aes128XtsContext * ctx , void * dst , const void * src , size_t size , u64 sector , size_t sector_size , bool encrypt ) ;
2020-10-21 05:27:48 +01:00
/// Initializes an output AES partial counter using an initial CTR value and an offset.
2021-04-26 00:10:34 +01:00
/// The sizes for 'out' and 'ctr' should be at least AES_BLOCK_SIZE and 8 bytes, respectively.
2020-10-21 05:27:48 +01:00
NX_INLINE void aes128CtrInitializePartialCtr ( u8 * out , const u8 * ctr , u64 offset )
{
if ( ! out | | ! ctr ) return ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
offset > > = 4 ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
for ( u8 i = 0 ; i < 8 ; i + + )
{
out [ i ] = ctr [ 0x8 - i - 1 ] ;
out [ 0x10 - i - 1 ] = ( u8 ) ( offset & 0xFF ) ;
offset > > = 8 ;
}
}
/// Updates the provided AES partial counter using an offset.
2021-04-26 00:10:34 +01:00
/// Size for 'out' should be at least AES_BLOCK_SIZE.
2020-10-21 05:27:48 +01:00
NX_INLINE void aes128CtrUpdatePartialCtr ( u8 * ctr , u64 offset )
{
if ( ! ctr ) return ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
offset > > = 4 ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
for ( u8 i = 0 ; i < 8 ; i + + )
{
ctr [ 0x10 - i - 1 ] = ( u8 ) ( offset & 0xFF ) ;
offset > > = 8 ;
}
}
/// Updates the provided AES partial counter using an offset and a 32-bit CTR value.
2021-04-26 00:10:34 +01:00
/// Size for 'out' should be at least AES_BLOCK_SIZE.
2020-10-21 05:27:48 +01:00
NX_INLINE void aes128CtrUpdatePartialCtrEx ( u8 * ctr , u32 ctr_val , u64 offset )
{
if ( ! ctr ) return ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
offset > > = 4 ;
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
for ( u8 i = 0 ; i < 8 ; i + + )
{
ctr [ 0x10 - i - 1 ] = ( u8 ) ( offset & 0xFF ) ;
offset > > = 8 ;
}
2022-07-05 02:04:28 +01:00
2020-10-21 05:27:48 +01:00
for ( u8 i = 0 ; i < 4 ; i + + )
{
ctr [ 0x8 - i - 1 ] = ( u8 ) ( ctr_val & 0xFF ) ;
ctr_val > > = 8 ;
}
}
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
}
2021-03-24 17:25:19 +00:00
# endif
# endif /* __AES_H__ */