void bdPrint(T p, size_t flags) { size_t n; assert(p); n = p->ndigits; if (n == 0) n = 1; if (flags & BD_PRINT_TRIM) /* Trim leading zeroes */ { if (flags & BD_PRINT_NL) /* add newlines */ mpPrintTrimNL(p->digits, n); else mpPrintTrim(p->digits, n); } else { if (flags & BD_PRINT_NL) /* add newlines */ mpPrintNL(p->digits, n); else mpPrint(p->digits, n); } }
int main(void) { DIGIT_T n[MOD_SIZE], e[MOD_SIZE], d[MOD_SIZE]; DIGIT_T s[MOD_SIZE], m[MOD_SIZE], m1[MOD_SIZE], s1[MOD_SIZE]; size_t nbytes; char decimal[MOD_SIZE*4]; /* Data in big-endian byte format:- */ unsigned char nn[] = { 0x0A, 0x66, 0x79, 0x1D, 0xC6, 0x98, 0x81, 0x68, 0xDE, 0x7A, 0xB7, 0x74, 0x19, 0xBB, 0x7F, 0xB0, 0xC0, 0x01, 0xC6, 0x27, 0x10, 0x27, 0x00, 0x75, 0x14, 0x29, 0x42, 0xE1, 0x9A, 0x8D, 0x8C, 0x51, 0xD0, 0x53, 0xB3, 0xE3, 0x78, 0x2A, 0x1D, 0xE5, 0xDC, 0x5A, 0xF4, 0xEB, 0xE9, 0x94, 0x68, 0x17, 0x01, 0x14, 0xA1, 0xDF, 0xE6, 0x7C, 0xDC, 0x9A, 0x9A, 0xF5, 0x5D, 0x65, 0x56, 0x20, 0xBB, 0xAB, }; unsigned char ee[] = { 0x01, 0x00, 0x01 }; unsigned char dd[] = { 0x01, 0x23, 0xC5, 0xB6, 0x1B, 0xA3, 0x6E, 0xDB, 0x1D, 0x36, 0x79, 0x90, 0x41, 0x99, 0xA8, 0x9E, 0xA8, 0x0C, 0x09, 0xB9, 0x12, 0x2E, 0x14, 0x00, 0xC0, 0x9A, 0xDC, 0xF7, 0x78, 0x46, 0x76, 0xD0, 0x1D, 0x23, 0x35, 0x6A, 0x7D, 0x44, 0xD6, 0xBD, 0x8B, 0xD5, 0x0E, 0x94, 0xBF, 0xC7, 0x23, 0xFA, 0x87, 0xD8, 0x86, 0x2B, 0x75, 0x17, 0x76, 0x91, 0xC1, 0x1D, 0x75, 0x76, 0x92, 0xDF, 0x88, 0x81, }; unsigned char mm[] = { 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10, 0xDC, 0xA9, 0xEC, 0xF1, 0xC1, 0x5C, 0x1B, 0xD2, 0x66, 0xAF, 0xF9, 0xC8, 0x79, 0x93, 0x65, 0xCD, }; unsigned char ss[] = { 0x06, 0xDB, 0x36, 0xCB, 0x18, 0xD3, 0x47, 0x5B, 0x9C, 0x01, 0xDB, 0x3C, 0x78, 0x95, 0x28, 0x08, 0x02, 0x79, 0xBB, 0xAE, 0xFF, 0x2B, 0x7D, 0x55, 0x8E, 0xD6, 0x61, 0x59, 0x87, 0xC8, 0x51, 0x86, 0x3F, 0x8A, 0x6C, 0x2C, 0xFF, 0xBC, 0x89, 0xC3, 0xF7, 0x5A, 0x18, 0xD9, 0x6B, 0x12, 0x7C, 0x71, 0x7D, 0x54, 0xD0, 0xD8, 0x04, 0x8D, 0xA8, 0xA0, 0x54, 0x46, 0x26, 0xD1, 0x7A, 0x2A, 0x8F, 0xBE, }; printf("Test BIGDIGITS using 508-bit RSA key from 'Some Examples of the PKCS Standards'\n"); /* Convert bytes to BIGDIGITS */ mpConvFromOctets(n, MOD_SIZE, nn, sizeof(nn)); mpConvFromOctets(e, MOD_SIZE, ee, sizeof(ee)); mpConvFromOctets(d, MOD_SIZE, dd, sizeof(dd)); mpConvFromOctets(m, MOD_SIZE, mm, sizeof(mm)); mpConvFromOctets(s1, MOD_SIZE, ss, sizeof(ss)); printf("n ="); mpPrintNL(n, MOD_SIZE); printf("e ="); mpPrintNL(e, MOD_SIZE); printf("d ="); mpPrintNL(d, MOD_SIZE); printf("m ="); mpPrintNL(m, MOD_SIZE); /* Sign, i.e. Encrypt with private key, s = m^d mod n */ mpModExp(s, m, d, n, MOD_SIZE); printf("s ="); mpPrintNL(s, MOD_SIZE); /* Did we get the same answer as expected? */ if (!mpEqual(s1, s, MOD_SIZE)) printf("<= ERROR - no match\n"); else printf("<= OK\n"); assert(mpEqual(s1, s, MOD_SIZE)); /* Verify, i.e. Decrypt with public key m' = s^e mod n */ mpModExp(m1, s, e, n, MOD_SIZE); printf("m'="); mpPrintNL(m1, MOD_SIZE); /* Check that we got back where we started */ if (!mpEqual(m1, m, MOD_SIZE)) printf("<= ERROR - no match\n"); else printf("<= OK\n"); assert(mpEqual(m1, m, MOD_SIZE)); /* Now convert back to octets (bytes) */ memset(mm, 0, sizeof(mm)); nbytes = mpConvToOctets(m, MOD_SIZE, mm, sizeof(mm)); printf("%d non-zero bytes converted from m:\n", nbytes); pr_bytes(mm, sizeof(mm)); memset(ee, 0, sizeof(ee)); nbytes = mpConvToOctets(e, MOD_SIZE, ee, sizeof(ee)); printf("%d non-zero bytes converted from e:\n", nbytes); pr_bytes(ee, sizeof(ee)); /* Do a conversion to decimal */ nbytes = mpConvToDecimal(e, MOD_SIZE, decimal, sizeof(decimal)); printf("%d non-zero decimal digits converted from e:\n", nbytes); printf("%s\n", decimal); assert(strcmp(decimal, "65537") == 0); printf("OK, successfully completed tests.\n"); return 0; }