diff options
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_pk1.c')
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_pk1.c | 241 |
1 files changed, 122 insertions, 119 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c index 36133b6d12..f5492315cc 100644 --- a/src/lib/libcrypto/rsa/rsa_pk1.c +++ b/src/lib/libcrypto/rsa/rsa_pk1.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: rsa_pk1.c,v 1.7 2014/06/12 15:49:30 deraadt Exp $ */ | 1 | /* $OpenBSD: rsa_pk1.c,v 1.8 2014/07/09 08:20:08 miod Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -62,160 +62,163 @@ | |||
62 | #include <openssl/rsa.h> | 62 | #include <openssl/rsa.h> |
63 | #include <openssl/rand.h> | 63 | #include <openssl/rand.h> |
64 | 64 | ||
65 | int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, | 65 | int |
66 | const unsigned char *from, int flen) | 66 | RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, |
67 | { | 67 | const unsigned char *from, int flen) |
68 | { | ||
68 | int j; | 69 | int j; |
69 | unsigned char *p; | 70 | unsigned char *p; |
70 | 71 | ||
71 | if (flen > (tlen-RSA_PKCS1_PADDING_SIZE)) | 72 | if (flen > (tlen-RSA_PKCS1_PADDING_SIZE)) { |
72 | { | 73 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1, |
73 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | 74 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
74 | return(0); | 75 | return 0; |
75 | } | 76 | } |
76 | 77 | ||
77 | p=(unsigned char *)to; | 78 | p = (unsigned char *)to; |
78 | 79 | ||
79 | *(p++)=0; | 80 | *(p++) = 0; |
80 | *(p++)=1; /* Private Key BT (Block Type) */ | 81 | *(p++) = 1; /* Private Key BT (Block Type) */ |
81 | 82 | ||
82 | /* pad out with 0xff data */ | 83 | /* pad out with 0xff data */ |
83 | j=tlen-3-flen; | 84 | j = tlen - 3 - flen; |
84 | memset(p,0xff,j); | 85 | memset(p, 0xff, j); |
85 | p+=j; | 86 | p += j; |
86 | *(p++)='\0'; | 87 | *(p++) = '\0'; |
87 | memcpy(p,from,(unsigned int)flen); | 88 | memcpy(p, from, (unsigned int)flen); |
88 | return(1); | 89 | return 1; |
89 | } | 90 | } |
90 | 91 | ||
91 | int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, | 92 | int |
92 | const unsigned char *from, int flen, int num) | 93 | RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, |
93 | { | 94 | const unsigned char *from, int flen, int num) |
94 | int i,j; | 95 | { |
96 | int i, j; | ||
95 | const unsigned char *p; | 97 | const unsigned char *p; |
96 | 98 | ||
97 | p=from; | 99 | p = from; |
98 | if ((num != (flen+1)) || (*(p++) != 01)) | 100 | if (num != flen + 1 || *(p++) != 01) { |
99 | { | 101 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
100 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01); | 102 | RSA_R_BLOCK_TYPE_IS_NOT_01); |
101 | return(-1); | 103 | return -1; |
102 | } | 104 | } |
103 | 105 | ||
104 | /* scan over padding data */ | 106 | /* scan over padding data */ |
105 | j=flen-1; /* one for type. */ | 107 | j = flen - 1; /* one for type. */ |
106 | for (i=0; i<j; i++) | 108 | for (i = 0; i < j; i++) { |
107 | { | 109 | if (*p != 0xff) { /* should decrypt to 0xff */ |
108 | if (*p != 0xff) /* should decrypt to 0xff */ | 110 | if (*p == 0) { |
109 | { | 111 | p++; |
110 | if (*p == 0) | 112 | break; |
111 | { p++; break; } | 113 | } else { |
112 | else { | 114 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
113 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT); | 115 | RSA_R_BAD_FIXED_HEADER_DECRYPT); |
114 | return(-1); | 116 | return -1; |
115 | } | ||
116 | } | 117 | } |
117 | p++; | ||
118 | } | 118 | } |
119 | p++; | ||
120 | } | ||
119 | 121 | ||
120 | if (i == j) | 122 | if (i == j) { |
121 | { | 123 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
122 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING); | 124 | RSA_R_NULL_BEFORE_BLOCK_MISSING); |
123 | return(-1); | 125 | return -1; |
124 | } | 126 | } |
125 | 127 | ||
126 | if (i < 8) | 128 | if (i < 8) { |
127 | { | 129 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
128 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT); | 130 | RSA_R_BAD_PAD_BYTE_COUNT); |
129 | return(-1); | 131 | return -1; |
130 | } | 132 | } |
131 | i++; /* Skip over the '\0' */ | 133 | i++; /* Skip over the '\0' */ |
132 | j-=i; | 134 | j -= i; |
133 | if (j > tlen) | 135 | if (j > tlen) { |
134 | { | 136 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, |
135 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE); | 137 | RSA_R_DATA_TOO_LARGE); |
136 | return(-1); | 138 | return -1; |
137 | } | ||
138 | memcpy(to,p,(unsigned int)j); | ||
139 | |||
140 | return(j); | ||
141 | } | 139 | } |
140 | memcpy(to, p, (unsigned int)j); | ||
142 | 141 | ||
143 | int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, | 142 | return j; |
144 | const unsigned char *from, int flen) | 143 | } |
145 | { | 144 | |
145 | int | ||
146 | RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, | ||
147 | const unsigned char *from, int flen) | ||
148 | { | ||
146 | int i,j; | 149 | int i,j; |
147 | unsigned char *p; | 150 | unsigned char *p; |
148 | 151 | ||
149 | if (flen > (tlen-11)) | 152 | if (flen > tlen - 11) { |
150 | { | 153 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2, |
151 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | 154 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
152 | return(0); | 155 | return 0; |
153 | } | 156 | } |
154 | 157 | ||
155 | p=(unsigned char *)to; | 158 | p = (unsigned char *)to; |
156 | 159 | ||
157 | *(p++)=0; | 160 | *(p++) = 0; |
158 | *(p++)=2; /* Public Key BT (Block Type) */ | 161 | *(p++) = 2; /* Public Key BT (Block Type) */ |
159 | 162 | ||
160 | /* pad out with non-zero random data */ | 163 | /* pad out with non-zero random data */ |
161 | j=tlen-3-flen; | 164 | j = tlen - 3 - flen; |
162 | 165 | ||
163 | if (RAND_bytes(p,j) <= 0) | 166 | if (RAND_bytes(p, j) <= 0) |
164 | return(0); | 167 | return 0; |
165 | for (i=0; i<j; i++) | 168 | for (i = 0; i < j; i++) { |
166 | { | ||
167 | if (*p == '\0') | 169 | if (*p == '\0') |
168 | do { | 170 | do { |
169 | if (RAND_bytes(p,1) <= 0) | 171 | if (RAND_bytes(p, 1) <= 0) |
170 | return(0); | 172 | return 0; |
171 | } while (*p == '\0'); | 173 | } while (*p == '\0'); |
172 | p++; | 174 | p++; |
173 | } | 175 | } |
174 | 176 | ||
175 | *(p++)='\0'; | 177 | *(p++) = '\0'; |
176 | 178 | ||
177 | memcpy(p,from,(unsigned int)flen); | 179 | memcpy(p, from, (unsigned int)flen); |
178 | return(1); | 180 | return 1; |
179 | } | 181 | } |
180 | 182 | ||
181 | int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, | 183 | int |
182 | const unsigned char *from, int flen, int num) | 184 | RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, |
183 | { | 185 | const unsigned char *from, int flen, int num) |
184 | int i,j; | 186 | { |
187 | int i, j; | ||
185 | const unsigned char *p; | 188 | const unsigned char *p; |
186 | 189 | ||
187 | p=from; | 190 | p = from; |
188 | if ((num != (flen+1)) || (*(p++) != 02)) | 191 | if (num != flen + 1 || *(p++) != 02) { |
189 | { | 192 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, |
190 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02); | 193 | RSA_R_BLOCK_TYPE_IS_NOT_02); |
191 | return(-1); | 194 | return -1; |
192 | } | 195 | } |
193 | 196 | ||
194 | /* scan over padding data */ | 197 | /* scan over padding data */ |
195 | j=flen-1; /* one for type. */ | 198 | j = flen - 1; /* one for type. */ |
196 | for (i=0; i<j; i++) | 199 | for (i = 0; i < j; i++) |
197 | if (*(p++) == 0) break; | 200 | if (*(p++) == 0) |
198 | 201 | break; | |
199 | if (i == j) | 202 | |
200 | { | 203 | if (i == j) { |
201 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING); | 204 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, |
202 | return(-1); | 205 | RSA_R_NULL_BEFORE_BLOCK_MISSING); |
203 | } | 206 | return -1; |
207 | } | ||
204 | 208 | ||
205 | if (i < 8) | 209 | if (i < 8) { |
206 | { | 210 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, |
207 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT); | 211 | RSA_R_BAD_PAD_BYTE_COUNT); |
208 | return(-1); | 212 | return -1; |
209 | } | 213 | } |
210 | i++; /* Skip over the '\0' */ | 214 | i++; /* Skip over the '\0' */ |
211 | j-=i; | 215 | j -= i; |
212 | if (j > tlen) | 216 | if (j > tlen) { |
213 | { | 217 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, |
214 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE); | 218 | RSA_R_DATA_TOO_LARGE); |
215 | return(-1); | 219 | return -1; |
216 | } | ||
217 | memcpy(to,p,(unsigned int)j); | ||
218 | |||
219 | return(j); | ||
220 | } | 220 | } |
221 | memcpy(to, p, (unsigned int)j); | ||
221 | 222 | ||
223 | return j; | ||
224 | } | ||