blob: 8df6cf7a84728f53d0a517a01eb7a8f8c120f8cc [file] [log] [blame]
Kenny Rootdb0850c2013-10-08 12:52:07 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Google Inc. nor the names of its contributors may
12 * be used to endorse or promote products derived from this software
13 * without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 * EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <string.h>
28
29#include "mincrypt/p256.h"
30
31/**
32 * Trims off the leading zero bytes and copy it to a buffer aligning it to the end.
33 */
34static inline int trim_to_p256_bytes(unsigned char dst[P256_NBYTES], unsigned char *src,
35 int src_len) {
36 int dst_offset;
37 while (*src == '\0' && src_len > 0) {
38 src++;
39 src_len--;
40 }
41 if (src_len > P256_NBYTES || src_len < 1) {
42 return 0;
43 }
44 dst_offset = P256_NBYTES - src_len;
45 memset(dst, 0, dst_offset);
46 memcpy(dst + dst_offset, src, src_len);
47 return 1;
48}
49
50/**
51 * Unpacks the ASN.1 DSA signature sequence.
52 */
53int dsa_sig_unpack(unsigned char* sig, int sig_len, p256_int* r_int, p256_int* s_int) {
54 /*
55 * Structure is:
56 * 0x30 0xNN SEQUENCE + s_length
57 * 0x02 0xNN INTEGER + r_length
58 * 0xAA 0xBB .. r_length bytes of "r" (offset 4)
59 * 0x02 0xNN INTEGER + s_length
60 * 0xMM 0xNN .. s_length bytes of "s" (offset 6 + r_len)
61 */
62 int seq_len;
63 unsigned char r_bytes[P256_NBYTES];
64 unsigned char s_bytes[P256_NBYTES];
65 int r_len;
66 int s_len;
67
68 memset(r_bytes, 0, sizeof(r_bytes));
69 memset(s_bytes, 0, sizeof(s_bytes));
70
71 /*
72 * Must have at least:
73 * 2 bytes sequence header and length
74 * 2 bytes R integer header and length
75 * 1 byte of R
76 * 2 bytes S integer header and length
77 * 1 byte of S
78 *
79 * 8 bytes total
80 */
81 if (sig_len < 8 || sig[0] != 0x30 || sig[2] != 0x02) {
82 return 0;
83 }
84
85 seq_len = sig[1];
86 if ((seq_len <= 0) || (seq_len + 2 != sig_len)) {
87 return 0;
88 }
89
90 r_len = sig[3];
91 /*
92 * Must have at least:
93 * 2 bytes for R header and length
94 * 2 bytes S integer header and length
95 * 1 byte of S
96 */
97 if ((r_len < 1) || (r_len > seq_len - 5) || (sig[4 + r_len] != 0x02)) {
98 return 0;
99 }
100 s_len = sig[5 + r_len];
101
102 /**
103 * Must have:
104 * 2 bytes for R header and length
105 * r_len bytes for R
106 * 2 bytes S integer header and length
107 */
108 if ((s_len < 1) || (s_len != seq_len - 4 - r_len)) {
109 return 0;
110 }
111
112 /*
113 * ASN.1 encoded integers are zero-padded for positive integers. Make sure we have
114 * a correctly-sized buffer and that the resulting integer isn't too large.
115 */
116 if (!trim_to_p256_bytes(r_bytes, &sig[4], r_len)
117 || !trim_to_p256_bytes(s_bytes, &sig[6 + r_len], s_len)) {
118 return 0;
119 }
120
121 p256_from_bin(r_bytes, r_int);
122 p256_from_bin(s_bytes, s_int);
123
124 return 1;
125}