From 2e3b42610b1239e8643d58396b7471b73e3989f6 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Wed, 21 Jun 2017 14:40:23 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/544177 Reviewed-by: Vadim Bendebury --- include/base32.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 include/base32.h (limited to 'include/base32.h') 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 -- cgit v1.2.1