PageRenderTime 77ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/openswan-2.6.38/testing/crypto/pk-dh-03/modp-case0.c

#
C | 226 lines | 150 code | 41 blank | 35 comment | 0 complexity | e72ea32131c2b2e3695ea9146f458471 MD5 | raw file
Possible License(s): GPL-2.0, ISC, LGPL-2.0
  1. /*
  2. * unit tests for cryptographic helper function - calculate KE and nonce
  3. *
  4. * Copyright (C) 2006 Michael C. Richardson <mcr@xelerance.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include <fcntl.h>
  17. #define VULCAN_PK 1
  18. #define PK_DH_REGRESS 1
  19. #include "../../../programs/pluto/hmac.c"
  20. #include "../../../programs/pluto/crypto.c"
  21. #include "../../../programs/pluto/ike_alg.c"
  22. #include "../../../programs/pluto/crypt_utils.c"
  23. #include "../../../programs/pluto/vulcan/vulcanpk_funcs.c"
  24. #include "crypto.h"
  25. char *progname;
  26. void exit_log(const char *message, ...)
  27. {
  28. va_list args;
  29. char m[LOG_WIDTH]; /* longer messages will be truncated */
  30. va_start(args, message);
  31. vsnprintf(m, sizeof(m), message, args);
  32. va_end(args);
  33. fprintf(stderr, "FATAL ERROR: %s\n", m);
  34. exit(0);
  35. }
  36. void exit_tool(int code)
  37. {
  38. exit(code);
  39. }
  40. #define U32B u_int32_t
  41. /*
  42. * Input/output data for modp operation.
  43. *
  44. */
  45. /* Operand_A */
  46. U32B aAddOperandA[] = {
  47. 0x80000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  48. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  49. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  50. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  51. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  52. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  53. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  54. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  55. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  56. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  57. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  58. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  59. };
  60. /* Operand_B */
  61. U32B aAddOperandB[] = {
  62. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  63. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  64. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  65. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  66. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  67. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  68. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  69. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  70. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  71. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  72. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  73. 0x00000000, 0x00000000, 0x00000000, 0x00000004
  74. };
  75. /*ExpectedResult */
  76. U32B aAddExpectedRes[] = {
  77. 0x80000001, 0x00000000, 0x00000000, 0x00000000,
  78. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  79. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  80. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  81. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  82. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  83. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  84. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  85. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  86. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  87. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  88. 0x00000000, 0x00000000, 0x00000000, 0x00000003
  89. };
  90. /******************* MODULUS data *******************/
  91. u_int32_t aModulus[] = {
  92. 0x80000000, 0x00000000, 0x00000000, 0x00000000,
  93. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  94. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  95. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  96. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  97. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  98. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  99. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  100. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  101. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  102. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  103. 0x00000000, 0x00000000, 0x00000000, 0x00000000
  104. };
  105. /******************* RECIPROCAL of MODULUS data *******************/
  106. u_int32_t aReciprocal[] = {
  107. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  108. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  109. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  110. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  111. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  112. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  113. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  114. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  115. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  116. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  117. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  118. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  119. };
  120. void bigendianize(u_int32_t *data, int len)
  121. {
  122. while(len-->0) {
  123. *data = htonl(*data);
  124. data++;
  125. }
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129. u_int8_t *mapping;
  130. u_int8_t aModExpOperandA_l[192];
  131. u_int8_t aModExpOperandB_l[192];
  132. u_int8_t aModExpExpectedRes_l[192];
  133. u_int8_t aModulus_l[192];
  134. u_int8_t aReciprocal_l[192];
  135. u_int8_t gtothex[192];
  136. struct pkprogram expModP;
  137. memset(&expModP, 0, sizeof(expModP));
  138. progname = argv[0];
  139. mapping = mapvulcanpk();
  140. /* initialize chip */
  141. vulcanpk_init(mapping);
  142. memcpy(aModExpOperandA_l, aAddOperandA, 192);
  143. bigendianize((u_int32_t *)aModExpOperandA_l, 192/sizeof(u_int32_t));
  144. memcpy(aModExpOperandB_l, aAddOperandB, 192);
  145. bigendianize((u_int32_t *)aModExpOperandB_l, 192/sizeof(u_int32_t));
  146. memcpy(aModExpExpectedRes_l, aAddExpectedRes, 192);
  147. bigendianize((u_int32_t *)aModExpExpectedRes_l, 192/sizeof(u_int32_t));
  148. memcpy(aModulus_l, aModulus, 192);
  149. bigendianize((u_int32_t *)aModulus_l, 192/sizeof(u_int32_t));
  150. memcpy(aReciprocal_l, aReciprocal, 192);
  151. bigendianize((u_int32_t *)aReciprocal_l, 192/sizeof(u_int32_t));
  152. expModP.valuesLittleEndian = FALSE;
  153. /* g-value */
  154. expModP.aValues[0] = aModExpOperandA_l;
  155. expModP.aValueLen[0]= 192;
  156. /* ^x-value */
  157. expModP.aValues[1] = aModExpOperandB_l;
  158. expModP.aValueLen[1]= 192;
  159. /* register 2 is result. */
  160. /* register 3 is scratch */
  161. /* M = modulus */
  162. expModP.aValues[4] = aModulus_l;
  163. expModP.aValueLen[4]= 192;
  164. /* reciprocal M(1) */
  165. expModP.aValues[5] = aReciprocal_l;
  166. expModP.aValueLen[5]= 192;
  167. /* registers 6,7,8 is M(2),M(3),M(4), scratch */
  168. expModP.oOffset = 2; /* B(1) is result */
  169. expModP.oValue = gtothex;
  170. expModP.oValueLen = sizeof(gtothex);
  171. /* ask to have the exponentiation done now! */
  172. expModP.pk_program[0]=/* sizes are ModLen=96(*32=3072),
  173. EXP_len=1,RED_len=0*/
  174. (0<<24)|(1<<8)|(96);
  175. expModP.pk_program[1]=/* opcode 1100=0xC (mod-exp),
  176. with A=0, B=1(6),M=4(24)*/
  177. (0x1<<24)|(24<<16)|(6<<8)|(0<<0);
  178. expModP.pk_proglen=2;
  179. execute_pkprogram(mapping, &expModP);
  180. printf("got: \n");
  181. hexdump(gtothex, 0, 192);
  182. printf("expected: \n");
  183. hexdump(aModExpExpectedRes_l, 0, 192);
  184. exit(0);
  185. }