Coverage Report

Created: 2024-11-21 07:03

/src/openssl/crypto/rsa/rsa_x931.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/bn.h>
19
#include <openssl/rsa.h>
20
#include <openssl/objects.h>
21
22
/*
23
 * X9.31 Embeds the hash inside the following data structure
24
 *
25
 * header (4 bits = 0x6)
26
 * padding (consisting of zero or more 4 bit values of a sequence of 0xB,
27
 *          ending with the terminator 0xA)
28
 * hash(Msg) hash of a message (the output size is related to the hash function)
29
 * trailer (consists of 2 bytes)
30
 *         The 1st byte is related to a part number for a hash algorithm
31
 *         (See RSA_X931_hash_id()), followed by the fixed value 0xCC
32
 *
33
 * The RSA modulus size n (which for X9.31 is 1024 + 256*s) is the size of the data
34
 * structure, which determines the padding size.
35
 * i.e. len(padding) = n - len(header) - len(hash) - len(trailer)
36
 *
37
 * Params:
38
 *     to The output buffer to write the data structure to.
39
 *     tolen The size of 'to' in bytes (it is the size of the n)
40
 *     from The input hash followed by the 1st byte of the trailer.
41
 *     flen The size of the input hash + 1 (trailer byte)
42
 */
43
int RSA_padding_add_X931(unsigned char *to, int tlen,
44
                         const unsigned char *from, int flen)
45
0
{
46
0
    int j;
47
0
    unsigned char *p;
48
49
    /*
50
     * We need at least 1 byte for header + padding (0x6A)
51
     * And 2 trailer bytes (but we subtract 1 since flen includes 1 trailer byte)
52
     */
53
0
    j = tlen - flen - 2;
54
55
0
    if (j < 0) {
56
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
57
0
        return -1;
58
0
    }
59
60
0
    p = (unsigned char *)to;
61
62
    /* If no padding start and end nibbles are in one byte */
63
0
    if (j == 0) {
64
0
        *p++ = 0x6A;
65
0
    } else {
66
0
        *p++ = 0x6B;
67
0
        if (j > 1) {
68
0
            memset(p, 0xBB, j - 1);
69
0
            p += j - 1;
70
0
        }
71
0
        *p++ = 0xBA;
72
0
    }
73
0
    memcpy(p, from, (unsigned int)flen);
74
0
    p += flen;
75
0
    *p = 0xCC;
76
0
    return 1;
77
0
}
78
79
int RSA_padding_check_X931(unsigned char *to, int tlen,
80
                           const unsigned char *from, int flen, int num)
81
0
{
82
0
    int i = 0, j;
83
0
    const unsigned char *p;
84
85
0
    p = from;
86
0
    if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) {
87
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_HEADER);
88
0
        return -1;
89
0
    }
90
91
0
    if (*p++ == 0x6B) {
92
0
        j = flen - 3;
93
0
        for (i = 0; i < j; i++) {
94
0
            unsigned char c = *p++;
95
0
            if (c == 0xBA)
96
0
                break;
97
0
            if (c != 0xBB) {
98
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING);
99
0
                return -1;
100
0
            }
101
0
        }
102
103
0
        j -= i;
104
105
0
        if (i == 0) {
106
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING);
107
0
            return -1;
108
0
        }
109
110
0
    } else {
111
0
        j = flen - 2;
112
0
    }
113
114
0
    if (p[j] != 0xCC) {
115
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
116
0
        return -1;
117
0
    }
118
119
0
    memcpy(to, p, (unsigned int)j);
120
121
0
    return j;
122
0
}
123
124
/*
125
 * Translate between X9.31 hash ids and NIDs
126
 * The returned values relate to ISO/IEC 10118 part numbers which consist of
127
 * a hash algorithm and hash number. The returned values are used as the
128
 * first byte of the 'trailer'.
129
 */
130
131
int RSA_X931_hash_id(int nid)
132
0
{
133
0
    switch (nid) {
134
0
    case NID_sha1:
135
0
        return 0x33;
136
137
0
    case NID_sha256:
138
0
        return 0x34;
139
140
0
    case NID_sha384:
141
0
        return 0x36;
142
143
0
    case NID_sha512:
144
0
        return 0x35;
145
146
0
    }
147
0
    return -1;
148
0
}