1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     main.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include <nn/os.h>
20 #include <nn/dbg.h>
21 #include <nn/crypto.h>
22 
23 
24 #define MAX_BUF_SIZE 1024
25 
26 // Public keys and private keys are defined with key.o.
27 // The buffer address and buffer size for each key are defined with the following variables
28 // key.o is a DER-format 512-bit RSA key
29 // Can be used only for the purpose of trial use
30 extern unsigned char rsa_der_data_pub[];
31 extern unsigned char rsa_der_data_priv[];
32 extern int rsa_der_data_pub_len;
33 extern int rsa_der_data_priv_len;
34 
35 
encrypt_dectypt_test(void)36 void encrypt_dectypt_test(void)
37 {
38     nn::Result result;
39     char string[32] = "this is test string.";   /* The data string to encrypt            */
40     unsigned char buf1[MAX_BUF_SIZE];           /* Output buffer in which to put the     */
41                                                 /* encrypted data                        */
42     unsigned char buf2[MAX_BUF_SIZE];           /* Output buffer in which to put the     */
43                                                 /* decrypted data                        */
44     unsigned int len = MAX_BUF_SIZE;            /* Output buffer length                  */
45     size_t encrypt_size, decrypt_size;
46 
47     nn::crypto::RsaContext swRsaPriv, swRsaPub;
48 
49     NN_LOG("ENCRYPT\n");
50 
51     swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len);
52     result = swRsaPub.Encrypt(&encrypt_size, buf1, len, string, std::strlen(string));
53     if(result.IsFailure())
54     {
55         NN_PANIC("Encrypt failed");
56     }
57     else
58     {
59         NN_LOG("Encrypt SUCCESS\n");
60     }
61     swRsaPub.Finalize();
62 
63 
64     NN_LOG("DECRYPT\n");
65 
66     swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len);
67     result = swRsaPriv.Decrypt(&decrypt_size, buf2, len, buf1, encrypt_size);
68     if(result.IsFailure())
69     {
70         NN_PANIC("Decrypt failed");
71     }
72     else
73     {
74         NN_LOG("Decrypt SUCCESS\n");
75     }
76     swRsaPriv.Finalize();
77 
78     /* Add terminal symbol to the end of decrypted message. */
79     buf2[decrypt_size] = '\0';
80 
81     /* Print the decrypted data. */
82     NN_LOG("DECRYPTED MESSAGE:\n");
83     NN_LOG(reinterpret_cast<char*>(buf2));
84     NN_LOG("\n");
85 }
86 
sign_verify_test(void)87 void sign_verify_test(void)
88 {
89     nn::Result result;
90     char string[] = "Copyright 2008-2010 Nintendo.  All rights reserved.";
91                                                 /* The data string to sign               */
92     unsigned char buf1[MAX_BUF_SIZE];           /* Output buffer in which to put the     */
93                                                 /* signed data                           */
94     unsigned int len = MAX_BUF_SIZE;            /* Output buffer length                  */
95     size_t signature_size;
96 
97     nn::crypto::RsaContext swRsaPriv, swRsaPub;
98 
99     NN_LOG("SIGN\n");
100     swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len);
101     result = swRsaPriv.Sign(&signature_size, buf1, len, string, std::strlen(string));
102     if(result.IsFailure())
103     {
104         NN_PANIC("Encrypt failed");
105     }
106     swRsaPriv.Finalize();
107 
108     NN_LOG("VERIFY\n");
109 
110     swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len);
111     result = swRsaPub.Verify(string, std::strlen(string), buf1, signature_size);
112     if(result.IsFailure())
113     {
114         NN_PANIC("Verify failed");
115     }
116     else
117     {
118         NN_LOG("Verify SUCCESS\n");
119     }
120     swRsaPub.Finalize();
121 }
122 
sign_verify_test__pre_digest(void)123 void sign_verify_test__pre_digest(void)
124 {
125     nn::Result result;
126     const char string[] = "These coded instructions, statements, and computer programs contain\n"
127                           "proprietary information of Nintendo of America Inc.";
128                                                 /* The data string to sign               */
129     unsigned char buf1[MAX_BUF_SIZE];           /* Output buffer in which to put the     */
130                                                 /* signed data                           */
131     unsigned char dgst[MAX_BUF_SIZE];           /* Output buffer in which to put the     */
132                                                 /* digested data                         */
133     unsigned int len = MAX_BUF_SIZE;            /* Output buffer length                  */
134     size_t signature_size;
135 
136     NN_LOG("DIGEST and SIGN\n");
137 
138     nn::crypto::Sha256Context  sha256;
139 
140     sha256.Initialize();
141     sha256.Update(string, strlen(string));
142     sha256.GetHash(dgst);
143     sha256.Finalize();
144 
145     nn::crypto::RsaContext swRsaPriv, swRsaPub;
146 
147     swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len);
148     result = swRsaPriv.SignSha256(&signature_size, buf1, len, dgst);
149     if(result.IsFailure())
150     {
151         NN_PANIC("Digest and Sign failed");
152     }
153     else
154     {
155         NN_LOG("Digest and Sign SUCCESS\n");
156     }
157     swRsaPriv.Finalize();
158 
159     NN_LOG("VERIFY\n");
160 
161     swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len);
162     result = swRsaPub.VerifySha256(dgst, buf1, signature_size);
163     if(result.IsFailure())
164     {
165         NN_PANIC("Verify failed");
166     }
167     else
168     {
169         NN_LOG("Verify SUCCESS\n");
170     }
171     swRsaPub.Finalize();
172 }
173 
nnMain()174 extern "C" void nnMain()
175 {
176     NN_LOG("cryptoRSA sample\n");
177 
178     encrypt_dectypt_test();
179     sign_verify_test();
180     sign_verify_test__pre_digest();
181 
182     NN_LOG("RSA test FINISHED.\n");
183 
184     while(1)
185     {
186         nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromSeconds(5));
187     }
188 }
189