1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use core::{
    convert::{AsMut, AsRef},
    mem,
};

use crate::{error::Error, scalar::Scalar};

pub struct SignatureArray([u8; 6 + 33 + 33], usize);

impl SignatureArray {
    pub fn new(size: usize) -> Self {
        SignatureArray([0u8; 6 + 33 + 33], size)
    }

    pub fn len(&self) -> usize {
        self.1
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl AsRef<[u8]> for SignatureArray {
    fn as_ref(&self) -> &[u8] {
        &self.0[..self.1]
    }
}

impl AsMut<[u8]> for SignatureArray {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.0[..self.1]
    }
}

pub struct Decoder<'a>(&'a [u8], usize);

impl<'a> Decoder<'a> {
    pub fn new(arr: &'a [u8]) -> Self {
        Decoder(arr, 0)
    }

    pub fn remaining_len(&self) -> usize {
        self.0.len() - self.1
    }

    pub fn read(&mut self) -> Result<u8, Error> {
        if self.1 >= self.0.len() {
            Err(Error::InvalidSignature)
        } else {
            let v = self.0[self.1];
            self.1 += 1;
            Ok(v)
        }
    }

    pub fn peek(&self, forward: usize) -> Result<u8, Error> {
        if self.1 + forward >= self.0.len() {
            Err(Error::InvalidSignature)
        } else {
            let v = self.0[self.1 + forward];
            Ok(v)
        }
    }

    pub fn peek_slice(&self, len: usize) -> Result<&[u8], Error> {
        if (len == 0 && self.1 >= self.0.len()) || self.1 + len > self.0.len() {
            Err(Error::InvalidSignature)
        } else {
            let v = &self.0[self.1..(self.1 + len)];
            Ok(v)
        }
    }

    pub fn skip(&mut self, len: usize) -> Result<(), Error> {
        if (len == 0 && self.1 >= self.0.len()) || self.1 + len > self.0.len() {
            Err(Error::InvalidSignature)
        } else {
            self.1 += len;
            Ok(())
        }
    }

    pub fn read_constructed_sequence(&mut self) -> Result<(), Error> {
        let v = self.read()?;
        if v == 0x30 {
            Ok(())
        } else {
            Err(Error::InvalidSignature)
        }
    }

    pub fn read_len(&mut self) -> Result<usize, Error> {
        let b1 = self.read()?;
        if b1 == 0xff {
            return Err(Error::InvalidSignature);
        }

        // Short form
        if b1 & 0x80 == 0 {
            return Ok(b1 as usize);
        }

        // Infinite length is not allowed
        if b1 == 0x80 {
            return Err(Error::InvalidSignature);
        }

        let mut lenleft = (b1 & 0x7f) as usize;
        if lenleft > self.remaining_len() {
            return Err(Error::InvalidSignature);
        }

        if self.peek(0)? == 0 {
            // Not the shortest possible length encoding
            return Err(Error::InvalidSignature);
        }

        if lenleft > mem::size_of::<usize>() {
            return Err(Error::InvalidSignature);
        }

        let mut ret = 0;
        while lenleft > 0 {
            ret = (ret << 8) | (self.read()? as usize);
            if ret + lenleft > self.remaining_len() {
                return Err(Error::InvalidSignature);
            }
            lenleft -= 1;
        }

        if ret < 128 {
            // Not the shortest possible length encoding
            return Err(Error::InvalidSignature);
        }

        Ok(ret)
    }

    pub fn read_integer(&mut self) -> Result<Scalar, Error> {
        if self.read()? != 0x02 {
            return Err(Error::InvalidSignature);
        }

        let mut rlen = self.read_len()?;
        if rlen == 0 || rlen > self.remaining_len() {
            return Err(Error::InvalidSignature);
        }

        if self.peek(0)? == 0x00 && rlen > 1 && (self.peek(1)? & 0x80) == 0x00 {
            return Err(Error::InvalidSignature);
        }

        if self.peek(0)? == 0xff && rlen > 1 && (self.peek(1)? & 0x80) == 0x00 {
            return Err(Error::InvalidSignature);
        }

        let mut overflow = false;
        if self.peek(0)? & 0x80 == 0x80 {
            overflow |= true;
        }

        // Skip leading zero bytes
        while rlen > 0 && self.peek(0)? == 0 {
            rlen -= 1;
            self.read()?;
        }

        if rlen > 32 {
            overflow |= true;
        }

        let mut int = Scalar::default();

        if !overflow {
            let mut b32 = [0u8; 32];
            b32[32 - rlen..].copy_from_slice(self.peek_slice(rlen)?);
            self.skip(rlen)?;

            overflow |= bool::from(int.set_b32(&b32));
        }

        if overflow {
            int = Scalar::default();
        }

        Ok(int)
    }

    pub fn read_seq_len_lax(&mut self) -> Result<usize, Error> {
        let mut len = self.read()?;
        if len & 0x80 != 0x00 {
            len -= 0x80;
            if len as usize > self.remaining_len() {
                return Err(Error::InvalidSignature);
            }
            self.skip(len as usize)?;
        }

        Ok(len as usize)
    }

    pub fn read_len_lax(&mut self) -> Result<usize, Error> {
        let mut ret = 0usize;
        let mut len = self.read()?;
        if len & 0x80 != 0x00 {
            len -= 0x80;
            if len as usize > self.remaining_len() {
                return Err(Error::InvalidSignature);
            }
            while len > 0 && self.peek(0)? == 0 {
                self.skip(1)?;
                len -= 1;
            }
            if (len as usize) >= mem::size_of::<usize>() {
                return Err(Error::InvalidSignature);
            }
            while len > 0 {
                ret = (ret << 8) + (self.read()? as usize);
                len -= 1;
            }
        } else {
            ret = len as usize;
        }
        if ret > self.remaining_len() {
            return Err(Error::InvalidSignature);
        }

        Ok(ret)
    }

    pub fn read_integer_lax(&mut self) -> Result<Scalar, Error> {
        // Integer tag byte.
        if self.read()? != 0x02 {
            return Err(Error::InvalidSignature);
        }

        let mut len = self.read_len_lax()?;

        // Ignore leading zeroes.
        while len > 0 && self.peek(0)? == 0 {
            len -= 1;
            self.skip(1)?;
        }

        let mut overflow = false;
        // Copy value
        if len > 32 {
            overflow |= true;
        }

        let mut int = Scalar::default();

        if !overflow {
            let mut b32 = [0u8; 32];
            b32[32 - len..].copy_from_slice(&self.peek_slice(len)?);
            self.skip(len)?;

            overflow |= bool::from(int.set_b32(&b32));
        }

        if overflow {
            int = Scalar::default();
        }

        Ok(int)
    }
}