1*993229b6Sjkunz /*
2*993229b6Sjkunz * File: AESKey.cpp
3*993229b6Sjkunz *
4*993229b6Sjkunz * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5*993229b6Sjkunz * See included license file for license details.
6*993229b6Sjkunz */
7*993229b6Sjkunz
8*993229b6Sjkunz #include "AESKey.h"
9*993229b6Sjkunz #include <stdexcept>
10*993229b6Sjkunz #include "smart_ptr.h"
11*993229b6Sjkunz #include "HexValues.h"
12*993229b6Sjkunz #include <ctype.h>
13*993229b6Sjkunz
14*993229b6Sjkunz //! The data from the stream is expected to be hex encoded. Each two characters
15*993229b6Sjkunz //! from the stream encode a single result byte. All non-hexadecimal characters
16*993229b6Sjkunz //! are ignored, including newlines. Every two hexadecimal characters form
17*993229b6Sjkunz //! an encoded byte. This is true even if the two characters representing the
18*993229b6Sjkunz //! upper and lower nibbles are separated by whitespace or other characters.
19*993229b6Sjkunz //!
20*993229b6Sjkunz //! \post The stream read head is left pointing just after the last encoded byte.
21*993229b6Sjkunz //!
22*993229b6Sjkunz //! \param stream Input stream to read from.
23*993229b6Sjkunz //! \param bytes Number of encoded bytes to read. This is the number of \e
24*993229b6Sjkunz //! result bytes, not the count of bytes to read from the stream.
25*993229b6Sjkunz //! \param[out] buffer Pointer to the buffer where decoded data is written.
26*993229b6Sjkunz //!
27*993229b6Sjkunz //! \exception std::runtime_error This exception will be thrown if less
28*993229b6Sjkunz //! data than required is available from \a stream, or if some other
29*993229b6Sjkunz //! error occurs while reading from \a stream.
_readFromStream(std::istream & stream,unsigned bytes,uint8_t * buffer)30*993229b6Sjkunz void AESKeyBase::_readFromStream(std::istream & stream, unsigned bytes, uint8_t * buffer)
31*993229b6Sjkunz {
32*993229b6Sjkunz char temp[2];
33*993229b6Sjkunz char c;
34*993229b6Sjkunz char n = 0;
35*993229b6Sjkunz
36*993229b6Sjkunz while (bytes)
37*993229b6Sjkunz {
38*993229b6Sjkunz if (stream.get(c).fail())
39*993229b6Sjkunz {
40*993229b6Sjkunz throw std::runtime_error("not enough data in stream");
41*993229b6Sjkunz }
42*993229b6Sjkunz
43*993229b6Sjkunz if (isHexDigit(c))
44*993229b6Sjkunz {
45*993229b6Sjkunz temp[n++] = c;
46*993229b6Sjkunz if (n == 2)
47*993229b6Sjkunz {
48*993229b6Sjkunz *buffer++ = hexByteToInt(temp);
49*993229b6Sjkunz bytes--;
50*993229b6Sjkunz n = 0;
51*993229b6Sjkunz }
52*993229b6Sjkunz }
53*993229b6Sjkunz }
54*993229b6Sjkunz }
55*993229b6Sjkunz
56*993229b6Sjkunz //! Key data is written to \a stream as a sequence of hex encoded octets, each two
57*993229b6Sjkunz //! characters long. No spaces or newlines are inserted between the encoded octets
58*993229b6Sjkunz //! or at the end of the sequence.
59*993229b6Sjkunz //!
60*993229b6Sjkunz //! \exception std::runtime_error Thrown if the \a stream reports an error while
61*993229b6Sjkunz //! writing the key data.
_writeToStream(std::ostream & stream,unsigned bytes,const uint8_t * buffer)62*993229b6Sjkunz void AESKeyBase::_writeToStream(std::ostream & stream, unsigned bytes, const uint8_t * buffer)
63*993229b6Sjkunz {
64*993229b6Sjkunz const char hexChars[] = "0123456789ABCDEF";
65*993229b6Sjkunz while (bytes--)
66*993229b6Sjkunz {
67*993229b6Sjkunz uint8_t thisByte = *buffer++;
68*993229b6Sjkunz char byteString[2];
69*993229b6Sjkunz byteString[0] = hexChars[(thisByte & 0xf0) >> 4];
70*993229b6Sjkunz byteString[1] = hexChars[thisByte & 0x0f];
71*993229b6Sjkunz if (stream.write(byteString, 2).bad())
72*993229b6Sjkunz {
73*993229b6Sjkunz throw std::runtime_error("error while writing to stream");
74*993229b6Sjkunz }
75*993229b6Sjkunz }
76*993229b6Sjkunz }
77*993229b6Sjkunz
78*993229b6Sjkunz
79