/*---------------------------------------------------------------------------* Project: Horizon File: main.cpp Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 46365 $ *---------------------------------------------------------------------------*/ #include #include #include #include #include #define MAX_BUF_SIZE 1024 // Public keys and private keys are defined with key.o. // The buffer address and buffer size for each key are defined with the following variables // key.o is a DER-format 512-bit RSA key // Can be used only for the purpose of trial use extern unsigned char rsa_der_data_pub[]; extern unsigned char rsa_der_data_priv[]; extern int rsa_der_data_pub_len; extern int rsa_der_data_priv_len; void encrypt_dectypt_test(void) { nn::Result result; char string[32] = "this is test string."; /* The data string to encrypt */ unsigned char buf1[MAX_BUF_SIZE]; /* Output buffer in which to put the */ /* encrypted data */ unsigned char buf2[MAX_BUF_SIZE]; /* Output buffer in which to put the */ /* decrypted data */ unsigned int len = MAX_BUF_SIZE; /* Output buffer length */ size_t encrypt_size, decrypt_size; nn::crypto::RsaContext swRsaPriv, swRsaPub; NN_LOG("ENCRYPT\n"); swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len); result = swRsaPub.Encrypt(&encrypt_size, buf1, len, string, std::strlen(string)); if(result.IsFailure()) { NN_PANIC("Encrypt failed"); } else { NN_LOG("Encrypt SUCCESS\n"); } swRsaPub.Finalize(); NN_LOG("DECRYPT\n"); swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len); result = swRsaPriv.Decrypt(&decrypt_size, buf2, len, buf1, encrypt_size); if(result.IsFailure()) { NN_PANIC("Decrypt failed"); } else { NN_LOG("Decrypt SUCCESS\n"); } swRsaPriv.Finalize(); /* Add terminal symbol to the end of decrypted message. */ buf2[decrypt_size] = '\0'; /* Print the decrypted data. */ NN_LOG("DECRYPTED MESSAGE:\n"); NN_LOG(reinterpret_cast(buf2)); NN_LOG("\n"); } void sign_verify_test(void) { nn::Result result; char string[] = "Copyright 2008-2010 Nintendo. All rights reserved."; /* The data string to sign */ unsigned char buf1[MAX_BUF_SIZE]; /* Output buffer in which to put the */ /* signed data */ unsigned int len = MAX_BUF_SIZE; /* Output buffer length */ size_t signature_size; nn::crypto::RsaContext swRsaPriv, swRsaPub; NN_LOG("SIGN\n"); swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len); result = swRsaPriv.Sign(&signature_size, buf1, len, string, std::strlen(string)); if(result.IsFailure()) { NN_PANIC("Encrypt failed"); } swRsaPriv.Finalize(); NN_LOG("VERIFY\n"); swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len); result = swRsaPub.Verify(string, std::strlen(string), buf1, signature_size); if(result.IsFailure()) { NN_PANIC("Verify failed"); } else { NN_LOG("Verify SUCCESS\n"); } swRsaPub.Finalize(); } void sign_verify_test__pre_digest(void) { nn::Result result; const char string[] = "These coded instructions, statements, and computer programs contain\n" "proprietary information of Nintendo of America Inc."; /* The data string to sign */ unsigned char buf1[MAX_BUF_SIZE]; /* Output buffer in which to put the */ /* signed data */ unsigned char dgst[MAX_BUF_SIZE]; /* Output buffer in which to put the */ /* digested data */ unsigned int len = MAX_BUF_SIZE; /* Output buffer length */ size_t signature_size; NN_LOG("DIGEST and SIGN\n"); nn::crypto::Sha256Context sha256; sha256.Initialize(); sha256.Update(string, strlen(string)); sha256.GetHash(dgst); sha256.Finalize(); nn::crypto::RsaContext swRsaPriv, swRsaPub; swRsaPriv.Initialize(rsa_der_data_priv, rsa_der_data_priv_len); result = swRsaPriv.SignSha256(&signature_size, buf1, len, dgst); if(result.IsFailure()) { NN_PANIC("Digest and Sign failed"); } else { NN_LOG("Digest and Sign SUCCESS\n"); } swRsaPriv.Finalize(); NN_LOG("VERIFY\n"); swRsaPub.Initialize(rsa_der_data_pub, rsa_der_data_pub_len); result = swRsaPub.VerifySha256(dgst, buf1, signature_size); if(result.IsFailure()) { NN_PANIC("Verify failed"); } else { NN_LOG("Verify SUCCESS\n"); } swRsaPub.Finalize(); } extern "C" void nnMain() { NN_LOG("cryptoRSA sample\n"); encrypt_dectypt_test(); sign_verify_test(); sign_verify_test__pre_digest(); NN_LOG("RSA test FINISHED.\n"); while(1) { nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromSeconds(5)); } }