]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_x931.c
Remove useless NULL checks
[thirdparty/openssl.git] / crypto / rsa / rsa_x931.c
1 /*
2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/rand.h>
15 #include <openssl/objects.h>
16
17 int RSA_padding_add_X931(unsigned char *to, int tlen,
18 const unsigned char *from, int flen)
19 {
20 int j;
21 unsigned char *p;
22
23 /*
24 * Absolute minimum amount of padding is 1 header nibble, 1 padding
25 * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
26 */
27
28 j = tlen - flen - 2;
29
30 if (j < 0) {
31 RSAerr(RSA_F_RSA_PADDING_ADD_X931, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
32 return -1;
33 }
34
35 p = (unsigned char *)to;
36
37 /* If no padding start and end nibbles are in one byte */
38 if (j == 0)
39 *p++ = 0x6A;
40 else {
41 *p++ = 0x6B;
42 if (j > 1) {
43 memset(p, 0xBB, j - 1);
44 p += j - 1;
45 }
46 *p++ = 0xBA;
47 }
48 memcpy(p, from, (unsigned int)flen);
49 p += flen;
50 *p = 0xCC;
51 return (1);
52 }
53
54 int RSA_padding_check_X931(unsigned char *to, int tlen,
55 const unsigned char *from, int flen, int num)
56 {
57 int i = 0, j;
58 const unsigned char *p;
59
60 p = from;
61 if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) {
62 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_HEADER);
63 return -1;
64 }
65
66 if (*p++ == 0x6B) {
67 j = flen - 3;
68 for (i = 0; i < j; i++) {
69 unsigned char c = *p++;
70 if (c == 0xBA)
71 break;
72 if (c != 0xBB) {
73 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
74 return -1;
75 }
76 }
77
78 j -= i;
79
80 if (i == 0) {
81 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
82 return -1;
83 }
84
85 } else
86 j = flen - 2;
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
95 return (j);
96 }
97
98 /* Translate between X931 hash ids and NIDs */
99
100 int RSA_X931_hash_id(int nid)
101 {
102 switch (nid) {
103 case NID_sha1:
104 return 0x33;
105
106 case NID_sha256:
107 return 0x34;
108
109 case NID_sha384:
110 return 0x36;
111
112 case NID_sha512:
113 return 0x35;
114
115 }
116 return -1;
117 }