Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/rsa/rsa_x931.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2020 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
int RSA_padding_add_X931(unsigned char *to, int tlen,
23
                         const unsigned char *from, int flen)
24
0
{
25
0
    int j;
26
0
    unsigned char *p;
27
28
    /*
29
     * Absolute minimum amount of padding is 1 header nibble, 1 padding
30
     * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
31
     */
32
33
0
    j = tlen - flen - 2;
34
35
0
    if (j < 0) {
36
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
37
0
        return -1;
38
0
    }
39
40
0
    p = (unsigned char *)to;
41
42
    /* If no padding start and end nibbles are in one byte */
43
0
    if (j == 0) {
44
0
        *p++ = 0x6A;
45
0
    } else {
46
0
        *p++ = 0x6B;
47
0
        if (j > 1) {
48
0
            memset(p, 0xBB, j - 1);
49
0
            p += j - 1;
50
0
        }
51
0
        *p++ = 0xBA;
52
0
    }
53
0
    memcpy(p, from, (unsigned int)flen);
54
0
    p += flen;
55
0
    *p = 0xCC;
56
0
    return 1;
57
0
}
58
59
int RSA_padding_check_X931(unsigned char *to, int tlen,
60
                           const unsigned char *from, int flen, int num)
61
0
{
62
0
    int i = 0, j;
63
0
    const unsigned char *p;
64
65
0
    p = from;
66
0
    if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) {
67
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_HEADER);
68
0
        return -1;
69
0
    }
70
71
0
    if (*p++ == 0x6B) {
72
0
        j = flen - 3;
73
0
        for (i = 0; i < j; i++) {
74
0
            unsigned char c = *p++;
75
0
            if (c == 0xBA)
76
0
                break;
77
0
            if (c != 0xBB) {
78
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING);
79
0
                return -1;
80
0
            }
81
0
        }
82
83
0
        j -= i;
84
85
0
        if (i == 0) {
86
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING);
87
0
            return -1;
88
0
        }
89
90
0
    } else {
91
0
        j = flen - 2;
92
0
    }
93
94
0
    if (p[j] != 0xCC) {
95
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
96
0
        return -1;
97
0
    }
98
99
0
    memcpy(to, p, (unsigned int)j);
100
101
0
    return j;
102
0
}
103
104
/* Translate between X931 hash ids and NIDs */
105
106
int RSA_X931_hash_id(int nid)
107
0
{
108
0
    switch (nid) {
109
0
    case NID_sha1:
110
0
        return 0x33;
111
112
0
    case NID_sha256:
113
0
        return 0x34;
114
115
0
    case NID_sha384:
116
0
        return 0x36;
117
118
0
    case NID_sha512:
119
0
        return 0x35;
120
121
0
    }
122
0
    return -1;
123
0
}