121 lines · c
1/*2 Name: rtest.c3 Purpose: Test routines for RSA implementation.4 Author: M. J. Fromberger5 6 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.7 8 Permission is hereby granted, free of charge, to any person obtaining a copy9 of this software and associated documentation files (the "Software"), to deal10 in the Software without restriction, including without limitation the rights11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell12 copies of the Software, and to permit persons to whom the Software is13 furnished to do so, subject to the following conditions:14 15 The above copyright notice and this permission notice shall be included in16 all copies or substantial portions of the Software.17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE24 SOFTWARE.25 */26 27#include <limits.h>28#include <stdio.h>29#include <stdlib.h>30#include <string.h>31#include <time.h>32 33#include "rsamath.h"34 35void random_fill(unsigned char *buf, int len);36void print_buf(unsigned char *buf, int len, int brk, FILE *ofp);37 38int main(int argc, char *argv[]) {39 int buf_len, msg_len, i;40 unsigned char *buf;41 mp_result res;42 43 if (argc < 3) {44 fprintf(stderr, "Usage: %s <bufsize> <msglen>\n", argv[0]);45 return 1;46 }47 48 srand((unsigned int)time(NULL));49 50 if ((buf_len = atoi(argv[1])) <= 0) {51 fprintf(stderr, "Buffer length must be positive, not %d\n", buf_len);52 return 2;53 }54 if ((msg_len = atoi(argv[2])) <= 0) {55 fprintf(stderr, "Message length must be positive, not %d\n", msg_len);56 return 2;57 }58 if (msg_len > buf_len) msg_len = buf_len;59 60 buf = calloc(buf_len, sizeof(*buf));61 for (i = 0; i < msg_len; ++i) buf[i] = i + 1;62 63 printf(64 "Buffer size: %d bytes\n"65 "Message len: %d bytes\n\n",66 buf_len, msg_len);67 68 printf("Message:\n");69 print_buf(buf, msg_len, 16, stdout);70 fputc('\n', stdout);71 72 if ((res = rsa_pkcs1v15_encode(buf, msg_len, buf_len, 2, random_fill)) !=73 MP_OK) {74 printf("Error from encoding function: %d\n", res);75 free(buf);76 return 1;77 }78 printf("Encoded message:\n");79 print_buf(buf, buf_len, 16, stdout);80 fputc('\n', stdout);81 82 msg_len = -1; /* make decoder fill this in */83 if ((res = rsa_pkcs1v15_decode(buf, buf_len, 2, &msg_len)) != MP_OK) {84 printf("Error from decoding function: %d\n", res);85 free(buf);86 return 1;87 }88 printf("Decoded message (%d bytes):\n", msg_len);89 print_buf(buf, msg_len, 16, stdout);90 fputc('\n', stdout);91 92 free(buf);93 return 0;94}95 96void random_fill(unsigned char *buf, int len) {97 int i;98 99 for (i = 0; i < len; ++i) {100 unsigned char c = 0;101 102 while (c == 0) c = (unsigned char)rand();103 104 buf[i] = c;105 }106}107 108void print_buf(unsigned char *buf, int len, int brk, FILE *ofp) {109 int i;110 111 for (i = 0; i < len; ++i) {112 fprintf(ofp, "%02X", buf[i]);113 114 if ((i + 1) % brk == 0)115 fputc('\n', ofp);116 else117 fputc(' ', ofp);118 }119 if (i % brk) fputc('\n', ofp);120}121