rsa_oaep_sample.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <debug.h>
  32. #include <malloc.h>
  33. #include <string.h>
  34. #include <platform/mt_typedefs.h>
  35. #include <platform/rsa_oaep.h>
  36. #include <platform/rsa2048_export.h>
  37. #define RSA_TST_KEY_N 0xae, 0xd5, 0x2d, 0x35, 0xe7, 0x2e, 0x9d, 0xb1, 0x27, 0x78, 0x6c, 0xcc, 0xe5, 0x62, 0xba, 0xe2, \
  38. 0xa0, 0xa0, 0x4a, 0xbb, 0x7a, 0xbe, 0x27, 0x93, 0xa5, 0xe7, 0x92, 0x00, 0x53, 0x46, 0x28, 0x3f, \
  39. 0xc0, 0x4f, 0x87, 0x11, 0x05, 0x27, 0x86, 0x3d, 0xbb, 0x73, 0xe8, 0x65, 0xa9, 0x71, 0x24, 0xad, \
  40. 0xf8, 0x01, 0x28, 0xcc, 0x58, 0x37, 0x57, 0x49, 0xb4, 0x70, 0x99, 0x33, 0xcc, 0x21, 0x25, 0x11, \
  41. 0x1f, 0x07, 0xab, 0xb0, 0xe5, 0x0b, 0x0a, 0x3f, 0x5f, 0x91, 0x59, 0xc3, 0x1d, 0xfc, 0x9b, 0xb3, \
  42. 0xbd, 0xa3, 0x6b, 0x8a, 0xb4, 0x9f, 0x93, 0x48, 0x00, 0x9e, 0xdc, 0x10, 0xc5, 0x78, 0xf4, 0x99, \
  43. 0xc0, 0xff, 0xa1, 0xc8, 0xf3, 0xb7, 0x3a, 0x52, 0xfa, 0x20, 0x5f, 0x3a, 0x5c, 0x38, 0x0b, 0x8d, \
  44. 0x12, 0x49, 0xb6, 0x41, 0x4a, 0xbf, 0xaa, 0x72, 0xd8, 0x1a, 0x97, 0xb6, 0x8e, 0x58, 0xd5, 0xc3, \
  45. 0x0f, 0x06, 0x8f, 0xd6, 0x85, 0x48, 0xba, 0x3f, 0xf4, 0xbc, 0x60, 0x1f, 0xac, 0x66, 0x88, 0x8b, \
  46. 0xbf, 0x3b, 0xe9, 0xd1, 0x2e, 0x8c, 0x9d, 0x08, 0xd8, 0x58, 0x7c, 0x65, 0x54, 0xe6, 0x5c, 0xea, \
  47. 0xd7, 0x61, 0x24, 0x14, 0xd1, 0xb1, 0xe5, 0x7a, 0x88, 0x00, 0x1b, 0xb0, 0x8e, 0x78, 0x7e, 0xa2, \
  48. 0x34, 0x66, 0xca, 0x71, 0x5d, 0x1a, 0xcb, 0xb2, 0x2b, 0xcf, 0x1d, 0x94, 0x7d, 0x8b, 0x05, 0xf3, \
  49. 0x6a, 0xa2, 0x47, 0xfe, 0xb4, 0xa5, 0xb0, 0x91, 0x27, 0xf5, 0xa6, 0x1c, 0xae, 0xf1, 0xf4, 0x67, \
  50. 0x4b, 0x87, 0xe1, 0xa0, 0x68, 0xaf, 0x15, 0xc6, 0x03, 0xae, 0xe1, 0xaf, 0xf8, 0x54, 0xb8, 0xc9, \
  51. 0x3b, 0xd7, 0xc2, 0x2c, 0x7b, 0x77, 0xea, 0xbb, 0x22, 0x5a, 0xa6, 0x7e, 0xc3, 0x06, 0xfa, 0x80, \
  52. 0x3d, 0x54, 0x1a, 0xc2, 0x2c, 0xcb, 0x28, 0x3e, 0xa3, 0x15, 0x56, 0x49, 0x43, 0xc8, 0x06, 0x23
  53. #define RSA_TST_KEY_E 0x01, 0x00, 0x01
  54. #define RSA_TST_KEY_D 0x41, 0x09, 0x25, 0xdf, 0x46, 0x57, 0x4d, 0xeb, 0xc7, 0x8e, 0x01, 0x93, 0x1a, 0xc5, 0x66, 0x55, \
  55. 0xc5, 0x80, 0xbb, 0x2c, 0x10, 0x95, 0xc9, 0xed, 0x1f, 0xab, 0x0b, 0xe1, 0xb9, 0x18, 0x81, 0x02, \
  56. 0x6e, 0x0c, 0x67, 0x67, 0x0a, 0x6e, 0xd0, 0x7e, 0x08, 0x4c, 0x2e, 0xa8, 0x1a, 0xa6, 0xef, 0x09, \
  57. 0x14, 0x17, 0x82, 0x50, 0xb9, 0x84, 0x24, 0xa1, 0x53, 0x51, 0x0b, 0x7c, 0x7c, 0x33, 0xdc, 0x31, \
  58. 0x09, 0x44, 0xf6, 0x36, 0x40, 0x8d, 0xa1, 0xcb, 0x3f, 0x46, 0xb7, 0x88, 0x60, 0x90, 0x46, 0x6f, \
  59. 0x4f, 0x5d, 0x0a, 0x35, 0x37, 0xc9, 0xa0, 0xa5, 0x09, 0x2e, 0x0f, 0x18, 0xe2, 0x0c, 0x83, 0x98, \
  60. 0xea, 0x75, 0x6d, 0x7e, 0x6e, 0x42, 0xbb, 0x9c, 0x80, 0x55, 0x28, 0x77, 0x72, 0x34, 0x9a, 0xff, \
  61. 0x68, 0x4d, 0x29, 0x52, 0x12, 0x77, 0x47, 0xf5, 0x9e, 0xb4, 0x0d, 0xb9, 0xa8, 0x2c, 0x1d, 0x9f, \
  62. 0xeb, 0x7b, 0x9f, 0xe3, 0x10, 0xae, 0x18, 0x37, 0xc4, 0xda, 0x1e, 0x6c, 0x40, 0xa8, 0xc3, 0x72, \
  63. 0x32, 0x87, 0x6c, 0x80, 0x8a, 0xea, 0xce, 0x5f, 0xe3, 0x30, 0xdb, 0xb1, 0x98, 0xde, 0x2b, 0x8f, \
  64. 0xdf, 0xcd, 0xe7, 0x82, 0x9c, 0x9c, 0x24, 0x7a, 0x98, 0xe6, 0x70, 0xca, 0x2e, 0x9d, 0xdc, 0xd2, \
  65. 0xe9, 0x80, 0x7f, 0x68, 0x2e, 0x5c, 0xc0, 0x8d, 0x4a, 0x4e, 0xbb, 0x0c, 0x34, 0x3a, 0x87, 0x18, \
  66. 0x3e, 0x6d, 0xdc, 0x71, 0xa8, 0x97, 0xf2, 0xe5, 0x2d, 0x10, 0x45, 0x57, 0x24, 0x6b, 0x4a, 0xa1, \
  67. 0x96, 0x19, 0x36, 0xc9, 0xae, 0x54, 0x9f, 0xd8, 0xe7, 0xd6, 0xa4, 0xf4, 0xf2, 0x74, 0x46, 0x25, \
  68. 0xfd, 0x98, 0x5e, 0x37, 0xaa, 0x4d, 0xb5, 0xff, 0x09, 0xbc, 0x01, 0x69, 0x7c, 0x95, 0x75, 0x22, \
  69. 0x61, 0x30, 0xea, 0x2e, 0x90, 0x42, 0xff, 0xf3, 0xb3, 0x65, 0x3f, 0xb7, 0x44, 0xce, 0xd9, 0x31
  70. #define TST_MSG_SZ (16)
  71. static void dump_buf(unsigned char *buf, unsigned int buf_sz)
  72. {
  73. unsigned int idx = 0;
  74. if (buf_sz < 4)
  75. return;
  76. for (idx = 0; idx <= buf_sz - 4; idx += 4) {
  77. dprintf(CRITICAL, "[0x%x] 0x%x 0x%x 0x%x 0x%x\n", idx,
  78. *(buf + idx),
  79. *(buf + idx + 1),
  80. *(buf + idx + 2),
  81. *(buf + idx + 3));
  82. }
  83. return;
  84. }
  85. void rsa_oaep_sample(void)
  86. {
  87. unsigned int ret = 0;
  88. unsigned int idx = 0;
  89. unsigned char key_n[] = {RSA_TST_KEY_N};
  90. unsigned char key_e[] = {RSA_TST_KEY_E};
  91. unsigned char key_d[] = {RSA_TST_KEY_D};
  92. unsigned int key_n_sz = sizeof(key_n);
  93. unsigned int key_e_sz = sizeof(key_e);
  94. unsigned int key_d_sz = sizeof(key_d);
  95. unsigned char msg[TST_MSG_SZ] = {0};
  96. unsigned char emsg[RSA_MODULUS_SZ] = {0};
  97. unsigned int msg_sz = TST_MSG_SZ;
  98. unsigned int emsg_sz = RSA_MODULUS_SZ;
  99. dprintf(CRITICAL, "msg = ");
  100. for (idx = 0; idx < TST_MSG_SZ; idx++) {
  101. msg[idx] = idx % 16;
  102. dprintf(CRITICAL, "0x%x ", msg[idx]);
  103. }
  104. dprintf(CRITICAL, "\n");
  105. ret = rsa_oaep_encrypt(msg, msg_sz, key_e, key_e_sz, key_n, key_n_sz, emsg, &emsg_sz);
  106. if (ret) {
  107. dprintf(CRITICAL, "rsa_oaep_encrypt test fail(0x%x)\n", ret);
  108. return;
  109. }
  110. dprintf(CRITICAL, "emsg = ");
  111. for (idx = 0; idx < TST_MSG_SZ; idx++) {
  112. dprintf(CRITICAL, "0x%x ", emsg[idx]);
  113. }
  114. dprintf(CRITICAL, "\n");
  115. memset(msg, 0x0, TST_MSG_SZ);
  116. ret = rsa_oaep_decrypt(emsg, emsg_sz, key_d, key_d_sz, key_n, key_n_sz, msg, &msg_sz);
  117. if (ret) {
  118. dprintf(CRITICAL, "rsa_oaep_decrypt test fail(0x%x)\n", ret);
  119. return;
  120. }
  121. dprintf(CRITICAL, "dmsg = ");
  122. for (idx = 0; idx < TST_MSG_SZ; idx++) {
  123. dprintf(CRITICAL, "0x%x ", msg[idx]);
  124. }
  125. dprintf(CRITICAL, "\n");
  126. for (idx = 0; idx < TST_MSG_SZ; idx++) {
  127. if (msg[idx] != (idx % 16)) {
  128. dprintf(CRITICAL, "rsa_oaep test failed\n");
  129. return;
  130. }
  131. }
  132. dprintf(CRITICAL, "rsa_oaep test pass\n");
  133. return;
  134. }