raw
smg_comms_c_wrappers    1 //Basic tests for the C wrappers for rsa and mpi.
smg_comms_c_wrappers 2 //S.MG, 2018
smg_comms_c_wrappers 3
smg_comms_c_wrappers 4 #include "mpi.h"
smg_comms_c_wrappers 5 #include "smg_rsa.h"
smg_comms_c_wrappers 6
smg_comms_c_wrappers 7 #include "c_wrappers.h"
smg_comms_c_wrappers 8
smg_comms_c_wrappers 9 void test_mpi_cmp() {
smg_comms_c_wrappers 10 int result;
smg_comms_c_wrappers 11 int i;
smg_comms_c_wrappers 12 char a[KEY_LENGTH_OCTETS];
smg_comms_c_wrappers 13 char b[KEY_LENGTH_OCTETS];
smg_comms_c_wrappers 14
smg_comms_c_wrappers 15 //initialize mpis
smg_comms_c_wrappers 16 for (i=0;i<KEY_LENGTH_OCTETS;i++) {
smg_comms_c_wrappers 17 a[i] = i % 256;
smg_comms_c_wrappers 18 b[i] = a[i];
smg_comms_c_wrappers 19 }
smg_comms_c_wrappers 20
smg_comms_c_wrappers 21 //cmp same value mpi
smg_comms_c_wrappers 22 result = mpi_cmp_octets(a, KEY_LENGTH_OCTETS, b, KEY_LENGTH_OCTETS);
smg_comms_c_wrappers 23 if (result == 0)
smg_comms_c_wrappers 24 printf("PASS: mpi_cmp_octets on a == b.\n");
smg_comms_c_wrappers 25 else {
smg_comms_c_wrappers 26 printf("FAIL: mpi_cmp_octets on a == b ");
smg_comms_c_wrappers 27 printf("returned %d instead of 0.\n", result);
smg_comms_c_wrappers 28 }
smg_comms_c_wrappers 29
smg_comms_c_wrappers 30 //cmp a < b
smg_comms_c_wrappers 31 b[489] = 234;
smg_comms_c_wrappers 32 result = mpi_cmp_octets(a, KEY_LENGTH_OCTETS, b, KEY_LENGTH_OCTETS);
smg_comms_c_wrappers 33 if (result == -1)
smg_comms_c_wrappers 34 printf("PASS: mpi_cmp_octets on a < b.\n");
smg_comms_c_wrappers 35 else {
smg_comms_c_wrappers 36 printf("FAIL: mpi_cmp_octets on a < b ");
smg_comms_c_wrappers 37 printf("returned %d instead of -1.\n", result);
smg_comms_c_wrappers 38 }
smg_comms_c_wrappers 39
smg_comms_c_wrappers 40 //cmp a > b
smg_comms_c_wrappers 41 a[240] = 241;
smg_comms_c_wrappers 42 result = mpi_cmp_octets(a, KEY_LENGTH_OCTETS, b, KEY_LENGTH_OCTETS);
smg_comms_c_wrappers 43 if (result == 1)
smg_comms_c_wrappers 44 printf("PASS: mpi_cmp_octets on a > b.\n");
smg_comms_c_wrappers 45 else {
smg_comms_c_wrappers 46 printf("FAIL: mpi_cmp_octets on a > b ");
smg_comms_c_wrappers 47 printf("returned %d instead of 1.\n", result);
smg_comms_c_wrappers 48 }
smg_comms_c_wrappers 49 }
smg_comms_c_wrappers 50
smg_comms_c_wrappers 51 void test_gen_rsa_octets() {
smg_comms_c_wrappers 52 RSA_secret_key sk;
smg_comms_c_wrappers 53 RSA_public_key pk;
smg_comms_c_wrappers 54 int nlimbs = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS );
smg_comms_c_wrappers 55 int nlimbs_pq = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS / 2 );
smg_comms_c_wrappers 56
smg_comms_c_wrappers 57 //allocate memory
smg_comms_c_wrappers 58 sk.n = mpi_alloc(nlimbs);
smg_comms_c_wrappers 59 sk.e = mpi_alloc(nlimbs);
smg_comms_c_wrappers 60 sk.d = mpi_alloc(nlimbs);
smg_comms_c_wrappers 61 sk.p = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 62 sk.q = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 63 sk.u = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 64
smg_comms_c_wrappers 65 pk.n = mpi_alloc(nlimbs);
smg_comms_c_wrappers 66 pk.e = mpi_alloc(nlimbs);
smg_comms_c_wrappers 67
smg_comms_c_wrappers 68 //generate key pair
smg_comms_c_wrappers 69 int len_n = KEY_LENGTH_OCTETS;
smg_comms_c_wrappers 70 int len_e = len_n;
smg_comms_c_wrappers 71 int len_d = len_n;
smg_comms_c_wrappers 72 int len_p = KEY_LENGTH_OCTETS / 2;
smg_comms_c_wrappers 73 int len_q = len_p;
smg_comms_c_wrappers 74 int len_u = len_p;
smg_comms_c_wrappers 75 char n[KEY_LENGTH_OCTETS];
smg_comms_c_wrappers 76 char e[KEY_LENGTH_OCTETS];
smg_comms_c_wrappers 77 char d[KEY_LENGTH_OCTETS];
smg_comms_c_wrappers 78 char p[KEY_LENGTH_OCTETS / 2];
smg_comms_c_wrappers 79 char q[KEY_LENGTH_OCTETS / 2];
smg_comms_c_wrappers 80 char u[KEY_LENGTH_OCTETS / 2];
smg_comms_c_wrappers 81 gen_rsa_octets(n, &len_n,
smg_comms_c_wrappers 82 e, &len_e,
smg_comms_c_wrappers 83 d, &len_d,
smg_comms_c_wrappers 84 p, &len_p,
smg_comms_c_wrappers 85 q, &len_q,
smg_comms_c_wrappers 86 u, &len_u);
smg_comms_c_wrappers 87
smg_comms_c_wrappers 88 //check encryption/decr works
smg_comms_c_wrappers 89 mpi_set_buffer(sk.n, n, len_n, 0);
smg_comms_c_wrappers 90 mpi_set_buffer(sk.e, e, len_e, 0);
smg_comms_c_wrappers 91 mpi_set_buffer(sk.d, d, len_d, 0);
smg_comms_c_wrappers 92 mpi_set_buffer(sk.p, p, len_p, 0);
smg_comms_c_wrappers 93 mpi_set_buffer(sk.q, q, len_q, 0);
smg_comms_c_wrappers 94 mpi_set_buffer(sk.u, u, len_u, 0);
smg_comms_c_wrappers 95
smg_comms_c_wrappers 96 mpi_set_buffer(pk.n, n, len_n, 0);
smg_comms_c_wrappers 97 mpi_set_buffer(pk.e, e, len_e, 0);
smg_comms_c_wrappers 98
smg_comms_c_wrappers 99 MPI encr = mpi_alloc(0);
smg_comms_c_wrappers 100 MPI plain = mpi_alloc(0);
smg_comms_c_wrappers 101 MPI out = mpi_alloc(0);
smg_comms_c_wrappers 102 mpi_fromstr(plain, "0x\
smg_comms_c_wrappers 103 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
smg_comms_c_wrappers 104 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
smg_comms_c_wrappers 105 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
smg_comms_c_wrappers 106 28097EF92F6E78CAE0CB97");
smg_comms_c_wrappers 107 public_rsa(encr, plain, &pk);
smg_comms_c_wrappers 108 secret_rsa(out, encr, &sk);
smg_comms_c_wrappers 109
smg_comms_c_wrappers 110 if (mpi_cmp(out, plain) != 0)
smg_comms_c_wrappers 111 printf("FAIL: test_gen_rsa encr/decr failed.\n");
smg_comms_c_wrappers 112 else
smg_comms_c_wrappers 113 printf("PASS: test_gen_rsa encr/decr passed.\n");
smg_comms_c_wrappers 114
smg_comms_c_wrappers 115 //tidy up
smg_comms_c_wrappers 116 mpi_free(sk.n);
smg_comms_c_wrappers 117 mpi_free(sk.e);
smg_comms_c_wrappers 118 mpi_free(sk.d);
smg_comms_c_wrappers 119 mpi_free(sk.p);
smg_comms_c_wrappers 120 mpi_free(sk.q);
smg_comms_c_wrappers 121 mpi_free(sk.u);
smg_comms_c_wrappers 122
smg_comms_c_wrappers 123 mpi_free(pk.n);
smg_comms_c_wrappers 124 mpi_free(pk.e);
smg_comms_c_wrappers 125
smg_comms_c_wrappers 126 mpi_free(plain);
smg_comms_c_wrappers 127 mpi_free(encr);
smg_comms_c_wrappers 128 mpi_free(out);
smg_comms_c_wrappers 129 }
smg_comms_c_wrappers 130
smg_comms_c_wrappers 131 void test_rsa_octets() {
smg_comms_c_wrappers 132 int noctets = 512;
smg_comms_c_wrappers 133 RSA_public_key pk;
smg_comms_c_wrappers 134 pk.n = mpi_alloc(0);
smg_comms_c_wrappers 135 pk.e = mpi_alloc(0);
smg_comms_c_wrappers 136
smg_comms_c_wrappers 137 RSA_secret_key sk;
smg_comms_c_wrappers 138 sk.n = mpi_alloc(0);
smg_comms_c_wrappers 139 sk.e = mpi_alloc(0);
smg_comms_c_wrappers 140 sk.d = mpi_alloc(0);
smg_comms_c_wrappers 141 sk.p = mpi_alloc(0);
smg_comms_c_wrappers 142 sk.q = mpi_alloc(0);
smg_comms_c_wrappers 143 sk.u = mpi_alloc(0);
smg_comms_c_wrappers 144
smg_comms_c_wrappers 145 //key pair previously generated with EuCrypt
smg_comms_c_wrappers 146 mpi_fromstr(sk.n, "0x\
smg_comms_rsa_oaep 147 C6579F8646180EED0DC1F02E0DDD2B43EABB3F702D79D9928E2CDA5E1D42DF5D9ED777\
smg_comms_rsa_oaep 148 3F80B1F8D9B0DB7D4D00F55647640D70768F63D3CED56A39C681D08D6191F318BB79DC\
smg_comms_rsa_oaep 149 969B470A7364D53335C8318EF35E39D5DF706AB6F2393C6DD2128C142DBAB1806EB35E\
smg_comms_rsa_oaep 150 26C908F0A48419313D2D0F33DD430655DBFEC722899EC21C238E8DB7003430BBC39BAD\
smg_comms_rsa_oaep 151 990F9887F6B03E1344F537EC97389B78DBC656718ACD7B0FDC13DD24534F417BC7A18F\
smg_comms_rsa_oaep 152 077A0C4227354CEA19670331B6CAA3DFC17BBA7E70C14510D9EB3B63F3014994EC87BD\
smg_comms_rsa_oaep 153 23E868C0AE6E9EC55027577F62C0280B2D7DD1135001844923E5455C4566E066B3FDE9\
smg_comms_rsa_oaep 154 68C6BC4DC672F229FCE366440403D7A4F4A8BFBA5679B7D0844BA1231277D13A77C9E2\
smg_comms_rsa_oaep 155 B5A1CB138C1B7AB5B4D4832448723A3DE70ED2E86D5FC5174F949A02DE8E404304BEB9\
smg_comms_rsa_oaep 156 5F9BF40F3AA3CA15622D2776294BE7E19233406FF563CB8C25A1CB5AADBC1899DA3F2A\
smg_comms_rsa_oaep 157 E38533931FE032EE3232C2CD4F219FADF95B91635C0762A476A4DE5013F4384093F0FB\
smg_comms_rsa_oaep 158 715028D97F93B2E6F057B99EE344D83ADF2686FD5C9C793928BEF3182E568C4339C36C\
smg_comms_rsa_oaep 159 744C8E9CA7D4B9A16AA039CBF6F38CC97B12D87644E94C9DBD6BC93A93A03ED61ECC58\
smg_comms_rsa_oaep 160 74586E3A310E958F858735E30019D345C62E5127B80652C8A970A14B31F03B3A157CD5");
smg_comms_c_wrappers 161
smg_comms_c_wrappers 162 mpi_fromstr(sk.e, "0x\
smg_comms_rsa_oaep 163 F74D78E382FC19B064411C6C20E0FDB2985F843007A54C7D8400BB459468624126E7D1\
smg_comms_rsa_oaep 164 75F397E55C57AF25858EAE2D2952FB7998C119A6103606733EB5E1D27FCA1FACF14ADE\
smg_comms_rsa_oaep 165 94101D383D1B25DA511805569BC344EAD384EDBF3F3A541B34887FE199D99D7F62E6E9\
smg_comms_rsa_oaep 166 D516F88D6F5AD3E020DF04D402A02CC628A0064362FE8516CF7CD6040E9521407AB90E\
smg_comms_rsa_oaep 167 E6B5AFFF9EA9EBB16A7D3407CE81FD3844F519880556AB94AB349C1F3BBB6FDB4C4B37\
smg_comms_rsa_oaep 168 7FE4C091EBDC2C3A1BD3AA56382D8D80E7742B5C751008FD6ECDD2EC3B2E3B6C566F69\
smg_comms_rsa_oaep 169 8ED672000B403766DD63C3ACBDE16A14FB02E83A2EB6AA018BFC0020401E790DEE24E9");
smg_comms_c_wrappers 170
smg_comms_c_wrappers 171 mpi_fromstr(sk.d, "0x\
smg_comms_rsa_oaep 172 698DA05DA25B230211EEF0CBA12083A1457B749A11937AC9993859F69A3BF38D575E51\
smg_comms_rsa_oaep 173 66AF2EC88D77F1DF04E68AEA358EACF7659FD4722A4F5A1C8BA7676DA97A9FBA754511\
smg_comms_rsa_oaep 174 52F8F68887D3451A9CCFFFE9EB80979786E37495B17687A6212F77FA616E4C0CD8A8EB\
smg_comms_rsa_oaep 175 7AEB88EA6CCABB7F3E854FB94B35394A09F95F0D6F997947E865CC0606F437C30FE8C4\
smg_comms_rsa_oaep 176 8D96FBF5E2F52807BC9E9ED7BBEB23D5C45EDDCD16FE2BF410A9A1E5EF879E71C0D41F\
smg_comms_rsa_oaep 177 AE270C0C5D442860103F8C3944E802F33DB38432F11F763A7AF593656108E4A98A44A8\
smg_comms_rsa_oaep 178 549913CE5DCEC1A6500F280E3190991B2B938561CFACD8BC5183AAC9A4914BFE52C3BE\
smg_comms_rsa_oaep 179 39BB83688E1DE52479107EF8E087DCDB409432FC954C6349407E81DDFB11AE92BABB32\
smg_comms_rsa_oaep 180 A31868597958C9C76E0B4156F380955F0E09C1F3B98BB4CDD59E1B5C7D8CC2AA7491B0\
smg_comms_rsa_oaep 181 D319D219CF459A527CE1AA2729DEC53269653BF0ED3E0253F4451168437E3B069E4835\
smg_comms_rsa_oaep 182 0CA4C3EC82134E87135624C768D1330B0D70C6E447FD9945BF06FCB91AA334C0FD8EEF\
smg_comms_rsa_oaep 183 1ADBC15928B3DB62077B537F7E9F468CC95CD5AAFEAE1F760A863B48D07B163F670E2E\
smg_comms_rsa_oaep 184 5B550BB3E960230BA9FDAED9903AE2E669A7F3C4D1F1E25B8E8EDB8CC6E6FD2164E66F\
smg_comms_rsa_oaep 185 4E64ED77BEF1EC9E6CEA5624FD84C0680248746DC1C8187145F3CD2411659DAEAD11D");
smg_comms_c_wrappers 186
smg_comms_c_wrappers 187 mpi_fromstr(sk.p, "0x\
smg_comms_rsa_oaep 188 CDD6F7673A501FB24C44D56CA1D434F6CB3334E193E02F8E906241906BCB7412DD2159\
smg_comms_rsa_oaep 189 825B24C22002F373E647C2DA62A854F3841C00FD5985D03227CA9B54A69380BA9D63BE\
smg_comms_rsa_oaep 190 738BDF9E65C247E43E1220EEDD9281DCA78B32A4E1B786B7697ED0C3195D5AF2990881\
smg_comms_rsa_oaep 191 B11D6FC9EC9F940067B2DEA2A516FAA5F269C98F0B67628A6D2708515A4A58041AA17A\
smg_comms_rsa_oaep 192 93E4C4DD95C85BC38351DDA1DCF3DFD91C505B22383132649CF9F9233852C7207075BC\
smg_comms_rsa_oaep 193 F43C71038F043F1EC53E9787FB051B7927D020903233C16897B993C8089D8464451F08\
smg_comms_rsa_oaep 194 6E756CF20E46CE6ED4A6AC5C327A0AAFBECBAAFD177969E7C952C76A4F4E7C85BF7F63");
smg_comms_c_wrappers 195
smg_comms_c_wrappers 196 mpi_fromstr(sk.q, "0x\
smg_comms_rsa_oaep 197 F6ACF0790A250802C8D45DAC549CDBEF7806D5877A5DF0069136A458FAC4F0B0858060\
smg_comms_rsa_oaep 198 A873DA6355A965A064A0BC1BBB874872CD7ED89674AD407533041E74BCA317EC73597D\
smg_comms_rsa_oaep 199 335115523F61A05071E5ED81EE2A05331F65D4DC7A25AD7938B124CF03F49154B6693F\
smg_comms_rsa_oaep 200 B0B598B33ABDEF85C599A57A9B7347EAFF82638E1CBC28FCDFFF1FF04A18C2DBF39383\
smg_comms_rsa_oaep 201 95C2F8D1782B43D3A25EF7633B5DDAC89EFD3BAA64D976425A0891E00B876E9DE9FE4B\
smg_comms_rsa_oaep 202 6492B0EA8DFC7C8DEEC61721356EC816295B1BD9CD9DA3E30D2D90DC9CB3987F4BE042\
smg_comms_rsa_oaep 203 104900E036F3044A016749EF910CCFB9F377A90849B4CCCF4471A74E67EF6C814C9467");
smg_comms_c_wrappers 204
smg_comms_c_wrappers 205 mpi_fromstr(sk.u, "0x\
smg_comms_rsa_oaep 206 854B89ED10F52258D00D6B3FA7F1FD22752804668F51FF7806DB82E22CB8B3AA8448D9\
smg_comms_rsa_oaep 207 B8E9DB14D31A36AEC2BCFA89E341B7334D494E97ED8051244136192233332C4612D963\
smg_comms_rsa_oaep 208 E7B6AF2535FDB7FE97E28DDFEBDFB3E1AFC29D05DBDF37106A817D3AB1864C7F7F2479\
smg_comms_rsa_oaep 209 82897EDA6A92BED47D9C68305CD170C7301ACEB05F8A6382E73CC7614B2D8D758669B3\
smg_comms_rsa_oaep 210 A99AB64114809254B0BE21F40341A5B48B9B032603B14875B87EB5E16603FD16552E14\
smg_comms_rsa_oaep 211 6A0FC6964958DFC25AA9FFCCD1ED1F4DEAF9FBAA0D7357F5FF0803FEB9BA78E74AC6B3\
smg_comms_rsa_oaep 212 070F417CEC6CFC7A3CF1E305FC7B76B7ED71893999AF797B2EBDE41FE90F076CCEDBFB");
smg_comms_c_wrappers 213
smg_comms_c_wrappers 214 // copy the public key components
smg_comms_c_wrappers 215 pk.n = mpi_copy( sk.n );
smg_comms_c_wrappers 216 pk.e = mpi_copy( sk.e );
smg_comms_c_wrappers 217
smg_comms_c_wrappers 218 // some plain text message
smg_comms_c_wrappers 219 MPI plain = mpi_alloc(0);
smg_comms_c_wrappers 220 mpi_fromstr(plain, "0x\
smg_comms_c_wrappers 221 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
smg_comms_c_wrappers 222 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
smg_comms_c_wrappers 223 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
smg_comms_c_wrappers 224 28097EF92F6E78CAE0CB97");
smg_comms_c_wrappers 225
smg_comms_c_wrappers 226 // expected encrypted MPI (via rsa.c directly)
smg_comms_c_wrappers 227 MPI encr = mpi_alloc(0);
smg_comms_c_wrappers 228 public_rsa( encr, plain, &pk);
smg_comms_c_wrappers 229 MPI decr = mpi_alloc(0);
smg_comms_c_wrappers 230 secret_rsa( decr, encr, &sk);
smg_comms_c_wrappers 231 if (mpi_cmp(decr, plain) != 0)
smg_comms_c_wrappers 232 printf("FAIL: decrypted != plain in test_rsa_octets (MPI call)\n");
smg_comms_c_wrappers 233
smg_comms_c_wrappers 234 //allocate the char arrays for _octets rsa
smg_comms_c_wrappers 235 int len_n = noctets;
smg_comms_c_wrappers 236 int len_e = len_n;
smg_comms_c_wrappers 237 int len_d = len_n;
smg_comms_c_wrappers 238 int len_p = noctets / 2;
smg_comms_c_wrappers 239 int len_q = len_p;
smg_comms_c_wrappers 240 int len_u = len_p;
smg_comms_c_wrappers 241 char n[noctets];
smg_comms_c_wrappers 242 char e[noctets];
smg_comms_c_wrappers 243 char d[noctets];
smg_comms_c_wrappers 244 char p[noctets / 2];
smg_comms_c_wrappers 245 char q[noctets / 2];
smg_comms_c_wrappers 246 char u[noctets / 2];
smg_comms_c_wrappers 247
smg_comms_c_wrappers 248 //copy the key components into char arrays
smg_comms_c_wrappers 249 mpi_to_octets(n, &len_n, sk.n);
smg_comms_c_wrappers 250 mpi_to_octets(e, &len_e, sk.e);
smg_comms_c_wrappers 251 mpi_to_octets(d, &len_d, sk.d);
smg_comms_c_wrappers 252 mpi_to_octets(p, &len_p, sk.p);
smg_comms_c_wrappers 253 mpi_to_octets(q, &len_q, sk.q);
smg_comms_c_wrappers 254 mpi_to_octets(u, &len_u, sk.u);
smg_comms_c_wrappers 255
smg_comms_c_wrappers 256 //call _octets rsa and check results
smg_comms_c_wrappers 257 int len_encr = noctets;
smg_comms_c_wrappers 258 int len_decr = noctets;
smg_comms_c_wrappers 259 int len_plain = noctets;
smg_comms_c_wrappers 260 char plain_o[noctets];
smg_comms_c_wrappers 261 char encr_o[noctets];
smg_comms_c_wrappers 262 char expected_encr_o[noctets];
smg_comms_c_wrappers 263 char decr_o[noctets];
smg_comms_c_wrappers 264 char expected_decr_o[noctets];
smg_comms_c_wrappers 265
smg_comms_c_wrappers 266 mpi_to_octets(plain_o, &len_plain, plain);
smg_comms_c_wrappers 267 mpi_to_octets(expected_encr_o, &len_encr, encr);
smg_comms_c_wrappers 268 mpi_to_octets(expected_decr_o, &len_decr, decr);
smg_comms_c_wrappers 269 len_decr = noctets;
smg_comms_c_wrappers 270
smg_comms_c_wrappers 271 int len;
smg_comms_c_wrappers 272 len = public_rsa_octets( encr_o, len_encr, plain_o, len_plain,
smg_comms_c_wrappers 273 n, len_n, e, len_e);
smg_comms_c_wrappers 274 if (len != len_encr)
smg_comms_c_wrappers 275 printf("FAIL: actual len of encr is %d; expected %d\n", len, len_encr);
smg_comms_c_wrappers 276 else
smg_comms_c_wrappers 277 printf("PASS: actual len of encr matches expected: %d\n", len);
smg_comms_c_wrappers 278 int errors= 0;
smg_comms_c_wrappers 279 int i;
smg_comms_c_wrappers 280 for (i=0;i<len;i++)
smg_comms_c_wrappers 281 if (encr_o[i] != expected_encr_o[i])
smg_comms_c_wrappers 282 errors = errors + 1;
smg_comms_c_wrappers 283 if (errors>0)
smg_comms_c_wrappers 284 printf("FAIL: found %d errors in public_rsa_octets output\n", errors);
smg_comms_c_wrappers 285 else
smg_comms_c_wrappers 286 printf("PASS: no errors found in public_rsa_octets output\n");
smg_comms_c_wrappers 287
smg_comms_c_wrappers 288 len_encr = len;
smg_comms_c_wrappers 289 len = private_rsa_octets( decr_o, len_decr, encr_o, len_encr,
smg_comms_c_wrappers 290 n, len_n, e, len_e,
smg_comms_c_wrappers 291 d, len_d, p, len_p,
smg_comms_c_wrappers 292 q, len_q, u, len_u);
smg_comms_c_wrappers 293 if (len != len_plain)
smg_comms_c_wrappers 294 printf("FAIL: actual len of decr is %d; expected %d\n", len, len_plain);
smg_comms_c_wrappers 295 else
smg_comms_c_wrappers 296 printf("PASS: actual len of decr matches expected: %d\n", len);
smg_comms_c_wrappers 297
smg_comms_c_wrappers 298 errors = 0;
smg_comms_c_wrappers 299 for (i=0;i<len;i++)
smg_comms_c_wrappers 300 if (decr_o[i] != expected_decr_o[i])
smg_comms_c_wrappers 301 errors = errors + 1;
smg_comms_c_wrappers 302 if (errors>0)
smg_comms_c_wrappers 303 printf("FAIL: found %d errors in private_rsa_octets output\n", errors);
smg_comms_c_wrappers 304 else printf("PASS: no errors found in private_rsa_octets_output\n");
smg_comms_c_wrappers 305
smg_comms_c_wrappers 306 //tidy up
smg_comms_c_wrappers 307 mpi_free(sk.n);
smg_comms_c_wrappers 308 mpi_free(sk.e);
smg_comms_c_wrappers 309 mpi_free(sk.d);
smg_comms_c_wrappers 310 mpi_free(sk.p);
smg_comms_c_wrappers 311 mpi_free(sk.q);
smg_comms_c_wrappers 312 mpi_free(sk.u);
smg_comms_c_wrappers 313
smg_comms_c_wrappers 314 mpi_free(pk.n);
smg_comms_c_wrappers 315 mpi_free(pk.e);
smg_comms_c_wrappers 316
smg_comms_c_wrappers 317 mpi_free(plain);
smg_comms_c_wrappers 318 mpi_free(encr);
smg_comms_c_wrappers 319 mpi_free(decr);
smg_comms_c_wrappers 320 }
smg_comms_c_wrappers 321
smg_comms_c_wrappers 322 int main(int ac, char **av) {
smg_comms_c_wrappers 323 if (ac < 2) {
smg_comms_c_wrappers 324 printf("Usage: %s testID\n", av[0]);
smg_comms_c_wrappers 325 return -1;
smg_comms_c_wrappers 326 }
smg_comms_c_wrappers 327
smg_comms_c_wrappers 328 int id = atoi(av[1]);
smg_comms_c_wrappers 329 switch (id) {
smg_comms_c_wrappers 330 case 1:
smg_comms_c_wrappers 331 test_mpi_cmp();
smg_comms_c_wrappers 332 break;
smg_comms_c_wrappers 333 case 2:
smg_comms_c_wrappers 334 test_gen_rsa_octets();
smg_comms_c_wrappers 335 break;
smg_comms_c_wrappers 336 case 3:
smg_comms_c_wrappers 337 test_rsa_octets();
smg_comms_c_wrappers 338 break;
smg_comms_c_wrappers 339 default:
smg_comms_c_wrappers 340 printf("Current test ids:\n");
smg_comms_c_wrappers 341 printf("1 test of mpi_cmp_octets\n");
smg_comms_c_wrappers 342 printf("2 test of gen_rsa_octets (can be very SLOW!)\n");
smg_comms_c_wrappers 343 printf("3 test of rsa_octets (can take a few minutes)\n");
smg_comms_c_wrappers 344 }
smg_comms_c_wrappers 345 return 0;
smg_comms_c_wrappers 346 }