libctru  v1.6.0
ps.h
Go to the documentation of this file.
1 /**
2  * @file ps.h
3  * @brief PS service.
4  */
5 #pragma once
6 
7 /// PS AES algorithms.
8 typedef enum
9 {
10  PS_ALGORITHM_CBC_ENC, ///< CBC encryption.
11  PS_ALGORITHM_CBC_DEC, ///< CBC decryption.
12  PS_ALGORITHM_CTR_ENC, ///< CTR encryption.
13  PS_ALGORITHM_CTR_DEC, ///< CTR decryption(same as PS_ALGORITHM_CTR_ENC).
14  PS_ALGORITHM_CCM_ENC, ///< CCM encryption.
15  PS_ALGORITHM_CCM_DEC, ///< CCM decryption.
17 
18 /// PS key slots.
19 typedef enum
20 {
21  PS_KEYSLOT_0D, ///< Key slot 0x0D.
22  PS_KEYSLOT_2D, ///< Key slot 0x2D.
23  PS_KEYSLOT_31, ///< Key slot 0x31.
24  PS_KEYSLOT_38, ///< Key slot 0x38.
25  PS_KEYSLOT_32, ///< Key slot 0x32.
26  PS_KEYSLOT_39_DLP, ///< Key slot 0x39. (DLP)
27  PS_KEYSLOT_2E, ///< Key slot 0x2E.
28  PS_KEYSLOT_INVALID, ///< Invalid key slot.
29  PS_KEYSLOT_36, ///< Key slot 0x36.
30  PS_KEYSLOT_39_NFC ///< Key slot 0x39. (NFC)
32 
33 /// RSA context.
34 typedef struct {
35  u8 modulo[0x100];
36  u8 exponent[0x100];
37  u32 rsa_bitsize;//The signature byte size is rsa_bitsize>>3.
38  u32 unk;//Normally zero?
39 } psRSAContext;
40 
41 /// Initializes PS.
42 Result psInit(void);
43 
44 /**
45  * @brief Initializes PS with the specified session handle.
46  * @param handle Session handle.
47  */
48 Result psInitHandle(Handle handle);
49 
50 /// Exits PS.
51 void psExit(void);
52 
53 /// Returns the PS session handle.
55 
56 /**
57  * @brief Signs a RSA signature.
58  * @param hash SHA256 hash to sign.
59  * @param ctx RSA context.
60  * @param signature RSA signature.
61  */
62 Result PS_SignRsaSha256(u8 *hash, psRSAContext *ctx, u8 *signature);
63 
64 /**
65  * @brief Verifies a RSA signature.
66  * @param hash SHA256 hash to compare with.
67  * @param ctx RSA context.
68  * @param signature RSA signature.
69  */
70 Result PS_VerifyRsaSha256(u8 *hash, psRSAContext *ctx, u8 *signature);
71 
72 /**
73  * @brief Encrypts/Decrypts AES data. Does not support AES CCM.
74  * @param size Size of the data.
75  * @param in Input buffer.
76  * @param out Output buffer.
77  * @param aes_algo AES algorithm to use.
78  * @param key_type Key type to use.
79  * @param iv Pointer to the CTR/IV. The output CTR/IV is also written here.
80  */
81 Result PS_EncryptDecryptAes(u32 size, u8* in, u8* out, PS_AESAlgorithm aes_algo, PS_AESKeyType key_type, u8* iv);
82 
83 /**
84  * @brief Encrypts/Decrypts signed AES CCM data.
85  * When decrypting, if the MAC is invalid, 0xC9010401 is returned. After encrypting the MAC is located at inputbufptr.
86  * @param in Input buffer.
87  * @param in_size Size of the input buffer. Must include MAC size when decrypting.
88  * @param out Output buffer.
89  * @param out_size Size of the output buffer. Must include MAC size when encrypting.
90  * @param data_len Length of the data to be encrypted/decrypted.
91  * @param mac_data_len Length of the MAC data.
92  * @param mac_len Length of the MAC.
93  * @param aes_algo AES algorithm to use.
94  * @param key_type Key type to use.
95  * @param nonce Pointer to the nonce.
96  */
97 Result PS_EncryptSignDecryptVerifyAesCcm(u8* in, u32 in_size, u8* out, u32 out_size, u32 data_len, u32 mac_data_len, u32 mac_len, PS_AESAlgorithm aes_algo, PS_AESKeyType key_type, u8* nonce);
98 
99 /**
100  * @brief Gets the 64-bit console friend code seed.
101  * @param seed Pointer to write the friend code seed to.
102  */
104 
105 /**
106  * @brief Gets the 32-bit device ID.
107  * @param device_id Pointer to write the device ID to.
108  */
109 Result PS_GetDeviceId(u32* device_id);
110 
111 /**
112  * @brief Generates cryptographically secure random bytes.
113  * @param out Pointer to the buffer to write the bytes to.
114  * @param len Number of bytes to write.
115  */
116 Result PS_GenerateRandomBytes(void* out, size_t len);
Key slot 0x2D.
Definition: ps.h:22
CBC encryption.
Definition: ps.h:10
PS_AESKeyType
PS key slots.
Definition: ps.h:19
s32 Result
Function result.
Definition: types.h:42
Key slot 0x32.
Definition: ps.h:25
CBC decryption.
Definition: ps.h:11
Handle psGetSessionHandle(void)
Returns the PS session handle.
Key slot 0x39. (NFC)
Definition: ps.h:30
RSA context.
Definition: ps.h:34
u32 Handle
Resource handle.
Definition: types.h:41
Invalid key slot.
Definition: ps.h:28
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
Result PS_GetDeviceId(u32 *device_id)
Gets the 32-bit device ID.
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
void psExit(void)
Exits PS.
CCM encryption.
Definition: ps.h:14
Result PS_EncryptDecryptAes(u32 size, u8 *in, u8 *out, PS_AESAlgorithm aes_algo, PS_AESKeyType key_type, u8 *iv)
Encrypts/Decrypts AES data.
Key slot 0x0D.
Definition: ps.h:21
Key slot 0x36.
Definition: ps.h:29
uint32_t u32
32-bit unsigned integer
Definition: types.h:23
Key slot 0x2E.
Definition: ps.h:27
Key slot 0x38.
Definition: ps.h:24
Result PS_EncryptSignDecryptVerifyAesCcm(u8 *in, u32 in_size, u8 *out, u32 out_size, u32 data_len, u32 mac_data_len, u32 mac_len, PS_AESAlgorithm aes_algo, PS_AESKeyType key_type, u8 *nonce)
Encrypts/Decrypts signed AES CCM data.
CCM decryption.
Definition: ps.h:15
Result psInit(void)
Initializes PS.
CTR decryption(same as PS_ALGORITHM_CTR_ENC).
Definition: ps.h:13
Key slot 0x31.
Definition: ps.h:23
Result PS_SignRsaSha256(u8 *hash, psRSAContext *ctx, u8 *signature)
Signs a RSA signature.
PS_AESAlgorithm
PS AES algorithms.
Definition: ps.h:8
Result PS_VerifyRsaSha256(u8 *hash, psRSAContext *ctx, u8 *signature)
Verifies a RSA signature.
Result PS_GenerateRandomBytes(void *out, size_t len)
Generates cryptographically secure random bytes.
Result PS_GetLocalFriendCodeSeed(u64 *seed)
Gets the 64-bit console friend code seed.
Key slot 0x39. (DLP)
Definition: ps.h:26
Result psInitHandle(Handle handle)
Initializes PS with the specified session handle.
CTR encryption.
Definition: ps.h:12