summaryrefslogtreecommitdiff
path: root/include/base32.h
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2017-06-21 14:40:23 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-06-28 23:23:41 -0700
commit2e3b42610b1239e8643d58396b7471b73e3989f6 (patch)
treef5a4152e4e4a565d9feb55a6a992698e0e5a9fe7 /include/base32.h
parent031dccad78b9d4df6b970bc36ef2f9d469239708 (diff)
downloadchrome-ec-2e3b42610b1239e8643d58396b7471b73e3989f6.tar.gz
common: Add base32 encoding
Base32 encoding is used to turn the RMA reset binary challenge/response into less-typo-prone text, at 5 bits per character. BUG=b:37952913 BRANCH=none TEST=make runtests Change-Id: I474750a20204ba353cea1e91982aa03e8071c0c2 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/544177 Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Diffstat (limited to 'include/base32.h')
-rw-r--r--include/base32.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/include/base32.h b/include/base32.h
new file mode 100644
index 0000000000..e519034732
--- /dev/null
+++ b/include/base32.h
@@ -0,0 +1,72 @@
+/* Copyright 2017 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Base-32 encoding/decoding, designed for manual operator entry. */
+
+#ifndef __CROS_EC_BASE32_H
+#define __CROS_EC_BASE32_H
+
+/* Symbol map for base32 encoding */
+extern const char base32_map[33];
+
+/**
+ * 5-bit CRC
+ *
+ * @param sym New symbol to update CRC with
+ * @param previous_crc Existing CRC value
+ * @return The updated CRC.
+ */
+uint8_t crc5_sym(int sym, uint8_t previous_crc);
+
+/**
+ * base32-encode data into a null-terminated string
+ *
+ * Uses A-Z0-9 encoding, skipping I,O,0,1 since they're easy to get mixed up.
+ *
+ * @param dest Destination buffer; set to empty string on
+ * error
+ * @param destlen_chars Length of destination buffer in characters
+ * @param src Source binary data
+ * @param srclen_bits Length of source *in bits*. If this is not a
+ * multiple of 8, the *most significant* bits of
+ * the last byte will be used. If this is not a
+ * multiple of 5, the least significant bits of
+ * the last symbol will be padded with 0 bits.
+ * @param add_crc_every If non-zero, add a CRC symbol after each group
+ * of this many symbols. There must be an exact
+ * number of groups; that is, ceil(srclen_bits/5)
+ * must be a multiple of add_crc_every.
+ * @return EC_SUCCESS, or non-zero error code.
+ */
+int base32_encode(char *dest, int destlen_chars,
+ const void *srcbits, int srclen_bits,
+ int add_crc_every);
+
+/**
+ * base32-decode data from a null-terminated string
+ *
+ * Ignores whitespace and '-' dashes in the source string.
+ *
+ * If the destination is smaller than the decoded bitstream, only that many
+ * bits will be decoded. This is useful for decoding the first part of a
+ * bitstream to look for a struct version.
+ *
+ * If the destination is larger than the decoded bitstream, check the return
+ * value to determine how many bits were decoded from the source. Note that if
+ * padding was added by base32_encode (that is, the input length was not a
+ * multiple of 5 bits), the padding will be included in the count.
+ *
+ * @param dest Destination; must be at least
+ * ceil(destlen_bits/8) bytes.
+ * @param destlen_bits Length of destination *in bits*.
+ * @param src Source string (null-terminated)
+ * @param crc_after_every If non-zero, expect CRC symbol after every
+ * group of this many symbols.
+ * @return Number of decoded *bits*, or -1 if error.
+ */
+int base32_decode(uint8_t *dest, int destlen_bits, const char *src,
+ int crc_after_every);
+
+#endif