Coverage Report

Created: 2018-08-29 13:53

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