]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
8686c474 | 2 | * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved. |
429168e7 | 3 | * |
2039c421 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
429168e7 DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
429168e7 DSH |
12 | #include <openssl/bn.h> |
13 | #include <openssl/rsa.h> | |
429168e7 DSH |
14 | #include <openssl/objects.h> |
15 | ||
16 | int RSA_padding_add_X931(unsigned char *to, int tlen, | |
0f113f3e MC |
17 | const unsigned char *from, int flen) |
18 | { | |
19 | int j; | |
20 | unsigned char *p; | |
21 | ||
22 | /* | |
23 | * Absolute minimum amount of padding is 1 header nibble, 1 padding | |
24 | * nibble and 2 trailer bytes: but 1 hash if is already in 'from'. | |
25 | */ | |
26 | ||
27 | j = tlen - flen - 2; | |
28 | ||
29 | if (j < 0) { | |
30 | RSAerr(RSA_F_RSA_PADDING_ADD_X931, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | |
31 | return -1; | |
32 | } | |
33 | ||
34 | p = (unsigned char *)to; | |
35 | ||
36 | /* If no padding start and end nibbles are in one byte */ | |
90862ab4 | 37 | if (j == 0) { |
0f113f3e | 38 | *p++ = 0x6A; |
90862ab4 | 39 | } else { |
0f113f3e MC |
40 | *p++ = 0x6B; |
41 | if (j > 1) { | |
42 | memset(p, 0xBB, j - 1); | |
43 | p += j - 1; | |
44 | } | |
45 | *p++ = 0xBA; | |
46 | } | |
47 | memcpy(p, from, (unsigned int)flen); | |
48 | p += flen; | |
49 | *p = 0xCC; | |
8686c474 | 50 | return 1; |
0f113f3e | 51 | } |
429168e7 DSH |
52 | |
53 | int RSA_padding_check_X931(unsigned char *to, int tlen, | |
0f113f3e MC |
54 | const unsigned char *from, int flen, int num) |
55 | { | |
56 | int i = 0, j; | |
57 | const unsigned char *p; | |
58 | ||
59 | p = from; | |
60 | if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) { | |
61 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_HEADER); | |
62 | return -1; | |
63 | } | |
64 | ||
65 | if (*p++ == 0x6B) { | |
66 | j = flen - 3; | |
67 | for (i = 0; i < j; i++) { | |
68 | unsigned char c = *p++; | |
69 | if (c == 0xBA) | |
70 | break; | |
71 | if (c != 0xBB) { | |
72 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING); | |
73 | return -1; | |
74 | } | |
75 | } | |
76 | ||
77 | j -= i; | |
78 | ||
79 | if (i == 0) { | |
80 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING); | |
81 | return -1; | |
82 | } | |
83 | ||
90862ab4 | 84 | } else { |
0f113f3e | 85 | j = flen - 2; |
90862ab4 | 86 | } |
0f113f3e MC |
87 | |
88 | if (p[j] != 0xCC) { | |
89 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER); | |
90 | return -1; | |
91 | } | |
92 | ||
93 | memcpy(to, p, (unsigned int)j); | |
94 | ||
8686c474 | 95 | return j; |
0f113f3e | 96 | } |
429168e7 DSH |
97 | |
98 | /* Translate between X931 hash ids and NIDs */ | |
99 | ||
100 | int RSA_X931_hash_id(int nid) | |
0f113f3e MC |
101 | { |
102 | switch (nid) { | |
103 | case NID_sha1: | |
104 | return 0x33; | |
429168e7 | 105 | |
0f113f3e MC |
106 | case NID_sha256: |
107 | return 0x34; | |
429168e7 | 108 | |
0f113f3e MC |
109 | case NID_sha384: |
110 | return 0x36; | |
429168e7 | 111 | |
0f113f3e MC |
112 | case NID_sha512: |
113 | return 0x35; | |
429168e7 | 114 | |
0f113f3e MC |
115 | } |
116 | return -1; | |
117 | } |