/* * cifra - embedded cryptography library * Written in 2014 by Joseph Birr-Pixton * * To the extent possible under law, the author(s) have dedicated all * copyright and related and neighboring rights to this software to the * public domain worldwide. This software is distributed without any * warranty. * * You should have received a copy of the CC0 Public Domain Dedication * along with this software. If not, see * . */ #ifndef BITOPS_H #define BITOPS_H #include #include /* Assorted bitwise and common operations used in ciphers. */ /** Circularly rotate right x by n bits. * 0 > n > 32. */ static inline uint32_t rotr32(uint32_t x, unsigned n) { return (x >> n) | (x << (32 - n)); } /** Read 4 bytes from buf, as a 32-bit big endian quantity. */ static inline uint32_t read32_be(const uint8_t buf[4]) { return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3]); } /** Encode v as a 32-bit big endian quantity into buf. */ static inline void write32_be(uint32_t v, uint8_t buf[4]) { *buf++ = (v >> 24) & 0xff; *buf++ = (v >> 16) & 0xff; *buf++ = (v >> 8) & 0xff; *buf = v & 0xff; } /** Encode v as a 64-bit big endian quantity into buf. */ static inline void write64_be(uint64_t v, uint8_t buf[8]) { *buf++ = (v >> 56) & 0xff; *buf++ = (v >> 48) & 0xff; *buf++ = (v >> 40) & 0xff; *buf++ = (v >> 32) & 0xff; *buf++ = (v >> 24) & 0xff; *buf++ = (v >> 16) & 0xff; *buf++ = (v >> 8) & 0xff; *buf = v & 0xff; } #endif