1 | /* |
2 | Name: rtest.c |
3 | Purpose: Test routines for RSA implementation. |
4 | Author: M. J. Fromberger |
5 | |
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 copy |
9 | of this software and associated documentation files (the "Software"), to deal |
10 | in the Software without restriction, including without limitation the rights |
11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | copies of the Software, and to permit persons to whom the Software is |
13 | furnished to do so, subject to the following conditions: |
14 | |
15 | The above copyright notice and this permission notice shall be included in |
16 | all copies or substantial portions of the Software. |
17 | |
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | 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 THE |
24 | 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 | |
35 | void random_fill(unsigned char *buf, int len); |
36 | void print_buf(unsigned char *buf, int len, int brk, FILE *ofp); |
37 | |
38 | int 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, format: "Usage: %s <bufsize> <msglen>\n" , argv[0]); |
45 | return 1; |
46 | } |
47 | |
48 | srand(seed: (unsigned int)time(NULL)); |
49 | |
50 | if ((buf_len = atoi(nptr: argv[1])) <= 0) { |
51 | fprintf(stderr, format: "Buffer length must be positive, not %d\n" , buf_len); |
52 | return 2; |
53 | } |
54 | if ((msg_len = atoi(nptr: argv[2])) <= 0) { |
55 | fprintf(stderr, format: "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(nmemb: buf_len, size: sizeof(*buf)); |
61 | for (i = 0; i < msg_len; ++i) buf[i] = i + 1; |
62 | |
63 | printf( |
64 | format: "Buffer size: %d bytes\n" |
65 | "Message len: %d bytes\n\n" , |
66 | buf_len, msg_len); |
67 | |
68 | printf(format: "Message:\n" ); |
69 | print_buf(buf, len: msg_len, brk: 16, stdout); |
70 | fputc(c: '\n', stdout); |
71 | |
72 | if ((res = rsa_pkcs1v15_encode(buf, msg_len, buf_len, tag: 2, filler: random_fill)) != |
73 | MP_OK) { |
74 | printf(format: "Error from encoding function: %d\n" , res); |
75 | free(ptr: buf); |
76 | return 1; |
77 | } |
78 | printf(format: "Encoded message:\n" ); |
79 | print_buf(buf, len: buf_len, brk: 16, stdout); |
80 | fputc(c: '\n', stdout); |
81 | |
82 | msg_len = -1; /* make decoder fill this in */ |
83 | if ((res = rsa_pkcs1v15_decode(buf, buf_len, tag: 2, msg_len: &msg_len)) != MP_OK) { |
84 | printf(format: "Error from decoding function: %d\n" , res); |
85 | free(ptr: buf); |
86 | return 1; |
87 | } |
88 | printf(format: "Decoded message (%d bytes):\n" , msg_len); |
89 | print_buf(buf, len: msg_len, brk: 16, stdout); |
90 | fputc(c: '\n', stdout); |
91 | |
92 | free(ptr: buf); |
93 | return 0; |
94 | } |
95 | |
96 | void 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 | |
108 | void print_buf(unsigned char *buf, int len, int brk, FILE *ofp) { |
109 | int i; |
110 | |
111 | for (i = 0; i < len; ++i) { |
112 | fprintf(stream: ofp, format: "%02X" , buf[i]); |
113 | |
114 | if ((i + 1) % brk == 0) |
115 | fputc(c: '\n', stream: ofp); |
116 | else |
117 | fputc(c: ' ', stream: ofp); |
118 | } |
119 | if (i % brk) fputc(c: '\n', stream: ofp); |
120 | } |
121 | |