Branch data Line data Source code
1 : : /* rsa_x931.c */
2 : : /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 : : * project 2005.
4 : : */
5 : : /* ====================================================================
6 : : * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 : : *
8 : : * Redistribution and use in source and binary forms, with or without
9 : : * modification, are permitted provided that the following conditions
10 : : * are met:
11 : : *
12 : : * 1. Redistributions of source code must retain the above copyright
13 : : * notice, this list of conditions and the following disclaimer.
14 : : *
15 : : * 2. Redistributions in binary form must reproduce the above copyright
16 : : * notice, this list of conditions and the following disclaimer in
17 : : * the documentation and/or other materials provided with the
18 : : * distribution.
19 : : *
20 : : * 3. All advertising materials mentioning features or use of this
21 : : * software must display the following acknowledgment:
22 : : * "This product includes software developed by the OpenSSL Project
23 : : * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 : : *
25 : : * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 : : * endorse or promote products derived from this software without
27 : : * prior written permission. For written permission, please contact
28 : : * licensing@OpenSSL.org.
29 : : *
30 : : * 5. Products derived from this software may not be called "OpenSSL"
31 : : * nor may "OpenSSL" appear in their names without prior written
32 : : * permission of the OpenSSL Project.
33 : : *
34 : : * 6. Redistributions of any form whatsoever must retain the following
35 : : * acknowledgment:
36 : : * "This product includes software developed by the OpenSSL Project
37 : : * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 : : *
39 : : * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 : : * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 : : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 : : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 : : * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 : : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 : : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 : : * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 : : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 : : * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 : : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 : : * OF THE POSSIBILITY OF SUCH DAMAGE.
51 : : * ====================================================================
52 : : *
53 : : * This product includes cryptographic software written by Eric Young
54 : : * (eay@cryptsoft.com). This product includes software written by Tim
55 : : * Hudson (tjh@cryptsoft.com).
56 : : *
57 : : */
58 : :
59 : : #define OPENSSL_FIPSAPI
60 : :
61 : : #include <stdio.h>
62 : : #include "cryptlib.h"
63 : : #include <openssl/bn.h>
64 : : #include <openssl/rsa.h>
65 : : #include <openssl/rand.h>
66 : : #include <openssl/objects.h>
67 : :
68 : 0 : int RSA_padding_add_X931(unsigned char *to, int tlen,
69 : : const unsigned char *from, int flen)
70 : : {
71 : : int j;
72 : : unsigned char *p;
73 : :
74 : : /* Absolute minimum amount of padding is 1 header nibble, 1 padding
75 : : * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
76 : : */
77 : :
78 : 0 : j = tlen - flen - 2;
79 : :
80 [ # # ]: 0 : if (j < 0)
81 : : {
82 : 0 : RSAerr(RSA_F_RSA_PADDING_ADD_X931,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
83 : 0 : return -1;
84 : : }
85 : :
86 : 0 : p=(unsigned char *)to;
87 : :
88 : : /* If no padding start and end nibbles are in one byte */
89 [ # # ]: 0 : if (j == 0)
90 : 0 : *p++ = 0x6A;
91 : : else
92 : : {
93 : 0 : *p++ = 0x6B;
94 [ # # ]: 0 : if (j > 1)
95 : : {
96 : 0 : memset(p, 0xBB, j - 1);
97 : 0 : p += j - 1;
98 : : }
99 : 0 : *p++ = 0xBA;
100 : : }
101 : 0 : memcpy(p,from,(unsigned int)flen);
102 : 0 : p += flen;
103 : 0 : *p = 0xCC;
104 : 0 : return(1);
105 : : }
106 : :
107 : 0 : int RSA_padding_check_X931(unsigned char *to, int tlen,
108 : : const unsigned char *from, int flen, int num)
109 : : {
110 : 0 : int i = 0,j;
111 : : const unsigned char *p;
112 : :
113 : 0 : p=from;
114 [ # # ][ # # ]: 0 : if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B)))
115 : : {
116 : 0 : RSAerr(RSA_F_RSA_PADDING_CHECK_X931,RSA_R_INVALID_HEADER);
117 : 0 : return -1;
118 : : }
119 : :
120 [ # # ]: 0 : if (*p++ == 0x6B)
121 : : {
122 : 0 : j=flen-3;
123 [ # # ]: 0 : for (i = 0; i < j; i++)
124 : : {
125 : 0 : unsigned char c = *p++;
126 [ # # ]: 0 : if (c == 0xBA)
127 : : break;
128 [ # # ]: 0 : if (c != 0xBB)
129 : : {
130 : 0 : RSAerr(RSA_F_RSA_PADDING_CHECK_X931,
131 : : RSA_R_INVALID_PADDING);
132 : 0 : return -1;
133 : : }
134 : : }
135 : :
136 : 0 : j -= i;
137 : :
138 [ # # ]: 0 : if (i == 0)
139 : : {
140 : 0 : RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
141 : 0 : return -1;
142 : : }
143 : :
144 : : }
145 : 0 : else j = flen - 2;
146 : :
147 [ # # ]: 0 : if (p[j] != 0xCC)
148 : : {
149 : 0 : RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER);
150 : 0 : return -1;
151 : : }
152 : :
153 : 0 : memcpy(to,p,(unsigned int)j);
154 : :
155 : 0 : return(j);
156 : : }
157 : :
158 : : /* Translate between X931 hash ids and NIDs */
159 : :
160 : 0 : int RSA_X931_hash_id(int nid)
161 : : {
162 [ # # # # : 0 : switch (nid)
# ]
163 : : {
164 : : case NID_sha1:
165 : : return 0x33;
166 : :
167 : : case NID_sha256:
168 : 0 : return 0x34;
169 : :
170 : : case NID_sha384:
171 : 0 : return 0x36;
172 : :
173 : : case NID_sha512:
174 : 0 : return 0x35;
175 : :
176 : : }
177 : 0 : return -1;
178 : : }
179 : :
|